Skip to content

Commit

Permalink
simplify pallet-token-gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Jan 13, 2025
1 parent ff8e062 commit fb765ee
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 200 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.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,7 @@ tesseract-config = { path = "tesseract/config" }
[workspace.dependencies.ethabi]
version = "18.0.0"
default-features = false
features = [
"rlp",
"parity-codec",
]
features = ["rlp", "parity-codec"]

[workspace.dependencies.ethers]
git = "https://github.com/polytope-labs/ethers-rs"
Expand Down
4 changes: 2 additions & 2 deletions modules/pallets/testsuite/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use frame_support::{
traits::{ConstU32, ConstU64, Get},
PalletId,
};
use frame_system::{EnsureRoot, EventRecord};
use frame_system::{EnsureRoot, EnsureSigned, EventRecord};
use ismp::{
consensus::{
ConsensusClient, ConsensusClientId, StateCommitment, StateMachineClient,
Expand Down Expand Up @@ -245,7 +245,7 @@ impl pallet_token_gateway::Config for Test {
type Assets = Assets;
type NativeCurrency = Balances;
type NativeAssetId = NativeAssetId;
type AssetIdFactory = AssetIdFactory;
type CreateOrigin = EnsureSigned<AccountId32>;
type Decimals = Decimals;
type AssetAdmin = AssetAdmin;
type EvmToSubstrate = ();
Expand Down
37 changes: 1 addition & 36 deletions modules/pallets/testsuite/src/tests/pallet_token_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ use pallet_token_gateway::{
use sp_core::{ByteArray, Get, Pair, H160, H256, U256};

use sp_runtime::{AccountId32, MultiSignature};
use token_gateway_primitives::{
AssetMetadata, GatewayAssetRegistration, PALLET_TOKEN_GATEWAY_ID, TOKEN_GOVERNOR_ID,
};
use token_gateway_primitives::{GatewayAssetRegistration, PALLET_TOKEN_GATEWAY_ID};
use xcm_simulator_example::ALICE;

use crate::runtime::{
Expand Down Expand Up @@ -306,39 +304,6 @@ fn inspector_should_handle_timeout_correctly() {
});
}

#[test]
fn receiving_remote_asset_creation() {
new_test_ext().execute_with(|| {
let asset_metadata = AssetMetadata {
name: "USDC".as_bytes().to_vec().try_into().unwrap(),
symbol: "USDC".as_bytes().to_vec().try_into().unwrap(),
decimals: 6,
minimum_balance: None,
};

let post = PostRequest {
source: StateMachine::Polkadot(3367),
dest: StateMachine::Kusama(100),
nonce: 0,
from: TOKEN_GOVERNOR_ID.to_vec(),
to: PALLET_TOKEN_GATEWAY_ID.to_vec(),
timeout_timestamp: 0,
body: asset_metadata.encode(),
};

let module = TokenGateway::default();
let res = module.on_accept(post);
println!("{res:?}");
assert!(res.is_ok());
let local_asset_id =
AssetIdFactory::create_asset_id(asset_metadata.symbol.to_vec()).unwrap();
let asset = pallet_token_gateway::SupportedAssets::<Test>::get(local_asset_id).unwrap();
// For the test we use the same asset id construction for local and token gateway, they
// should be equal
assert_eq!(local_asset_id, asset);
})
}

#[test]
fn dispatching_remote_asset_creation() {
new_test_ext().execute_with(|| {
Expand Down
12 changes: 9 additions & 3 deletions modules/pallets/token-gateway/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
[package]
name = "pallet-token-gateway"
version = "15.0.0"
version = "15.1.0"
edition = "2021"
description = "A substrate implementation of the token gateway protocol"
authors = ["Polytope Labs <hello@polytope.technology>"]
license = "Apache-2.0"
repository = "https://github.com/polytope-labs/hyperbridge"
homepage = "https://docs.hyperbridge.network/developers/polkadot/tokengateway"
homepage = "https://docs.hyperbridge.network/developers/polkadot/token-gateway"
documentation = "https://docs.rs/pallet-token-gateway"
keywords = ["substrate", "polkadot-sdk", "ISMP", "interoperability", "pallet-assets"]
keywords = [
"substrate",
"polkadot-sdk",
"ISMP",
"interoperability",
"pallet-assets",
]
readme = "README.md"

[dependencies]
Expand Down
114 changes: 48 additions & 66 deletions modules/pallets/token-gateway/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{types::*, *};
use frame_benchmarking::v2::*;
use frame_support::{
traits::{fungible, fungibles},
traits::{fungible, fungibles, Currency},
BoundedVec,
};
use frame_system::RawOrigin;
Expand All @@ -12,34 +12,6 @@ use scale_info::prelude::collections::BTreeMap;
use sp_runtime::AccountId32;
use token_gateway_primitives::{GatewayAssetRegistration, GatewayAssetUpdate};

fn dummy_teleport_asset<T>(
asset_id: AssetId<T>,
) -> TeleportParams<AssetId<T>, <<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance>
where
T: Config,
<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance: From<u128>,
{
TeleportParams {
asset_id,
destination: StateMachine::Evm(100),
recepient: H256::from([1u8; 32]),
amount: 1100000000u128.into(),
timeout: 10,
token_gateway: vec![1, 2, 3, 4, 5],
relayer_fee: 1000000002u128.into(),
call_data: None,
}
}

fn create_dummy_asset<T: Config>(
asset_details: GatewayAssetRegistration,
) -> AssetRegistration<AssetId<T>>
where
{
let local_id = T::AssetIdFactory::create_asset_id(asset_details.symbol.to_vec()).unwrap();
AssetRegistration { local_id, reg: asset_details, native: true }
}

#[benchmarks(
where
<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance: From<u128>,
Expand All @@ -65,7 +37,11 @@ mod benches {
chains: vec![StateMachine::Evm(100)],
minimum_balance: Some(10),
};
let asset = create_dummy_asset::<T>(asset_details);
let asset = AssetRegistration {
local_id: T::NativeAssetId::get(),
reg: asset_details,
native: true,
};

<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&account, u128::MAX.into());

Expand All @@ -79,77 +55,83 @@ mod benches {
fn teleport() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

let asset_details = GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
symbol: BoundedVec::try_from(b"SPC".to_vec()).unwrap(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: None,
};
let asset = create_dummy_asset::<T>(asset_details);
let asset_id = T::NativeAssetId::get();

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(account.clone()).into(),
asset.clone(),
AssetRegistration {
local_id: asset_id.clone(),
reg: GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
symbol: BoundedVec::try_from(b"SPC".to_vec()).unwrap(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: None,
},
native: true,
},
)?;

let dummy_teleport_params = dummy_teleport_asset::<T>(asset.local_id);

<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&account, u128::MAX.into());
let _ = T::NativeCurrency::deposit_creating(&account, u128::MAX.into());
let teleport_params = TeleportParams {
asset_id,
destination: StateMachine::Evm(100),
recepient: H256::from([1u8; 32]),
amount: 10_000_000_000_000u128.into(),
timeout: 0,
token_gateway: vec![1, 2, 3, 4, 5],
relayer_fee: 0u128.into(),
call_data: None,
};

#[extrinsic_call]
teleport(RawOrigin::Signed(account), dummy_teleport_params);
_(RawOrigin::Signed(account), teleport_params);
Ok(())
}

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

let mut addresses = BTreeMap::new();
for i in 0..x {
let addr = i.to_string().as_bytes().to_vec();
addresses.insert(StateMachine::Evm(100), addr);
}

#[extrinsic_call]
_(RawOrigin::Root, addresses);
_(RawOrigin::Signed(account), addresses);
Ok(())
}

#[benchmark]
fn update_erc6160_asset() -> Result<(), BenchmarkError> {
let acc_origin: T::AccountId = whitelisted_caller();
let account: T::AccountId = whitelisted_caller();

let asset_details = GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
symbol: BoundedVec::try_from(b"SPC".to_vec()).unwrap(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: None,
};
let asset = create_dummy_asset::<T>(asset_details.clone());

// set balances
<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&acc_origin, u128::MAX.into());
let asset_id = T::AssetIdFactory::create_asset_id(asset_details.symbol.to_vec()).unwrap();
<T::Assets as fungibles::Create<T::AccountId>>::create(
asset_id.into(),
acc_origin.clone(),
true,
1000000000u128.into(),
)?;
let local_id = T::NativeAssetId::get();

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(acc_origin.clone()).into(),
asset.clone(),
RawOrigin::Signed(account.clone()).into(),
AssetRegistration {
local_id,
reg: GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
symbol: BoundedVec::try_from(b"SPC".to_vec()).unwrap(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: None,
},
native: true,
},
)?;

let asset_update = GatewayAssetUpdate {
asset_id: H256::zero(),
asset_id: sp_io::hashing::keccak_256(b"SPC".as_ref()).into(),
add_chains: BoundedVec::try_from(vec![StateMachine::Evm(200)]).unwrap(),
remove_chains: BoundedVec::try_from(Vec::new()).unwrap(),
new_admins: BoundedVec::try_from(Vec::new()).unwrap(),
};

#[extrinsic_call]
_(RawOrigin::Signed(acc_origin), asset_update);
_(RawOrigin::Signed(account), asset_update);
Ok(())
}
}
Loading

0 comments on commit fb765ee

Please sign in to comment.