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

Add support for pallet-xcm precompile #35

Merged
merged 23 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
30 changes: 30 additions & 0 deletions Cargo.lock

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

74 changes: 74 additions & 0 deletions precompiles/pallet-xcm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
[package]
name = "pallet-evm-precompile-xcm"
authors = { workspace = true }
description = "A Precompile to make pallet-xcm accessible to pallet-evm"
edition = "2021"
version = "0.1.0"

[dependencies]
log = { workspace = true }
num_enum = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true }
xcm-primitives = { workspace = true }

# Substrate
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
sp-weights = { workspace = true }

# Frontier
evm = { workspace = true, features = [ "with-codec" ] }
fp-evm = { workspace = true }
pallet-evm = { workspace = true, features = [ "forbid-evm-reentrancy" ] }

# Polkadot
xcm = { workspace = true }
pallet-xcm = { workspace = true }

# Cumulus
cumulus-primitives-core = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true, features = [ "testing", "codec-xcm" ] }
xcm-primitives = { workspace = true }

# Substrate
pallet-balances = { workspace = true, features = [ "std", "insecure_zero_ed" ] }
pallet-timestamp = { workspace = true }
parity-scale-codec = { workspace = true, features = [ "max-encoded-len" ] }
scale-info = { workspace = true, features = [ "derive" ] }
sp-io = { workspace = true }

# Polkadot
xcm-builder = { workspace = true }
xcm-executor = { workspace = true }

[features]
default = [ "std" ]
std = [
"cumulus-primitives-core/std",
"frame-support/std",
"frame-system/std",
"pallet-evm/std",
"pallet-xcm/std",
"precompile-utils/std",
"sp-core/std",
"sp-std/std",
"xcm/std",
"xcm-builder/std",
"xcm-executor/std",
"xcm-primitives/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
41 changes: 41 additions & 0 deletions precompiles/pallet-xcm/XcmInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

/// @author The Moonbeam Team
/// @title XCM precompile Interface
/// @dev The interface that Solidity contracts use to interact with the substrate pallet-xcm.
/// @custom:address 0x0000000000000000000000000000000000000820
interface XCM {
// A location is defined by its number of parents and the encoded junctions (interior)
struct Location {
uint8 parents;
bytes[] interior;
}

// Support for Weights V2
struct Weight {
uint64 refTime;
uint64 proofSize;
}

// A way to represent fungible assets in XCM
struct Asset {
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
Location location;
uint256 amount;
}

/// @dev Function to send assets via XCM using transfer_assets() pallet-xcm extrinsic.
/// @custom:selector 650ef8c7
/// @param dest The destination chain.
/// @param beneficiary The actual account that will receive the tokens in dest.
/// @param assets The combination (array) of assets to send.
/// @param feeAssetItem The index of the asset that will be used to pay for fees.
/// @param weight The weight to be used for the whole XCM operation.
function transferAssets(
Location memory dest,
Location memory beneficiary,
Asset[] memory assets,
uint32 feeAssetItem,
Weight memory weight
) external;
}
105 changes: 105 additions & 0 deletions precompiles/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright Moonsong Labs
// This file is part of Moonkit.

// Moonkit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonkit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonkit. If not, see <http://www.gnu.org/licenses/>.

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

use fp_evm::PrecompileHandle;
use frame_support::{
dispatch::{GetDispatchInfo, PostDispatchInfo},
traits::ConstU32,
};
use pallet_evm::AddressMapping;
use precompile_utils::prelude::*;

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

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

pub const MAX_ASSETS_ARRAY_LIMIT: u32 = 2;
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved

type GetArrayLimit = ConstU32<MAX_ASSETS_ARRAY_LIMIT>;

pub struct PalletXcmPrecompile<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> PalletXcmPrecompile<Runtime>
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>>,
{
#[precompile::public(
"transferAssets(\
(uint8,bytes[]),\
(uint8,bytes[]),\
((uint8,bytes[]),uint256)[],\
uint32,\
(uint64,uint64))"
)]
fn transfer_assets(
handle: &mut impl PrecompileHandle,
dest: Location,
beneficiary: Location,
assets: BoundedVec<(Location, Convert<U256, u128>), GetArrayLimit>,
fee_asset_item: u32,
weight: Weight,
) -> EvmResult {
// TODO: record proper cost
handle.record_cost(1000)?;
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved

let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);
let assets: Vec<_> = assets.into();

let assets_to_send: Assets = assets
.into_iter()
.map(|asset| Asset {
id: AssetId(asset.0),
fun: Fungibility::Fungible(asset.1.converted()),
})
.collect::<Vec<Asset>>()
.into();

let weight_limit = match weight.ref_time() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very interesting :)

u64::MAX => Unlimited,
_ => Limited(weight),
};

let call = pallet_xcm::Call::<Runtime>::transfer_assets {
dest: Box::new(VersionedLocation::V4(dest)),
Agusrodri marked this conversation as resolved.
Show resolved Hide resolved
beneficiary: Box::new(VersionedLocation::V4(beneficiary)),
assets: Box::new(VersionedAssets::V4(assets_to_send)),
fee_asset_item,
weight_limit,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;
Ok(())
}
}
Loading
Loading