Skip to content

Commit

Permalink
Conditional paraid (#725)
Browse files Browse the repository at this point in the history
* fix moonbeam bifrost-parachain account

* move slp migration to runtime

* fix paraId in bifrost-polkadot runtime

* add conditional compile

* impl CurrencyId for TryConvertFrom trait

* fix flexible fee tests

* fix integration tests
  • Loading branch information
herryho authored Aug 31, 2022
1 parent e4b6392 commit 683a808
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 87 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

17 changes: 12 additions & 5 deletions integration-tests/src/flexible_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use frame_support::{
weights::{GetDispatchInfo, Pays, PostDispatchInfo},
};
use frame_system::pallet_prelude::BlockNumberFor;
use node_primitives::TryConvertFrom;
use pallet_transaction_payment::OnChargeTransaction;
use sp_runtime::testing::TestXt;
use xcm_emulator::TestExt;
Expand Down Expand Up @@ -172,11 +173,17 @@ fn basic_setup() {
));

// create DEX pair
let asset_0_currency_id: AssetId = AssetId::try_from(CURRENCY_ID_0).unwrap();
let asset_1_currency_id: AssetId = AssetId::try_from(CURRENCY_ID_1).unwrap();
let asset_2_currency_id: AssetId = AssetId::try_from(CURRENCY_ID_2).unwrap();
let asset_3_currency_id: AssetId = AssetId::try_from(CURRENCY_ID_3).unwrap();
let asset_4_currency_id: AssetId = AssetId::try_from(CURRENCY_ID_4).unwrap();
let parachain_id: u32 = 2001;
let asset_0_currency_id: AssetId =
AssetId::try_convert_from(CURRENCY_ID_0, parachain_id).unwrap();
let asset_1_currency_id: AssetId =
AssetId::try_convert_from(CURRENCY_ID_1, parachain_id).unwrap();
let asset_2_currency_id: AssetId =
AssetId::try_convert_from(CURRENCY_ID_2, parachain_id).unwrap();
let asset_3_currency_id: AssetId =
AssetId::try_convert_from(CURRENCY_ID_3, parachain_id).unwrap();
let asset_4_currency_id: AssetId =
AssetId::try_convert_from(CURRENCY_ID_4, parachain_id).unwrap();

assert_ok!(ZenlinkProtocol::create_pair(
Origin::root(),
Expand Down
65 changes: 43 additions & 22 deletions integration-tests/src/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ fn register_subaccount_index_0() {
Delays { unlock_delay: TimeUnit::Era(10), leave_delegators_delay: Default::default() };
assert_ok!(Slp::set_currency_delays(Origin::root(), RelayCurrencyId::get(), Some(delay)));

let mins_and_maxs = MinimumsMaximums {
delegator_bonded_minimum: 100_000_000_000,
bond_extra_minimum: 0,
unbond_minimum: 0,
rebond_minimum: 0,
unbond_record_maximum: 32,
validators_back_maximum: 36,
delegator_active_staking_maximum: 200_000_000_000_000,
validators_reward_maximum: 0,
delegation_amount_minimum: 0,
delegators_maximum: 100,
validators_maximum: 300,
};

// Set minimums and maximums
assert_ok!(Slp::set_minimums_and_maximums(
Origin::root(),
RelayCurrencyId::get(),
Some(mins_and_maxs)
));

// First to setup index-multilocation relationship of subaccount_0
assert_ok!(Slp::add_delegator(
Origin::root(),
Expand Down Expand Up @@ -172,27 +193,6 @@ fn register_subaccount_index_0() {
XcmOperation::TransferBack,
Some((20_000_000_000, 10_000_000_000)),
));

let mins_and_maxs = MinimumsMaximums {
delegator_bonded_minimum: 100_000_000_000,
bond_extra_minimum: 0,
unbond_minimum: 0,
rebond_minimum: 0,
unbond_record_maximum: 32,
validators_back_maximum: 36,
delegator_active_staking_maximum: 200_000_000_000_000,
validators_reward_maximum: 0,
delegation_amount_minimum: 0,
delegators_maximum: 100,
validators_maximum: 300,
};

// Set minimums and maximums
assert_ok!(Slp::set_minimums_and_maximums(
Origin::root(),
RelayCurrencyId::get(),
Some(mins_and_maxs)
));
});
}

Expand Down Expand Up @@ -242,6 +242,27 @@ fn register_validators() {
let multi_hash_0 =
<Runtime as frame_system::Config>::Hashing::hash(&validator_0_location.encode());

let mins_and_maxs = MinimumsMaximums {
delegator_bonded_minimum: 100_000_000_000,
bond_extra_minimum: 0,
unbond_minimum: 0,
rebond_minimum: 0,
unbond_record_maximum: 32,
validators_back_maximum: 36,
delegator_active_staking_maximum: 200_000_000_000_000,
validators_reward_maximum: 0,
delegation_amount_minimum: 0,
delegators_maximum: 100,
validators_maximum: 300,
};

// Set minimums and maximums
assert_ok!(Slp::set_minimums_and_maximums(
Origin::root(),
RelayCurrencyId::get(),
Some(mins_and_maxs)
));

// Set delegator ledger
assert_ok!(Slp::add_validator(
Origin::root(),
Expand Down Expand Up @@ -641,9 +662,9 @@ fn rebond_works() {
#[test]
fn delegate_works() {
// bond 1 ksm for sub-account index 0
register_validators();
locally_bond_subaccount_0_1ksm_in_kusama();
register_subaccount_index_0();
register_validators();
register_delegator_ledger();
let subaccount_0 = subaccount_0();

Expand Down
18 changes: 10 additions & 8 deletions integration-tests/src/treasury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use frame_support::assert_ok;
use xcm_emulator::TestExt;
// use node_primitives::*;
// use zenlink_protocol::LIQUIDITY;
use node_primitives::TryConvertFrom;
use sp_runtime::AccountId32;
// use bifrost_polkadot_runtime::Permill;

Expand All @@ -40,6 +41,7 @@ fn kusama_treasury_propose_spend() {

#[test]
fn bifrost_treasury_operations() {
let para_id = 2001u32;
let treasury_account: AccountId32 =
bifrost_kusama_runtime::TreasuryPalletId::get().into_account_truncating();
Bifrost::execute_with(|| {
Expand Down Expand Up @@ -68,14 +70,14 @@ fn bifrost_treasury_operations() {

assert_ok!(bifrost_kusama_runtime::ZenlinkProtocol::create_pair(
bifrost_kusama_runtime::Origin::root(),
zenlink_protocol::AssetId::try_from(KSM).unwrap(),
zenlink_protocol::AssetId::try_from(VKSM).unwrap(),
zenlink_protocol::AssetId::try_convert_from(KSM, para_id).unwrap(),
zenlink_protocol::AssetId::try_convert_from(VKSM, para_id).unwrap(),
));

assert_ok!(bifrost_kusama_runtime::ZenlinkProtocol::add_liquidity(
bifrost_kusama_runtime::Origin::signed(treasury_account.clone()),
zenlink_protocol::AssetId::try_from(KSM).unwrap(),
zenlink_protocol::AssetId::try_from(VKSM).unwrap(),
zenlink_protocol::AssetId::try_convert_from(KSM, para_id).unwrap(),
zenlink_protocol::AssetId::try_convert_from(VKSM, para_id).unwrap(),
25_000_000_000_000_000,
25_000_000_000_000_000,
0,
Expand All @@ -84,8 +86,8 @@ fn bifrost_treasury_operations() {
));

let lp_asset_id = bifrost_kusama_runtime::ZenlinkProtocol::lp_asset_id(
&zenlink_protocol::AssetId::try_from(KSM).unwrap(),
&zenlink_protocol::AssetId::try_from(VKSM).unwrap(),
&zenlink_protocol::AssetId::try_convert_from(KSM, para_id).unwrap(),
&zenlink_protocol::AssetId::try_convert_from(VKSM, para_id).unwrap(),
);

let lp = bifrost_kusama_runtime::ZenlinkProtocol::foreign_balance_of(
Expand All @@ -95,8 +97,8 @@ fn bifrost_treasury_operations() {

assert_ok!(bifrost_kusama_runtime::ZenlinkProtocol::remove_liquidity(
bifrost_kusama_runtime::Origin::signed(treasury_account.clone()),
zenlink_protocol::AssetId::try_from(KSM).unwrap(),
zenlink_protocol::AssetId::try_from(VKSM).unwrap(),
zenlink_protocol::AssetId::try_convert_from(KSM, para_id).unwrap(),
zenlink_protocol::AssetId::try_convert_from(VKSM, para_id).unwrap(),
lp,
0,
0,
Expand Down
16 changes: 14 additions & 2 deletions node/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ edition = "2021"

[dependencies]
bstringify = "0.1.2"
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.1.2", default-features = false, features = [
"derive",
] }
serde = { version = "1.0.143", optional = true, features = ["derive"] }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false }
Expand All @@ -29,3 +33,11 @@ std = [
"sp-std/std",
"zenlink-protocol/std",
]

with-bifrost-runtime = [
"with-bifrost-kusama-runtime",
"with-bifrost-polkadot-runtime",
]
with-bifrost-kusama-runtime = []
with-bifrost-polkadot-runtime = []
with-all-runtime = ["with-bifrost-runtime"]
12 changes: 6 additions & 6 deletions node/primitives/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ use zenlink_protocol::{AssetId, LOCAL, NATIVE};

use crate::{
traits::{CurrencyIdExt, TokenInfo},
LeasePeriod, ParaId,
LeasePeriod, ParaId, TryConvertFrom,
};
pub const BIFROST_PARACHAIN_ID: u32 = 2001; // bifrost parachain id

pub const DOT_TOKEN_ID: u8 = 0u8;
pub const DOT: CurrencyId = CurrencyId::Token2(DOT_TOKEN_ID);
pub const GLMR_TOKEN_ID: u8 = 1u8;
Expand Down Expand Up @@ -71,14 +71,14 @@ macro_rules! create_currency_id {
}
}

impl TryFrom<CurrencyId> for AssetId {
impl TryConvertFrom<CurrencyId> for AssetId {
// DATA LAYOUT
//
// Empty: 2bytes
// Currency Discriminant: 1byte
// TokenSymbol Index: 1byte
type Error = ();
fn try_from(id: CurrencyId) -> Result<AssetId, ()> {
fn try_convert_from(id: CurrencyId, para_id: u32) -> Result<AssetId, ()> {
let _index = match id {
$(CurrencyId::Native(TokenSymbol::$symbol) => Ok((0_u64, TokenSymbol::$symbol as u64)),)*
$(CurrencyId::VToken(TokenSymbol::$symbol) => Ok((1_u64, TokenSymbol::$symbol as u64)),)*
Expand All @@ -100,10 +100,10 @@ macro_rules! create_currency_id {
};
let asset_index = ((_index?.0 << 8) & 0x0000_ff00) + (_index?.1 & 0x0000_00ff);
if id.is_native() {
Ok(AssetId { chain_id: BIFROST_PARACHAIN_ID, asset_type: NATIVE, asset_index: 0 })
Ok(AssetId { chain_id: para_id, asset_type: NATIVE, asset_index: 0 })
} else {
Ok(AssetId {
chain_id: BIFROST_PARACHAIN_ID,
chain_id: para_id,
asset_type: LOCAL,
asset_index: asset_index as u64,
})
Expand Down
7 changes: 7 additions & 0 deletions node/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,10 @@ pub trait VtokenMintingInterface<AccountId, CurrencyId, Balance> {
fn vtoken_id(token_id: CurrencyId) -> Option<CurrencyId>;
fn token_id(vtoken_id: CurrencyId) -> Option<CurrencyId>;
}

pub trait TryConvertFrom<CurrencyId> {
type Error;
fn try_convert_from(currency_id: CurrencyId, para_id: u32) -> Result<Self, Self::Error>
where
Self: Sized;
}
13 changes: 10 additions & 3 deletions pallets/flexible-fee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ authors = ["Herry Ho <herry.heyi@gmail.com>"]
edition = "2021"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.1.2", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.1.2", default-features = false, features = [
"derive",
] }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false, optional = true }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.26", default-features = false }
Expand All @@ -18,6 +22,8 @@ node-primitives = { path = "../../node/primitives", default-features = false }
zenlink-protocol = { version = "*", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
impl-trait-for-tuples = "0.2.1"
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.26", default-features = false }
bifrost-asset-registry = { path = "../../pallets/asset-registry", default-features = false }

[dev-dependencies]
orml-tokens = "0.4.1-dev"
Expand All @@ -31,7 +37,7 @@ bifrost-salp = { path = "../salp" }
bifrost-asset-registry = { path = "../asset-registry" }
bifrost-kusama-runtime = { path = "../../runtime/bifrost-kusama" }
bifrost-runtime-common = { path = "../../runtime/common" }
xcm-interface = { path = "../xcm-interface"}
xcm-interface = { path = "../xcm-interface" }

[features]
default = ["std"]
Expand All @@ -46,6 +52,7 @@ std = [
"pallet-transaction-payment/std",
"zenlink-protocol/std",
"sp-arithmetic/std",
"cumulus-primitives-core/std",
"bifrost-asset-registry/std",
]

Expand Down
29 changes: 20 additions & 9 deletions pallets/flexible-fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(deprecated)] // TODO: clear transaction

use core::convert::{Into, TryFrom};
use core::convert::Into;

use cumulus_primitives_core::ParaId;
use frame_support::{
pallet_prelude::*,
traits::{
Expand All @@ -30,7 +31,7 @@ use frame_support::{
transactional,
};
use frame_system::pallet_prelude::*;
use node_primitives::{CurrencyId, ExtraFeeName, TokenSymbol};
use node_primitives::{CurrencyId, ExtraFeeName, TokenSymbol, TryConvertFrom};
use orml_traits::MultiCurrency;
pub use pallet::*;
use pallet_transaction_payment::OnChargeTransaction;
Expand Down Expand Up @@ -103,6 +104,8 @@ pub mod pallet {
/// Alternative Fee currency exchange rate: ?x Fee currency: ?y Native currency
#[pallet::constant]
type AltFeeCurrencyExchangeRate: Get<(u32, u32)>;

type ParachainId: Get<ParaId>;
}

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
Expand Down Expand Up @@ -326,10 +329,14 @@ impl<T: Config> FeeDealer<T::AccountId, PalletBalanceOf<T>, CurrencyIdOf<T>> for

if let Some((currency_id, amount_in, amount_out)) = result_option {
if currency_id != T::NativeCurrencyId::get() {
let native_asset_id: AssetId = AssetId::try_from(T::NativeCurrencyId::get())
.map_err(|_| DispatchError::Other("Conversion Error."))?;
let asset_id: AssetId = AssetId::try_from(currency_id)
.map_err(|_| DispatchError::Other("Conversion Error."))?;
let native_asset_id: AssetId = AssetId::try_convert_from(
T::NativeCurrencyId::get(),
T::ParachainId::get().into(),
)
.map_err(|_| DispatchError::Other("Conversion Error."))?;
let asset_id: AssetId =
AssetId::try_convert_from(currency_id, T::ParachainId::get().into())
.map_err(|_| DispatchError::Other("Conversion Error."))?;
let path = vec![asset_id, native_asset_id];

T::DexOperator::inner_swap_assets_for_exact_assets(
Expand Down Expand Up @@ -399,8 +406,11 @@ impl<T: Config> Pallet<T> {
// If it is other assets
// If native token balance is below existential deposit requirement,
// go exchange fee + existential deposit. Else to exchange fee amount.
let native_asset_id: AssetId = AssetId::try_from(T::NativeCurrencyId::get())
.map_err(|_| Error::<T>::ConversionError)?;
let native_asset_id: AssetId = AssetId::try_convert_from(
T::NativeCurrencyId::get(),
T::ParachainId::get().into(),
)
.map_err(|_| Error::<T>::ConversionError)?;

let amount_out: AssetBalance = {
if native_balance > existential_deposit {
Expand All @@ -411,7 +421,8 @@ impl<T: Config> Pallet<T> {
};

let asset_id: AssetId =
AssetId::try_from(currency_id).map_err(|_| Error::<T>::ConversionError)?;
AssetId::try_convert_from(currency_id, T::ParachainId::get().into())
.map_err(|_| Error::<T>::ConversionError)?;

let path = vec![asset_id, native_asset_id];
// see if path exists, if not, continue.
Expand Down
Loading

0 comments on commit 683a808

Please sign in to comment.