Skip to content

Commit

Permalink
Enable Native ERC20 precompile in Moonriver (#1103)
Browse files Browse the repository at this point in the history
* Enable Native ERC20 precompile

* Cargo sort

* Cargo sort

* FMT
  • Loading branch information
girazoki authored Dec 17, 2021
1 parent 75e0a3a commit 60bb5ce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 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 runtime/moonriver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ xcm-transactor = { path = "../../pallets/xcm-transactor", default-features = fal
# Moonbeam precompiles
crowdloan-rewards-precompiles = { path = "../../precompiles/crowdloan-rewards", default-features = false }
pallet-evm-precompile-assets-erc20 = { path = "../../precompiles/assets-erc20", default-features = false }
pallet-evm-precompile-balances-erc20 = { path = "../../precompiles/balances-erc20", default-features = false }
parachain-staking-precompiles = { path = "../../precompiles/parachain-staking", default-features = false }
relay-encoder-precompiles = { path = "../../precompiles/relay-encoder", default-features = false }
xcm-transactor-precompiles = { path = "../../precompiles/xcm_transactor", default-features = false }
Expand Down Expand Up @@ -170,6 +171,7 @@ std = [
"pallet-democracy/std",
"pallet-ethereum-chain-id/std",
"pallet-ethereum/std",
"pallet-evm-precompile-balances-erc20/std",
"pallet-evm/std",
"pallet-maintenance-mode/std",
"pallet-migrations/std",
Expand Down
35 changes: 32 additions & 3 deletions runtime/moonriver/src/precompiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use fp_evm::{Context, ExitError, PrecompileOutput};
use moonbeam_relay_encoder::kusama::KusamaEncoder;
use pallet_evm::{AddressMapping, Precompile, PrecompileSet};
use pallet_evm_precompile_assets_erc20::Erc20AssetsPrecompileSet;
use pallet_evm_precompile_balances_erc20::{Erc20BalancesPrecompile, Erc20Metadata};
use pallet_evm_precompile_blake2::Blake2F;
use pallet_evm_precompile_bn128::{Bn128Add, Bn128Mul, Bn128Pairing};
use pallet_evm_precompile_dispatch::Dispatch;
Expand All @@ -33,6 +34,26 @@ use sp_std::marker::PhantomData;
use xcm_transactor_precompiles::XcmTransactorWrapper;
use xtokens_precompiles::XtokensWrapper;

pub struct NativeErc20Metadata;

/// ERC20 metadata for the native token.
impl Erc20Metadata for NativeErc20Metadata {
/// Returns the name of the token.
fn name() -> &'static str {
"MOVR token"
}

/// Returns the symbol of the token.
fn symbol() -> &'static str {
"MOVR"
}

/// Returns the decimals places of the token.
fn decimals() -> u8 {
18
}
}

/// The asset precompile address prefix. Addresses that match against this prefix will be routed
/// to Erc20AssetsPrecompileSet
pub const ASSET_PRECOMPILE_ADDRESS_PREFIX: &[u8] = &[255u8; 4];
Expand All @@ -51,9 +72,11 @@ where
/// Return all addresses that contain precompiles. This can be used to populate dummy code
/// under the precompile.
pub fn used_addresses() -> impl Iterator<Item = R::AccountId> {
sp_std::vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 2048, 2049, 2052, 2053, 2054]
.into_iter()
.map(|x| R::AddressMapping::into_account_id(hash(x)))
sp_std::vec![
1, 2, 3, 4, 5, 6, 7, 8, 9, 1024, 1025, 1026, 2048, 2049, 2050, 2052, 2053, 2054
]
.into_iter()
.map(|x| R::AddressMapping::into_account_id(hash(x)))
}
}

Expand All @@ -66,6 +89,7 @@ where
Dispatch<R>: Precompile,
ParachainStakingWrapper<R>: Precompile,
CrowdloanRewardsWrapper<R>: Precompile,
Erc20BalancesPrecompile<R, NativeErc20Metadata>: Precompile,
Erc20AssetsPrecompileSet<R>: PrecompileSet,
XtokensWrapper<R>: Precompile,
RelayEncoderWrapper<R, KusamaEncoder>: Precompile,
Expand Down Expand Up @@ -99,6 +123,11 @@ where
a if a == hash(2049) => Some(CrowdloanRewardsWrapper::<R>::execute(
input, target_gas, context,
)),
a if a == hash(2050) => {
Some(Erc20BalancesPrecompile::<R, NativeErc20Metadata>::execute(
input, target_gas, context,
))
}
a if a == hash(2052) => Some(XtokensWrapper::<R>::execute(input, target_gas, context)),
a if a == hash(2053) => Some(RelayEncoderWrapper::<R, KusamaEncoder>::execute(
input, target_gas, context,
Expand Down

0 comments on commit 60bb5ce

Please sign in to comment.