Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paymaster: transfer_callback: handle payments in case of fail #121

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading