Skip to content

Commit

Permalink
add WeightV2 struct, implement EvmData traits
Browse files Browse the repository at this point in the history
  • Loading branch information
gitofdeepanshu committed Aug 10, 2023
1 parent 2a21e1e commit 9719ae9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 41 deletions.
39 changes: 38 additions & 1 deletion precompiles/utils/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::Address;
use sp_core::U256;
use {
crate::{bytes::*, revert, EvmData, EvmDataReader, EvmDataWriter, EvmResult},
frame_support::{ensure, traits::ConstU32},
frame_support::{ensure, pallet_prelude::Weight, traits::ConstU32},
sp_core::H256,
sp_std::vec::Vec,
xcm::latest::{Junction, Junctions, MultiLocation, NetworkId},
Expand Down Expand Up @@ -353,6 +353,43 @@ impl EvmData for MultiLocation {
<(u8, Junctions)>::has_static_size()
}
}

#[derive(Debug, Clone)]
pub struct WeightV2 {
ref_time: u64,
proof_size: u64,
}
impl WeightV2 {
pub fn from(ref_time: u64, proof_size: u64) -> Self {
WeightV2 {
ref_time,
proof_size,
}
}
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
}
}
impl EvmData for WeightV2 {
fn read(reader: &mut EvmDataReader) -> EvmResult<Self> {
let (ref_time, proof_size) = reader.read()?;
Ok(WeightV2 {
ref_time,
proof_size,
})
}

fn write(writer: &mut EvmDataWriter, value: Self) {
EvmData::write(writer, (value.ref_time, value.proof_size));
}

fn has_static_size() -> bool {
<(U256, U256)>::has_static_size()
}
}
#[derive(Debug)]
pub struct EvmMultiAsset {
location: MultiLocation,
Expand Down
7 changes: 6 additions & 1 deletion precompiles/xcm/XCM_v2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ interface XCM {
uint8 parents;
bytes[] interior;
}

struct WeightV2{
uint64 ref_time;
uint64 proof_size;
}

/**
* @dev Withdraw assets using PalletXCM call.
Expand Down Expand Up @@ -45,7 +50,7 @@ interface XCM {
address payment_asset_id,
uint256 payment_amount,
bytes calldata call,
uint64 transact_weight
WeightV2 transact_weight
) external returns (bool);

/**
Expand Down
17 changes: 11 additions & 6 deletions precompiles/xcm/Xtokens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface Xtokens {
bytes[] interior;
}

struct WeightV2{
uint64 ref_time;
uint64 proof_size;
}

// A MultiAsset is defined by a multilocation and an amount
struct MultiAsset {
Multilocation location;
Expand All @@ -35,7 +40,7 @@ interface Xtokens {
address currencyAddress,
uint256 amount,
Multilocation memory destination,
uint64 weight
WeightV2 memory weight
) external returns (bool);

/// Transfer a token through XCM based on its currencyId specifying fee
Expand All @@ -50,7 +55,7 @@ interface Xtokens {
uint256 amount,
uint256 fee,
Multilocation memory destination,
uint64 weight
WeightV2 memory weight
) external returns (bool);

/// Transfer a token through XCM based on its MultiLocation
Expand All @@ -65,7 +70,7 @@ interface Xtokens {
Multilocation memory asset,
uint256 amount,
Multilocation memory destination,
uint64 weight
WeightV2 memory weight
) external returns (bool);

/// Transfer a token through XCM based on its MultiLocation specifying fee
Expand All @@ -81,7 +86,7 @@ interface Xtokens {
uint256 amount,
uint256 fee,
Multilocation memory destination,
uint64 weight
WeightV2 memory weight
) external returns (bool);

/// Transfer several tokens at once through XCM based on its address specifying fee
Expand All @@ -95,7 +100,7 @@ interface Xtokens {
Currency[] memory currencies,
uint32 feeItem,
Multilocation memory destination,
uint64 weight
WeightV2 memory weight
) external returns (bool);

/// Transfer several tokens at once through XCM based on its location specifying fee
Expand All @@ -109,6 +114,6 @@ interface Xtokens {
MultiAsset[] memory assets,
uint32 feeItem,
Multilocation memory destination,
uint64 weight
WeightV2 memory weight
) external returns (bool);
}
46 changes: 23 additions & 23 deletions precompiles/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use precompile_utils::{
bytes::BoundedBytes,
data::BoundedVec,
revert, succeed,
xcm::{Currency, EvmMultiAsset},
xcm::{Currency, EvmMultiAsset, WeightV2},
Address, Bytes, EvmDataWriter, EvmResult, FunctionModifier, PrecompileHandleExt, RuntimeHelper,
};
#[cfg(test)]
Expand Down Expand Up @@ -80,7 +80,7 @@ pub enum Action {
/// Dummy H160 address representing native currency (e.g. ASTR or SDN)
const NATIVE_ADDRESS: H160 = H160::zero();
/// Dummy default 64KB
const DEFAULT_PROOF_SIZE: u64 = 1024 * 64;
const DEFAULT_PROOF_SIZE: u64 = 1024 * 256;

pub type XBalanceOf<Runtime> = <Runtime as orml_xtokens::Config>::Balance;

Expand Down Expand Up @@ -524,10 +524,10 @@ where
let fee_amount = input.read::<U256>()?;

let remote_call: Vec<u8> = input.read::<Bytes>()?.into();
let transact_weight = input.read::<u64>()?;
let transact_weight = input.read::<WeightV2>()?;

log::trace!(target: "xcm-precompile::remote_transact", "Raw arguments: dest: {:?}, fee_asset_addr: {:?} \
fee_amount: {:?}, remote_call: {:?}, transact_weight: {}",
fee_amount: {:?}, remote_call: {:?}, transact_weight: {:?}",
dest, fee_asset_addr, fee_amount, remote_call, transact_weight);

let fee_asset = {
Expand Down Expand Up @@ -568,7 +568,7 @@ where
},
Transact {
origin_kind: OriginKind::SovereignAccount,
require_weight_at_most: Weight::from_parts(transact_weight, DEFAULT_PROOF_SIZE),
require_weight_at_most: transact_weight.get_weight(),
call: remote_call.into(),
},
]);
Expand Down Expand Up @@ -704,14 +704,14 @@ where
.try_into()
.map_err(|_| revert("error converting amount_of_tokens, maybe value too large"))?;
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<u64>()?;
let weight = input.read::<WeightV2>()?;

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 == u64::MAX {
let dest_weight_limit = if weight.is_max() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(Weight::from_parts(weight, DEFAULT_PROOF_SIZE))
WeightLimit::Limited(weight.get_weight())
};

log::trace!(target: "xcm-precompile::transfer", "Raw arguments: currency_address: {:?}, amount_of_tokens: {:?}, destination: {:?}, \
Expand Down Expand Up @@ -752,14 +752,14 @@ where
.map_err(|_| revert("can't convert fee"))?;

let destination = input.read::<MultiLocation>()?;
let weight = input.read::<u64>()?;
let weight = input.read::<WeightV2>()?;

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 == u64::MAX {
let dest_weight_limit = if weight.is_max() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(Weight::from_parts(weight, DEFAULT_PROOF_SIZE))
WeightLimit::Limited(weight.get_weight())
};

log::trace!(target: "xcm-precompile::transfer_with_fee", "Raw arguments: currency_address: {:?}, amount_of_tokens: {:?}, destination: {:?}, \
Expand Down Expand Up @@ -796,12 +796,12 @@ where
.try_into()
.map_err(|_| revert("error converting amount_of_tokens, maybe value too large"))?;
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<u64>()?;
let weight = input.read::<WeightV2>()?;

let dest_weight_limit = if weight == u64::MAX {
let dest_weight_limit = if weight.is_max() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(Weight::from_parts(weight, DEFAULT_PROOF_SIZE))
WeightLimit::Limited(weight.get_weight())
};

log::trace!(target: "xcm-precompile::transfer_multiasset", "Raw arguments: asset_location: {:?}, amount_of_tokens: {:?}, destination: {:?}, \
Expand Down Expand Up @@ -845,12 +845,12 @@ where
.try_into()
.map_err(|_| revert("can't convert fee"))?;
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<u64>()?;
let weight = input.read::<WeightV2>()?;

let dest_weight_limit = if weight == u64::MAX {
let dest_weight_limit = if weight.is_max() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(Weight::from_parts(weight, DEFAULT_PROOF_SIZE))
WeightLimit::Limited(weight.get_weight())
};

log::trace!(target: "xcm-precompile::transfer_multiasset_with_fee", "Raw arguments: asset_location: {:?}, amount_of_tokens: {:?}, fee{:?}, destination: {:?}, \
Expand Down Expand Up @@ -892,7 +892,7 @@ where
.into();
let fee_item = input.read::<u32>()?;
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<u64>()?;
let weight = input.read::<WeightV2>()?;

let currencies = currencies
.into_iter()
Expand All @@ -911,10 +911,10 @@ where
))
})
.collect::<EvmResult<_>>()?;
let dest_weight_limit = if weight == u64::MAX {
let dest_weight_limit = if weight.is_max() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(Weight::from_parts(weight, DEFAULT_PROOF_SIZE))
WeightLimit::Limited(weight.get_weight())
};

log::trace!(target: "xcm-precompile::transfer_multi_currencies", "Raw arguments: currencies: {:?}, fee_item{:?}, destination: {:?}, \
Expand Down Expand Up @@ -948,12 +948,12 @@ where
.into();
let fee_item = input.read::<u32>()?;
let destination = input.read::<MultiLocation>()?;
let weight = input.read::<u64>()?;
let weight = input.read::<WeightV2>()?;

let dest_weight_limit = if weight == u64::MAX {
let dest_weight_limit = if weight.is_max() {
WeightLimit::Unlimited
} else {
WeightLimit::Limited(Weight::from_parts(weight, DEFAULT_PROOF_SIZE))
WeightLimit::Limited(weight.get_weight())
};

log::trace!(target: "xcm-precompile::transfer_multi_assets", "Raw arguments: assets: {:?}, fee_item{:?}, destination: {:?}, \
Expand Down
Loading

0 comments on commit 9719ae9

Please sign in to comment.