Skip to content

Commit

Permalink
Merge pull request #1505 from multiversx/vault-proxy
Browse files Browse the repository at this point in the history
Vault proxy
  • Loading branch information
andrei-marinica authored Mar 27, 2024
2 parents 04a5f97 + 8a0c266 commit 8f96dde
Show file tree
Hide file tree
Showing 20 changed files with 869 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ pub trait ForwarderRawDeployUpgrade {
) {
self.tx()
.to(child_sc_address)
.raw_deploy()
.raw_upgrade()
.code(new_code)
.code_metadata(code_metadata)
.arguments_raw(args.to_arg_buffer())
.upgrade_async_call();
.upgrade_async_call_and_exit();
}

#[endpoint]
Expand All @@ -65,11 +65,11 @@ pub trait ForwarderRawDeployUpgrade {
) {
self.tx()
.to(sc_address)
.raw_deploy()
.raw_upgrade()
.from_source(source_contract_address)
.code_metadata(code_metadata)
.arguments_raw(args.to_arg_buffer())
.with_gas_limit(self.blockchain().get_gas_left())
.upgrade_async_call();
.upgrade_async_call_and_exit();
}
}
55 changes: 31 additions & 24 deletions contracts/feature-tests/composability/forwarder/src/call_async.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::vault_proxy;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

Expand All @@ -14,13 +16,11 @@ const PERCENTAGE_TOTAL: u64 = 10_000; // 100%

#[multiversx_sc::module]
pub trait ForwarderAsyncCallModule {
#[proxy]
fn vault_proxy(&self) -> vault::Proxy<Self::Api>;

#[endpoint]
fn echo_args_async(&self, to: ManagedAddress, args: MultiValueEncoded<ManagedBuffer>) {
self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.echo_arguments(args)
.async_call()
.with_callback(self.callbacks().echo_args_callback())
Expand Down Expand Up @@ -57,8 +57,9 @@ pub trait ForwarderAsyncCallModule {
#[payable("*")]
fn forward_async_accept_funds(&self, to: ManagedAddress) {
let payment = self.call_value().egld_or_single_esdt();
self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer(payment)
.async_call()
Expand All @@ -70,8 +71,9 @@ pub trait ForwarderAsyncCallModule {
fn forward_async_accept_funds_half_payment(&self, to: ManagedAddress) {
let payment = self.call_value().egld_or_single_esdt();
let half_payment = payment.amount / 2u32;
self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer((
payment.token_identifier,
Expand All @@ -89,8 +91,9 @@ pub trait ForwarderAsyncCallModule {
let fees = &payment.amount * &percentage_fees / PERCENTAGE_TOTAL;
let amount_to_send = &payment.amount - &fees;

self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer((
payment.token_identifier,
Expand All @@ -109,8 +112,9 @@ pub trait ForwarderAsyncCallModule {
token_nonce: u64,
amount: BigUint,
) {
self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.retrieve_funds(token, token_nonce, amount)
.async_call()
.with_callback(self.callbacks().retrieve_funds_callback())
Expand Down Expand Up @@ -146,16 +150,17 @@ pub trait ForwarderAsyncCallModule {
token_identifier: &EgldOrEsdtTokenIdentifier,
amount: &BigUint,
) {
self.vault_proxy()
.contract(to.clone())
self.tx()
.to(to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer((token_identifier.clone(), 0, amount.clone()))
.with_egld_or_single_esdt_transfer((token_identifier.clone(), 0u64, amount.clone()))
.async_call()
.with_callback(
self.callbacks()
.send_funds_twice_callback(to, token_identifier, amount),
)
.call_and_exit()
.call_and_exit();
}

#[callback]
Expand All @@ -165,12 +170,13 @@ pub trait ForwarderAsyncCallModule {
token_identifier: &EgldOrEsdtTokenIdentifier,
cb_amount: &BigUint,
) {
self.vault_proxy()
.contract(to.clone())
self.tx()
.to(to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer((token_identifier.clone(), 0, cb_amount.clone()))
.with_egld_or_single_esdt_transfer((token_identifier.clone(), 0u64, cb_amount.clone()))
.async_call()
.call_and_exit()
.call_and_exit();
}

#[endpoint]
Expand All @@ -188,12 +194,13 @@ pub trait ForwarderAsyncCallModule {
all_token_payments.push(payment);
}

self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_multi_token_transfer(all_token_payments)
.async_call()
.call_and_exit()
.call_and_exit();
}

#[view]
Expand Down
103 changes: 58 additions & 45 deletions contracts/feature-tests/composability/forwarder/src/call_sync.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use crate::vault_proxy;

multiversx_sc::imports!();

const PERCENTAGE_TOTAL: u64 = 10_000; // 100%

#[multiversx_sc::module]
pub trait ForwarderSyncCallModule {
#[proxy]
fn vault_proxy(&self) -> vault::Proxy<Self::Api>;

#[endpoint]
#[payable("*")]
fn echo_arguments_sync(&self, to: ManagedAddress, args: MultiValueEncoded<ManagedBuffer>) {
let half_gas = self.blockchain().get_gas_left() / 2;

let result: MultiValueEncoded<ManagedBuffer> = self
.vault_proxy()
.contract(to)
.echo_arguments(args)
let result = self
.tx()
.to(&to)
.with_gas_limit(half_gas)
.execute_on_dest_context();
.typed(vault_proxy::VaultProxy)
.echo_arguments(args)
.returns(ReturnsResult)
.sync_call();

self.execute_on_dest_context_result_event(&result.into_vec_of_buffers());
}
Expand All @@ -31,21 +32,25 @@ pub trait ForwarderSyncCallModule {
) {
let one_third_gas = self.blockchain().get_gas_left() / 3;

let result: MultiValueEncoded<ManagedBuffer> = self
.vault_proxy()
.contract(to.clone())
.echo_arguments(&args)
let result = self
.tx()
.to(&to)
.with_gas_limit(one_third_gas)
.execute_on_dest_context();
.typed(vault_proxy::VaultProxy)
.echo_arguments(args.clone())
.returns(ReturnsResult)
.sync_call();

self.execute_on_dest_context_result_event(&result.into_vec_of_buffers());

let result: MultiValueEncoded<ManagedBuffer> = self
.vault_proxy()
.contract(to)
.echo_arguments(&args)
let result = self
.tx()
.to(&to)
.with_gas_limit(one_third_gas)
.execute_on_dest_context();
.typed(vault_proxy::VaultProxy)
.echo_arguments(args)
.returns(ReturnsResult)
.sync_call();

self.execute_on_dest_context_result_event(&result.into_vec_of_buffers());
}
Expand All @@ -59,13 +64,16 @@ pub trait ForwarderSyncCallModule {
let payment = self.call_value().egld_or_single_esdt();
let half_gas = self.blockchain().get_gas_left() / 2;

let result: MultiValue2<BigUint, MultiValueEncoded<EsdtTokenPaymentMultiValue>> = self
.vault_proxy()
.contract(to)
let result = self
.tx()
.to(&to)
.with_gas_limit(half_gas)
.typed(vault_proxy::VaultProxy)
.accept_funds_echo_payment()
.with_egld_or_single_esdt_transfer(payment)
.with_gas_limit(half_gas)
.execute_on_dest_context();
.returns(ReturnsResult)
.sync_call();

let (egld_value, esdt_transfers_multi) = result.into_tuple();

self.accept_funds_sync_result_event(&egld_value, &esdt_transfers_multi);
Expand All @@ -78,12 +86,13 @@ pub trait ForwarderSyncCallModule {
let fees = &payment * &percentage_fees / PERCENTAGE_TOTAL;
let amount_to_send = payment - fees;

let () = self
.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer((token_id, 0, amount_to_send))
.execute_on_dest_context();
.with_egld_or_single_esdt_transfer((token_id, 0u64, amount_to_send))
.returns(ReturnsResult)
.sync_call();
}

#[event("accept_funds_sync_result")]
Expand All @@ -97,17 +106,19 @@ pub trait ForwarderSyncCallModule {
#[payable("*")]
fn forward_sync_accept_funds_then_read(&self, to: ManagedAddress) -> usize {
let payment = self.call_value().egld_or_single_esdt();
self.vault_proxy()
.contract(to.clone())
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_egld_or_single_esdt_transfer(payment)
.execute_on_dest_context::<()>();
.sync_call();

self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.call_counts(b"accept_funds")
.execute_on_dest_context::<SingleValue<usize>>()
.into()
.returns(ReturnsResult)
.sync_call()
}

#[endpoint]
Expand All @@ -118,10 +129,11 @@ pub trait ForwarderSyncCallModule {
token_nonce: u64,
amount: BigUint,
) {
self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.retrieve_funds(token, token_nonce, amount)
.execute_on_dest_context::<()>();
.sync_call();
}

#[payable("*")]
Expand All @@ -134,15 +146,16 @@ pub trait ForwarderSyncCallModule {
) {
let payments = self.call_value().all_esdt_transfers();

self.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.retrieve_funds_with_transfer_exec(
token,
amount,
OptionalValue::<ManagedBuffer>::Some(b"accept_funds_func".into()),
)
.with_multi_token_transfer(payments.clone_value())
.execute_on_dest_context::<()>();
.sync_call();
}

#[payable("*")]
Expand All @@ -163,11 +176,11 @@ pub trait ForwarderSyncCallModule {
all_token_payments.push(payment);
}

let () = self
.vault_proxy()
.contract(to)
self.tx()
.to(&to)
.typed(vault_proxy::VaultProxy)
.accept_funds()
.with_multi_token_transfer(all_token_payments)
.execute_on_dest_context();
.sync_call();
}
}
Loading

0 comments on commit 8f96dde

Please sign in to comment.