From 213d2559f5f431d722119b118f4cde45f649e56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Mon, 13 Jan 2025 09:56:13 +0200 Subject: [PATCH] Add blackbox test for builtin_func_features --- .../src/builtin_func_features.rs | 1 + .../src/builtin_func_features_proxy.rs | 132 ++++++++++++++++++ .../esdt_transfer_promise_blackbox_tests.rs | 110 +++++++++++++++ .../builtin-func-features/wasm/src/lib.rs | 7 +- 4 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features_proxy.rs create mode 100644 contracts/feature-tests/composability/builtin-func-features/tests/esdt_transfer_promise_blackbox_tests.rs diff --git a/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features.rs b/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features.rs index 1a62d3f575..6087d08125 100644 --- a/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features.rs +++ b/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features.rs @@ -2,6 +2,7 @@ multiversx_sc::imports!(); +pub mod builtin_func_features_proxy; pub mod esdt_features; /// Test contract for investigating async calls. diff --git a/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features_proxy.rs b/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features_proxy.rs new file mode 100644 index 0000000000..26ca587c5a --- /dev/null +++ b/contracts/feature-tests/composability/builtin-func-features/src/builtin_func_features_proxy.rs @@ -0,0 +1,132 @@ +// Code generated by the multiversx-sc proxy generator. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +#![allow(dead_code)] +#![allow(clippy::all)] + +use multiversx_sc::proxy_imports::*; + +pub struct BuiltinFuncFeaturesProxy; + +impl TxProxyTrait for BuiltinFuncFeaturesProxy +where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + type TxProxyMethods = BuiltinFuncFeaturesProxyMethods; + + fn proxy_methods(self, tx: Tx) -> Self::TxProxyMethods { + BuiltinFuncFeaturesProxyMethods { wrapped_tx: tx } + } +} + +pub struct BuiltinFuncFeaturesProxyMethods +where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + wrapped_tx: Tx, +} + +#[rustfmt::skip] +impl BuiltinFuncFeaturesProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + Gas: TxGas, +{ + pub fn init< + Arg0: ProxyArg>, + Arg1: ProxyArg>, + >( + self, + fungible_token_id: Arg0, + non_fungible_token_id: Arg1, + ) -> TxTypedDeploy { + self.wrapped_tx + .payment(NotPayable) + .raw_deploy() + .argument(&fungible_token_id) + .argument(&non_fungible_token_id) + .original_result() + } +} + +#[rustfmt::skip] +impl BuiltinFuncFeaturesProxyMethods +where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + To: TxTo, + Gas: TxGas, +{ + pub fn call_set_user_name< + Arg0: ProxyArg>, + Arg1: ProxyArg>, + >( + self, + address: Arg0, + name: Arg1, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("call_set_user_name") + .argument(&address) + .argument(&name) + .original_result() + } + + pub fn call_delete_user_name< + Arg0: ProxyArg>, + >( + self, + address: Arg0, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("call_delete_user_name") + .argument(&address) + .original_result() + } + + pub fn transfer_fungible_promise_no_callback< + Arg0: ProxyArg>, + Arg1: ProxyArg>, + >( + self, + to: Arg0, + amount: Arg1, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("transferFungiblePromiseNoCallback") + .argument(&to) + .argument(&amount) + .original_result() + } + + pub fn transfer_fungible_promise_with_callback< + Arg0: ProxyArg>, + Arg1: ProxyArg>, + >( + self, + to: Arg0, + amount: Arg1, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("transferFungiblePromiseWithCallback") + .argument(&to) + .argument(&amount) + .original_result() + } +} diff --git a/contracts/feature-tests/composability/builtin-func-features/tests/esdt_transfer_promise_blackbox_tests.rs b/contracts/feature-tests/composability/builtin-func-features/tests/esdt_transfer_promise_blackbox_tests.rs new file mode 100644 index 0000000000..315924bdab --- /dev/null +++ b/contracts/feature-tests/composability/builtin-func-features/tests/esdt_transfer_promise_blackbox_tests.rs @@ -0,0 +1,110 @@ +use multiversx_sc_scenario::imports::*; + +use builtin_func_features::*; + +const OWNER_ADDRESS: TestAddress = TestAddress::new("owner"); +const USER_ADDRESS: TestAddress = TestAddress::new("user"); + +const BUILTIN_FEATURES_FUNC_ADDRESS: TestSCAddress = TestSCAddress::new("builtin-func-features"); +const CODE_PATH: MxscPath = MxscPath::new("output/builtin-func-features.mxsc.json"); + +const FUNGIBLE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("FUNG-123456"); +const NON_FUNGIBLE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("NONFUNG-123456"); + +fn world() -> ScenarioWorld { + let mut blockchain = ScenarioWorld::new(); + + blockchain.set_current_dir_from_workspace( + "contracts/feature-tests/composability/builtin-func-features", + ); + blockchain.register_contract(CODE_PATH, builtin_func_features::ContractBuilder); + blockchain +} + +#[test] +fn transfer_fungible_promise_no_callback_blackbox_test() { + let mut world = world(); + + world + .account(USER_ADDRESS) + .nonce(1) + .balance(1000) + .esdt_balance(FUNGIBLE_TOKEN_ID, 1000) + .esdt_balance(NON_FUNGIBLE_TOKEN_ID, 1000); + + world.start_trace(); + + world.account(OWNER_ADDRESS).nonce(1); + + let new_address = world + .tx() + .from(OWNER_ADDRESS) + .typed(builtin_func_features_proxy::BuiltinFuncFeaturesProxy) + .init(FUNGIBLE_TOKEN_ID, NON_FUNGIBLE_TOKEN_ID) + .code(CODE_PATH) + .new_address(BUILTIN_FEATURES_FUNC_ADDRESS) + .returns(ReturnsNewAddress) + .run(); + + assert_eq!(new_address, BUILTIN_FEATURES_FUNC_ADDRESS.to_address()); + + world + .tx() + .from(OWNER_ADDRESS) + .to(BUILTIN_FEATURES_FUNC_ADDRESS) + .typed(builtin_func_features_proxy::BuiltinFuncFeaturesProxy) + .transfer_fungible_promise_no_callback(USER_ADDRESS, 1000u64) + .run(); + + world + .check_account(USER_ADDRESS) + .esdt_balance(FUNGIBLE_TOKEN_ID, 1000u64); + + world + .check_account(BUILTIN_FEATURES_FUNC_ADDRESS) + .esdt_balance(FUNGIBLE_TOKEN_ID, 0u64); +} + +#[test] +fn transfer_fungible_promise_with_callback_blackbox_test() { + let mut world = world(); + + world + .account(USER_ADDRESS) + .nonce(1) + .balance(1000) + .esdt_balance(FUNGIBLE_TOKEN_ID, 1000) + .esdt_balance(NON_FUNGIBLE_TOKEN_ID, 1000); + + world.start_trace(); + + world.account(OWNER_ADDRESS).nonce(1); + + let new_address = world + .tx() + .from(OWNER_ADDRESS) + .typed(builtin_func_features_proxy::BuiltinFuncFeaturesProxy) + .init(FUNGIBLE_TOKEN_ID, NON_FUNGIBLE_TOKEN_ID) + .code(CODE_PATH) + .new_address(BUILTIN_FEATURES_FUNC_ADDRESS) + .returns(ReturnsNewAddress) + .run(); + + assert_eq!(new_address, BUILTIN_FEATURES_FUNC_ADDRESS.to_address()); + + world + .tx() + .from(OWNER_ADDRESS) + .to(BUILTIN_FEATURES_FUNC_ADDRESS) + .typed(builtin_func_features_proxy::BuiltinFuncFeaturesProxy) + .transfer_fungible_promise_with_callback(USER_ADDRESS, 1000u64) + .run(); + + world + .check_account(USER_ADDRESS) + .esdt_balance(FUNGIBLE_TOKEN_ID, 1000u64); + + world + .check_account(BUILTIN_FEATURES_FUNC_ADDRESS) + .esdt_balance(FUNGIBLE_TOKEN_ID, 0u64); +} diff --git a/contracts/feature-tests/composability/builtin-func-features/wasm/src/lib.rs b/contracts/feature-tests/composability/builtin-func-features/wasm/src/lib.rs index 0c28faa4cb..227092bb2d 100644 --- a/contracts/feature-tests/composability/builtin-func-features/wasm/src/lib.rs +++ b/contracts/feature-tests/composability/builtin-func-features/wasm/src/lib.rs @@ -5,9 +5,10 @@ //////////////////////////////////////////////////// // Init: 1 -// Endpoints: 3 +// Endpoints: 4 // Async Callback (empty): 1 -// Total number of exported functions: 5 +// Promise callbacks: 1 +// Total number of exported functions: 7 #![no_std] @@ -21,6 +22,8 @@ multiversx_sc_wasm_adapter::endpoints! { call_set_user_name => call_set_user_name call_delete_user_name => call_delete_user_name transferFungiblePromiseNoCallback => transfer_fungible_promise_no_callback + transferFungiblePromiseWithCallback => transfer_fungible_promise_with_callback + transfer_callback => transfer_callback ) }