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

Pallet Token Gateway #320

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 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
50 changes: 50 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ members = [
"modules/ismp/pallets/call-decompressor",
"modules/ismp/pallets/asset-gateway",
"modules/ismp/pallets/token-governor",
"modules/ismp/pallets/token-gateway",
"modules/ismp/pallets/token-gateway-inspector",
"modules/ismp/pallets/hyperbridge",
"modules/ismp/pallets/state-coprocessor",
"modules/ismp/testsuite",
Expand Down Expand Up @@ -304,6 +306,8 @@ pallet-asset-gateway = { path = "modules/ismp/pallets/asset-gateway", default-fe
pallet-token-governor = { path = "modules/ismp/pallets/token-governor", default-features = false }
pallet-state-coprocessor = { path = "modules/ismp/pallets/state-coprocessor", default-features = false }
pallet-mmr = { path = "modules/trees/mmr/pallet", default-features = false }
pallet-token-gateway = { version = "1.15.0", path = "modules/ismp/pallets/token-gateway", default-features = false }
pallet-token-gateway-inspector = { path = "modules/ismp/pallets/token-gateway-inspector", default-features = false }

# merkle trees
pallet-mmr-runtime-api = { path = "modules/trees/mmr/pallet/runtime-api", default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion modules/ismp/pallets/asset-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pallet-xcm = { workspace = true }
staging-xcm = { workspace = true }
staging-xcm-builder = { workspace = true }
staging-xcm-executor = { workspace = true }
pallet-token-gateway = { workspace = true }

[features]
default = ["std"]
Expand All @@ -56,6 +57,7 @@ std = [
"pallet-token-governor/std",
"alloy-sol-types/std",
"alloy-primitives/std",
"anyhow/std"
"anyhow/std",
"pallet-token-gateway/std"
]
try-runtime = []
80 changes: 4 additions & 76 deletions modules/ismp/pallets/asset-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ extern crate alloc;
use alloc::{boxed::Box, string::ToString, vec};
use alloy_sol_types::SolType;
use core::marker::PhantomData;
use pallet_token_gateway::{
impls::{convert_to_balance, convert_to_erc20},
types::Body,
};
use pallet_token_governor::TokenGatewayParams;

use frame_support::{
Expand Down Expand Up @@ -273,22 +277,6 @@ where
}
}

alloy_sol_macro::sol! {
#![sol(all_derives)]
struct Body {
// Amount of the asset to be sent
uint256 amount;
// The asset identifier
bytes32 asset_id;
// Flag to redeem the erc20 asset on the destination
bool redeem;
// Sender address
bytes32 from;
// Recipient address
bytes32 to;
}
}

#[derive(Clone)]
pub struct Module<T>(PhantomData<T>);

Expand Down Expand Up @@ -514,63 +502,3 @@ where
}
}
}

/// Converts an ERC20 U256 to a DOT u128
pub fn convert_to_balance(value: U256) -> Result<u128, anyhow::Error> {
let dec_str = (value / U256::from(100_000_000u128)).to_string();
dec_str.parse().map_err(|e| anyhow::anyhow!("{e:?}"))
}

/// Converts a DOT u128 to an Erc20 denomination
pub fn convert_to_erc20(value: u128) -> U256 {
U256::from(value) * U256::from(100_000_000u128)
}

#[cfg(test)]
mod tests {
use sp_core::U256;
use sp_runtime::Permill;
use std::ops::Mul;

use crate::{convert_to_balance, convert_to_erc20};
#[test]
fn test_per_mill() {
let per_mill = Permill::from_parts(1_000);

println!("{}", per_mill.mul(20_000_000u128));
}

#[test]
fn balance_conversions() {
let supposedly_small_u256 = U256::from_dec_str("1000000000000000000").unwrap();
// convert erc20 value to dot value
let converted_balance = convert_to_balance(supposedly_small_u256).unwrap();
println!("{}", converted_balance);

let dot = 10_000_000_000u128;

assert_eq!(converted_balance, dot);

// Convert 1 dot to erc20

let dot = 10_000_000_000u128;
let erc_20_val = convert_to_erc20(dot);
assert_eq!(erc_20_val, U256::from_dec_str("1000000000000000000").unwrap());
}

#[test]
fn max_value_check() {
let max = U256::MAX;

let converted_balance = convert_to_balance(max);
assert!(converted_balance.is_err())
}

#[test]
fn min_value_check() {
let min = U256::from(1u128);

let converted_balance = convert_to_balance(min).unwrap();
assert_eq!(converted_balance, 0);
}
}
2 changes: 2 additions & 0 deletions modules/ismp/pallets/testsuite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ pallet-ismp-relayer = { workspace = true, default-features = true }
pallet-fishermen = { workspace = true, default-features = true }
pallet-call-decompressor = { workspace = true, default-features = true }
pallet-asset-gateway = { workspace = true, default-features = true }
pallet-token-gateway = { workspace = true, default-features = true }
sp-state-machine = { workspace = true, default-features = true }
mmr-primitives = { workspace = true, default-features = true }
pallet-mmr = { workspace = true, default-features = true }
pallet-token-governor = { workspace = true, default-features = true }
pallet-token-gateway-inspector = { workspace = true, default-features = true }

# Polkadot
pallet-xcm = { workspace = true, default-features = true }
Expand Down
49 changes: 47 additions & 2 deletions modules/ismp/pallets/testsuite/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

extern crate alloc;

use std::collections::BTreeSet;

use alloc::collections::BTreeMap;
use cumulus_pallet_parachain_system::ParachainSetCode;
use frame_support::{
Expand All @@ -39,16 +41,21 @@ use ismp::{
};
use ismp_sync_committee::constants::sepolia::Sepolia;
use pallet_ismp::{mmr::Leaf, ModuleId};
use pallet_token_governor::GatewayParams;
use sp_core::{
crypto::AccountId32,
offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt},
H256,
H160, H256,
};
use sp_runtime::{
traits::{IdentityLookup, Keccak256},
BuildStorage,
};
use staging_xcm::prelude::Location;
use substrate_state_machine::SubstrateStateMachine;
use xcm_simulator_example::ALICE;

pub const INITIAL_BALANCE: u128 = 1_000_000_000_000_000_000;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
Expand All @@ -75,6 +82,8 @@ frame_support::construct_runtime!(
TokenGovernor: pallet_token_governor,
Sudo: pallet_sudo,
IsmpSyncCommittee: ismp_sync_committee::pallet,
TokenGateway: pallet_token_gateway,
TokenGatewayInspector: pallet_token_gateway_inspector,
}
);

Expand Down Expand Up @@ -203,6 +212,22 @@ impl pallet_hyperbridge::Config for Test {
type IsmpHost = Ismp;
}

parameter_types! {
pub const NativeAssetId: Location = Location::here();
}

impl pallet_token_gateway::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Dispatcher = Ismp;
type Assets = Assets;
type Currency = Balances;
type NativeAssetId = NativeAssetId;
}

impl pallet_token_gateway_inspector::Config for Test {
type RuntimeEvent = RuntimeEvent;
}

impl ismp_sync_committee::pallet::Config for Test {
type AdminOrigin = EnsureRoot<AccountId32>;
type IsmpHost = Ismp;
Expand Down Expand Up @@ -424,7 +449,10 @@ where
pub fn new_test_ext() -> sp_io::TestExternalities {
let _ = env_logger::builder().is_test(true).try_init();

let storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut storage = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
pallet_balances::GenesisConfig::<Test> { balances: vec![(ALICE, INITIAL_BALANCE)] }
.assimilate_storage(&mut storage)
.unwrap();

let mut ext = sp_io::TestExternalities::new(storage);
register_offchain_ext(&mut ext);
Expand All @@ -435,6 +463,23 @@ 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(Location::here(), H256::zero());
pallet_token_gateway::LocalAssets::<Test>::insert(H256::zero(), Location::here());
pallet_token_gateway::TokenGatewayAddresses::<Test>::insert(
StateMachine::Evm(1),
H160::zero().0.to_vec(),
);
pallet_token_gateway_inspector::StandaloneChainAssets::<Test>::insert(
StateMachine::Kusama(100),
vec![H256::zero()].into_iter().collect::<BTreeSet<_>>(),
);

let params = GatewayParams {
address: H160::zero(),
host: H160::zero(),
call_dispatcher: H160::random(),
};
pallet_token_governor::TokenGatewayParams::<Test>::insert(StateMachine::Evm(1), params);
});
ext
}
Expand Down
2 changes: 2 additions & 0 deletions modules/ismp/pallets/testsuite/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ mod pallet_ismp_host_executive;
mod pallet_ismp_relayer;

mod xcm_integration_test;

mod pallet_token_gateway;
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use ismp::{
module::IsmpModule,
router::{PostRequest, Request, Timeout},
};
use pallet_asset_gateway::{convert_to_erc20, Body, Module};
use pallet_asset_gateway::Module;
use pallet_token_gateway::{impls::convert_to_erc20, types::Body};
use sp_core::{ByteArray, H160};
use staging_xcm::v4::{Junction, Junctions, Location, NetworkId, WeightLimit};
use xcm_simulator::TestExt;
Expand Down
Loading
Loading