-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1913 from multiversx/raw-esdt-transfer-tests
esdt transfer raw tests
- Loading branch information
Showing
4 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
contracts/feature-tests/composability/builtin-func-features/src/esdt_features.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
multiversx_sc::imports!(); | ||
multiversx_sc::derive_imports!(); | ||
|
||
static ESDT_TRANSFER_FUNC_NAME: &[u8] = b"ESDTTransfer"; | ||
|
||
const GAS_LIMIT_ESDT_TRANSFER: u64 = 500_000; | ||
const CALLBACK_ESDT_TRANSFER_GAS_LIMIT: u64 = 100_000; | ||
|
||
#[derive(TopEncode, TopDecode)] | ||
pub enum TransferResult { | ||
None, | ||
Success, | ||
Fail, | ||
} | ||
|
||
#[multiversx_sc::module] | ||
pub trait EsdtFeaturesModule { | ||
#[endpoint(transferFungiblePromiseNoCallback)] | ||
fn transfer_fungible_promise_no_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) | ||
.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>; | ||
} |
125 changes: 125 additions & 0 deletions
125
...ts/feature-tests/composability/builtin-func-features/tests/esdt_transfer_promise_tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use builtin_func_features::{ | ||
esdt_features::{EsdtFeaturesModule, TransferResult}, | ||
BuiltinFuncFeatures, | ||
}; | ||
use multiversx_sc::{codec::Empty, types::Address}; | ||
use multiversx_sc_scenario::{ | ||
imports::{BlockchainStateWrapper, ContractObjWrapper}, | ||
managed_address, managed_biguint, managed_token_id, rust_biguint, DebugApi, | ||
}; | ||
|
||
pub static FUNGIBLE_TOKEN_ID: &[u8] = b"FUNG-123456"; | ||
pub static NON_FUNGIBLE_TOKEN_ID: &[u8] = b"NONFUNG-123456"; | ||
pub const INIT_BALANCE: u64 = 100_000; | ||
pub const INIT_NONCE: u64 = 1; | ||
|
||
pub struct BuiltInFuncFeaturesSetup<BuiltInFuncBuilder> | ||
where | ||
BuiltInFuncBuilder: 'static + Copy + Fn() -> builtin_func_features::ContractObj<DebugApi>, | ||
{ | ||
pub b_mock: BlockchainStateWrapper, | ||
pub user: Address, | ||
pub sc_wrapper: | ||
ContractObjWrapper<builtin_func_features::ContractObj<DebugApi>, BuiltInFuncBuilder>, | ||
} | ||
|
||
impl<BuiltInFuncBuilder> BuiltInFuncFeaturesSetup<BuiltInFuncBuilder> | ||
where | ||
BuiltInFuncBuilder: 'static + Copy + Fn() -> builtin_func_features::ContractObj<DebugApi>, | ||
{ | ||
pub fn new(built_in_func_builder: BuiltInFuncBuilder) -> Self { | ||
let mut b_mock = BlockchainStateWrapper::new(); | ||
let user = b_mock.create_user_account(&rust_biguint!(0)); | ||
let sc_wrapper = b_mock.create_sc_account( | ||
&rust_biguint!(0), | ||
Some(&user), | ||
built_in_func_builder, | ||
"built in func features", | ||
); | ||
b_mock | ||
.execute_tx(&user, &sc_wrapper, &rust_biguint!(0), |sc| { | ||
sc.init( | ||
managed_token_id!(FUNGIBLE_TOKEN_ID), | ||
managed_token_id!(NON_FUNGIBLE_TOKEN_ID), | ||
); | ||
}) | ||
.assert_ok(); | ||
|
||
b_mock.set_esdt_balance( | ||
sc_wrapper.address_ref(), | ||
FUNGIBLE_TOKEN_ID, | ||
&rust_biguint!(INIT_BALANCE), | ||
); | ||
b_mock.set_nft_balance( | ||
sc_wrapper.address_ref(), | ||
NON_FUNGIBLE_TOKEN_ID, | ||
INIT_NONCE, | ||
&rust_biguint!(INIT_BALANCE), | ||
&Empty, | ||
); | ||
|
||
BuiltInFuncFeaturesSetup { | ||
b_mock, | ||
user, | ||
sc_wrapper, | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn transfer_fungible_promise_no_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_no_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), | ||
); | ||
} | ||
|
||
#[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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters