Skip to content

Commit

Permalink
transfer with callback test
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Jan 8, 2025
1 parent e409053 commit fe41565
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

static ESDT_TRANSFER_FUNC_NAME: &[u8] = b"ESDTTransfer";
// static ESDT_NFT_TRANSFER_FUNC_NAME: &[u8] = b"ESDTNFTTransfer";

const GAS_LIMIT_ESDT_TRANSFER: u64 = 50_0000;
// const CALLBACK_ESDT_TRANSFER_GAS_LIMIT: u64 = 100_000; // TODO: Change if needed
const CALLBACK_ESDT_TRANSFER_GAS_LIMIT: u64 = 100_000;

#[derive(TopEncode, TopDecode)]
pub enum TransferResult {
None,
Success,
Fail,
}

#[multiversx_sc::module]
pub trait EsdtFeaturesModule {
Expand All @@ -20,9 +27,38 @@ pub trait EsdtFeaturesModule {
.register_promise();
}

#[endpoint(transferFungiblePromiseWithCallback)]
fn transfer_fungible_promise_with_callback(&self, to: ManagedAddress, amount: BigUint) {
let token_id = self.fungible_esdt_token_id().get_token_id();
self.tx()
.to(to)
.raw_call(ESDT_TRANSFER_FUNC_NAME)
.argument(&token_id)
.argument(&amount)
.gas(GAS_LIMIT_ESDT_TRANSFER)
.callback(self.callbacks().transfer_callback())
.gas_for_callback(CALLBACK_ESDT_TRANSFER_GAS_LIMIT)
.register_promise();
}

#[promises_callback]
fn transfer_callback(&self, #[call_result] result: ManagedAsyncCallResult<()>) {
match result {
ManagedAsyncCallResult::Ok(()) => {
self.latest_transfer_result().set(TransferResult::Success);
},
ManagedAsyncCallResult::Err(_) => {
self.latest_transfer_result().set(TransferResult::Fail);
},
}
}

#[storage_mapper("fungibleEsdtTokenId")]
fn fungible_esdt_token_id(&self) -> FungibleTokenMapper;

#[storage_mapper("nonFungibleEsdtTokenId")]
fn non_fungible_esdt_token_id(&self) -> NonFungibleTokenMapper;

#[storage_mapper("latestTransferResult")]
fn latest_transfer_result(&self) -> SingleValueMapper<TransferResult>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use builtin_func_features::{esdt_features::EsdtFeaturesModule, BuiltinFuncFeatures};
use builtin_func_features::{
esdt_features::{EsdtFeaturesModule, TransferResult},
BuiltinFuncFeatures,
};
use multiversx_sc::{codec::Empty, types::Address};
use multiversx_sc_scenario::{
imports::{BlockchainStateWrapper, ContractObjWrapper},
Expand Down Expand Up @@ -86,3 +89,37 @@ fn transfer_fungible_promise_no_callback_test() {
&rust_biguint!(0),
);
}

#[test]
fn transfer_fungible_promise_with_callback_test() {
let mut setup = BuiltInFuncFeaturesSetup::new(builtin_func_features::contract_obj);
let user_addr = setup.user.clone();
setup
.b_mock
.execute_tx(&setup.user, &setup.sc_wrapper, &rust_biguint!(0), |sc| {
sc.transfer_fungible_promise_with_callback(
managed_address!(&user_addr),
managed_biguint!(INIT_BALANCE),
);
})
.assert_ok();

setup
.b_mock
.check_esdt_balance(&setup.user, FUNGIBLE_TOKEN_ID, &rust_biguint!(INIT_BALANCE));
setup.b_mock.check_esdt_balance(
setup.sc_wrapper.address_ref(),
FUNGIBLE_TOKEN_ID,
&rust_biguint!(0),
);

setup
.b_mock
.execute_query(&setup.sc_wrapper, |sc| {
assert!(matches!(
sc.latest_transfer_result().get(),
TransferResult::Success
));
})
.assert_ok();
}

0 comments on commit fe41565

Please sign in to comment.