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

Fix integration tests for sibling XCM transfer for karura-runtime #2138

Merged
merged 15 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 0 additions & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ with-mandala-runtime = [
]
with-karura-runtime = [
"karura-runtime",
"karura-runtime/integration-tests",
"acala-service/with-karura-runtime",
"module-relaychain/kusama"
]
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/src/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn test_trading_pair() {

assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(MultiLocation::new(1, X2(Parachain(2001), GeneralKey(KAR.encode()))).into()),
Box::new(MultiLocation::new(1, X2(Parachain(2002), GeneralKey(KAR.encode()))).into()),
Box::new(AssetMetadata {
name: b"Sibling Token".to_vec(),
symbol: b"ST".to_vec(),
Expand Down
228 changes: 136 additions & 92 deletions runtime/integration-tests/src/relaychain/kusama_cross_chain_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use frame_support::assert_ok;
use sp_runtime::traits::AccountIdConversion;
use xcm_builder::ParentIsPreset;

use karura_runtime::parachains::bifrost::BNC_KEY;
use karura_runtime::parachains::bifrost::{BNC_KEY, ID as BIFROST_ID};
use karura_runtime::{AssetRegistry, KaruraTreasuryAccount};
use module_relaychain::RelayChainCallBuilder;
use module_support::CallBuilder;
use orml_traits::MultiCurrency;
use primitives::currency::AssetMetadata;
use primitives::currency::{AssetMetadata, BNC};
use xcm_emulator::TestExt;
use xcm_executor::traits::Convert;

Expand Down Expand Up @@ -97,13 +97,31 @@ fn transfer_sibling_chain_asset() {
fn karura_reserve_account() -> AccountId {
polkadot_parachain::primitives::Sibling::from(2000).into_account()
}
fn sibling_reserve_account() -> AccountId {
polkadot_parachain::primitives::Sibling::from(2002).into_account()
syan095 marked this conversation as resolved.
Show resolved Hide resolved
}

Karura::execute_with(|| {
assert_ok!(Tokens::deposit(BNC, &AccountId::from(ALICE), 100_000_000_000_000));
});

Sibling::execute_with(|| {
assert_ok!(Tokens::deposit(BNC, &karura_reserve_account(), 100_000_000_000_000));
MockBifrost::execute_with(|| {
// Register native BNC's incoming address as a foreign asset so it can handle reserve transfers
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(MultiLocation::new(0, X1(GeneralKey(BNC_KEY.to_vec()))).into()),
Box::new(AssetMetadata {
name: b"Native BNC".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: Balances::minimum_balance() / 10, // 10%
})
));
assert_ok!(Tokens::deposit(
CurrencyId::ForeignAsset(0),
&karura_reserve_account(),
100_000_000_000_000
));
});

Karura::execute_with(|| {
Expand All @@ -115,7 +133,7 @@ fn transfer_sibling_chain_asset() {
MultiLocation::new(
1,
X2(
Parachain(2001),
Parachain(2002),
Junction::AccountId32 {
network: NetworkId::Any,
id: BOB.into(),
Expand All @@ -130,9 +148,25 @@ fn transfer_sibling_chain_asset() {
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(ALICE)), 90_000_000_000_000);
});

MockBifrost::execute_with(|| {
// Due to reanchoring BNC is not treated as native BNC due to the change of Multilocation
assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 0);
assert_eq!(Tokens::free_balance(BNC, &sibling_reserve_account()), 0);

// Registered Foreign asset 0 is used to handle reservation for BNC token.
// Karura -->(transfer 10_000_000_000_000)--> Sibling
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &karura_reserve_account()),
90_000_000_000_000
);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &sibling_reserve_account()),
9_999_360_000_000
);
});

Sibling::execute_with(|| {
assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 90_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 9_989_760_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 9_989_120_000_000);

assert_ok!(XTokens::transfer(
Origin::signed(BOB.into()),
Expand All @@ -154,12 +188,23 @@ fn transfer_sibling_chain_asset() {
1_000_000_000,
));

assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 95_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 4_989_760_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 4_989_120_000_000);
});

MockBifrost::execute_with(|| {
// Sibling -->(transfer 5_000_000_000_000)--> Karura
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &karura_reserve_account()),
94_999_360_000_000
);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &sibling_reserve_account()),
4_999_360_000_000
);
});

Karura::execute_with(|| {
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(ALICE)), 94_989_760_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(ALICE)), 94_989_120_000_000);
});
}

Expand Down Expand Up @@ -338,7 +383,7 @@ fn subscribe_version_notify_works() {
Karura::execute_with(|| {
let r = pallet_xcm::Pallet::<karura_runtime::Runtime>::force_subscribe_version_notify(
Origin::root(),
Box::new((Parent, Parachain(2001)).into()),
Box::new((Parent, Parachain(2002)).into()),
);
assert_ok!(r);
});
Expand All @@ -364,75 +409,75 @@ fn test_asset_registry_module() {
fn karura_reserve_account() -> AccountId {
polkadot_parachain::primitives::Sibling::from(2000).into_account()
}
fn sibling_reserve_account() -> AccountId {
polkadot_parachain::primitives::Sibling::from(2002).into_account()
}
syan095 marked this conversation as resolved.
Show resolved Hide resolved

Karura::execute_with(|| {
// register foreign asset
assert_ok!(Tokens::deposit(BNC, &AccountId::from(ALICE), 100_000_000_000_000));
});

MockBifrost::execute_with(|| {
// Register native BNC's incoming address as a foreign asset so it can handle reserve transfers
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(MultiLocation::new(1, X2(Parachain(2001), GeneralKey(BNC_KEY.to_vec()))).into()),
Box::new(MultiLocation::new(0, X1(GeneralKey(BNC_KEY.to_vec()))).into()),
Box::new(AssetMetadata {
name: b"Sibling Token".to_vec(),
symbol: b"ST".to_vec(),
name: b"Native BNC".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: Balances::minimum_balance() / 10, // 10%
})
));

assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &TreasuryAccount::get()),
0
);
assert_ok!(Tokens::deposit(
CurrencyId::ForeignAsset(0),
&karura_reserve_account(),
100_000_000_000_000
));
});

Sibling::execute_with(|| {
let _ = Tokens::deposit(BNC, &AccountId::from(BOB), 100_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 0);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 100_000_000_000_000);

assert_ok!(XTokens::transfer(
Origin::signed(BOB.into()),
BNC,
5_000_000_000_000,
Box::new(
MultiLocation::new(
1,
X2(
Parachain(2000),
Junction::AccountId32 {
network: NetworkId::Any,
id: ALICE.into(),
}
)
)
.into()
),
1_000_000_000,
// Register BNC as foreign asset(0)
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(MultiLocation::new(1, X2(Parachain(BIFROST_ID), GeneralKey(BNC_KEY.to_vec()))).into()),
Box::new(AssetMetadata {
name: b"Bifrost BNC".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: Balances::minimum_balance() / 10, // 10%
})
));

assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 5_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 95_000_000_000_000);
});

Karura::execute_with(|| {
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(ALICE)),
4_989_760_000_000
);
// ToTreasury
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &TreasuryAccount::get()),
10_240_000_000
);
// Register BNC as foreign asset(0)
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(MultiLocation::new(1, X2(Parachain(BIFROST_ID), GeneralKey(BNC_KEY.to_vec()))).into()),
Box::new(AssetMetadata {
name: b"Bifrost BNC".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: Balances::minimum_balance() / 10, // 10%
})
));

assert_ok!(Tokens::deposit(
CurrencyId::ForeignAsset(0),
&AccountId::from(ALICE),
100_000_000_000_000
));

assert_ok!(XTokens::transfer(
Origin::signed(ALICE.into()),
CurrencyId::ForeignAsset(0),
1_000_000_000_000,
10_000_000_000_000,
Box::new(
MultiLocation::new(
1,
X2(
Parachain(2001),
Parachain(2002),
Junction::AccountId32 {
network: NetworkId::Any,
id: BOB.into(),
Expand All @@ -446,38 +491,32 @@ fn test_asset_registry_module() {

assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(ALICE)),
3_989_760_000_000
90_000_000_000_000
);
});

Sibling::execute_with(|| {
assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 4_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 95_989_760_000_000);
});

// remove it
Karura::execute_with(|| {
// register foreign asset
assert_ok!(AssetRegistry::update_foreign_asset(
Origin::root(),
0,
Box::new(MultiLocation::new(1, X2(Parachain(2001), GeneralKey(BNC_KEY.to_vec()))).into()),
Box::new(AssetMetadata {
name: b"Sibling Token".to_vec(),
symbol: b"ST".to_vec(),
decimals: 12,
minimal_balance: 0, // buy_weight 0
})
));
MockBifrost::execute_with(|| {
// Registered Foreign asset 0 is used to handle reservation for BNC token.
// Karura -->(transfer 10_000_000_000_000)--> Sibling
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &karura_reserve_account()),
90_000_000_000_000
);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &sibling_reserve_account()),
9_999_360_000_000
);
});

Sibling::execute_with(|| {
assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 4_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 95_989_760_000_000);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(BOB)),
9_989_120_000_000
);

assert_ok!(XTokens::transfer(
Origin::signed(BOB.into()),
BNC,
CurrencyId::ForeignAsset(0),
5_000_000_000_000,
Box::new(
MultiLocation::new(
Expand All @@ -495,20 +534,28 @@ fn test_asset_registry_module() {
1_000_000_000,
));

assert_eq!(Tokens::free_balance(BNC, &karura_reserve_account()), 9_000_000_000_000);
assert_eq!(Tokens::free_balance(BNC, &AccountId::from(BOB)), 90_989_760_000_000);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(BOB)),
4_989_120_000_000
);
});

Karura::execute_with(|| {
MockBifrost::execute_with(|| {
// Sibling -->(transfer 5_000_000_000_000)--> Karura
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(ALICE)),
8_979_520_000_000
Tokens::free_balance(CurrencyId::ForeignAsset(0), &karura_reserve_account()),
94_999_360_000_000
);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &sibling_reserve_account()),
4_999_360_000_000
);
});

// ToTreasury
Karura::execute_with(|| {
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &TreasuryAccount::get()),
20_480_000_000
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(ALICE)),
94_989_120_000_000
);
});
}
Expand Down Expand Up @@ -738,7 +785,7 @@ fn sibling_trap_assets_works() {
let (bnc_asset_amount, kar_asset_amount) = (cent(BNC) / 10, cent(KAR));

fn sibling_account() -> AccountId {
polkadot_parachain::primitives::Sibling::from(2001).into_account()
polkadot_parachain::primitives::Sibling::from(2002).into_account()
}

Karura::execute_with(|| {
Expand All @@ -757,10 +804,7 @@ fn sibling_trap_assets_works() {
},
WithdrawAsset(
(
(
Parent,
X2(Parachain(2001), GeneralKey(parachains::bifrost::BNC_KEY.to_vec())),
),
(Parent, X2(Parachain(BIFROST_ID), GeneralKey(BNC_KEY.to_vec()))),
bnc_asset_amount,
)
.into(),
Expand Down
Loading