Skip to content

Commit

Permalink
remove sibling token
Browse files Browse the repository at this point in the history
  • Loading branch information
zqhxuyuan committed Jan 16, 2022
1 parent 8bd22c5 commit dd3c441
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 105 deletions.
3 changes: 2 additions & 1 deletion modules/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub mod module {
pub symbol: Vec<u8>,
pub decimals: u8,
pub minimal_balance: Balance,
// pub units_per_second: Option<Balance>,
}

#[pallet::error]
Expand Down Expand Up @@ -324,7 +325,7 @@ impl<T: Config> Pallet<T> {
})
}

fn do_register_foreign_asset(
pub fn do_register_foreign_asset(
location: &MultiLocation,
metadata: &AssetMetadata<BalanceOf<T>>,
) -> Result<ForeignAssetId, DispatchError> {
Expand Down
4 changes: 2 additions & 2 deletions node/service/src/chain_spec/karura.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use karura_runtime::{
DexConfig, FinancialCouncilMembershipConfig, GeneralCouncilMembershipConfig, HomaCouncilMembershipConfig,
OperatorMembershipAcalaConfig, OrmlNFTConfig, ParachainInfoConfig, PolkadotXcmConfig, SS58Prefix, SessionConfig,
SessionDuration, SessionKeys, SessionManagerConfig, SudoConfig, SystemConfig, TechnicalCommitteeMembershipConfig,
TokensConfig, VestingConfig, BNC, KAR, KSM, KUSD, LKSM, PHA, VSKSM,
TokensConfig, VestingConfig, KAR, KSM, KUSD, LKSM,
};
use runtime_common::TokenInfo;

Expand All @@ -46,7 +46,7 @@ fn karura_properties() -> Properties {
let mut properties = Map::new();
let mut token_symbol: Vec<String> = vec![];
let mut token_decimals: Vec<u32> = vec![];
[KAR, KUSD, KSM, LKSM, BNC, VSKSM, PHA].iter().for_each(|token| {
[KAR, KUSD, KSM, LKSM].iter().for_each(|token| {
token_symbol.push(token.symbol().unwrap().to_string());
token_decimals.push(token.decimals().unwrap() as u32);
});
Expand Down
10 changes: 5 additions & 5 deletions primitives/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ create_currency_id! {
// 149: Reserved for renBTC
// 150: Reserved for CASH
// 168 - 255: Kusama parachain tokens
BNC("Bifrost Native Token", 12) = 168,
VSKSM("Bifrost Voucher Slot KSM", 12) = 169,
PHA("Phala Native Token", 12) = 170,
KINT("Kintsugi Native Token", 12) = 171,
KBTC("Kintsugi Wrapped BTC", 8) = 172,
// BNC("Bifrost Native Token", 12) = 168,
// VSKSM("Bifrost Voucher Slot KSM", 12) = 169,
// PHA("Phala Native Token", 12) = 170,
// KINT("Kintsugi Native Token", 12) = 171,
// KBTC("Kintsugi Wrapped BTC", 8) = 172,
}
}

Expand Down
10 changes: 5 additions & 5 deletions runtime/acala/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,12 +749,12 @@ parameter_type_with_key! {
TokenSymbol::KSM |
TokenSymbol::LKSM |
TokenSymbol::RENBTC |
TokenSymbol::BNC |
TokenSymbol::PHA |
TokenSymbol::VSKSM |
// TokenSymbol::BNC |
// TokenSymbol::PHA |
// TokenSymbol::VSKSM |
TokenSymbol::ACA |
TokenSymbol::KBTC |
TokenSymbol::KINT |
// TokenSymbol::KBTC |
// TokenSymbol::KINT |
TokenSymbol::TAI |
TokenSymbol::CASH => Balance::max_value() // unsupported
},
Expand Down
2 changes: 1 addition & 1 deletion runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub use precompile::{
StateRentPrecompile,
};
pub use primitives::{
currency::{TokenInfo, ACA, AUSD, BNC, DOT, KAR, KBTC, KINT, KSM, KUSD, LDOT, LKSM, PHA, RENBTC, VSKSM},
currency::{TokenInfo, ACA, AUSD, DOT, KAR, KSM, KUSD, LDOT, LKSM, RENBTC},
AccountId,
};
use sp_std::{marker::PhantomData, prelude::*};
Expand Down
2 changes: 1 addition & 1 deletion runtime/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ xcm-emulator = { git = "https://github.com/shaunxw/xcm-simulator", rev = "4d3bb9
acala-service = { path = "../../node/service", features = ["with-all-runtime"] }

[features]
default = ["std"]
default = ["std", "with-karura-runtime"]
no_std = []
with-mandala-runtime = [
"acala-service/with-mandala-runtime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::setup::*;

use frame_support::assert_ok;

use karura_runtime::parachains::bifrost::BNC_KEY;
use karura_runtime::{AssetRegistry, KaruraTreasuryAccount};
use module_asset_registry::AssetMetadata;
use module_relaychain::RelayChainCallBuilder;
Expand Down Expand Up @@ -92,18 +93,53 @@ fn transfer_to_sibling() {
polkadot_parachain::primitives::Sibling::from(2000).into_account()
}

Karura::execute_with(|| {
assert_ok!(Tokens::deposit(BNC, &AccountId::from(ALICE), 100_000_000_000_000));
});
let sibling_location = MultiLocation::new(1, X2(Parachain(2001), GeneralKey(BNC_KEY.to_vec())));

Sibling::execute_with(|| {
assert_ok!(Tokens::deposit(BNC, &karura_reserve_account(), 100_000_000_000_000));
// register asset
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(sibling_location.clone().into()),
Box::new(AssetMetadata {
name: b"Native Token".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(|| {
// register foreign asset
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(sibling_location.clone().into()),
Box::new(AssetMetadata {
name: b"Sibling Token".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_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &TreasuryAccount::get()),
0
);

assert_ok!(XTokens::transfer(
Origin::signed(ALICE.into()),
BNC,
CurrencyId::ForeignAsset(0),
10_000_000_000_000,
Box::new(
MultiLocation::new(
Expand All @@ -121,16 +157,25 @@ fn transfer_to_sibling() {
1_000_000_000,
));

assert_eq!(Tokens::free_balance(BNC, &AccountId::from(ALICE)), 90_000_000_000_000);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(ALICE)),
90_000_000_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(CurrencyId::ForeignAsset(0), &karura_reserve_account()),
90_000_000_000_000
);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(BOB)),
9_989_760_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 @@ -148,12 +193,21 @@ fn transfer_to_sibling() {
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(CurrencyId::ForeignAsset(0), &karura_reserve_account()),
95_000_000_000_000
);
assert_eq!(
Tokens::free_balance(CurrencyId::ForeignAsset(0), &AccountId::from(BOB)),
4_989_760_000_000
);
});

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

Expand Down Expand Up @@ -736,15 +790,28 @@ fn sibling_trap_assets_works() {
TestNet::reset();

let mut kar_treasury_amount = 0;
let (bnc_asset_amount, kar_asset_amount) = (cent(BNC) / 10, cent(KAR));
let (bnc_asset_amount, kar_asset_amount) = (1_000_000_000, cent(KAR));

fn sibling_account() -> AccountId {
use sp_runtime::traits::AccountIdConversion;
polkadot_parachain::primitives::Sibling::from(2001).into_account()
}

let sibling_location = MultiLocation::new(1, X2(Parachain(2001), GeneralKey(BNC_KEY.to_vec())));

Karura::execute_with(|| {
assert_ok!(Tokens::deposit(BNC, &sibling_account(), dollar(BNC)));
assert_ok!(AssetRegistry::register_foreign_asset(
Origin::root(),
Box::new(sibling_location.clone().into()),
Box::new(AssetMetadata {
name: b"Sibling Token".to_vec(),
symbol: b"BNC".to_vec(),
decimals: 12,
minimal_balance: Balances::minimum_balance() / 10, // 10%
})
));

assert_ok!(Tokens::deposit(BNC, &sibling_account(), 1_000_000_000_000));
let _ = pallet_balances::Pallet::<Runtime>::deposit_creating(&sibling_account(), dollar(KAR));
kar_treasury_amount = Currencies::free_balance(KAR, &KaruraTreasuryAccount::get());
});
Expand Down
11 changes: 11 additions & 0 deletions runtime/integration-tests/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ fn currency_id_convert() {
CurrencyIdConvert::convert(MultiLocation::sibling_parachain_general_key(id, LDOT.encode())),
None
);

initiate_asset_registry();

assert_eq!(
CurrencyIdConvert::convert(MultiLocation::sibling_parachain_general_key(
parachains::bifrost::ID,
Expand All @@ -147,6 +150,10 @@ fn currency_id_convert() {
)),
Some(VSKSM)
);
assert_eq!(
CurrencyIdConvert::convert(MultiLocation::new(1, X1(Parachain(parachains::phala::ID)))),
Some(PHA)
);

assert_eq!(
CurrencyIdConvert::convert(BNC),
Expand All @@ -162,6 +169,10 @@ fn currency_id_convert() {
parachains::bifrost::VSKSM_KEY.to_vec()
))
);
assert_eq!(
CurrencyIdConvert::convert(PHA),
Some(MultiLocation::new(1, X1(Parachain(parachains::phala::ID))))
);

let native_currency: MultiAsset = (
MultiLocation::sibling_parachain_general_key(id, NATIVE_CURRENCY.encode()),
Expand Down
54 changes: 53 additions & 1 deletion runtime/integration-tests/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod mandala_imports {
pub use karura_imports::*;
#[cfg(feature = "with-karura-runtime")]
mod karura_imports {
pub use frame_support::{parameter_types, weights::Weight};
pub use frame_support::{assert_ok, parameter_types, weights::Weight};
pub use karura_runtime::{
constants::parachains, create_x2_parachain_multilocation, get_all_module_accounts, AcalaOracle, AccountId,
AssetRegistry, AuctionManager, Authority, AuthoritysOriginId, Balance, Balances, BlockNumber, Call, CdpEngine,
Expand All @@ -86,9 +86,11 @@ mod karura_imports {
TipPerWeightStep, TokenSymbol, Tokens, TreasuryPalletId, Utility, Vesting, XTokens, XcmConfig, XcmExecutor,
EVM, NFT,
};
pub use module_asset_registry::AssetMetadata;
pub use primitives::TradingPair;
pub use runtime_common::{calculate_asset_ratio, cent, dollar, millicent, KAR, KSM, KUSD, LKSM};
pub use sp_runtime::{traits::AccountIdConversion, FixedPointNumber};
pub use xcm::latest::prelude::*;

parameter_types! {
pub EnabledTradingPairs: Vec<TradingPair> = vec![
Expand Down Expand Up @@ -123,6 +125,56 @@ mod karura_imports {
(LKSM, NativeTokenExistentialDeposit::get() - 1),
];
}

pub fn initiate_asset_registry() {
// BNC: ForeignAsset(0)
assert_ok!(module_asset_registry::Pallet::<Runtime>::do_register_foreign_asset(
&(
1,
X2(
Parachain(parachains::bifrost::ID),
GeneralKey(parachains::bifrost::BNC_KEY.to_vec()),
)
)
.into(),
&AssetMetadata {
name: "BNC".as_bytes().to_vec(),
symbol: "BNC".as_bytes().to_vec(),
decimals: 12,
minimal_balance: 800 * (10u128.saturating_pow(12))
}
));
// VSKSM: ForeignAsset(1)
assert_ok!(module_asset_registry::Pallet::<Runtime>::do_register_foreign_asset(
&(
1,
X2(
Parachain(parachains::bifrost::ID),
GeneralKey(parachains::bifrost::VSKSM_KEY.to_vec()),
)
)
.into(),
&AssetMetadata {
name: "VSKSM".as_bytes().to_vec(),
symbol: "VSKSM".as_bytes().to_vec(),
decimals: 12,
minimal_balance: 10 * (10u128.saturating_pow(12))
}
));
// PHA: ForeignAsset(2)
assert_ok!(module_asset_registry::Pallet::<Runtime>::do_register_foreign_asset(
&(1, X1(Parachain(parachains::phala::ID))).into(),
&AssetMetadata {
name: "PHA".as_bytes().to_vec(),
symbol: "PHA".as_bytes().to_vec(),
decimals: 12,
minimal_balance: 4000 * (10u128.saturating_pow(12))
}
));
}
pub const BNC: CurrencyId = CurrencyId::ForeignAsset(0);
pub const VSKSM: CurrencyId = CurrencyId::ForeignAsset(1);
pub const PHA: CurrencyId = CurrencyId::ForeignAsset(2);
}

#[cfg(feature = "with-acala-runtime")]
Expand Down
Loading

0 comments on commit dd3c441

Please sign in to comment.