Skip to content

Commit

Permalink
support a remotely custodied asset as the native currency
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 committed Jan 15, 2025
1 parent 06decb4 commit 3b1a42a
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

32 changes: 17 additions & 15 deletions modules/pallets/testsuite/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use ismp::{
};
use ismp_sync_committee::constants::sepolia::Sepolia;
use pallet_ismp::{offchain::Leaf, ModuleId};
use pallet_token_gateway::types::CreateAssetId;
use pallet_token_gateway::types::NativeAssetLocation;
use pallet_token_governor::GatewayParams;
use sp_core::{
crypto::AccountId32,
Expand Down Expand Up @@ -215,19 +215,11 @@ parameter_types! {
pub const Decimals: u8 = 10;
}

pub struct AssetIdFactory;

pub struct NativeAssetId;

impl Get<H256> for NativeAssetId {
fn get() -> H256 {
sp_io::hashing::keccak_256(b"BRIDGE").into()
}
}

impl CreateAssetId<H256> for AssetIdFactory {
fn create_asset_id(symbol: Vec<u8>) -> Result<H256, anyhow::Error> {
Ok(sp_io::hashing::keccak_256(&symbol).into())
impl Get<NativeAssetLocation<H256>> for NativeAssetId {
fn get() -> NativeAssetLocation<H256> {
NativeAssetLocation::Local(sp_io::hashing::keccak_256(b"BRIDGE").into())
}
}

Expand Down Expand Up @@ -493,9 +485,19 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
pallet_token_governor::Params::<Balance> { registration_fee: Default::default() };

pallet_token_governor::ProtocolParams::<Test>::put(protocol_params);
pallet_token_gateway::SupportedAssets::<Test>::insert(NativeAssetId::get(), H256::zero());
pallet_token_gateway::LocalAssets::<Test>::insert(H256::zero(), NativeAssetId::get());
pallet_token_gateway::Decimals::<Test>::insert(NativeAssetId::get(), 18);
pallet_token_gateway::SupportedAssets::<Test>::insert(
NativeAssetId::get().asset_id(),
H256::zero(),
);
pallet_token_gateway::LocalAssets::<Test>::insert(
H256::zero(),
NativeAssetId::get().asset_id(),
);
pallet_token_gateway::Decimals::<Test>::insert(
NativeAssetId::get().asset_id(),
StateMachine::Evm(1),
18,
);
pallet_token_gateway::TokenGatewayAddresses::<Test>::insert(
StateMachine::Evm(1),
H160::zero().0.to_vec(),
Expand Down
25 changes: 12 additions & 13 deletions modules/pallets/testsuite/src/tests/pallet_token_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use ismp::{
};
use pallet_token_gateway::{
impls::convert_to_erc20,
types::{
AssetRegistration, Body, BodyWithCall, CreateAssetId, SubstrateCalldata, TeleportParams,
},
types::{AssetRegistration, Body, BodyWithCall, SubstrateCalldata, TeleportParams},
};

use sp_core::{ByteArray, Get, Pair, H160, H256, U256};
Expand All @@ -20,8 +18,8 @@ use token_gateway_primitives::{GatewayAssetRegistration, PALLET_TOKEN_GATEWAY_ID
use xcm_simulator_example::ALICE;

use crate::runtime::{
new_test_ext, AssetIdFactory, NativeAssetId, RuntimeOrigin, Test, TokenGateway,
TokenGatewayInspector, INITIAL_BALANCE,
new_test_ext, NativeAssetId, RuntimeOrigin, Test, TokenGateway, TokenGatewayInspector,
INITIAL_BALANCE,
};
use ismp::module::IsmpModule;

Expand All @@ -31,7 +29,7 @@ const SEND_AMOUNT: u128 = 1000_000_000_0000;
fn should_teleport_asset_correctly() {
new_test_ext().execute_with(|| {
let params = TeleportParams {
asset_id: NativeAssetId::get(),
asset_id: NativeAssetId::get().asset_id(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand All @@ -53,7 +51,7 @@ fn should_teleport_asset_correctly() {
fn should_receive_asset_correctly() {
new_test_ext().execute_with(|| {
let params = TeleportParams {
asset_id: NativeAssetId::get(),
asset_id: NativeAssetId::get().asset_id(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand Down Expand Up @@ -107,7 +105,7 @@ fn should_receive_asset_correctly() {
fn should_timeout_request_correctly() {
new_test_ext().execute_with(|| {
let params = TeleportParams {
asset_id: NativeAssetId::get(),
asset_id: NativeAssetId::get().asset_id(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand All @@ -125,8 +123,8 @@ fn should_timeout_request_correctly() {

let module = TokenGateway::default();
let post = PostRequest {
source: StateMachine::Evm(1),
dest: StateMachine::Kusama(100),
source: StateMachine::Evm(97),
dest: StateMachine::Evm(1),
nonce: 0,
from: H160::zero().0.to_vec(),
to: H160::zero().0.to_vec(),
Expand Down Expand Up @@ -307,16 +305,17 @@ fn inspector_should_handle_timeout_correctly() {
#[test]
fn dispatching_remote_asset_creation() {
new_test_ext().execute_with(|| {
let local_asset_id = AssetIdFactory::create_asset_id("MDG".as_bytes().to_vec()).unwrap();
let local_asset_id = sp_io::hashing::keccak_256("MDG".as_bytes()).into();
let reg = AssetRegistration::<H256> {
local_id: local_asset_id,
reg: GatewayAssetRegistration {
name: "MOODENG".as_bytes().to_vec().try_into().unwrap(),
symbol: "MDG".as_bytes().to_vec().try_into().unwrap(),
chains: vec![StateMachine::Evm(97)],
chains: vec![StateMachine::Evm(1)],
minimum_balance: None,
},
native: true,
precision: vec![(StateMachine::Evm(1), 18)].into_iter().collect(),
};

TokenGateway::create_erc6160_asset(RuntimeOrigin::signed(ALICE), reg).unwrap();
Expand All @@ -332,7 +331,7 @@ fn dispatching_remote_asset_creation() {
fn should_receive_asset_with_call_correctly() {
new_test_ext().execute_with(|| {
let params = TeleportParams {
asset_id: NativeAssetId::get(),
asset_id: NativeAssetId::get().asset_id(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand Down
2 changes: 1 addition & 1 deletion modules/pallets/token-gateway/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-token-gateway"
version = "15.1.1"
version = "15.2.1"
edition = "2021"
description = "A substrate implementation of the token gateway protocol"
authors = ["Polytope Labs <hello@polytope.technology>"]
Expand Down
17 changes: 13 additions & 4 deletions modules/pallets/token-gateway/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mod benches {
use super::*;

#[benchmark]
fn create_erc6160_asset() -> Result<(), BenchmarkError> {
fn create_erc6160_asset(x: Linear<1, 100>) -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

let asset_details = GatewayAssetRegistration {
Expand All @@ -37,10 +37,17 @@ mod benches {
chains: vec![StateMachine::Evm(100)],
minimum_balance: Some(10),
};

let mut precision = BTreeMap::new();
for i in 0..x {
precision.insert(StateMachine::Evm(i as u32), 18);
}

let asset = AssetRegistration {
local_id: T::NativeAssetId::get(),
local_id: T::NativeAssetId::get().asset_id(),
reg: asset_details,
native: true,
precision,
};

<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&account, u128::MAX.into());
Expand All @@ -55,7 +62,7 @@ mod benches {
fn teleport() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

let asset_id = T::NativeAssetId::get();
let asset_id = T::NativeAssetId::get().asset_id();

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(account.clone()).into(),
Expand All @@ -68,6 +75,7 @@ mod benches {
minimum_balance: None,
},
native: true,
precision: vec![(StateMachine::Evm(100), 18)].into_iter().collect(),
},
)?;

Expand Down Expand Up @@ -107,7 +115,7 @@ mod benches {
fn update_erc6160_asset() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

let local_id = T::NativeAssetId::get();
let local_id = T::NativeAssetId::get().asset_id();

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(account.clone()).into(),
Expand All @@ -120,6 +128,7 @@ mod benches {
minimum_balance: None,
},
native: true,
precision: Default::default(),
},
)?;

Expand Down
Loading

0 comments on commit 3b1a42a

Please sign in to comment.