Skip to content
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
Prev Previous commit
Next Next commit
add orml unknown tokens module
  • Loading branch information
herryho committed Sep 1, 2021
commit 75bb160c96eb9470121c3bc8b0a12f782febf363
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,5 @@ orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-li
orml-currencies = {git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "8d5432c3458702a7df14b374bddde43a2a20aa43" }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "8d5432c3458702a7df14b374bddde43a2a20aa43" }
orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "8d5432c3458702a7df14b374bddde43a2a20aa43" }
orml-unknown-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "8d5432c3458702a7df14b374bddde43a2a20aa43" }
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "8d5432c3458702a7df14b374bddde43a2a20aa43" }
2 changes: 2 additions & 0 deletions runtime/asgard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ orml-currencies = { version = "0.4.1-dev", default-features = false }
orml-tokens = { version = "0.4.1-dev", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
orml-xtokens = { version = "0.4.1-dev", default-features = false }
orml-unknown-tokens = { version = "0.4.1-dev", default-features = false }
orml-xcm-support = { version = "0.4.1-dev", default-features = false }

zenlink-protocol = { version = "*", default-features = false }
Expand Down Expand Up @@ -182,6 +183,7 @@ std = [
"orml-traits/std",
"orml-tokens/std",
"orml-xtokens/std",
"orml-unknown-tokens/std",
"orml-xcm-support/std",
"zenlink-protocol/std",
"zenlink-protocol-runtime-api/std",
Expand Down
11 changes: 9 additions & 2 deletions runtime/asgard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ use node_primitives::{
// orml imports
use orml_currencies::BasicCurrencyAdapter;
use orml_traits::MultiCurrency;
use orml_xcm_support::MultiCurrencyAdapter;
use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
use sp_runtime::traits::ConvertInto;
Expand All @@ -109,7 +110,7 @@ use xcm_builder::{
SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit,
};
use xcm_executor::{Config, XcmExecutor};
use xcm_support::{BifrostCurrencyAdapter, BifrostXcmAdaptor};
use xcm_support::BifrostXcmAdaptor;
// zenlink imports
use zenlink_protocol::{
make_x2_location, AssetBalance, AssetId, LocalAssetHandler, MultiAssetsHandler, PairInfo,
Expand Down Expand Up @@ -758,8 +759,9 @@ pub type Barrier = (
BifrostXcmTransactFilter<Everything>,
);

pub type BifrostAssetTransactor = BifrostCurrencyAdapter<
pub type BifrostAssetTransactor = MultiCurrencyAdapter<
Currencies,
UnknownTokens,
BifrostAssetMatcher<CurrencyId, BifrostCurrencyIdConvert<SelfParaChainId>>,
AccountId,
LocationToAccountId,
Expand Down Expand Up @@ -1265,6 +1267,10 @@ impl orml_xtokens::Config for Runtime {
type BaseXcmWeight = XcmWeight;
}

impl orml_unknown_tokens::Config for Runtime {
type Event = Event;
}

// orml runtime end

construct_runtime! {
Expand Down Expand Up @@ -1324,6 +1330,7 @@ construct_runtime! {
XTokens: orml_xtokens::{Pallet, Call, Event<T>} = 70,
Tokens: orml_tokens::{Pallet, Call, Storage, Event<T>, Config<T>} = 71,
Currencies: orml_currencies::{Pallet, Call, Event<T>} = 72,
UnknownTokens: orml_unknown_tokens::{Pallet, Storage, Event} = 73,

// Bifrost modules
VtokenMint: bifrost_vtoken_mint::{Pallet, Call, Storage, Event<T>, Config<T>} = 101,
Expand Down
4 changes: 3 additions & 1 deletion runtime/bifrost/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ xcm-support = { path = "../../xcm-support", default-features = false }
orml-currencies = { version = "0.4.1-dev", default-features = false }
orml-tokens = { version = "0.4.1-dev", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
orml-xcm-support = { version = "0.4.1-dev", default-features = false }
orml-xtokens = { version = "0.4.1-dev", default-features = false }
orml-unknown-tokens = { version = "0.4.1-dev", default-features = false }
orml-xcm-support = { version = "0.4.1-dev", default-features = false }

[build-dependencies]
substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" }
Expand Down Expand Up @@ -155,6 +156,7 @@ std = [
"orml-traits/std",
"orml-tokens/std",
"orml-xtokens/std",
"orml-unknown-tokens/std",
"orml-xcm-support/std",
"bifrost-runtime-common/std",
"bifrost-flexible-fee/std",
Expand Down
11 changes: 9 additions & 2 deletions runtime/bifrost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ use node_primitives::{
// orml imports
use orml_currencies::BasicCurrencyAdapter;
use orml_traits::MultiCurrency;
use orml_xcm_support::MultiCurrencyAdapter;
use pallet_xcm::XcmPassthrough;
// XCM imports
use polkadot_parachain::primitives::Sibling;
Expand All @@ -102,7 +103,7 @@ use xcm_builder::{
SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit,
};
use xcm_executor::{Config, XcmExecutor};
use xcm_support::{BifrostCurrencyAdapter, BifrostXcmAdaptor, Get};
use xcm_support::{BifrostXcmAdaptor, Get};

// Weights used in the runtime.
mod weights;
Expand Down Expand Up @@ -670,8 +671,9 @@ pub type Barrier = (
BifrostXcmTransactFilter<Everything>,
);

pub type BifrostAssetTransactor = BifrostCurrencyAdapter<
pub type BifrostAssetTransactor = MultiCurrencyAdapter<
Currencies,
UnknownTokens,
BifrostAssetMatcher<CurrencyId, BifrostCurrencyIdConvert<SelfParaChainId>>,
AccountId,
LocationToAccountId,
Expand Down Expand Up @@ -884,6 +886,10 @@ impl orml_xtokens::Config for Runtime {
type BaseXcmWeight = XcmWeight;
}

impl orml_unknown_tokens::Config for Runtime {
type Event = Event;
}

// orml runtime end

// Bifrost modules start
Expand Down Expand Up @@ -1065,6 +1071,7 @@ construct_runtime! {
XTokens: orml_xtokens::{Pallet, Call, Event<T>} = 70,
Tokens: orml_tokens::{Pallet, Call, Storage, Event<T>} = 71,
Currencies: orml_currencies::{Pallet, Call, Event<T>} = 72,
UnknownTokens: orml_unknown_tokens::{Pallet, Storage, Event} = 73,

// Bifrost modules
FlexibleFee: bifrost_flexible_fee::{Pallet, Call, Storage, Event<T>} = 100,
Expand Down
11 changes: 9 additions & 2 deletions runtime/dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ use node_primitives::{
// orml imports
use orml_currencies::BasicCurrencyAdapter;
use orml_traits::MultiCurrency;
use orml_xcm_support::MultiCurrencyAdapter;
use pallet_xcm::XcmPassthrough;
use polkadot_parachain::primitives::Sibling;
use sp_runtime::traits::ConvertInto;
Expand All @@ -112,7 +113,7 @@ use xcm_builder::{
UsingComponents,
};
use xcm_executor::{Config, XcmExecutor};
use xcm_support::{BifrostCurrencyAdapter, BifrostXcmAdaptor};
use xcm_support::BifrostXcmAdaptor;
// zenlink imports
use zenlink_protocol::{
make_x2_location, AssetBalance, AssetId, LocalAssetHandler, MultiAssetsHandler, PairInfo,
Expand Down Expand Up @@ -757,8 +758,9 @@ pub type Barrier = (
BifrostXcmTransactFilter<Everything>,
);

pub type BifrostAssetTransactor = BifrostCurrencyAdapter<
pub type BifrostAssetTransactor = MultiCurrencyAdapter<
Currencies,
UnknownTokens,
BifrostAssetMatcher<CurrencyId, BifrostCurrencyIdConvert<SelfParaChainId>>,
AccountId,
LocationToAccountId,
Expand Down Expand Up @@ -1199,6 +1201,10 @@ impl orml_xtokens::Config for Runtime {
type BaseXcmWeight = XcmWeight;
}

impl orml_unknown_tokens::Config for Runtime {
type Event = Event;
}

// orml runtime end

construct_runtime! {
Expand Down Expand Up @@ -1254,6 +1260,7 @@ construct_runtime! {
XTokens: orml_xtokens::{Pallet, Call, Event<T>} = 70,
Tokens: orml_tokens::{Pallet, Call, Storage, Event<T>, Config<T>} = 71,
Currencies: orml_currencies::{Pallet, Call, Event<T>} = 72,
UnknownTokens: orml_unknown_tokens::{Pallet, Storage, Event} = 73,

// Bifrost modules
VtokenMint: bifrost_vtoken_mint::{Pallet, Call, Storage, Event<T>, Config<T>} = 101,
Expand Down
79 changes: 3 additions & 76 deletions xcm-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,23 @@

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Encode, FullCodec};
use codec::Encode;
pub use cumulus_primitives_core::{self, ParaId};
pub use frame_support::{traits::Get, weights::Weight};
pub use paste;
use sp_runtime::traits::{Convert, MaybeSerializeDeserialize, SaturatedConversion};
pub use sp_std::{cell::RefCell, marker::PhantomData};
use sp_std::{
cmp::{Eq, PartialEq},
fmt::Debug,
prelude::*,
vec,
};
pub use xcm::VersionedXcm;
use xcm::{
v0::{
Error as XcmError, Junction, MultiAsset, MultiLocation, OriginKind, Result as XcmResult,
Error as XcmError, Junction, MultiAsset, MultiLocation, OriginKind,
SendXcm, Xcm,
},
DoubleEncoded,
};
use xcm_executor::traits::{Convert as xcmConvert, MatchesFungible, TransactAsset};
pub use xcm_executor::XcmExecutor;
mod calls;
mod traits;
Expand All @@ -68,6 +64,7 @@ mod mock;
mod tests;

/// Asset transaction errors.
#[allow(dead_code)]
enum Error {
/// Failed to match fungible.
FailedToMatchFungible,
Expand All @@ -90,76 +87,6 @@ impl From<Error> for XcmError {
}
}

/// The `TransactAsset` implementation, to handle `MultiAsset` deposit/withdraw.
///
/// If the asset is known, deposit/withdraw will be handled by `MultiCurrency`,
/// else by `UnknownAsset` if unknown.
pub struct BifrostCurrencyAdapter<
MultiCurrency,
Matcher,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
>(
PhantomData<(
MultiCurrency,
Matcher,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
)>,
);

impl<
MultiCurrency: orml_traits::MultiCurrency<AccountId, CurrencyId = CurrencyId>,
Matcher: MatchesFungible<MultiCurrency::Balance>,
AccountId: sp_std::fmt::Debug + sp_std::clone::Clone,
AccountIdConvert: xcmConvert<MultiLocation, AccountId>,
CurrencyId: FullCodec + Eq + PartialEq + Copy + MaybeSerializeDeserialize + Debug,
CurrencyIdConvert: Convert<MultiAsset, Option<CurrencyId>>,
> TransactAsset
for BifrostCurrencyAdapter<
MultiCurrency,
Matcher,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
>
{
fn deposit_asset(asset: &MultiAsset, location: &MultiLocation) -> XcmResult {
match (
AccountIdConvert::convert_ref(location.clone()),
CurrencyIdConvert::convert(asset.clone()),
Matcher::matches_fungible(&asset),
) {
// known asset
(Ok(who), Some(currency_id), Some(amount)) =>
MultiCurrency::deposit(currency_id, &who, amount)
.map_err(|e| XcmError::FailedToTransactAsset(e.into())),
_ => Err(XcmError::AssetNotFound),
}
}

fn withdraw_asset(
asset: &MultiAsset,
location: &MultiLocation,
) -> Result<xcm_executor::Assets, XcmError> {
let who = AccountIdConvert::convert_ref(location)
.map_err(|_| XcmError::from(Error::AccountIdConversionFailed))?;
let currency_id = CurrencyIdConvert::convert(asset.clone())
.ok_or_else(|| XcmError::from(Error::CurrencyIdConversionFailed))?;
let amount: MultiCurrency::Balance = Matcher::matches_fungible(&asset)
.ok_or_else(|| XcmError::from(Error::FailedToMatchFungible))?
.saturated_into();
MultiCurrency::withdraw(currency_id, &who, amount)
.map_err(|e| XcmError::FailedToTransactAsset(e.into()))?;
Ok(asset.clone().into())
}
}

pub struct BifrostXcmAdaptor<XcmSender, BaseXcmWeight, WeightToFee>(
PhantomData<(XcmSender, BaseXcmWeight, WeightToFee)>,
);
Expand Down