Skip to content

Commit

Permalink
Merge branch 'main' into upgrade-0.54.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi authored Nov 12, 2024
2 parents e9c27f3 + 648957e commit 423efa7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
13 changes: 11 additions & 2 deletions contracts/paymaster/src/forward_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ pub trait ForwardCall {
.to(&dest)
.raw_call(endpoint_name)
.arguments_raw(endpoint_args.to_arg_buffer())
.payment(payments)
.callback(self.callbacks().transfer_callback(original_caller))
.payment(payments.clone())
.callback(
self.callbacks()
.transfer_callback(original_caller, payments),
)
.async_call_and_exit();
}

#[callback]
fn transfer_callback(
&self,
original_caller: ManagedAddress,
payments: PaymentsVec<Self::Api>,
#[call_result] result: ManagedAsyncCallResult<MultiValueEncoded<ManagedBuffer>>,
) -> MultiValueEncoded<ManagedBuffer> {
let back_transfers = self.blockchain().get_back_transfers();
Expand All @@ -54,6 +58,11 @@ pub trait ForwardCall {
return_values
}
ManagedAsyncCallResult::Err(err) => {
let egld_value = self.call_value().egld_value();
// Send the resulted tokens to the original caller
self.tx().to(&original_caller).payment(&payments).transfer();
self.tx().to(&original_caller).egld(egld_value).transfer();

let mut err_result = MultiValueEncoded::new();
err_result.push(ManagedBuffer::new_from_bytes(ERR_CALLBACK_MSG));
err_result.push(err.err_msg);
Expand Down
58 changes: 58 additions & 0 deletions contracts/paymaster/tests/paymaster_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,61 @@ fn test_forward_call_fails_wegld_0_amount() {
// Caller has the original balance
state.check_esdt_balance(CALLER_ADDRESS_EXPR, WEGLD_TOKEN_ID_EXPR, BALANCE);
}

#[test]
fn test_forward_call_fails_check_amounts() {
let mut state = PaymasterTestState::new();
state.deploy_paymaster_contract();
state.deploy_wegld_contract();

state.check_esdt_balance(CALLER_ADDRESS_EXPR, FEE_TOKEN_ID_EXPR, BALANCE);
state.check_esdt_balance(CALLER_ADDRESS_EXPR, WEGLD_TOKEN_ID_EXPR, BALANCE);

let mut payments: MultiEsdtPayment<StaticApi> = MultiEsdtPayment::new();
payments.push(EsdtTokenPayment::new(
TokenIdentifier::from(FEE_TOKEN_ID_EXPR),
0,
BigUint::from(FEE_AMOUNT),
));

let sent_amount = 1_000u64;
payments.push(EsdtTokenPayment::new(
TokenIdentifier::from(WEGLD_TOKEN_ID),
0,
BigUint::from(sent_amount),
));

state
.world
.tx()
.from(OWNER_ADDRESS_EXPR)
.to(CALLEE_SC_WEGLD_ADDRESS_EXPR)
.typed(wegld_proxy::EgldEsdtSwapProxy)
.pause_endpoint()
.run();

// Call fails because wrong WEGLD token provided
state
.world
.tx()
.from(CALLER_ADDRESS_EXPR)
.to(PAYMASTER_ADDRESS_EXPR)
.typed(paymaster_proxy::PaymasterContractProxy)
.forward_execution(
RELAYER_ADDRESS_EXPR,
CALLEE_SC_WEGLD_ADDRESS_EXPR,
100u64,
UNWRAP_ENDPOINT_NAME,
MultiValueEncoded::new(),
)
.multi_esdt(payments)
.run();

// Fee is kept by the relayer
let new_fee_amount: u64 = 99_980_000;
state.check_esdt_balance(RELAYER_ADDRESS_EXPR, FEE_TOKEN_ID_EXPR, FEE_AMOUNT);
state.check_esdt_balance(CALLER_ADDRESS_EXPR, FEE_TOKEN_ID_EXPR, new_fee_amount);

// Caller has the original balance
state.check_esdt_balance(CALLER_ADDRESS_EXPR, WEGLD_TOKEN_ID_EXPR, BALANCE);
}

0 comments on commit 423efa7

Please sign in to comment.