From 20893c880c1cf9557d0a24b523e2467e8775486a Mon Sep 17 00:00:00 2001 From: Deepanshu Hooda Date: Wed, 4 Oct 2023 16:26:39 +0530 Subject: [PATCH] refactor, weight unlimite logic change --- precompiles/utils/src/xcm.rs | 5 +++-- precompiles/xcm/XCM_v2.sol | 38 +++++++++++++++--------------------- precompiles/xcm/src/lib.rs | 13 ++++++------ precompiles/xcm/src/tests.rs | 35 ++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 31 deletions(-) diff --git a/precompiles/utils/src/xcm.rs b/precompiles/utils/src/xcm.rs index d6072dc125..16897c774a 100644 --- a/precompiles/utils/src/xcm.rs +++ b/precompiles/utils/src/xcm.rs @@ -24,6 +24,7 @@ use crate::Address; use sp_core::U256; +use sp_runtime::traits::Zero; use { crate::{bytes::*, revert, EvmData, EvmDataReader, EvmDataWriter, EvmResult}, frame_support::{ensure, pallet_prelude::Weight, traits::ConstU32}, @@ -369,8 +370,8 @@ impl WeightV2 { pub fn get_weight(&self) -> Weight { Weight::from_parts(self.ref_time, self.proof_size) } - pub fn is_max(&self) -> bool { - self.ref_time == u64::MAX + pub fn is_zero(&self) -> bool { + self.ref_time.is_zero() } } impl EvmData for WeightV2 { diff --git a/precompiles/xcm/XCM_v2.sol b/precompiles/xcm/XCM_v2.sol index 53aa556d88..51487986a6 100644 --- a/precompiles/xcm/XCM_v2.sol +++ b/precompiles/xcm/XCM_v2.sol @@ -18,7 +18,7 @@ interface XCM { } // A MultiAsset is defined by a multilocation and an amount - struct EvmMultiAsset { + struct MultiAsset { Multilocation location; uint256 amount; } @@ -29,15 +29,14 @@ interface XCM { uint256 amount; } - /// Transfer a token through XCM based on its currencyId + /// Transfer a token through XCM based on its address /// /// @dev The token transfer burns/transfers the corresponding amount before sending /// @param currencyAddress The ERC20 address of the currency we want to transfer /// @param amount The amount of tokens we want to transfer /// @param destination The Multilocation to which we want to send the tokens - /// @param weight The weight we want to buy in the destination chain, to provide - /// unlimited weight, you should use maximum u64 value (i.e. 18446744073709551615 ) - /// for ref_time + /// @param weight The weight we want to buy in the destination chain, to set the + /// weightlimit to Unlimited, you should use the value 0 for ref_time function transfer( address currencyAddress, uint256 amount, @@ -45,15 +44,14 @@ interface XCM { WeightV2 memory weight ) external returns (bool); - /// Transfer a token through XCM based on its currencyId specifying fee + /// Transfer a token through XCM based on its address specifying fee /// /// @dev The token transfer burns/transfers the corresponding amount before sending /// @param currencyAddress The ERC20 address of the currency we want to transfer /// @param amount The amount of tokens we want to transfer /// @param destination The Multilocation to which we want to send the tokens - /// @param weight The weight we want to buy in the destination chain, to provide - /// unlimited weight, you should use maximum u64 value (i.e. 18446744073709551615 ) - /// for ref_time + /// @param weight The weight we want to buy in the destination chain, to set the + /// weightlimit to Unlimited, you should use the value 0 for ref_time function transfer_with_fee( address currencyAddress, uint256 amount, @@ -69,9 +67,8 @@ interface XCM { /// Currently only Concrete Fungible assets /// @param amount The amount of tokens we want to transfer /// @param destination The Multilocation to which we want to send the tokens - /// @param weight The weight we want to buy in the destination chain, to provide - /// unlimited weight, you should use maximum u64 value (i.e. 18446744073709551615 ) - /// for ref_time + /// @param weight The weight we want to buy in the destination chain, to set the + /// weightlimit to Unlimited, you should use the value 0 for ref_time function transfer_multiasset( Multilocation memory asset, uint256 amount, @@ -86,9 +83,8 @@ interface XCM { /// Currently only Concrete Fungible assets /// @param amount The amount of tokens we want to transfer /// @param destination The Multilocation to which we want to send the tokens - /// @param weight The weight we want to buy in the destination chain, to provide - /// unlimited weight, you should use maximum u64 value (i.e. 18446744073709551615 ) - /// for ref_time + /// @param weight The weight we want to buy in the destination chain, to set the + /// weightlimit to Unlimited, you should use the value 0 for ref_time function transfer_multiasset_with_fee( Multilocation memory asset, uint256 amount, @@ -103,9 +99,8 @@ interface XCM { /// @param currencies The currencies we want to transfer, defined by their address and amount. /// @param feeItem Which of the currencies to be used as fee /// @param destination The Multilocation to which we want to send the tokens - /// @param weight The weight we want to buy in the destination chain, to provide - /// unlimited weight, you should use maximum u64 value (i.e. 18446744073709551615 ) - /// for ref_time + /// @param weight The weight we want to buy in the destination chain, to set the + /// weightlimit to Unlimited, you should use the value 0 for ref_time function transfer_multi_currencies( Currency[] memory currencies, uint32 feeItem, @@ -119,11 +114,10 @@ interface XCM { /// @param assets The assets we want to transfer, defined by their location and amount. /// @param feeItem Which of the currencies to be used as fee /// @param destination The Multilocation to which we want to send the tokens - /// @param weight The weight we want to buy in the destination chain, to provide - /// unlimited weight, you should use maximum u64 value (i.e. 18446744073709551615 ) - /// for ref_time + /// @param weight The weight we want to buy in the destination chain, to set the + /// weightlimit to Unlimited, you should use the value 0 for ref_time function transfer_multi_assets( - EvmMultiAsset[] memory assets, + MultiAsset[] memory assets, uint32 feeItem, Multilocation memory destination, WeightV2 memory weight diff --git a/precompiles/xcm/src/lib.rs b/precompiles/xcm/src/lib.rs index 28e16f56cc..8da693ff9e 100644 --- a/precompiles/xcm/src/lib.rs +++ b/precompiles/xcm/src/lib.rs @@ -76,6 +76,7 @@ pub enum Action { /// Dummy H160 address representing native currency (e.g. ASTR or SDN) const NATIVE_ADDRESS: H160 = H160::zero(); +/// Default proof_size of 256KB const DEFAULT_PROOF_SIZE: u64 = 1024 * 256; pub type XBalanceOf = ::Balance; @@ -487,7 +488,7 @@ where let asset_id = Runtime::address_to_asset_id(currency_address.into()) .ok_or(revert("Failed to resolve fee asset id from address"))?; - let dest_weight_limit = if weight.is_max() { + let dest_weight_limit = if weight.is_zero() { WeightLimit::Unlimited } else { WeightLimit::Limited(weight.get_weight()) @@ -535,7 +536,7 @@ where let asset_id = Runtime::address_to_asset_id(currency_address.into()) .ok_or(revert("Failed to resolve fee asset id from address"))?; - let dest_weight_limit = if weight.is_max() { + let dest_weight_limit = if weight.is_zero() { WeightLimit::Unlimited } else { WeightLimit::Limited(weight.get_weight()) @@ -577,7 +578,7 @@ where let destination = input.read::()?; let weight = input.read::()?; - let dest_weight_limit = if weight.is_max() { + let dest_weight_limit = if weight.is_zero() { WeightLimit::Unlimited } else { WeightLimit::Limited(weight.get_weight()) @@ -625,7 +626,7 @@ where let destination = input.read::()?; let weight = input.read::()?; - let dest_weight_limit = if weight.is_max() { + let dest_weight_limit = if weight.is_zero() { WeightLimit::Unlimited } else { WeightLimit::Limited(weight.get_weight()) @@ -685,7 +686,7 @@ where )) }) .collect::>()?; - let dest_weight_limit = if weight.is_max() { + let dest_weight_limit = if weight.is_zero() { WeightLimit::Unlimited } else { WeightLimit::Limited(weight.get_weight()) @@ -724,7 +725,7 @@ where let destination = input.read::()?; let weight = input.read::()?; - let dest_weight_limit = if weight.is_max() { + let dest_weight_limit = if weight.is_zero() { WeightLimit::Unlimited } else { WeightLimit::Limited(weight.get_weight()) diff --git a/precompiles/xcm/src/tests.rs b/precompiles/xcm/src/tests.rs index e44f875965..91fa0e7ee0 100644 --- a/precompiles/xcm/src/tests.rs +++ b/precompiles/xcm/src/tests.rs @@ -482,6 +482,7 @@ mod xcm_new_interface_test { }, ), }; + let native_token_location: MultiLocation = (Here).into(); let amount = 4200u64; // relay token to relay @@ -524,7 +525,7 @@ mod xcm_new_interface_test { .write(relay_token_location) // zero address by convention .write(U256::from(amount)) .write(para_destination) - .write(weight) + .write(weight.clone()) .build(), ) .expect_no_logs() @@ -545,6 +546,38 @@ mod xcm_new_interface_test { // Assert that the events vector contains the one expected assert!(events().contains(&expected)); + + // native token to para + + precompiles() + .prepare_test( + TestAccount::Alice, + PRECOMPILE_ADDRESS, + EvmDataWriter::new_with_selector(Action::XtokensTransferMultiasset) + .write(native_token_location) // zero address by convention + .write(U256::from(amount)) + .write(para_destination) + .write(weight.clone()) + .build(), + ) + .expect_no_logs() + .execute_returns(EvmDataWriter::new().write(true).build()); + + let expected_asset: MultiAsset = MultiAsset { + id: AssetId::Concrete(native_token_location), + fun: Fungibility::Fungible(amount.into()), + }; + let expected: crate::mock::RuntimeEvent = + mock::RuntimeEvent::Xtokens(XtokensEvent::TransferredMultiAssets { + sender: TestAccount::Alice.into(), + assets: vec![expected_asset.clone()].into(), + fee: expected_asset, + dest: para_destination, + }) + .into(); + + // Assert that the events vector contains the one expected + assert!(events().contains(&expected)); }); }