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

esdt transfer raw tests #1913

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

multiversx_sc::imports!();

pub mod esdt_features;

/// Test contract for investigating async calls.
#[multiversx_sc::contract]
pub trait BuiltinFuncFeatures {
pub trait BuiltinFuncFeatures: esdt_features::EsdtFeaturesModule {
#[init]
fn init(&self) {}
fn init(&self, fungible_token_id: TokenIdentifier, non_fungible_token_id: TokenIdentifier) {
self.fungible_esdt_token_id()
.set_token_id(fungible_token_id);
self.non_fungible_esdt_token_id()
.set_token_id(non_fungible_token_id);
}

#[endpoint]
fn call_set_user_name(&self, address: ManagedAddress, name: ManagedBuffer) {
Expand Down
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 = 50_0000;
dorin-iancu marked this conversation as resolved.
Show resolved Hide resolved
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>;
}
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
////////////////////////////////////////////////////

// Init: 1
// Endpoints: 2
// Endpoints: 3
// Async Callback (empty): 1
// Total number of exported functions: 4
// Total number of exported functions: 5

#![no_std]

Expand All @@ -20,6 +20,7 @@ multiversx_sc_wasm_adapter::endpoints! {
init => init
call_set_user_name => call_set_user_name
call_delete_user_name => call_delete_user_name
transferFungiblePromiseNoCallback => transfer_fungible_promise_no_callback
)
}

Expand Down
Loading