Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xcm trap assets ed #1654

Merged
merged 9 commits into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 11 additions & 50 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use sp_runtime::{
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, DispatchResult, FixedPointNumber, Perbill, Percent, Permill, Perquintill,
};
use sp_std::{marker::PhantomData, prelude::*};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
Expand All @@ -58,8 +58,7 @@ use module_relaychain::RelayChainCallBuilder;
use module_support::{DispatchableTask, ForeignAssetIdMapping};
use module_transaction_payment::{Multiplier, TargetedFeeAdjustment};
use orml_traits::{
create_median_value_data_provider, parameter_type_with_key, DataFeeder, DataProviderExtended, GetByKey,
MultiCurrency,
create_median_value_data_provider, parameter_type_with_key, DataFeeder, DataProviderExtended, MultiCurrency,
};
use pallet_transaction_payment::RuntimeDispatchInfo;

Expand Down Expand Up @@ -96,7 +95,6 @@ pub use pallet_staking::StakerStatus;
pub use pallet_timestamp::Call as TimestampCall;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;
use xcm_executor::traits::DropAssets;

pub use authority::AuthorityConfigImpl;
pub use constants::{fee::*, time::*};
Expand All @@ -105,6 +103,7 @@ pub use primitives::{
AuctionId, AuthoritysOriginId, Balance, BlockNumber, CurrencyId, DataProviderId, EraIndex, Hash, Moment, Nonce,
ReserveIdentifier, Share, Signature, TokenSymbol, TradingPair,
};
use runtime_common::AcalaDropAssets;
pub use runtime_common::{
cent, dollar, microcent, millicent, EnsureRootOrAllGeneralCouncil, EnsureRootOrAllTechnicalCommittee,
EnsureRootOrHalfFinancialCouncil, EnsureRootOrHalfGeneralCouncil, EnsureRootOrHalfHomaCouncil,
Expand Down Expand Up @@ -730,17 +729,6 @@ impl DataFeeder<CurrencyId, Price, AccountId> for AggregatedDataProvider {
}
}

pub struct ExistentialDepositsForDropAssets;
impl ExistentialDepositsForDropAssets {
fn get(currency_id: &CurrencyId) -> Balance {
if currency_id == &GetNativeCurrencyId::get() {
NativeTokenExistentialDeposit::get()
} else {
<ExistentialDeposits as GetByKey<CurrencyId, Balance>>::get(currency_id)
}
}
}

parameter_type_with_key! {
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
match currency_id {
Expand Down Expand Up @@ -1442,40 +1430,6 @@ impl TakeRevenue for ToTreasury {
}
}

pub struct AcalaDropAssets<X, T>(PhantomData<(X, T)>);
impl<X, T> DropAssets for AcalaDropAssets<X, T>
where
X: DropAssets,
T: TakeRevenue,
{
fn drop_assets(origin: &MultiLocation, assets: Assets) -> Weight {
let multi_assets: Vec<MultiAsset> = assets.into();
let mut asset_traps: Vec<MultiAsset> = vec![];
for asset in multi_assets {
if let MultiAsset {
id: Concrete(location),
fun: Fungible(amount),
} = asset.clone()
{
let currency_id = CurrencyIdConvert::convert(location);
// burn asset(do nothing here) if convert result is None
if let Some(currency_id) = currency_id {
let ed = ExistentialDepositsForDropAssets::get(&currency_id);
if amount < ed {
T::take_revenue(asset);
} else {
asset_traps.push(asset);
}
}
}
}
if !asset_traps.is_empty() {
X::drop_assets(origin, asset_traps.into());
}
0
}
}

pub type Trader = (
FixedRateOfFungible<DotPerSecond, ToTreasury>,
FixedRateOfFungible<AusdPerSecond, ToTreasury>,
Expand All @@ -1497,7 +1451,14 @@ impl xcm_executor::Config for XcmConfig {
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type Trader = Trader;
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
type AssetTrap = AcalaDropAssets<
PolkadotXcm,
ToTreasury,
CurrencyIdConvert,
GetNativeCurrencyId,
NativeTokenExistentialDeposit,
ExistentialDeposits,
>;
type AssetClaims = PolkadotXcm;
type SubscriptionService = PolkadotXcm;
}
Expand Down
8 changes: 8 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ module-transaction-payment = { path = "../../modules/transaction-payment", defau
module-nft = { path = "../../modules/nft", default-features = false }
module-dex = { path = "../../modules/dex", default-features = false }

xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.12", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.12", default-features = false }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.12", default-features = false }

[dev-dependencies]
serde_json = "1.0.64"
hex-literal = "0.3.1"
Expand Down Expand Up @@ -87,6 +91,10 @@ std = [
"module-transaction-payment/std",
"module-nft/std",
"module-dex/std",

"xcm/std",
"xcm-executor/std",
"xcm-builder/std",
]
with-ethereum-compatibility = [
"module-evm/with-ethereum-compatibility",
Expand Down
60 changes: 60 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::traits::Get;
use frame_support::{
parameter_types,
traits::Contains,
Expand Down Expand Up @@ -49,6 +50,7 @@ mod homa;
pub use homa::*;

pub mod precompile;
use orml_traits::GetByKey;
pub use precompile::{
AllPrecompiles, DexPrecompile, MultiCurrencyPrecompile, NFTPrecompile, OraclePrecompile, ScheduleCallPrecompile,
StateRentPrecompile,
Expand All @@ -57,6 +59,10 @@ pub use primitives::{
currency::{TokenInfo, ACA, AUSD, BNC, DOT, KAR, KSM, KUSD, LDOT, LKSM, PHA, RENBTC, VSKSM},
AccountId,
};
use sp_std::{marker::PhantomData, prelude::*};
pub use xcm::latest::prelude::*;
pub use xcm_builder::TakeRevenue;
pub use xcm_executor::{traits::DropAssets, Assets};

pub type TimeStampedPrice = orml_oracle::TimestampedValue<Price, primitives::Moment>;

Expand Down Expand Up @@ -331,6 +337,60 @@ pub enum RelayChainSubAccountId {
HomaLite = 0,
}

pub struct AcalaDropAssets<X, T, C, NC, NB, GK>(PhantomData<(X, T, C, NC, NB, GK)>);
zqhxuyuan marked this conversation as resolved.
Show resolved Hide resolved
impl<X, T, C, NC, NB, GK> DropAssets for AcalaDropAssets<X, T, C, NC, NB, GK>
where
X: DropAssets,
T: TakeRevenue,
C: Convert<MultiLocation, Option<CurrencyId>>,
NC: Get<CurrencyId>,
NB: Get<Balance>,
GK: GetByKey<CurrencyId, Balance>,
{
fn drop_assets(origin: &MultiLocation, assets: Assets) -> Weight {
let multi_assets: Vec<MultiAsset> = assets.into();
let mut asset_traps: Vec<MultiAsset> = vec![];
for asset in multi_assets {
if let MultiAsset {
id: Concrete(location),
fun: Fungible(amount),
} = asset.clone()
{
let currency_id = C::convert(location);
// burn asset(do nothing here) if convert result is None
if let Some(currency_id) = currency_id {
let ed = ExistentialDepositsForDropAssets::<NC, NB, GK>::get(&currency_id);
if amount < ed {
T::take_revenue(asset);
} else {
asset_traps.push(asset);
}
}
}
}
if !asset_traps.is_empty() {
X::drop_assets(origin, asset_traps.into());
}
0
}
}

pub struct ExistentialDepositsForDropAssets<NC, NB, GK>(PhantomData<(NC, NB, GK)>);
impl<NC, NB, GK> ExistentialDepositsForDropAssets<NC, NB, GK>
where
NC: Get<CurrencyId>,
NB: Get<Balance>,
GK: GetByKey<CurrencyId, Balance>,
{
fn get(currency_id: &CurrencyId) -> Balance {
if currency_id == &NC::get() {
NB::get()
} else {
GK::get(currency_id)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
65 changes: 12 additions & 53 deletions runtime/karura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use sp_runtime::{
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, DispatchResult, FixedPointNumber, Perbill, Percent, Permill, Perquintill,
};
use sp_std::{marker::PhantomData, prelude::*};
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;
Expand All @@ -59,8 +59,7 @@ use module_support::{DispatchableTask, ForeignAssetIdMapping};
use module_transaction_payment::{Multiplier, TargetedFeeAdjustment};

use orml_traits::{
create_median_value_data_provider, parameter_type_with_key, DataFeeder, DataProviderExtended, GetByKey,
MultiCurrency,
create_median_value_data_provider, parameter_type_with_key, DataFeeder, DataProviderExtended, MultiCurrency,
};
use pallet_transaction_payment::RuntimeDispatchInfo;

Expand All @@ -77,10 +76,7 @@ pub use xcm_builder::{
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeRevenue, TakeWeightCredit,
};
pub use xcm_executor::{
traits::{DropAssets, WeightTrader},
Assets, Config, XcmExecutor,
};
pub use xcm_executor::{traits::WeightTrader, Assets, Config, XcmExecutor};

/// Weights for pallets used in the runtime.
mod weights;
Expand Down Expand Up @@ -108,6 +104,7 @@ pub use primitives::{
AuctionId, AuthoritysOriginId, Balance, BlockNumber, CurrencyId, DataProviderId, EraIndex, Hash, Moment, Nonce,
ReserveIdentifier, Share, Signature, TokenSymbol, TradingPair,
};
use runtime_common::AcalaDropAssets;
pub use runtime_common::{
cent, dollar, microcent, millicent, EnsureRootOrAllGeneralCouncil, EnsureRootOrAllTechnicalCommittee,
EnsureRootOrHalfFinancialCouncil, EnsureRootOrHalfGeneralCouncil, EnsureRootOrHalfHomaCouncil,
Expand Down Expand Up @@ -742,17 +739,6 @@ impl DataFeeder<CurrencyId, Price, AccountId> for AggregatedDataProvider {
}
}

pub struct ExistentialDepositsForDropAssets;
impl ExistentialDepositsForDropAssets {
fn get(currency_id: &CurrencyId) -> Balance {
if currency_id == &GetNativeCurrencyId::get() {
NativeTokenExistentialDeposit::get()
} else {
<ExistentialDeposits as GetByKey<CurrencyId, Balance>>::get(currency_id)
}
}
}

parameter_type_with_key! {
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
match currency_id {
Expand Down Expand Up @@ -1483,40 +1469,6 @@ impl TakeRevenue for ToTreasury {
}
}

pub struct AcalaDropAssets<X, T>(PhantomData<(X, T)>);
impl<X, T> DropAssets for AcalaDropAssets<X, T>
where
X: DropAssets,
T: TakeRevenue,
{
fn drop_assets(origin: &MultiLocation, assets: Assets) -> Weight {
let multi_assets: Vec<MultiAsset> = assets.into();
let mut asset_traps: Vec<MultiAsset> = vec![];
for asset in multi_assets {
if let MultiAsset {
id: Concrete(location),
fun: Fungible(amount),
} = asset.clone()
{
let currency_id = CurrencyIdConvert::convert(location);
// burn asset(do nothing here) if convert result is None
if let Some(currency_id) = currency_id {
let ed = ExistentialDepositsForDropAssets::get(&currency_id);
if amount < ed {
T::take_revenue(asset);
} else {
asset_traps.push(asset);
}
}
}
}
if !asset_traps.is_empty() {
X::drop_assets(origin, asset_traps.into());
}
0
}
}

parameter_types! {
pub BncPerSecond: (AssetId, u128) = (
MultiLocation::new(
Expand Down Expand Up @@ -1562,7 +1514,14 @@ impl xcm_executor::Config for XcmConfig {
type Weigher = FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type Trader = Trader;
type ResponseHandler = PolkadotXcm;
type AssetTrap = AcalaDropAssets<PolkadotXcm, ToTreasury>;
type AssetTrap = AcalaDropAssets<
PolkadotXcm,
ToTreasury,
CurrencyIdConvert,
GetNativeCurrencyId,
NativeTokenExistentialDeposit,
ExistentialDeposits,
>;
type AssetClaims = PolkadotXcm;
type SubscriptionService = PolkadotXcm;
}
Expand Down
Loading