Skip to content

Commit

Permalink
MultiCurrency Asset Transactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Apr 25, 2024
1 parent 2c698b9 commit e5bd9da
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
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 @@ -139,6 +139,7 @@ orml-asset-registry = { version = "0.7.0", default-features = false }
orml-currencies = { version = "0.7.0", default-features = false }
orml-tokens = { version = "0.7.0", default-features = false }
orml-traits = { version = "0.7.0", default-features = false }
orml-xcm-support = { version = "0.7.0", default-features = false }

# Polytope Labs
ismp = { git="https://github.com/Szegoo/hyperbridge.git", branch="fix-to-string", default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn session_keys(keys: AuraId) -> regionx_runtime::SessionKeys {
pub fn development_config(id: u32) -> ChainSpec<regionx_runtime::RuntimeGenesisConfig> {
// Give your base currency a unit name and decimal places
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "M4X".into());
properties.insert("tokenSymbol".into(), "REGX".into());
properties.insert("tokenDecimals".into(), 12.into());
// TODO: chose an ss58Format
properties.insert("ss58Format".into(), 42.into());
Expand Down Expand Up @@ -130,7 +130,7 @@ pub fn development_config(id: u32) -> ChainSpec<regionx_runtime::RuntimeGenesisC
pub fn local_testnet_config(id: u32) -> ChainSpec<regionx_runtime::RuntimeGenesisConfig> {
// Give your base currency a unit name and decimal places
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "M4X".into());
properties.insert("tokenSymbol".into(), "REGX".into());
properties.insert("tokenDecimals".into(), 12.into());
// TODO: chose an ss58Format
properties.insert("ss58Format".into(), 42.into());
Expand Down
2 changes: 2 additions & 0 deletions runtime/regionx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ orml-asset-registry = { workspace = true }
orml-currencies = { workspace = true }
orml-tokens = { workspace = true }
orml-traits = { workspace = true }
orml-xcm-support = { workspace = true }

# Substrate
frame-benchmarking = { workspace = true, optional = true }
Expand Down Expand Up @@ -123,6 +124,7 @@ std = [
"orml-currencies/std",
"orml-tokens/std",
"orml-traits/std",
"orml-xcm-support/std",
"pallet-aura/std",
"pallet-authorship/std",
"pallet-asset-rate/std",
Expand Down
64 changes: 48 additions & 16 deletions runtime/regionx/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

use super::{
AccountId, AllPalletsWithSystem, Balances, ParachainInfo, ParachainSystem, PolkadotXcm,
Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue,
AccountId, AllPalletsWithSystem, AssetId, Balances, Currencies, ParachainInfo, ParachainSystem,
PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, WeightToFee, XcmpQueue,
};
use frame_support::{
match_types, parameter_types,
traits::{ConstU32, Everything, Nothing},
};
use frame_system::EnsureRoot;
use orml_xcm_support::{IsNativeConcrete, MultiCurrencyAdapter};
use pallet_xcm::XcmPassthrough;
use polkadot_parachain_primitives::primitives::Sibling;
use polkadot_runtime_common::impls::ToAuthor;
use regionx_primitives::assets::{REGX_ASSET_ID, RELAY_CHAIN_ASSET_ID};
use sp_runtime::traits::Convert;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowTopLevelPaidExecutionFrom,
DenyReserveTransferToRelayChain, DenyThenTry, EnsureXcmOrigin, FixedWeightBounds,
FrameTransactionalProcessor, FungibleAdapter, IsConcrete, NativeAsset, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WithComputedOrigin, WithUniqueTopic,
FrameTransactionalProcessor, NativeAsset, ParentIsPreset, RelayChainAsNative,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, TrailingSetTopicAsId,
UsingComponents, WithComputedOrigin, WithUniqueTopic,
};
use xcm_executor::XcmExecutor;

Expand All @@ -56,19 +59,48 @@ pub type LocationToAccountId = (
);

/// Means for transacting assets on this chain.
pub type LocalAssetTransactor = FungibleAdapter<
// Use this currency:
Balances,
// Use this currency when it is a fungible asset matching the given location or name:
IsConcrete<RelayLocation>,
// Do a simple punn to convert an AccountId32 MultiLocation into a native chain account ID:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
pub type FungiblesAssetTransactor = MultiCurrencyAdapter<
Currencies,
(), // TODO: handle unknown tokens
IsNativeConcrete<AssetId, AssetIdConverter>,
AccountId,
// We don't track any teleports.
LocationToAccountId,
AssetId,
AssetIdConverter,
(),
>;

pub struct AssetIdConverter;
impl Convert<AssetId, Option<MultiLocation>> for AssetIdConverter {
fn convert(id: AssetId) -> Option<MultiLocation> {
match id {
RELAY_CHAIN_ASSET_ID => Some(MultiLocation::parent()),
REGX_ASSET_ID => Some(MultiLocation::here()),
_ => None,
}
}
}

impl Convert<MultiLocation, Option<AssetId>> for AssetIdConverter {
fn convert(location: MultiLocation) -> Option<AssetId> {
match location {
MultiLocation { parents: 1, interior: Here } => Some(RELAY_CHAIN_ASSET_ID),
MultiLocation { parents: 0, interior: Here } => Some(REGX_ASSET_ID),
_ => None,
}
}
}

impl Convert<MultiAsset, Option<AssetId>> for AssetIdConverter {
fn convert(asset: MultiAsset) -> Option<AssetId> {
if let MultiAsset { id: Concrete(location), .. } = asset {
Self::convert(location)
} else {
None
}
}
}

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
/// biases the kind of local `Origin` it will become.
Expand Down Expand Up @@ -127,7 +159,7 @@ impl xcm_executor::Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = XcmRouter;
// How to withdraw and deposit an asset.
type AssetTransactor = LocalAssetTransactor;
type AssetTransactor = FungiblesAssetTransactor;
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type IsReserve = NativeAsset;
type IsTeleporter = (); // Teleporting is disabled.
Expand Down
2 changes: 1 addition & 1 deletion zombienet_tests/0001-block-production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ addToGenesis = false
[parachains.collator]
name = "regionx-collator01"
command = "regionx-node"
args = [ "-lruntime=debug,parachain=trace" ]
args = [ "--log=xcm=trace" ]

0 comments on commit e5bd9da

Please sign in to comment.