Skip to content

Commit

Permalink
start converter implementation (PoC)
Browse files Browse the repository at this point in the history
  • Loading branch information
Agusrodri committed Apr 8, 2024
1 parent ebeeeda commit 4975538
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 15 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ flume = "0.11.0"
jsonrpsee = { version = "0.20.3" }
hex-literal = "0.4.1"

# Moonkit
pallet-foreign-asset-creator = { path = "pallets/foreign-asset-creator", default-features = false }

# Substrate (wasm)
frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.7.0", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions precompiles/pallet-xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ precompile-utils = { workspace = true, features = [ "testing", "codec-xcm" ] }
xcm-primitives = { workspace = true }

# Substrate
pallet-assets = { workspace = true, features = [ "std" ] }
pallet-balances = { workspace = true, features = [ "std", "insecure_zero_ed" ] }
pallet-foreign-asset-creator = { workspace = true, features = [ "std" ] }
pallet-timestamp = { workspace = true }
parity-scale-codec = { workspace = true, features = [ "max-encoded-len" ] }
scale-info = { workspace = true, features = [ "derive" ] }
Expand Down
40 changes: 35 additions & 5 deletions precompiles/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,48 @@ use frame_support::{
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;

use sp_core::U256;
use sp_runtime::traits::Dispatchable;
use sp_core::{H160, U256};
use sp_runtime::traits::{Dispatchable, MaybeEquivalence};
use sp_std::marker::PhantomData;
use sp_weights::Weight;
use xcm::{
latest::{Asset, AssetId, Assets, Fungibility, Location},
prelude::WeightLimit::*,
VersionedAssets, VersionedLocation,
};
use xcm_primitives::location_converter::{AccountIdToLocationConverter, GetAssetId};

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

pub const MAX_ASSETS_ARRAY_LIMIT: u32 = 2;

type GetArrayLimit = ConstU32<MAX_ASSETS_ARRAY_LIMIT>;

pub struct PalletXcmPrecompile<Runtime>(PhantomData<Runtime>);
pub struct PalletXcmPrecompile<Runtime, AssetId, AssetIdToLocationManager, AssetIdInfoGetter>(
PhantomData<(
Runtime,
AssetId,
AssetIdToLocationManager,
AssetIdInfoGetter,
)>,
);

#[precompile_utils::precompile]
impl<Runtime> PalletXcmPrecompile<Runtime>
impl<Runtime, AssetId, AssetIdToLocationManager, AssetIdInfoGetter>
PalletXcmPrecompile<Runtime, AssetId, AssetIdToLocationManager, AssetIdInfoGetter>
where
Runtime: pallet_xcm::Config + pallet_evm::Config + frame_system::Config,
<Runtime as frame_system::Config>::RuntimeCall:
Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
<<Runtime as frame_system::Config>::RuntimeCall as Dispatchable>::RuntimeOrigin:
From<Option<Runtime::AccountId>>,
<Runtime as frame_system::Config>::RuntimeCall: From<pallet_xcm::Call<Runtime>>,
AssetIdToLocationManager: MaybeEquivalence<Location, AssetId>,
AssetIdInfoGetter: GetAssetId<AssetId>,
Runtime::AccountId: From<H160> + Into<H160>,
AssetId: From<u8> + TryFrom<u16> + TryFrom<u128>,
{
#[precompile::public(
"transferAssets(\
Expand Down Expand Up @@ -103,4 +115,22 @@ where
RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;
Ok(())
}

// TODO: finish
#[precompile::public("transferAssetsWithAddress()")]
fn transfer_assets_with_address(handle: &mut impl PrecompileHandle) -> EvmResult {
/*
// Just testing stuff
let asset_info = AssetIdInfoGetter::get_asset_id_info();
let account: <Runtime as frame_system::Config>::AccountId =
H160::from_low_u64_be(2050).into();
let result = AccountIdToLocationConverter::<_, AssetId, AssetIdToLocationManager>::convert(
account, asset_info, 3u8, 42u8,
);
println!("RESULT: {:#?}", result); */

Ok(())
}
}
67 changes: 58 additions & 9 deletions precompiles/pallet-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use frame_support::{
traits::{Everything, Nothing, OriginTrait},
weights::{RuntimeDbWeight, Weight},
};
use frame_system::EnsureRoot;
use pallet_evm::{EnsureAddressNever, EnsureAddressRoot, GasWeightMapping};
use precompile_utils::{
mock_account,
Expand All @@ -38,6 +39,7 @@ use xcm_executor::{
traits::{ConvertLocation, TransactAsset, WeightTrader},
AssetsInHolding,
};
pub use xcm_primitives::location_converter::AssetIdInfoGetter;
use Junctions::Here;

pub type AccountId = MockAccount;
Expand All @@ -52,7 +54,9 @@ construct_runtime!(
Balances: pallet_balances,
Evm: pallet_evm,
Timestamp: pallet_timestamp,
PolkadotXcm: pallet_xcm
PolkadotXcm: pallet_xcm,
Assets: pallet_assets,
ForeignAssetCreator: pallet_foreign_asset_creator,
}
);

Expand Down Expand Up @@ -114,11 +118,56 @@ impl pallet_balances::Config for Runtime {
type MaxFreezes = ();
type RuntimeFreezeReason = ();
}
parameter_types! {
pub const AssetDeposit: u64 = 0;
pub const ApprovalDeposit: u64 = 0;
pub const StringLimit: u32 = 50;
pub const MetadataDepositBase: u64 = 0;
pub const MetadataDepositPerByte: u64 = 0;
}

pub type AssetId = u16;

impl pallet_assets::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = AssetId;
type AssetIdParameter = parity_scale_codec::Compact<AssetId>;
type Currency = Balances;
type CreateOrigin = frame_support::traits::NeverEnsureOrigin<AccountId>;
type ForceOrigin = EnsureRoot<AccountId>;
type AssetDeposit = AssetDeposit;
type AssetAccountDeposit = AssetDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = StringLimit;
type Freezer = ();
type Extra = ();
type CallbackHandle = ();
type WeightInfo = ();
type RemoveItemsLimit = ConstU32<1000>;
pallet_assets::runtime_benchmarks_enabled! {
type BenchmarkHelper = ();
}
}

impl pallet_foreign_asset_creator::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type ForeignAsset = Location;
type ForeignAssetCreatorOrigin = EnsureRoot<AccountId>;
type ForeignAssetModifierOrigin = EnsureRoot<AccountId>;
type ForeignAssetDestroyerOrigin = EnsureRoot<AccountId>;
type Fungibles = Assets;
type WeightInfo = ();
type OnForeignAssetCreated = ();
type OnForeignAssetDestroyed = ();
}

pub type Precompiles<R> =
PrecompileSetBuilder<R, PrecompileAt<AddressU64<1>, PalletXcmPrecompile<R>>>;
pub type Precompiles<R, A, F, G> =
PrecompileSetBuilder<R, PrecompileAt<AddressU64<1>, PalletXcmPrecompile<R, A, F, G>>>;

pub type PCall = PalletXcmPrecompileCall<Runtime>;
pub type PCall = PalletXcmPrecompileCall<Runtime, AssetId, ForeignAssetCreator, AssetIdInfoGetter>;

mock_account!(ParentAccount, |_| MockAccount::from_u64(4));

Expand All @@ -134,7 +183,7 @@ const BLOCK_STORAGE_LIMIT: u64 = 40 * 1024;

parameter_types! {
pub BlockGasLimit: U256 = U256::from(u64::MAX);
pub PrecompilesValue: Precompiles<Runtime> = Precompiles::new();
pub PrecompilesValue: Precompiles<Runtime, AssetId, ForeignAssetCreator, AssetIdInfoGetter> = Precompiles::new();
pub const WeightPerGas: Weight = Weight::from_parts(1, 0);
pub GasLimitPovSizeRatio: u64 = {
let block_gas_limit = BlockGasLimit::get().min(u64::MAX.into()).low_u64();
Expand Down Expand Up @@ -169,7 +218,7 @@ impl pallet_evm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type PrecompilesValue = PrecompilesValue;
type PrecompilesType = Precompiles<Self>;
type PrecompilesType = Precompiles<Self, AssetId, ForeignAssetCreator, AssetIdInfoGetter>;
type ChainId = ();
type OnChargeTransaction = ();
type BlockGasLimit = BlockGasLimit;
Expand Down Expand Up @@ -291,7 +340,7 @@ impl pallet_xcm::Config for Runtime {
}

use sp_std::cell::RefCell;
use xcm::latest::opaque;
use xcm::latest::{opaque, Assets as XcmAssets};
// Simulates sending a XCM message
thread_local! {
pub static SENT_XCM: RefCell<Vec<(Location, opaque::Xcm)>> = RefCell::new(Vec::new());
Expand All @@ -309,7 +358,7 @@ impl SendXcm for TestSendXcm {
q.borrow_mut()
.push((destination.clone().unwrap(), message.clone().unwrap()))
});
Ok(((), Assets::new()))
Ok(((), XcmAssets::new()))
}

fn deliver(_: Self::Ticket) -> Result<XcmHash, SendError> {
Expand All @@ -325,7 +374,7 @@ impl SendXcm for DoNothingRouter {
_destination: &mut Option<Location>,
_message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
Ok(((), Assets::new()))
Ok(((), XcmAssets::new()))
}

fn deliver(_: Self::Ticket) -> Result<XcmHash, SendError> {
Expand Down
2 changes: 1 addition & 1 deletion precompiles/pallet-xcm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use precompile_utils::testing::*;
use sp_weights::Weight;
use xcm::latest::Junction::*;

fn precompiles() -> Precompiles<Runtime> {
fn precompiles() -> Precompiles<Runtime, AssetId, ForeignAssetCreator, AssetIdInfoGetter> {
PrecompilesValue::get()
}

Expand Down
10 changes: 10 additions & 0 deletions primitives/xcm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ edition = "2021"
version = "0.1.0"

[dependencies]
frame-support = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
parity-scale-codec = { workspace = true, features = [ "derive" ] }
xcm = { workspace = true }

[features]
default = [ "std" ]
std = [
"frame-support/std",
"sp-core/std",
"sp-runtime/std",
"sp-std/std",
"parity-scale-codec/std",
"xcm/std"
]
runtime-benchmarks = []
2 changes: 2 additions & 0 deletions primitives/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

use sp_runtime::DispatchResult;

pub mod location_converter;

/// Pause and resume execution of XCM
pub trait PauseXcmExecution {
fn suspend_xcm_execution() -> DispatchResult;
Expand Down
Loading

0 comments on commit 4975538

Please sign in to comment.