From 5ecb7ac98ce20e4791844e5174d9ff5d5fd5cab1 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 11 Oct 2023 16:00:42 +0300 Subject: [PATCH] contract call argument method --- .../contract_base/wrappers/send_wrapper.rs | 8 +- framework/base/src/esdt/system_sc_proxy.rs | 130 +++++++----------- .../types/interaction/contract_call_trait.rs | 12 ++ 3 files changed, 64 insertions(+), 86 deletions(-) diff --git a/framework/base/src/contract_base/wrappers/send_wrapper.rs b/framework/base/src/contract_base/wrappers/send_wrapper.rs index 02387193a1..78f4f045d6 100644 --- a/framework/base/src/contract_base/wrappers/send_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/send_wrapper.rs @@ -65,7 +65,7 @@ where pub fn contract_call( &self, to: ManagedAddress, - endpoint_name: ManagedBuffer, + endpoint_name: impl Into>, ) -> ContractCallNoPayment { ContractCallNoPayment::new(to, endpoint_name) } @@ -375,10 +375,8 @@ where child_sc_address: ManagedAddress, new_owner: &ManagedAddress, ) -> ContractCallNoPayment { - let mut contract_call = - ContractCallNoPayment::new(child_sc_address, CHANGE_OWNER_BUILTIN_FUNC_NAME); - contract_call.proxy_arg(&new_owner); - contract_call + self.contract_call(child_sc_address, CHANGE_OWNER_BUILTIN_FUNC_NAME) + .argument(&new_owner) } /// Allows synchronously calling a local function by name. Execution is resumed afterwards. diff --git a/framework/base/src/esdt/system_sc_proxy.rs b/framework/base/src/esdt/system_sc_proxy.rs index eee0f20483..dcfb003900 100644 --- a/framework/base/src/esdt/system_sc_proxy.rs +++ b/framework/base/src/esdt/system_sc_proxy.rs @@ -183,15 +183,6 @@ where ) -> ContractCallWithEgld { let esdt_system_sc_address = self.esdt_system_sc_address(); - let mut contract_call = ContractCallWithEgld::new( - esdt_system_sc_address, - ISSUE_AND_SET_ALL_ROLES_ENDPOINT_NAME, - issue_cost, - ); - - contract_call.proxy_arg(&token_display_name); - contract_call.proxy_arg(&token_ticker); - let token_type_name = match token_type { EsdtTokenType::Fungible => "FNG", EsdtTokenType::NonFungible => "NFT", @@ -199,10 +190,16 @@ where EsdtTokenType::Meta => "META", EsdtTokenType::Invalid => "", }; - contract_call.proxy_arg(&token_type_name); - contract_call.proxy_arg(&num_decimals); - contract_call + ContractCallWithEgld::new( + esdt_system_sc_address, + ISSUE_AND_SET_ALL_ROLES_ENDPOINT_NAME, + issue_cost, + ) + .argument(&token_display_name) + .argument(&token_ticker) + .argument(&token_type_name) + .argument(&num_decimals) } /// Deduplicates code from all the possible issue functions @@ -268,12 +265,9 @@ where token_identifier: &TokenIdentifier, amount: &BigUint, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("mint"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(amount); - - contract_call + self.esdt_system_sc_call_no_args("mint") + .argument(token_identifier) + .argument(amount) } /// Produces a contract call to the ESDT system SC, @@ -283,31 +277,22 @@ where token_identifier: &TokenIdentifier, amount: &BigUint, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("ESDTBurn"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(amount); - - contract_call + self.esdt_system_sc_call_no_args("ESDTBurn") + .argument(token_identifier) + .argument(amount) } /// The manager of an ESDT token may choose to suspend all transactions of the token, /// except minting, freezing/unfreezing and wiping. pub fn pause(self, token_identifier: &TokenIdentifier) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("pause"); - - contract_call.proxy_arg(token_identifier); - - contract_call + self.esdt_system_sc_call_no_args("pause") + .argument(token_identifier) } /// The reverse operation of `pause`. pub fn unpause(self, token_identifier: &TokenIdentifier) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("unPause"); - - contract_call.proxy_arg(token_identifier); - - contract_call + self.esdt_system_sc_call_no_args("unPause") + .argument(token_identifier) } /// The manager of an ESDT token may freeze the tokens held by a specific account. @@ -318,12 +303,9 @@ where token_identifier: &TokenIdentifier, address: &ManagedAddress, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("freeze"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(address); - - contract_call + self.esdt_system_sc_call_no_args("freeze") + .argument(token_identifier) + .argument(address) } /// The reverse operation of `freeze`, unfreezing, will allow further transfers to and from the account. @@ -332,12 +314,9 @@ where token_identifier: &TokenIdentifier, address: &ManagedAddress, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("unFreeze"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(address); - - contract_call + self.esdt_system_sc_call_no_args("unFreeze") + .argument(token_identifier) + .argument(address) } /// The manager of an ESDT token may wipe out all the tokens held by a frozen account. @@ -349,12 +328,9 @@ where token_identifier: &TokenIdentifier, address: &ManagedAddress, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("wipe"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(address); - - contract_call + self.esdt_system_sc_call_no_args("wipe") + .argument(token_identifier) + .argument(address) } /// This function converts an SFT to a metaESDT by adding decimals to its structure in the metachain ESDT System SC. @@ -364,12 +340,9 @@ where token_identifier: &TokenIdentifier, num_decimals: usize, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("changeSFTToMetaESDT"); - - contract_call.proxy_arg(&token_identifier); - contract_call.proxy_arg(&num_decimals); - - contract_call + self.esdt_system_sc_call_no_args("changeSFTToMetaESDT") + .argument(&token_identifier) + .argument(&num_decimals) } /// This function can be called only if canSetSpecialRoles was set to true. @@ -382,10 +355,10 @@ where token_identifier: &TokenIdentifier, roles_iter: RoleIter, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("setSpecialRole"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(address); + let mut contract_call = self + .esdt_system_sc_call_no_args("setSpecialRole") + .argument(token_identifier) + .argument(address); for role in roles_iter { if role != EsdtLocalRole::None { contract_call.push_raw_argument(role.as_role_name()); @@ -405,10 +378,10 @@ where token_identifier: &TokenIdentifier, roles_iter: RoleIter, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("unSetSpecialRole"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(address); + let mut contract_call = self + .esdt_system_sc_call_no_args("unSetSpecialRole") + .argument(token_identifier) + .argument(address); for role in roles_iter { if role != EsdtLocalRole::None { contract_call.push_raw_argument(role.as_role_name()); @@ -423,12 +396,9 @@ where token_identifier: &TokenIdentifier, new_owner: &ManagedAddress, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("transferOwnership"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(new_owner); - - contract_call + self.esdt_system_sc_call_no_args("transferOwnership") + .argument(token_identifier) + .argument(new_owner) } pub fn transfer_nft_create_role( @@ -437,13 +407,10 @@ where old_creator: &ManagedAddress, new_creator: &ManagedAddress, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("transferNFTCreateRole"); - - contract_call.proxy_arg(token_identifier); - contract_call.proxy_arg(old_creator); - contract_call.proxy_arg(new_creator); - - contract_call + self.esdt_system_sc_call_no_args("transferNFTCreateRole") + .argument(token_identifier) + .argument(old_creator) + .argument(new_creator) } pub fn control_changes( @@ -451,8 +418,9 @@ where token_identifier: &TokenIdentifier, property_arguments: &TokenPropertyArguments, ) -> ContractCallNoPayment { - let mut contract_call = self.esdt_system_sc_call_no_args("controlChanges"); - contract_call.proxy_arg(token_identifier); + let mut contract_call = self + .esdt_system_sc_call_no_args("controlChanges") + .argument(token_identifier); append_token_property_arguments(&mut contract_call, property_arguments); contract_call } diff --git a/framework/base/src/types/interaction/contract_call_trait.rs b/framework/base/src/types/interaction/contract_call_trait.rs index a4aa03b092..ead9ff4bcc 100644 --- a/framework/base/src/types/interaction/contract_call_trait.rs +++ b/framework/base/src/types/interaction/contract_call_trait.rs @@ -32,6 +32,18 @@ where endpoint_arg.multi_encode_or_handle_err(&mut self.get_mut_basic().arg_buffer, h); } + /// Serializes and pushes a value to the arguments buffer. + /// + /// Accepts multi-values, so it might effectively be adding more than one raw argument. + /// + /// Warning: this method serializes any serializable type, + /// but does no type checking against the destination endpoint ABI. + /// Only use for raw calls, built without a proxy. + fn argument(mut self, arg: &T) -> Self { + self.proxy_arg(arg); + self + } + /// For cases where we build the contract call by hand. /// /// No serialization occurs, just direct conversion to ManagedBuffer.