Skip to content

Commit f38ef9d

Browse files
authored
Add bind existing sidechin asset function (#84)
* add bind function, tests and benchmarks
1 parent 2269286 commit f38ef9d

File tree

5 files changed

+256
-94
lines changed

5 files changed

+256
-94
lines changed

pallets/parachain-app/src/benchmarking.rs

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ benchmarks! {
8383
assert_eq!(SidechainPrecision::<T>::iter_prefix(BASE_NETWORK_ID).count(), 1);
8484
}
8585

86+
bind_sidechain_asset {
87+
let a in 1..100;
88+
let asset_id = <T as Config>::AssetRegistry::register_asset(GenericNetworkId::Sub(Default::default()), Default::default(), Default::default())?;
89+
}: _(RawOrigin::Root, BASE_NETWORK_ID, asset_id.clone(), [0u8; 32].into(), 6, (0..a).collect::<Vec<_>>(), 1u32.into())
90+
verify {
91+
assert_eq!(SidechainPrecision::<T>::get(BASE_NETWORK_ID, asset_id).unwrap(), 6);
92+
}
93+
8694
add_assetid_paraid {
8795
let asset_id = <T as Config>::AssetRegistry::register_asset(GenericNetworkId::Sub(Default::default()), Default::default(), Default::default())?;
8896
ParachainApp::<T>::register_thischain_asset(RawOrigin::Root.into(), BASE_NETWORK_ID, asset_id.clone(), PARENT_PARACHAIN_ASSET, Default::default(), 1u32.into())?;

pallets/parachain-app/src/lib.rs

+39
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,45 @@ pub mod pallet {
522522
)?;
523523
Ok(())
524524
}
525+
526+
#[pallet::call_index(9)]
527+
#[pallet::weight(<T as Config>::WeightInfo::bind_sidechain_asset(allowed_parachains.len() as u32))]
528+
pub fn bind_sidechain_asset(
529+
origin: OriginFor<T>,
530+
network_id: SubNetworkId,
531+
asset_id: AssetIdOf<T>,
532+
sidechain_asset: ParachainAssetId,
533+
sidechain_precision: u8,
534+
allowed_parachains: Vec<u32>,
535+
minimal_xcm_amount: BalanceOf<T>,
536+
) -> DispatchResult {
537+
ensure_root(origin)?;
538+
ensure!(
539+
!AssetKinds::<T>::contains_key(network_id, &asset_id),
540+
Error::<T>::TokenAlreadyRegistered
541+
);
542+
543+
let (_, minimal_xcm_amount) = T::BalancePrecisionConverter::to_sidechain(
544+
&asset_id,
545+
sidechain_precision,
546+
minimal_xcm_amount,
547+
)
548+
.ok_or(Error::<T>::WrongAmount)?;
549+
550+
ensure!(minimal_xcm_amount > 0, Error::<T>::WrongAmount);
551+
552+
Self::register_asset_inner(
553+
network_id,
554+
asset_id,
555+
sidechain_asset,
556+
AssetKind::Sidechain,
557+
sidechain_precision,
558+
allowed_parachains,
559+
minimal_xcm_amount,
560+
)?;
561+
562+
Ok(())
563+
}
525564
}
526565

527566
impl<T: Config> Pallet<T> {

pallets/parachain-app/src/mock.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use xcm::v3::Junction::Parachain;
6262
use xcm::v3::Junctions::X2;
6363
use xcm::v3::MultiLocation;
6464

65-
use crate as substrate_app;
65+
use crate as parachain_app;
6666

6767
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
6868
type Block = frame_system::mocking::MockBlock<Test>;
@@ -86,6 +86,7 @@ pub enum AssetId {
8686
Xor,
8787
Eth,
8888
Dai,
89+
Usdt,
8990
Custom(u8),
9091
}
9192

@@ -105,7 +106,7 @@ frame_support::construct_runtime!(
105106
Balances: pallet_balances::{Pallet, Call, Storage, Event<T>},
106107
Dispatch: dispatch::{Pallet, Call, Storage, Origin<T>, Event<T>},
107108
BridgeOutboundChannel: substrate_bridge_channel::outbound::{Pallet, Config<T>, Storage, Event<T>},
108-
ParachainApp: substrate_app::{Pallet, Call, Config<T>, Storage, Event<T>},
109+
ParachainApp: parachain_app::{Pallet, Call, Config<T>, Storage, Event<T>},
109110
}
110111
);
111112

@@ -280,6 +281,11 @@ impl BridgeAssetRegistry<AccountId, AssetId> for AssetRegistryImpl {
280281
symbol: "XOR".to_owned().into(),
281282
precision: 18,
282283
},
284+
AssetId::Usdt => bridge_types::types::RawAssetInfo {
285+
name: "USDT".to_owned().into(),
286+
symbol: "USDT".to_owned().into(),
287+
precision: 6,
288+
},
283289
AssetId::Custom(1) => bridge_types::types::RawAssetInfo {
284290
name: "KSM".to_owned().into(),
285291
symbol: "KSM".to_owned().into(),
@@ -326,7 +332,7 @@ impl BalancePrecisionConverter<AssetId, Balance, Balance> for BalancePrecisionCo
326332
}
327333
}
328334

329-
impl substrate_app::Config for Test {
335+
impl parachain_app::Config for Test {
330336
type RuntimeEvent = RuntimeEvent;
331337
type MessageStatusNotifier = ();
332338
type CallOrigin =
@@ -413,7 +419,7 @@ pub fn new_tester() -> sp_io::TestExternalities {
413419
AssetKind::Thischain,
414420
)
415421
.expect("XOR registration finalization failed");
416-
let kusama_asset = substrate_app::RelaychainAsset::<Test>::get(SubNetworkId::Kusama);
422+
let kusama_asset = parachain_app::RelaychainAsset::<Test>::get(SubNetworkId::Kusama);
417423
ParachainApp::finalize_asset_registration(
418424
origin_kusama,
419425
kusama_asset.unwrap(),

pallets/parachain-app/src/tests.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ fn it_fails_burn_outbound_channel_submit() {
559559

560560
#[test]
561561
fn it_works_register_thischain_asset() {
562-
new_tester_no_registered_assets().execute_with(|| {
562+
new_tester().execute_with(|| {
563563
let origin = Origin::<Test>::Root;
564564
let network_id = SubNetworkId::Mainnet;
565565
let asset_id = AssetId::Xor;
@@ -587,6 +587,37 @@ fn it_works_register_thischain_asset() {
587587
});
588588
}
589589

590+
#[test]
591+
fn it_works_bind_sidechain_asset() {
592+
new_tester_no_registered_assets().execute_with(|| {
593+
let origin = Origin::<Test>::Root;
594+
let network_id = SubNetworkId::Mainnet;
595+
let asset_id = AssetId::Usdt;
596+
let sidechain_asset = ParachainAssetId::Concrete(MultiLocation::new(
597+
1,
598+
X2(
599+
Parachain(PARA_A),
600+
Junction::AccountId32 {
601+
network: None,
602+
id: Keyring::Bob.into(),
603+
},
604+
),
605+
));
606+
let allowed_parachains = vec![1000];
607+
let minimal_xcm_amount = 10;
608+
609+
assert_ok!(ParachainApp::bind_sidechain_asset(
610+
origin.into(),
611+
network_id,
612+
asset_id,
613+
sidechain_asset,
614+
6,
615+
allowed_parachains,
616+
minimal_xcm_amount
617+
));
618+
});
619+
}
620+
590621
#[test]
591622
fn it_works_register_asset_inner() {
592623
new_tester_no_registered_assets().execute_with(|| {

0 commit comments

Comments
 (0)