Skip to content

Commit

Permalink
refactor, weight unlimite logic change
Browse files Browse the repository at this point in the history
  • Loading branch information
gitofdeepanshu committed Oct 4, 2023
1 parent 40e36d5 commit 20893c8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
5 changes: 3 additions & 2 deletions precompiles/utils/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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 {
Expand Down
38 changes: 16 additions & 22 deletions precompiles/xcm/XCM_v2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface XCM {
}

// A MultiAsset is defined by a multilocation and an amount
struct EvmMultiAsset {
struct MultiAsset {
Multilocation location;
uint256 amount;
}
Expand All @@ -29,31 +29,29 @@ 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,
Multilocation memory destination,
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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions precompiles/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime> = <Runtime as orml_xtokens::Config>::Balance;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -577,7 +578,7 @@ where
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<WeightV2>()?;

let dest_weight_limit = if weight.is_max() {
let dest_weight_limit = if weight.is_zero() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(weight.get_weight())
Expand Down Expand Up @@ -625,7 +626,7 @@ where
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<WeightV2>()?;

let dest_weight_limit = if weight.is_max() {
let dest_weight_limit = if weight.is_zero() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(weight.get_weight())
Expand Down Expand Up @@ -685,7 +686,7 @@ where
))
})
.collect::<EvmResult<_>>()?;
let dest_weight_limit = if weight.is_max() {
let dest_weight_limit = if weight.is_zero() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(weight.get_weight())
Expand Down Expand Up @@ -724,7 +725,7 @@ where
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<WeightV2>()?;

let dest_weight_limit = if weight.is_max() {
let dest_weight_limit = if weight.is_zero() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(weight.get_weight())
Expand Down
35 changes: 34 additions & 1 deletion precompiles/xcm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ mod xcm_new_interface_test {
},
),
};
let native_token_location: MultiLocation = (Here).into();

let amount = 4200u64;
// relay token to relay
Expand Down Expand Up @@ -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()
Expand All @@ -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));
});
}

Expand Down

0 comments on commit 20893c8

Please sign in to comment.