Skip to content

Commit

Permalink
Simplify token gateway config (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 authored Jan 19, 2025
1 parent 2ff1d4e commit 7c62bbb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 72 deletions.
11 changes: 1 addition & 10 deletions docs/pages/developers/polkadot/token-gateway.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,14 @@ The first step is to implement the pallet config for the runtime.
use frame_support::parameter_types;
use ismp::module::IsmpModule;
use ismp::router::IsmpRouter;
use pallet_token_gateway::types::NativeAssetLocation;

parameter_types! {
// Set the correct decimals for the native currency
pub const Decimals: u8 = 12;
}

/// A constant value that represents the native asset
/// `NativeAssetLocation::Local` indicates the native asset is custodied locally.
/// `NativeAssetLocation::Remote` indicates that the native asset is custodied on some remote chain.
pub struct NativeAssetId;

impl Get<NativeAssetLocation<u32>> for NativeAssetId {
fn get() -> NativeAssetLocation<u32> {
NativeAssetLocation::Local(0)
}
}
pub const NativeAssetId: u32 = 0;

/// Should provide an account that is funded and can be used to pay for asset creation
pub struct AssetAdmin;
Expand Down
20 changes: 7 additions & 13 deletions modules/pallets/testsuite/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use ismp::{
};
use ismp_sync_committee::constants::sepolia::Sepolia;
use pallet_ismp::{offchain::Leaf, ModuleId};
use pallet_token_gateway::types::NativeAssetLocation;
use pallet_token_governor::GatewayParams;
use sp_core::{
crypto::AccountId32,
Expand Down Expand Up @@ -217,9 +216,9 @@ parameter_types! {

pub struct NativeAssetId;

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

Expand Down Expand Up @@ -485,16 +484,11 @@ 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().asset_id(),
H256::zero(),
);
pallet_token_gateway::LocalAssets::<Test>::insert(
H256::zero(),
NativeAssetId::get().asset_id(),
);
pallet_token_gateway::SupportedAssets::<Test>::insert(NativeAssetId::get(), H256::zero());
pallet_token_gateway::NativeAssets::<Test>::insert(NativeAssetId::get(), true);
pallet_token_gateway::LocalAssets::<Test>::insert(H256::zero(), NativeAssetId::get());
pallet_token_gateway::Precisions::<Test>::insert(
NativeAssetId::get().asset_id(),
NativeAssetId::get(),
StateMachine::Evm(1),
18,
);
Expand Down
8 changes: 4 additions & 4 deletions modules/pallets/testsuite/src/tests/pallet_token_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,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(),
asset_id: NativeAssetId::get(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand All @@ -52,7 +52,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(),
asset_id: NativeAssetId::get(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand Down Expand Up @@ -107,7 +107,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(),
asset_id: NativeAssetId::get(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand Down Expand Up @@ -334,7 +334,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(),
asset_id: NativeAssetId::get(),
destination: StateMachine::Evm(1),
recepient: H256::random(),
timeout: 0,
Expand Down
9 changes: 1 addition & 8 deletions modules/pallets/token-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ parameter_types! {
/// A constant value that represents the native asset
/// `NativeAssetLocation::Local` indicates the native asset is custodied locally.
/// `NativeAssetLocation::Remote` indicates that the native asset is custodied on some remote chain.
pub struct NativeAssetId;
const NativeAssetId: u32 = 0;
impl Get<NativeAssetLocation<u32>> for NativeAssetId {
fn get() -> NativeAssetLocation<u32> {
NativeAssetLocation::Local(0)
}
}
/// Should provide an account that is funded and can be used to pay for asset creation
pub struct AssetAdmin;
Expand Down
10 changes: 5 additions & 5 deletions modules/pallets/token-gateway/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod benches {
}

let asset = AssetRegistration {
local_id: T::NativeAssetId::get().asset_id(),
local_id: T::NativeAssetId::get(),
reg: asset_details,
native: true,
precision,
Expand All @@ -62,7 +62,7 @@ mod benches {
fn teleport() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

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

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(account.clone()).into(),
Expand All @@ -89,7 +89,7 @@ mod benches {
token_gateway: vec![1, 2, 3, 4, 5],
relayer_fee: 0u128.into(),
call_data: None,
redeem: false
redeem: false,
};

#[extrinsic_call]
Expand All @@ -116,7 +116,7 @@ mod benches {
fn update_erc6160_asset() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

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

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(account.clone()).into(),
Expand Down Expand Up @@ -154,7 +154,7 @@ mod benches {
precisions.insert(StateMachine::Evm(i as u32), 18);
}

let update = PrecisionUpdate { asset_id: T::NativeAssetId::get().asset_id(), precisions };
let update = PrecisionUpdate { asset_id: T::NativeAssetId::get(), precisions };

#[extrinsic_call]
_(RawOrigin::Signed(account), update);
Expand Down
23 changes: 13 additions & 10 deletions modules/pallets/token-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub mod pallet {
use alloc::collections::BTreeMap;
use pallet_hyperbridge::PALLET_HYPERBRIDGE;
use sp_runtime::traits::AccountIdConversion;
use types::{AssetRegistration, NativeAssetLocation, PrecisionUpdate, TeleportParams};
use types::{AssetRegistration, PrecisionUpdate, TeleportParams};

use super::*;
use frame_support::{
Expand Down Expand Up @@ -109,7 +109,7 @@ pub mod pallet {
+ fungibles::metadata::Mutate<Self::AccountId>;

/// The native asset ID
type NativeAssetId: Get<NativeAssetLocation<AssetId<Self>>>;
type NativeAssetId: Get<AssetId<Self>>;

/// The decimals of the native currency
#[pallet::constant]
Expand Down Expand Up @@ -251,9 +251,10 @@ pub mod pallet {
let dispatcher = <T as Config>::Dispatcher::default();
let asset_id = SupportedAssets::<T>::get(params.asset_id.clone())
.ok_or_else(|| Error::<T>::UnregisteredAsset)?;
let decimals = if params.asset_id == T::NativeAssetId::get().asset_id() {
let decimals = if params.asset_id == T::NativeAssetId::get() {
// Custody funds in pallet
if T::NativeAssetId::get().is_local() {
let is_native = NativeAssets::<T>::get(T::NativeAssetId::get());
if is_native {
<T as Config>::NativeCurrency::transfer(
&who,
&Self::pallet_account(),
Expand Down Expand Up @@ -550,7 +551,7 @@ where
}
})?;

let decimals = if local_asset_id == T::NativeAssetId::get().asset_id() {
let decimals = if local_asset_id == T::NativeAssetId::get() {
T::Decimals::get()
} else {
<T::Assets as fungibles::metadata::Inspect<T::AccountId>>::decimals(
Expand All @@ -569,8 +570,9 @@ where
meta: Meta { source, dest, nonce },
})?;
let beneficiary: T::AccountId = body.to.0.into();
if local_asset_id == T::NativeAssetId::get().asset_id() {
if T::NativeAssetId::get().is_local() {
if local_asset_id == T::NativeAssetId::get() {
let is_native = NativeAssets::<T>::get(T::NativeAssetId::get());
if is_native {
<T as Config>::NativeCurrency::transfer(
&Pallet::<T>::pallet_account(),
&beneficiary,
Expand Down Expand Up @@ -696,7 +698,7 @@ where
msg: "Token Gateway: Unknown asset".to_string(),
meta: Meta { source, dest, nonce },
})?;
let decimals = if local_asset_id == T::NativeAssetId::get().asset_id() {
let decimals = if local_asset_id == T::NativeAssetId::get() {
T::Decimals::get()
} else {
<T::Assets as fungibles::metadata::Inspect<T::AccountId>>::decimals(
Expand All @@ -715,8 +717,9 @@ where
meta: Meta { source, dest, nonce },
})?;

if local_asset_id == T::NativeAssetId::get().asset_id() {
if T::NativeAssetId::get().is_local() {
if local_asset_id == T::NativeAssetId::get() {
let is_native = NativeAssets::<T>::get(T::NativeAssetId::get());
if is_native {
<T as Config>::NativeCurrency::transfer(
&Pallet::<T>::pallet_account(),
&beneficiary,
Expand Down
22 changes: 0 additions & 22 deletions modules/pallets/token-gateway/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,6 @@ use crate::Config;
pub type AssetId<T> =
<<T as Config>::Assets as fungibles::Inspect<<T as frame_system::Config>::AccountId>>::AssetId;

/// A type that describes where the chain's native asset is custodied
#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq)]
pub enum NativeAssetLocation<AssetId> {
/// Use this when the native asset is custodied on this chain
Local(AssetId),
/// Use this if the native asset is custodied on a remote chain
Remote(AssetId),
}

impl<AssetId: Clone> NativeAssetLocation<AssetId> {
pub fn asset_id(&self) -> AssetId {
match &self {
NativeAssetLocation::Local(id) => id.clone(),
NativeAssetLocation::Remote(id) => id.clone(),
}
}

pub fn is_local(&self) -> bool {
matches!(&self, NativeAssetLocation::Local(_))
}
}

/// Asset teleportation parameters
#[derive(Debug, Clone, Encode, Decode, scale_info::TypeInfo, PartialEq, Eq)]
pub struct TeleportParams<AssetId, Balance> {
Expand Down

0 comments on commit 7c62bbb

Please sign in to comment.