Skip to content

Commit

Permalink
promise transfer no data example
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed May 29, 2024
1 parent a9f22d0 commit be1e792
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub trait CommonModule {
#[indexed] payment: &BigUint,
);

#[event("callback_result")]
fn callback_result(&self, #[indexed] result: MultiValueEncoded<ManagedBuffer>);

#[view]
#[storage_mapper("callback_data")]
fn callback_data(&self) -> VecMapper<CallbackData<Self::Api>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,51 @@ pub trait CallPromisesModule: common::CommonModule {
args: ManagedVec::new(),
});
}

#[endpoint]
#[payable("*")]
fn forward_payment_callback(&self, to: ManagedAddress) {
let payment = self.call_value().any_payment();
let gas_limit = self.blockchain().get_gas_left() / 2;

self.tx()
.to(&to)
.raw_call("")
.gas(gas_limit)
.payment(payment)
.callback(self.callbacks().transfer_callback())
.register_promise();
}

#[promises_callback]
fn transfer_callback(&self, #[call_result] result: MultiValueEncoded<ManagedBuffer>) {
self.callback_result(result);

let call_value = self.call_value().any_payment();
match call_value {
EgldOrMultiEsdtPayment::Egld(egld) => {
self.retrieve_funds_callback_event(&EgldOrEsdtTokenIdentifier::egld(), 0, &egld);
let _ = self.callback_data().push(&CallbackData {
callback_name: ManagedBuffer::from(b"transfer_callback"),
token_identifier: EgldOrEsdtTokenIdentifier::egld(),
token_nonce: 0,
token_amount: egld,
args: ManagedVec::new(),
});
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt) => {
for esdt in multi_esdt.into_iter() {
let token_identifier = EgldOrEsdtTokenIdentifier::esdt(esdt.token_identifier);
self.retrieve_funds_callback_event(&token_identifier, 0, &esdt.amount);
let _ = self.callback_data().push(&CallbackData {
callback_name: ManagedBuffer::from(b"transfer_callback"),
token_identifier,
token_nonce: 0,
token_amount: esdt.amount,
args: ManagedVec::new(),
});
}
},
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 10
// Endpoints: 11
// Async Callback (empty): 1
// Promise callbacks: 3
// Total number of exported functions: 15
// Promise callbacks: 4
// Total number of exported functions: 17

#![no_std]

Expand All @@ -24,12 +24,14 @@ multiversx_sc_wasm_adapter::endpoints! {
clear_callback_data => clear_callback_data
forward_promise_accept_funds => forward_promise_accept_funds
forward_promise_retrieve_funds => forward_promise_retrieve_funds
forward_payment_callback => forward_payment_callback
promise_raw_single_token => promise_raw_single_token
promise_raw_multi_transfer => promise_raw_multi_transfer
forward_sync_retrieve_funds_bt => forward_sync_retrieve_funds_bt
forward_sync_retrieve_funds_bt_twice => forward_sync_retrieve_funds_bt_twice
forward_promise_retrieve_funds_back_transfers => forward_promise_retrieve_funds_back_transfers
retrieve_funds_callback => retrieve_funds_callback
transfer_callback => transfer_callback
the_one_callback => the_one_callback
retrieve_funds_back_transfers_callback => retrieve_funds_back_transfers_callback
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"steps": [
{
"step": "setState",
"accounts": {
"address:a_user": {
"nonce": "0",
"balance": "1000"
},
"sc:vault": {
"nonce": "0",
"balance": "0",
"code": "mxsc:../vault/output/vault.mxsc.json"
},
"sc:forwarder": {
"nonce": "0",
"balance": "0",
"code": "mxsc:../promises-features/output/promises-features.mxsc.json"
}
}
},
{
"step": "scCall",
"id": "1",
"tx": {
"from": "address:a_user",
"to": "sc:forwarder",
"egldValue": "1000",
"function": "forward_payment_callback",
"arguments": [
"sc:vault"
],
"gasLimit": "60,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "0",
"logs": [
{
"address": "sc:forwarder",
"endpoint": "str:transferValueOnly",
"topics": [
"1000",
"sc:vault"
],
"data": [
"str:AsyncCall",
"str:accept_funds"
]
},
{
"address": "sc:vault",
"endpoint": "str:accept_funds",
"topics": [
"str:accept_funds",
"1000"
],
"data": [
""
]
}
],
"gas": "*",
"refund": "*"
}
},
{
"step": "checkState",
"accounts": {
"address:a_user": {
"nonce": "*",
"balance": "0",
"storage": {},
"code": ""
},
"sc:vault": {
"nonce": "0",
"balance": "1000",
"storage": {
"str:call_counts|nested:str:accept_funds": "1"
},
"code": "mxsc:../vault/output/vault.mxsc.json"
},
"sc:forwarder": {
"nonce": "0",
"balance": "0",
"storage": {},
"code": "mxsc:../promises-features/output/promises-features.mxsc.json"
}
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ fn promises_call_callback_directly_go() {
world().run("scenarios/promises_call_callback_directly.scen.json");
}

#[test]
fn promises_call_transfer_callback_go() {
world().run("scenarios/promises_call_transfer_callback.scen.json");
}

#[test]
#[ignore = "TODO"]
fn promises_multi_transfer_go() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ fn promises_call_callback_directly_rs() {
world().run("scenarios/promises_call_callback_directly.scen.json");
}

#[test]
#[ignore = "VM does not support this"]
fn promises_call_transfer_callback_rs() {
world().run("scenarios/promises_call_transfer_callback.scen.json");
}

#[test]
fn promises_multi_transfer_rs() {
world().run("scenarios/promises_multi_transfer.scen.json");
Expand Down

0 comments on commit be1e792

Please sign in to comment.