Skip to content

Commit

Permalink
XCM Trader: Replace FixedFungibleRate by TakeFirstAssetTrader (#273)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* Adding XCM emulator from cumulus

* WIP

* Adding FirstAssetTrader

* Cleaning up xcm file

* Fixing unnused warning

* Code review changes + clippy lints

---------

Co-authored-by: Daniel Olano <[email protected]>
  • Loading branch information
darkforest0202 and olanod authored Jul 6, 2023
1 parent b221a88 commit 605a496
Show file tree
Hide file tree
Showing 26 changed files with 2,034 additions and 28,195 deletions.
2,155 changes: 1,560 additions & 595 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ members = [
'node',
'runtime/virto',
'runtime/seedling',
'runtime/kreivo'
]
exclude = [
'virto-xcm-emulator'
'runtime/kreivo',
'virto-xcm-emulator',
]
10 changes: 7 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ _task-selector:
@version:
echo {{ ver }}

@list-crates:
open Cargo.toml | get workspace.members | each { open ($in + /Cargo.toml) | get package.name } | str join "\n"

@_check_deps:
rustup component add clippy

check: _check_deps
cargo clippy --features runtime-benchmarks --all-targets --workspace -- --deny warnings
cargo clippy --all-targets -- --deny warnings
cargo fmt --all -- --check

test:
cargo test

@test crate="":
cargo test (if not ("{{crate}}" | is-empty) { "-p" } else {""}) {{crate}}

build-local:
cargo build --release
Expand Down
1 change: 1 addition & 0 deletions runtime/kreivo/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub mod fee {

pub mod locations {
pub const STATEMINE_PARA_ID: u32 = 1000;
// Even if they are not used yet, let's keep these for the future.
pub const STATEMINE_ASSET_PALLET_ID: u8 = 50;
pub const USDT_ASSET_ID: u128 = 1984;
}
30 changes: 15 additions & 15 deletions runtime/kreivo/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,26 @@ impl InstanceFilter<RuntimeCall> for ProxyType {
}
ProxyType::AssetOwner => matches!(
c,
RuntimeCall::Assets(TrustBackedAssetsCall::create { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::start_destroy { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::destroy_accounts { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::destroy_approvals { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::finish_destroy { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::transfer_ownership { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::set_team { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::set_metadata { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::clear_metadata { .. })
RuntimeCall::Assets(KreivoAssetsCall::create { .. })
| RuntimeCall::Assets(KreivoAssetsCall::start_destroy { .. })
| RuntimeCall::Assets(KreivoAssetsCall::destroy_accounts { .. })
| RuntimeCall::Assets(KreivoAssetsCall::destroy_approvals { .. })
| RuntimeCall::Assets(KreivoAssetsCall::finish_destroy { .. })
| RuntimeCall::Assets(KreivoAssetsCall::transfer_ownership { .. })
| RuntimeCall::Assets(KreivoAssetsCall::set_team { .. })
| RuntimeCall::Assets(KreivoAssetsCall::set_metadata { .. })
| RuntimeCall::Assets(KreivoAssetsCall::clear_metadata { .. })
| RuntimeCall::Utility { .. }
| RuntimeCall::Multisig { .. }
),
ProxyType::AssetManager => matches!(
c,
RuntimeCall::Assets(TrustBackedAssetsCall::mint { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::burn { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::freeze { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::thaw { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::freeze_asset { .. })
| RuntimeCall::Assets(TrustBackedAssetsCall::thaw_asset { .. })
RuntimeCall::Assets(KreivoAssetsCall::mint { .. })
| RuntimeCall::Assets(KreivoAssetsCall::burn { .. })
| RuntimeCall::Assets(KreivoAssetsCall::freeze { .. })
| RuntimeCall::Assets(KreivoAssetsCall::thaw { .. })
| RuntimeCall::Assets(KreivoAssetsCall::freeze_asset { .. })
| RuntimeCall::Assets(KreivoAssetsCall::thaw_asset { .. })
| RuntimeCall::Utility { .. }
| RuntimeCall::Multisig { .. }
),
Expand Down
11 changes: 7 additions & 4 deletions runtime/kreivo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub use parachains_common::{

pub use virto_common::impls::DealWithFees;

use constants::{currency::*, fee::WeightToFee};
pub use constants::{currency::*, fee::WeightToFee};

/// The address format for describing accounts.
pub type Address = MultiAddress<AccountId, ()>;
Expand Down Expand Up @@ -456,9 +456,12 @@ parameter_types! {
}

/// We allow root to execute privileged asset operations.

pub type AssetsForceOrigin = EnsureRoot<AccountId>;
type TrustBackedAssetsCall = pallet_assets::Call<Runtime>;
impl pallet_assets::Config for Runtime {
pub type KreivoAssetsInstance = pallet_assets::Instance1;
type KreivoAssetsCall = pallet_assets::Call<Runtime, KreivoAssetsInstance>;

impl pallet_assets::Config<KreivoAssetsInstance> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = AssetIdForTrustBackedAssets;
Expand Down Expand Up @@ -553,7 +556,7 @@ construct_runtime!(
Balances: pallet_balances = 10,
TransactionPayment: pallet_transaction_payment = 11,
Burner: pallet_burner = 12,
Assets: pallet_assets = 13,
Assets: pallet_assets::<KreivoAssetsInstance> = 13,

// Collator support. The order of these 4 are important and shall not change.
Authorship: pallet_authorship = 20,
Expand Down
47 changes: 26 additions & 21 deletions runtime/kreivo/src/xcm_config.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
use super::{
AccountId, AllPalletsWithSystem, AssetIdForTrustBackedAssets, AssetRegistry, Assets, Balance, Balances,
ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee,
XcmpQueue,
KreivoAssetsInstance, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent,
RuntimeOrigin, Treasury, WeightToFee, XcmpQueue,
};
use crate::constants::fee::default_fee_per_second;
use crate::constants::locations::{STATEMINE_ASSET_PALLET_ID, STATEMINE_PARA_ID, USDT_ASSET_ID};

use crate::constants::locations::STATEMINE_PARA_ID;
use frame_support::{
match_types, parameter_types,
traits::{ConstU32, ContainsPair, Everything, Get, Nothing, PalletInfoAccess},
weights::Weight,
};
use frame_system::EnsureRoot;
use pallet_xcm::XcmPassthrough;
use parachains_common::xcm_config::AssetFeeAsExistentialDepositMultiplier;
use polkadot_parachain::primitives::Sibling;
use sp_runtime::traits::ConvertInto;
use sp_std::marker::PhantomData;
use virto_common::impls::{AsAssetMultiLocation, ConvertedRegisteredAssetId, DealWithFees};
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom, CurrencyAdapter,
EnsureXcmOrigin, FixedRateOfFungible, FixedWeightBounds, FungiblesAdapter, IsConcrete, LocalMint, MintLocation,
NativeAsset, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
EnsureXcmOrigin, FixedWeightBounds, FungiblesAdapter, IsConcrete, LocalMint, MintLocation, NativeAsset,
ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents,
WithComputedOrigin,
};
Expand All @@ -32,11 +34,10 @@ parameter_types! {
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub CheckAccount: (AccountId, MintLocation) = (PolkadotXcm::check_account(), MintLocation::Local);
pub CheckingAccount: AccountId = PolkadotXcm::check_account();
pub TrustBackedAssetsPalletLocation: MultiLocation =
pub AssetsPalletLocation: MultiLocation =
PalletInstance(<Assets as PalletInfoAccess>::index() as u8).into();
pub UniversalLocation: InteriorMultiLocation = (
//TODO: to change the NetworkId to Kusama once we finish testing on Rococo.
GlobalConsensus(NetworkId::Rococo),
GlobalConsensus(NetworkId::Kusama),
Parachain(ParachainInfo::parachain_id().into()),
).into();

Expand All @@ -56,7 +57,7 @@ pub type LocationToAccountId = (
);

pub type TrustBackedAssetsConvertedConcreteId =
assets_common::TrustBackedAssetsConvertedConcreteId<TrustBackedAssetsPalletLocation, Balance>;
assets_common::TrustBackedAssetsConvertedConcreteId<AssetsPalletLocation, Balance>;

/// Means for transacting assets besides the native currency on this chain.
pub type FungiblesTransactor = FungiblesAdapter<
Expand Down Expand Up @@ -122,6 +123,7 @@ parameter_types! {
pub UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 64 * 1024);
pub const MaxInstructions: u32 = 100;
pub const MaxAssetsIntoHolding: u32 = 64;
pub XcmAssetFeesReceiver: AccountId = Treasury::account_id();
}

match_types! {
Expand All @@ -147,16 +149,7 @@ pub type Barrier = (
pub type AssetTransactors = (CurrencyTransactor, FungiblesTransactor);

parameter_types! {
pub UsdtPerSecond: (xcm::v3::AssetId, u128, u128) = (
MultiLocation::new(1, X3(Parachain(STATEMINE_PARA_ID), PalletInstance(STATEMINE_ASSET_PALLET_ID), GeneralIndex(USDT_ASSET_ID))).into(),
default_fee_per_second() * 10,
0
);
pub StatemineLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(STATEMINE_PARA_ID)));
// ALWAYS ensure that the index in PalletInstance stays up-to-date with
// Statemine's Assets pallet index
pub StatemineAssetsPalletLocation: MultiLocation =
MultiLocation::new(1, X2(Parachain(STATEMINE_PARA_ID), PalletInstance(STATEMINE_ASSET_PALLET_ID)));
}

//- From PR https://github.com/paritytech/cumulus/pull/936
Expand Down Expand Up @@ -185,9 +178,21 @@ impl<T: Get<MultiLocation>> ContainsPair<MultiAsset, MultiLocation> for ReserveA
}
}

pub type AssetFeeAsExistentialDepositMultiplierFeeCharger = AssetFeeAsExistentialDepositMultiplier<
Runtime,
WeightToFee,
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto, KreivoAssetsInstance>,
KreivoAssetsInstance,
>;

pub type Traders = (
// USDT
FixedRateOfFungible<UsdtPerSecond, ()>,
cumulus_primitives_utility::TakeFirstAssetTrader<
AccountId,
AssetFeeAsExistentialDepositMultiplierFeeCharger,
TrustBackedAssetsConvertedConcreteId,
Assets,
cumulus_primitives_utility::XcmFeesTo32ByteAccount<FungiblesTransactor, AccountId, XcmAssetFeesReceiver>,
>,
// Everything else
UsingComponents<WeightToFee, RelayLocation, AccountId, Balances, DealWithFees<Runtime>>,
);
Expand Down
Loading

0 comments on commit 605a496

Please sign in to comment.