From afab864db5a78553793220961d2b2c9d2dbce790 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Tue, 3 Dec 2024 03:09:54 +0200 Subject: [PATCH] ManagedVecItem - ref for EsdtTokenPayment --- chain/core/src/types/flags/esdt_token_type.rs | 2 +- contracts/core/wegld-swap/src/wegld.rs | 8 ++-- .../digital-cash/src/pay_fee_and_fund.rs | 4 +- .../src/esdt_transfer_with_fee.rs | 4 +- .../fractional-nfts/src/fractional_nfts.rs | 2 +- .../examples/nft-subscription/src/lib.rs | 38 ++++++++++++---- .../order-book/pair/src/validation.rs | 14 ++++-- .../seed-nft-minter/src/seed_nft_minter.rs | 2 +- .../src/storage_mapper_fungible_token.rs | 9 ++-- .../first-contract/src/lib.rs | 12 +++--- .../child/src/lib.rs | 2 +- .../src/fwd_call_transf_exec_legacy.rs | 2 +- .../forwarder-legacy/src/fwd_esdt_legacy.rs | 4 +- .../forwarder-raw/src/forwarder_raw.rs | 4 +- .../forwarder-raw/src/forwarder_raw_async.rs | 4 +- .../composability/forwarder/src/fwd_esdt.rs | 4 +- .../src/fwd_call_promises_bt.rs | 14 +++--- .../composability/vault/src/vault.rs | 4 +- .../payable-features/src/payable_features.rs | 10 ++--- .../rust-testing-framework-tester/src/lib.rs | 4 +- .../use-module/src/token_merge_mod_impl.rs | 2 +- .../bonding_curve/utils/owner_endpoints.rs | 19 ++++---- .../src/bonding_curve/utils/user_endpoints.rs | 17 +++++--- .../modules/src/default_issue_callbacks.rs | 2 +- contracts/modules/src/governance/mod.rs | 2 +- contracts/modules/src/token_merge/mod.rs | 2 +- .../wrappers/call_value_wrapper.rs | 43 +++++++++++++------ .../contract_call_convert.rs | 2 +- .../contract_call_exec.rs | 2 +- .../result_handlers/returns_bt_single_esdt.rs | 3 +- .../managed/wrapped/esdt_token_payment.rs | 7 ++- .../scenario/tx_to_step/tx_to_step_call.rs | 2 +- .../tx_to_step/tx_to_step_transfer.rs | 2 +- 33 files changed, 152 insertions(+), 100 deletions(-) diff --git a/chain/core/src/types/flags/esdt_token_type.rs b/chain/core/src/types/flags/esdt_token_type.rs index 74db07c9a6..3668ca6fe6 100644 --- a/chain/core/src/types/flags/esdt_token_type.rs +++ b/chain/core/src/types/flags/esdt_token_type.rs @@ -13,7 +13,7 @@ const ESDT_TYPE_INVALID: &[u8] = &[]; // Note: In the current implementation, SemiFungible is never returned -#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug)] +#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, Copy, PartialEq, Eq, Debug)] pub enum EsdtTokenType { Fungible, NonFungible, diff --git a/contracts/core/wegld-swap/src/wegld.rs b/contracts/core/wegld-swap/src/wegld.rs index 23a4493494..1c9cf11a2a 100644 --- a/contracts/core/wegld-swap/src/wegld.rs +++ b/contracts/core/wegld-swap/src/wegld.rs @@ -39,10 +39,10 @@ pub trait EgldEsdtSwap: multiversx_sc_modules::pause::PauseModule { let (payment_token, payment_amount) = self.call_value().single_fungible_esdt(); let wrapped_egld_token_id = self.wrapped_egld_token_id().get(); - require!(payment_token == wrapped_egld_token_id, "Wrong esdt token"); - require!(payment_amount > 0u32, "Must pay more than 0 tokens!"); + require!(*payment_token == wrapped_egld_token_id, "Wrong esdt token"); + require!(*payment_amount > 0u32, "Must pay more than 0 tokens!"); require!( - payment_amount <= self.get_locked_egld_balance(), + *payment_amount <= self.get_locked_egld_balance(), "Contract does not have enough funds" ); @@ -51,7 +51,7 @@ pub trait EgldEsdtSwap: multiversx_sc_modules::pause::PauseModule { // 1 wrapped eGLD = 1 eGLD, so we pay back the same amount let caller = self.blockchain().get_caller(); - self.tx().to(&caller).egld(&payment_amount).transfer(); + self.tx().to(&caller).egld(&*payment_amount).transfer(); } #[view(getLockedEgldBalance)] diff --git a/contracts/examples/digital-cash/src/pay_fee_and_fund.rs b/contracts/examples/digital-cash/src/pay_fee_and_fund.rs index 45e913c51d..edff9df18e 100644 --- a/contracts/examples/digital-cash/src/pay_fee_and_fund.rs +++ b/contracts/examples/digital-cash/src/pay_fee_and_fund.rs @@ -7,8 +7,8 @@ pub trait PayFeeAndFund: storage::StorageModule + helpers::HelpersModule { #[endpoint(payFeeAndFundESDT)] #[payable("*")] fn pay_fee_and_fund_esdt(&self, address: ManagedAddress, valability: u64) { - let mut payments = self.call_value().all_esdt_transfers().clone_value(); - let fee = EgldOrEsdtTokenPayment::from(payments.get(0)); + let mut payments = self.call_value().all_esdt_transfers().clone(); + let fee = EgldOrEsdtTokenPayment::from(payments.get(0).clone()); let caller_address = self.blockchain().get_caller(); self.update_fees(caller_address, &address, fee); diff --git a/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs b/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs index de883ef83a..5967c8feaa 100644 --- a/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs +++ b/contracts/examples/esdt-transfer-with-fee/src/esdt_transfer_with_fee.rs @@ -71,13 +71,13 @@ pub trait EsdtTransferWithFee { "Mismatching payment for covering fees" ); let _ = self.get_payment_after_fees(fee_type, &next_payment); - new_payments.push(payment); + new_payments.push(payment.clone()); }, Fee::Percentage(_) => { new_payments.push(self.get_payment_after_fees(fee_type, &payment)); }, Fee::Unset => { - new_payments.push(payment); + new_payments.push(payment.clone()); }, } } diff --git a/contracts/examples/fractional-nfts/src/fractional_nfts.rs b/contracts/examples/fractional-nfts/src/fractional_nfts.rs index 16f1c87aed..2b9844449a 100644 --- a/contracts/examples/fractional-nfts/src/fractional_nfts.rs +++ b/contracts/examples/fractional-nfts/src/fractional_nfts.rs @@ -74,7 +74,7 @@ pub trait FractionalNfts: default_issue_callbacks::DefaultIssueCallbacksModule { let fractional_token = fractional_token_mapper.get_token_id_ref(); let hash = ManagedBuffer::new(); let fractional_info = - FractionalUriInfo::new(original_payment, initial_fractional_amount.clone()); + FractionalUriInfo::new(original_payment.clone(), initial_fractional_amount.clone()); let uris = fractional_info.to_uris(); let fractional_nonce = self.send().esdt_nft_create( diff --git a/contracts/examples/nft-subscription/src/lib.rs b/contracts/examples/nft-subscription/src/lib.rs index 7b0b63096e..210a20020f 100644 --- a/contracts/examples/nft-subscription/src/lib.rs +++ b/contracts/examples/nft-subscription/src/lib.rs @@ -49,34 +49,54 @@ pub trait NftSubscription: #[payable("*")] #[endpoint] fn update_attributes(&self, attributes: ManagedBuffer) { - let (id, nonce, _) = self.call_value().single_esdt().into_tuple(); - self.update_subscription_attributes::(&id, nonce, attributes); + let payment = self.call_value().single_esdt(); + self.update_subscription_attributes::( + &payment.token_identifier, + payment.token_nonce, + attributes, + ); self.tx() .to(ToCaller) - .single_esdt(&id, nonce, &BigUint::from(1u8)) + .single_esdt( + &payment.token_identifier, + payment.token_nonce, + &BigUint::from(1u8), + ) .transfer(); } #[payable("*")] #[endpoint] fn renew(&self, duration: u64) { - let (id, nonce, _) = self.call_value().single_esdt().into_tuple(); - self.renew_subscription::(&id, nonce, duration); + let payment = self.call_value().single_esdt(); + self.renew_subscription::( + &payment.token_identifier, + payment.token_nonce, + duration, + ); self.tx() .to(ToCaller) - .single_esdt(&id, nonce, &BigUint::from(1u8)) + .single_esdt( + &payment.token_identifier, + payment.token_nonce, + &BigUint::from(1u8), + ) .transfer(); } #[payable("*")] #[endpoint] fn cancel(&self) { - let (id, nonce, _) = self.call_value().single_esdt().into_tuple(); - self.cancel_subscription::(&id, nonce); + let payment = self.call_value().single_esdt(); + self.cancel_subscription::(&payment.token_identifier, payment.token_nonce); self.tx() .to(ToCaller) - .single_esdt(&id, nonce, &BigUint::from(1u8)) + .single_esdt( + &payment.token_identifier, + payment.token_nonce, + &BigUint::from(1u8), + ) .transfer(); } diff --git a/contracts/examples/order-book/pair/src/validation.rs b/contracts/examples/order-book/pair/src/validation.rs index 488edbdb24..306f299014 100644 --- a/contracts/examples/order-book/pair/src/validation.rs +++ b/contracts/examples/order-book/pair/src/validation.rs @@ -72,22 +72,28 @@ pub trait ValidationModule: common::CommonModule { let (token_id, amount) = self.call_value().single_fungible_esdt(); let second_token_id = self.second_token_id().get(); require!( - token_id == second_token_id, + *token_id == second_token_id, "Token in and second token id should be the same" ); - Payment { token_id, amount } + Payment { + token_id: token_id.clone(), + amount: amount.clone(), + } } fn require_valid_sell_payment(&self) -> Payment { let (token_id, amount) = self.call_value().single_fungible_esdt(); let first_token_id = self.first_token_id().get(); require!( - token_id == first_token_id, + *token_id == first_token_id, "Token in and first token id should be the same" ); - Payment { token_id, amount } + Payment { + token_id: token_id.clone(), + amount: amount.clone(), + } } fn require_valid_match_input_order_ids(&self, order_ids: &ManagedVec) { diff --git a/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs b/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs index 510212891b..98734c0fb0 100644 --- a/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs +++ b/contracts/examples/seed-nft-minter/src/seed_nft_minter.rs @@ -102,7 +102,7 @@ pub trait SeedNftMinter: } else { esdt_payments .try_get(0) - .map(|esdt_payment| esdt_payment.amount) + .map(|esdt_payment| esdt_payment.amount.clone()) .unwrap_or_default() }; total_amount += amount; diff --git a/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs b/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs index 6eccdd3f83..feeecb2b33 100644 --- a/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs +++ b/contracts/feature-tests/basic-features/src/storage_mapper_fungible_token.rs @@ -61,8 +61,9 @@ pub trait FungibleTokenMapperFeatures: fn custom_issue_non_zero_supply_cb(&self, #[call_result] result: ManagedAsyncCallResult<()>) { match result { ManagedAsyncCallResult::Ok(()) => { - let token_identifier = self.call_value().single_esdt().token_identifier; - self.fungible_token_mapper().set_token_id(token_identifier); + let token_identifier = &self.call_value().single_esdt().token_identifier; + self.fungible_token_mapper() + .set_token_id(token_identifier.clone()); }, ManagedAsyncCallResult::Err(_) => { self.fungible_token_mapper().clear(); @@ -128,9 +129,9 @@ pub trait FungibleTokenMapperFeatures: #[payable("*")] #[endpoint] fn require_same_token_fungible(&self) { - let payment_token = self.call_value().single_esdt().token_identifier; + let payment_token = &self.call_value().single_esdt().token_identifier; self.fungible_token_mapper() - .require_same_token(&payment_token); + .require_same_token(payment_token); } #[payable("*")] diff --git a/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs b/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs index bf77e7ab4e..f4d09b555e 100644 --- a/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs +++ b/contracts/feature-tests/composability/esdt-contract-pair/first-contract/src/lib.rs @@ -25,7 +25,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); @@ -45,13 +45,13 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); self.call_esdt_second_contract( &expected_token_identifier, - &(esdt_value / 2u32), + &(esdt_value.clone() / 2u32), &self.get_second_contract_address(), &ManagedBuffer::from(SECOND_CONTRACT_ACCEPT_ESDT_PAYMENT), &ManagedVec::new(), @@ -65,7 +65,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); @@ -86,7 +86,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); @@ -107,7 +107,7 @@ pub trait FirstContract { let expected_token_identifier = self.get_contract_esdt_token_identifier(); require!( - actual_token_identifier == expected_token_identifier, + *actual_token_identifier == expected_token_identifier, "Wrong esdt token" ); diff --git a/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs b/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs index e763a222af..dcb634f054 100644 --- a/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs +++ b/contracts/feature-tests/composability/execute-on-dest-esdt-issue-callback/child/src/lib.rs @@ -46,7 +46,7 @@ pub trait Child { #[callback] fn esdt_issue_callback(&self, #[call_result] _result: IgnoreValue) { let (token_identifier, _amount) = self.call_value().single_fungible_esdt(); - self.wrapped_egld_token_identifier().set(&token_identifier); + self.wrapped_egld_token_identifier().set(token_identifier); } // storage diff --git a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs index e789e4644f..645f173b28 100644 --- a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs +++ b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_call_transf_exec_legacy.rs @@ -28,7 +28,7 @@ pub trait ForwarderTransferExecuteModule { self.vault_proxy() .contract(to) .accept_funds() - .payment((payment.token_identifier, 0, payment.amount)) + .single_esdt(&payment.token_identifier, 0, &payment.amount) .transfer_execute(); } diff --git a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs index 6fa20793d8..7ba477b448 100644 --- a/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs +++ b/contracts/feature-tests/composability/forwarder-legacy/src/fwd_esdt_legacy.rs @@ -39,8 +39,8 @@ pub trait ForwarderEsdtModule: fwd_storage_legacy::ForwarderStorageModule { #[endpoint] fn send_esdt_with_fees(&self, to: ManagedAddress, percentage_fees: BigUint) { let (token_id, payment) = self.call_value().single_fungible_esdt(); - let fees = &payment * &percentage_fees / PERCENTAGE_TOTAL; - let amount_to_send = payment - fees; + let fees = &*payment * &percentage_fees / PERCENTAGE_TOTAL; + let amount_to_send = payment.clone() - fees; self.send().direct_esdt(&to, &token_id, 0, &amount_to_send); } diff --git a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs index a436e97b7a..796bea5684 100644 --- a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs +++ b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw.rs @@ -37,9 +37,9 @@ pub trait ForwarderRaw: } else { for payment in payments.iter() { let _ = self.callback_payments().push(&( - EgldOrEsdtTokenIdentifier::esdt(payment.token_identifier), + EgldOrEsdtTokenIdentifier::esdt(payment.token_identifier.clone()), payment.token_nonce, - payment.amount, + payment.amount.clone(), )); } } diff --git a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs index 47240f3c52..df66f30a30 100644 --- a/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs +++ b/contracts/feature-tests/composability/forwarder-raw/src/forwarder_raw_async.rs @@ -114,8 +114,8 @@ pub trait ForwarderRawAsync: super::forwarder_raw_common::ForwarderRawCommon { let (token, payment) = self.call_value().single_fungible_esdt(); self.forward_contract_call( to, - EgldOrEsdtTokenIdentifier::esdt(token), - payment, + EgldOrEsdtTokenIdentifier::esdt(token.clone()), + payment.clone(), endpoint_name, args, ) diff --git a/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs b/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs index a4dbaa28b1..425ca07053 100644 --- a/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs +++ b/contracts/feature-tests/composability/forwarder/src/fwd_esdt.rs @@ -42,8 +42,8 @@ pub trait ForwarderEsdtModule: fwd_storage::ForwarderStorageModule { #[endpoint] fn send_esdt_with_fees(&self, to: ManagedAddress, percentage_fees: BigUint) { let (token_id, payment) = self.call_value().single_fungible_esdt(); - let fees = &payment * &percentage_fees / PERCENTAGE_TOTAL; - let amount_to_send = payment - fees; + let fees = percentage_fees * &*payment / PERCENTAGE_TOTAL; + let amount_to_send = payment.clone() - fees; self.tx() .to(&to) diff --git a/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs b/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs index 2634e2c5ac..ba73843958 100644 --- a/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs +++ b/contracts/feature-tests/composability/promises-features/src/fwd_call_promises_bt.rs @@ -45,15 +45,19 @@ pub trait CallPromisesBackTransfersModule: common::CommonModule { } for esdt_transfer in &back_transfers.esdt_payments { - let (token, nonce, payment) = esdt_transfer.into_tuple(); - let esdt_token_id = EgldOrEsdtTokenIdentifier::esdt(token); - self.retrieve_funds_callback_event(&esdt_token_id, nonce, &payment); + let esdt_token_id = + EgldOrEsdtTokenIdentifier::esdt(esdt_transfer.token_identifier.clone()); + self.retrieve_funds_callback_event( + &esdt_token_id, + esdt_transfer.token_nonce, + &esdt_transfer.amount, + ); let _ = self.callback_data().push(&CallbackData { callback_name: ManagedBuffer::from(b"retrieve_funds_callback"), token_identifier: esdt_token_id, - token_nonce: nonce, - token_amount: payment, + token_nonce: esdt_transfer.token_nonce, + token_amount: esdt_transfer.amount.clone(), args: ManagedVec::new(), }); } diff --git a/contracts/feature-tests/composability/vault/src/vault.rs b/contracts/feature-tests/composability/vault/src/vault.rs index 37b0d65b9e..d6f3feb81b 100644 --- a/contracts/feature-tests/composability/vault/src/vault.rs +++ b/contracts/feature-tests/composability/vault/src/vault.rs @@ -240,9 +240,9 @@ pub trait Vault { ); new_tokens.push(EsdtTokenPayment::new( - payment.token_identifier, + payment.token_identifier.clone(), new_token_nonce, - payment.amount, + payment.amount.clone(), )); } diff --git a/contracts/feature-tests/payable-features/src/payable_features.rs b/contracts/feature-tests/payable-features/src/payable_features.rs index d8296f8592..6cad32b135 100644 --- a/contracts/feature-tests/payable-features/src/payable_features.rs +++ b/contracts/feature-tests/payable-features/src/payable_features.rs @@ -35,7 +35,7 @@ pub trait PayableFeatures { #[payable("*")] fn payment_array_3(&self) -> MultiValue3 { let [payment_a, payment_b, payment_c] = self.call_value().multi_esdt(); - (payment_a, payment_b, payment_c).into() + (payment_a.clone(), payment_b.clone(), payment_c.clone()).into() } #[endpoint] @@ -129,7 +129,7 @@ pub trait PayableFeatures { &self, #[payment] payment: BigUint, ) -> MultiValue2 { - let token = self.call_value().single_esdt().token_identifier; + let token = self.call_value().single_esdt().token_identifier.clone(); (payment, token).into() } @@ -140,14 +140,14 @@ pub trait PayableFeatures { #[payment_token] token: EgldOrEsdtTokenIdentifier, ) -> MultiValue2 { let payment = self.call_value().single_esdt(); - (payment.amount, token).into() + (payment.amount.clone(), token).into() } #[endpoint] #[payable("PAYABLE-FEATURES-TOKEN")] fn payable_token_4(&self) -> MultiValue2 { - let payment = self.call_value().single_esdt().amount; - let token = self.call_value().single_esdt().token_identifier; + let payment = self.call_value().single_esdt().amount.clone(); + let token = self.call_value().single_esdt().token_identifier.clone(); (payment, token).into() } } diff --git a/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs b/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs index ffc411dad0..9d7be05858 100644 --- a/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs +++ b/contracts/feature-tests/rust-testing-framework-tester/src/lib.rs @@ -71,7 +71,7 @@ pub trait RustTestingFrameworkTester: dummy_module::DummyModule { #[endpoint] fn receive_esdt(&self) -> (TokenIdentifier, BigUint) { let payment = self.call_value().single_esdt(); - (payment.token_identifier, payment.amount) + (payment.token_identifier.clone(), payment.amount.clone()) } #[payable("*")] @@ -84,7 +84,7 @@ pub trait RustTestingFrameworkTester: dummy_module::DummyModule { #[endpoint] fn receive_esdt_half(&self) { let payment = self.call_value().single_esdt(); - let amount = payment.amount / 2u32; + let amount = &payment.amount / 2u32; self.tx() .to(ToCaller) diff --git a/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs b/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs index b5d7711481..04f7b9cc7d 100644 --- a/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs +++ b/contracts/feature-tests/use-module/src/token_merge_mod_impl.rs @@ -55,7 +55,7 @@ pub trait TokenMergeModImpl: ) -> ManagedVec { let payment = self.call_value().single_esdt(); let attributes_creator = DefaultMergedAttributesWrapper::new(); - self.split_token_partial(payment, tokens_to_remove, &attributes_creator) + self.split_token_partial(payment.clone(), tokens_to_remove, &attributes_creator) } } diff --git a/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs b/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs index 382e1fbc28..1c51d47905 100644 --- a/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs +++ b/contracts/modules/src/bonding_curve/utils/owner_endpoints.rs @@ -90,11 +90,14 @@ pub trait OwnerEndpointsModule: storage::StorageModule + events::EventsModule { + PartialEq + Default, { - let (identifier, nonce, amount) = self.call_value().single_esdt().into_tuple(); + let esdt_payment = self.call_value().single_esdt(); + let identifier = &esdt_payment.token_identifier; + let nonce = esdt_payment.token_nonce; + let amount = &esdt_payment.amount; let caller = self.blockchain().get_caller(); let mut set_payment = EgldOrEsdtTokenIdentifier::egld(); - if self.bonding_curve(&identifier).is_empty() { + if self.bonding_curve(identifier).is_empty() { match payment_token { OptionalValue::Some(token) => set_payment = EgldOrEsdtTokenIdentifier::esdt(token), OptionalValue::None => { @@ -102,27 +105,27 @@ pub trait OwnerEndpointsModule: storage::StorageModule + events::EventsModule { }, }; } - if self.token_details(&identifier).is_empty() { + if self.token_details(identifier).is_empty() { let nonces = ManagedVec::from_single_item(nonce); - self.token_details(&identifier).set(&TokenOwnershipData { + self.token_details(identifier).set(&TokenOwnershipData { token_nonces: nonces, owner: caller.clone(), }); } else { - let mut details = self.token_details(&identifier).get(); + let mut details = self.token_details(identifier).get(); require!( details.owner == caller, "The token was already deposited by another address" ); if !details.token_nonces.contains(&nonce) { details.token_nonces.push(nonce); - self.token_details(&identifier).set(&details); + self.token_details(identifier).set(&details); } } - self.set_curve_storage::(&identifier, amount.clone(), set_payment); + self.set_curve_storage::(identifier, amount.clone(), set_payment); self.owned_tokens(&caller).insert(identifier.clone()); - self.nonce_amount(&identifier, nonce) + self.nonce_amount(identifier, nonce) .update(|current_amount| *current_amount += amount); } diff --git a/contracts/modules/src/bonding_curve/utils/user_endpoints.rs b/contracts/modules/src/bonding_curve/utils/user_endpoints.rs index f627d26256..a51236512a 100644 --- a/contracts/modules/src/bonding_curve/utils/user_endpoints.rs +++ b/contracts/modules/src/bonding_curve/utils/user_endpoints.rs @@ -21,11 +21,14 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { + PartialEq + Default, { - let (offered_token, nonce, sell_amount) = self.call_value().single_esdt().into_tuple(); - let _ = self.check_owned_return_payment_token::(&offered_token, &sell_amount); + let esdt_payment = self.call_value().single_esdt(); + let offered_token = &esdt_payment.token_identifier; + let nonce = esdt_payment.token_nonce; + let sell_amount = &esdt_payment.amount; + let _ = self.check_owned_return_payment_token::(offered_token, sell_amount); let (calculated_price, payment_token) = - self.bonding_curve(&offered_token).update(|buffer| { + self.bonding_curve(offered_token).update(|buffer| { let serializer = ManagedSerializer::new(); let mut bonding_curve: BondingCurve = @@ -35,9 +38,9 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { bonding_curve.sell_availability, "Selling is not available on this token" ); - let price = self.compute_sell_price::(&offered_token, &sell_amount); + let price = self.compute_sell_price::(offered_token, sell_amount); bonding_curve.payment.amount -= &price; - bonding_curve.arguments.balance += &sell_amount; + bonding_curve.arguments.balance += sell_amount; let payment_token = bonding_curve.payment_token(); *buffer = serializer.top_encode_to_managed_buffer(&bonding_curve); (price, payment_token) @@ -45,7 +48,7 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { let caller = self.blockchain().get_caller(); - self.nonce_amount(&offered_token, nonce) + self.nonce_amount(offered_token, nonce) .update(|val| *val += sell_amount); self.tx() @@ -53,7 +56,7 @@ pub trait UserEndpointsModule: storage::StorageModule + events::EventsModule { .egld_or_single_esdt(&payment_token, 0u64, &calculated_price) .transfer(); - self.token_details(&offered_token) + self.token_details(offered_token) .update(|details| details.add_nonce(nonce)); self.sell_token_event(&caller, &calculated_price); diff --git a/contracts/modules/src/default_issue_callbacks.rs b/contracts/modules/src/default_issue_callbacks.rs index 6edd7abeec..8e9e1c6e90 100644 --- a/contracts/modules/src/default_issue_callbacks.rs +++ b/contracts/modules/src/default_issue_callbacks.rs @@ -35,7 +35,7 @@ pub trait DefaultIssueCallbacksModule { let key = StorageKey::from(storage_key); match result { ManagedAsyncCallResult::Ok(()) => { - let token_id = self.call_value().single_esdt().token_identifier; + let token_id = self.call_value().single_esdt().token_identifier.clone(); storage_set(key.as_ref(), &TokenMapperState::Token(token_id)); }, ManagedAsyncCallResult::Err(_) => { diff --git a/contracts/modules/src/governance/mod.rs b/contracts/modules/src/governance/mod.rs index 85dfa4e1b4..4d407cf593 100644 --- a/contracts/modules/src/governance/mod.rs +++ b/contracts/modules/src/governance/mod.rs @@ -435,7 +435,7 @@ pub trait GovernanceModule: payment.token_identifier == self.governance_token_id().get(), "Only Governance token accepted as payment" ); - payment + payment.clone() } fn require_valid_proposal_id(&self, proposal_id: usize) { diff --git a/contracts/modules/src/token_merge/mod.rs b/contracts/modules/src/token_merge/mod.rs index b940980dfe..dad6665c0d 100644 --- a/contracts/modules/src/token_merge/mod.rs +++ b/contracts/modules/src/token_merge/mod.rs @@ -69,7 +69,7 @@ pub trait TokenMergeModule: } for single_token_instance in single_tokens { - all_merged_instances.add_or_update_instance(single_token_instance); + all_merged_instances.add_or_update_instance(single_token_instance.clone()); } let merged_token_payment = diff --git a/framework/base/src/contract_base/wrappers/call_value_wrapper.rs b/framework/base/src/contract_base/wrappers/call_value_wrapper.rs index 3006805ef0..3fb85e1658 100644 --- a/framework/base/src/contract_base/wrappers/call_value_wrapper.rs +++ b/framework/base/src/contract_base/wrappers/call_value_wrapper.rs @@ -8,8 +8,8 @@ use crate::{ err_msg, types::{ BigUint, ConstDecimals, EgldOrEsdtTokenIdentifier, EgldOrEsdtTokenPayment, - EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedDecimal, ManagedRef, ManagedVec, - TokenIdentifier, + EgldOrMultiEsdtPayment, EsdtTokenPayment, ManagedDecimal, ManagedRef, ManagedType, + ManagedVec, ManagedVecRef, TokenIdentifier, }, }; @@ -71,12 +71,12 @@ where /// Can be used to extract all payments in one line like this: /// /// `let [payment_a, payment_b, payment_c] = self.call_value().multi_esdt();`. - pub fn multi_esdt(&self) -> [EsdtTokenPayment; N] { - self.all_esdt_transfers() - .to_array_of_refs::() - .unwrap_or_else(|| { - A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()) - }) + pub fn multi_esdt(&self) -> [ManagedVecRef<'static, EsdtTokenPayment>; N] { + let esdt_transfers = self.all_esdt_transfers(); + let array = esdt_transfers.to_array_of_refs::().unwrap_or_else(|| { + A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()) + }); + unsafe { core::mem::transmute(array) } } /// Expects precisely one ESDT token transfer, fungible or not. @@ -84,9 +84,13 @@ where /// Will return the received ESDT payment. /// /// The amount cannot be 0, since that would not qualify as an ESDT transfer. - pub fn single_esdt(&self) -> EsdtTokenPayment { - let [payments] = self.multi_esdt(); - payments + pub fn single_esdt(&self) -> ManagedVecRef<'static, EsdtTokenPayment> { + let esdt_transfers = self.all_esdt_transfers(); + if esdt_transfers.len() != 1 { + A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()) + } + let value = esdt_transfers.get(0); + unsafe { core::mem::transmute(value) } } /// Expects precisely one fungible ESDT token transfer. @@ -94,12 +98,23 @@ where /// Returns the token ID and the amount for fungible ESDT transfers. /// /// The amount cannot be 0, since that would not qualify as an ESDT transfer. - pub fn single_fungible_esdt(&self) -> (TokenIdentifier, BigUint) { + pub fn single_fungible_esdt( + &self, + ) -> ( + ManagedRef<'static, A, TokenIdentifier>, + ManagedRef<'static, A, BigUint>, + ) { let payment = self.single_esdt(); if payment.token_nonce != 0 { A::error_api_impl().signal_error(err_msg::FUNGIBLE_TOKEN_EXPECTED_ERR_MSG.as_bytes()); } - (payment.token_identifier, payment.amount) + + unsafe { + ( + ManagedRef::wrap_handle(payment.token_identifier.get_handle()), + ManagedRef::wrap_handle(payment.amount.get_handle()), + ) + } } /// Accepts and returns either an EGLD payment, or a single ESDT token. @@ -115,7 +130,7 @@ where token_nonce: 0, amount: self.egld_value().clone_value(), }, - 1 => esdt_transfers.get(0).into(), + 1 => esdt_transfers.get(0).clone().into(), _ => A::error_api_impl().signal_error(err_msg::INCORRECT_NUM_ESDT_TRANSFERS.as_bytes()), } } diff --git a/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs b/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs index fe4225d9a4..6a66c3e0f5 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/contract_call_convert.rs @@ -20,7 +20,7 @@ where ) -> Self { match payments.len() { 0 => self, - 1 => self.convert_to_single_transfer_esdt_call(payments.get(0)), + 1 => self.convert_to_single_transfer_esdt_call(payments.get(0).clone()), _ => self.convert_to_multi_transfer_esdt_call(payments), } } diff --git a/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs b/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs index f0f1722f24..e2281a4b38 100644 --- a/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs +++ b/framework/base/src/types/interaction/contract_call_legacy/contract_call_exec.rs @@ -208,7 +208,7 @@ where pub(super) fn transfer_execute_esdt(self, payments: ManagedVec>) { match payments.len() { 0 => self.transfer_execute_egld(BigUint::zero()), - 1 => self.transfer_execute_single_esdt(payments.get(0)), + 1 => self.transfer_execute_single_esdt(payments.get(0).clone()), _ => self.transfer_execute_multi_esdt(payments), } } diff --git a/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs b/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs index 996891c1c2..10a4c59003 100644 --- a/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs +++ b/framework/base/src/types/interaction/result_handlers/returns_bt_single_esdt.rs @@ -28,6 +28,7 @@ where Env::Api::error_api_impl().signal_error(b"Back transfers expected to be a single ESDT") } - esdt_payments.get(0) + let x = esdt_payments.get(0).clone(); + x } } diff --git a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs index b676fa42cc..041ddd1f64 100644 --- a/framework/base/src/types/managed/wrapped/esdt_token_payment.rs +++ b/framework/base/src/types/managed/wrapped/esdt_token_payment.rs @@ -15,7 +15,7 @@ use crate::{ use super::{ managed_vec_item_read_from_payload_index, managed_vec_item_save_to_payload_index, ManagedVec, - ManagedVecItemPayloadBuffer, + ManagedVecItemPayloadBuffer, ManagedVecRef, }; #[type_abi] @@ -168,7 +168,7 @@ impl IntoMultiValue for EsdtTokenPayment { impl ManagedVecItem for EsdtTokenPayment { type PAYLOAD = ManagedVecItemPayloadBuffer<16>; const SKIPS_RESERIALIZATION: bool = false; - type Ref<'a> = Self; + type Ref<'a> = ManagedVecRef<'a, Self>; fn read_from_payload(payload: &Self::PAYLOAD) -> Self { let mut index = 0; @@ -182,8 +182,7 @@ impl ManagedVecItem for EsdtTokenPayment { } unsafe fn borrow_from_payload<'a>(payload: &Self::PAYLOAD) -> Self::Ref<'a> { - // TODO: managed ref - Self::read_from_payload(payload) + ManagedVecRef::new(Self::read_from_payload(payload)) } fn save_to_payload(self, payload: &mut Self::PAYLOAD) { diff --git a/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs b/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs index 0661806509..752437a130 100644 --- a/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs +++ b/framework/scenario/src/scenario/tx_to_step/tx_to_step_call.rs @@ -71,7 +71,7 @@ where step.tx.esdt_value = full_payment_data .multi_esdt .iter() - .map(TxESDT::from) + .map(|item| TxESDT::from(item.clone())) .collect(); } diff --git a/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs b/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs index d2baa6bc6c..782f86dc16 100644 --- a/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs +++ b/framework/scenario/src/scenario/tx_to_step/tx_to_step_transfer.rs @@ -52,7 +52,7 @@ where step.tx.esdt_value = full_payment_data .multi_esdt .iter() - .map(TxESDT::from) + .map(|item| TxESDT::from(item.clone())) .collect(); }