Skip to content

Commit

Permalink
adject the local mock runtime impl for xcm bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
Freddy Li authored and Freddy Li committed Jan 15, 2024
1 parent 674b1bc commit c310a89
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 25 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions bridge/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ frame_support::construct_runtime!(
SygmaBridge: sygma_bridge::{Pallet, Call, Storage, Event<T>} = 6,
SygmaPercentageFeeHandler: sygma_percentage_feehandler::{Pallet, Call, Storage, Event<T>} = 7,
SygmaFeeHandlerRouter: sygma_fee_handler_router::{Pallet, Call, Storage, Event<T>} = 8,
SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Call, Storage, Event<T>} = 9,
SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Call, Storage, Event<T>} = 10,
SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Event<T>} = 9,
SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Event<T>} = 10,
}
);

Expand Down
1 change: 0 additions & 1 deletion xcm-bridge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ cumulus-primitives-utility = { git = "https://github.com/paritytech/polkadot-sdk
# Local
sygma-traits = { path = "../traits" }
pallet-parachain-info = { path = "../parachain-info" }
sygma-bridge = { path = "../bridge" }

[features]
default = ["std"]
Expand Down
3 changes: 1 addition & 2 deletions xcm-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,9 @@ pub mod pallet {

use crate::mock::{
ALICE, BOB, ENDOWED_BALANCE,
ParaA, ParaAssets, ParaB, ParaBalances, TestNet,
ParaA, ParaAssets, ParaB, ParaBalances, TestNet, UsdtLocation
};
use crate::mock::para::Runtime;
use sygma_bridge::mock::UsdtLocation;

#[test]
fn test_transfer_self_reserve_asset_to_parachain() {
Expand Down
95 changes: 77 additions & 18 deletions xcm-bridge/src/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#![cfg(test)]

use std::marker::PhantomData;
use std::result;

use frame_support::{construct_runtime, pallet_prelude::ConstU32, parameter_types, sp_runtime::{
AccountId32,
BuildStorage,
Expand All @@ -13,15 +16,13 @@ use polkadot_parachain_primitives::primitives::Sibling;
use sp_io::TestExternalities;
use xcm::latest::{prelude::*, Weight as XCMWeight};
use xcm_builder::{AccountId32Aliases, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, CurrencyAdapter, FixedWeightBounds, FungiblesAdapter, IsConcrete, NativeAsset, NoChecking, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative, SovereignSignedViaLocation, TakeWeightCredit};
use xcm_executor::{traits::WithOriginFilter, XcmExecutor};
use xcm_executor::{Config, traits::{Error as ExecutionError, MatchesFungibles, WithOriginFilter}, XcmExecutor};
use xcm_simulator::{decl_test_network, decl_test_parachain, decl_test_relay_chain, TestExt};

use sygma_bridge::XCMAssetTransactor;

use crate as sygma_xcm_bridge;

mod relay;
pub(crate) mod para;
pub mod relay;
pub mod para;

type Block = frame_system::mocking::MockBlock<Runtime>;

Expand All @@ -34,9 +35,10 @@ construct_runtime!(
System: frame_system::{Pallet, Call, Storage, Config<T>, Event<T>},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
ParachainInfo: pallet_parachain_info::{Pallet, Storage, Config<T>},
CumulusXcm: cumulus_pallet_xcm::{Pallet, Event<T>, Origin},
SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Storage, Event<T>},
SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Call, Storage, Event<T>},
SygmaXcmBridge: sygma_xcm_bridge::{Pallet, Event<T>},
SygmaBridgeForwarder: sygma_bridge_forwarder::{Pallet, Event<T>},
}
);

Expand Down Expand Up @@ -109,10 +111,12 @@ parameter_types! {
pub const MetadataDepositPerByte: Balance = 1;
}

pub type AssetId = u32;

impl pallet_assets::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = u32;
type AssetId = AssetId;
type AssetIdParameter = codec::Compact<u32>;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId32>>;
Expand Down Expand Up @@ -141,17 +145,20 @@ impl sygma_xcm_bridge::Config for Runtime {
type SelfLocation = SelfLocation;
}

impl sygma_bridge_forwarder::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SygmaBridge = ();
type XCMBridge = ();
}

impl pallet_parachain_info::Config for Runtime {}

pub struct XcmConfig;

impl Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = XcmRouter;
type AssetTransactor = XCMAssetTransactor<
CurrencyTransactor,
FungiblesTransactor,
SygmaXcmBridge,
SygmaBridgeForwarder,
>;
type AssetTransactor = (CurrencyTransactor, FungiblesTransactor);
type OriginConverter = XcmOriginToTransactDispatchOrigin;
type IsReserve = NativeAsset;
type IsTeleporter = ();
Expand Down Expand Up @@ -209,6 +216,42 @@ parameter_types! {
pub const MaxInstructions: u32 = 100;
}

impl cumulus_pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type XcmExecutor = XcmExecutor<XcmConfig>;
}

parameter_types! {
pub NativeLocation: MultiLocation = MultiLocation::here();
pub UsdtAssetId: AssetId = 0;
pub UsdtLocation: MultiLocation = MultiLocation::new(
1,
X3(
Parachain(2005),
slice_to_generalkey(b"sygma"),
slice_to_generalkey(b"usdt"),
),
);
pub CheckingAccount: AccountId32 = AccountId32::new([102u8; 32]);
}

pub struct SimpleForeignAssetConverter(PhantomData<()>);

impl MatchesFungibles<AssetId, Balance> for SimpleForeignAssetConverter {
fn matches_fungibles(a: &MultiAsset) -> result::Result<(AssetId, Balance), ExecutionError> {
match (&a.fun, &a.id) {
(Fungible(ref amount), Concrete(ref id)) => {
if id == &UsdtLocation::get() {
Ok((UsdtAssetId::get(), *amount))
} else {
Err(ExecutionError::AssetNotHandled)
}
}
_ => Err(ExecutionError::AssetNotHandled),
}
}
}

pub type CurrencyTransactor = CurrencyAdapter<
// Use this currency:
Balances,
Expand All @@ -227,7 +270,7 @@ pub type FungiblesTransactor = FungiblesAdapter<
// Use this fungibles implementation:
Assets,
// Use this currency when it is a fungible asset matching the given location or name:
sygma_bridge::SimpleForeignAssetConverter,
SimpleForeignAssetConverter,
// Convert an XCM MultiLocation into a local account id:
LocationToAccountId,
// Our chain's account ID type (we can't get away without mentioning it explicitly):
Expand All @@ -239,11 +282,11 @@ pub type FungiblesTransactor = FungiblesAdapter<
>;

parameter_types! {
pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));
pub SelfLocation: MultiLocation = MultiLocation::new(1, X1(Parachain(ParachainInfo::parachain_id().into())));
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let t = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
Expand All @@ -253,7 +296,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
// include the most recent event, but do not have to include every past event.
pub fn assert_events(mut expected: Vec<RuntimeEvent>) {
let mut actual: Vec<RuntimeEvent> =
system::Pallet::<Test>::events().iter().map(|e| e.event.clone()).collect();
system::Pallet::<Runtime>::events().iter().map(|e| e.event.clone()).collect();

expected.reverse();

Expand All @@ -268,6 +311,9 @@ pub const BOB: AccountId32 = AccountId32::new([1u8; 32]);
pub const ENDOWED_BALANCE: u128 = 100_000_000;
pub const TEST_THRESHOLD: u32 = 2;

pub type ParaBalances = pallet_balances::Pallet<para::Runtime>;
pub type ParaAssets = pallet_assets::Pallet<para::Runtime>;

decl_test_parachain! {
pub struct ParaA {
Runtime = para::Runtime,
Expand Down Expand Up @@ -359,4 +405,17 @@ pub fn relay_ext() -> sp_io::TestExternalities {
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}

pub fn slice_to_generalkey(key: &[u8]) -> Junction {
let len = key.len();
assert!(len <= 32);
GeneralKey {
length: len as u8,
data: {
let mut data = [0u8; 32];
data[..len].copy_from_slice(key);
data
},
}
}
1 change: 0 additions & 1 deletion xcm-bridge/src/mock/para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ construct_runtime!(
Balances: pallet_balances::{Pallet, Call, Config<T>, Storage, Event<T>},
Assets: pallet_assets::{Pallet, Call, Storage, Event<T>},

ParachainInfo: pallet_parachain_info::{Pallet, Storage, Config<T>},
ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Storage, Config<T>},

XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event<T>},
Expand Down

0 comments on commit c310a89

Please sign in to comment.