Skip to content

Commit

Permalink
Merge pull request #757 from bifrost-finance/fix-vbnc
Browse files Browse the repository at this point in the history
fix: 🐛 ExistentialDeposits
  • Loading branch information
yooml authored Sep 22, 2022
2 parents e7aaa2a + 3a04052 commit 2e81d8a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions pallets/asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,12 @@ impl<T: Config> CurrencyIdRegister<CurrencyId> for AssetIdMaps<T> {
let vtoken_metadata = Pallet::<T>::convert_to_vtoken_metadata(token_metadata);
Pallet::<T>::do_register_metadata(CurrencyId::VToken(token_symbol), &vtoken_metadata)?;
return Ok(());
} else if let Some(token_metadata) =
CurrencyMetadatas::<T>::get(CurrencyId::Native(token_symbol))
{
let vtoken_metadata = Pallet::<T>::convert_to_vtoken_metadata(token_metadata);
Pallet::<T>::do_register_metadata(CurrencyId::VToken(token_symbol), &vtoken_metadata)?;
return Ok(());
} else {
return Err(Error::<T>::CurrencyIdNotExists.into());
}
Expand Down
3 changes: 3 additions & 0 deletions pallets/vtoken-minting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.28" }
bifrost-slp = { path = "../../pallets/slp", default-features = false }
bifrost-asset-registry = { path = "../asset-registry", default-features = false }
bifrost-runtime-common = { package = "bifrost-runtime-common", path = "../../runtime/common", default-features = false }
env_logger = "0.9.0"

[features]
default = ["std"]
Expand All @@ -47,6 +49,7 @@ std = [
"orml-traits/std",
"bifrost-slp/std",
"bifrost-asset-registry/std",
"bifrost-runtime-common/std",
]

runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
Expand Down
44 changes: 30 additions & 14 deletions pallets/vtoken-minting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![allow(non_upper_case_globals)]

use bifrost_asset_registry::AssetIdMaps;
use bifrost_runtime_common::{micro, milli};
use bifrost_slp::{QueryId, QueryResponseManager};
use codec::{Decode, Encode};
pub use cumulus_primitives_core::ParaId;
Expand All @@ -34,7 +35,7 @@ use frame_support::{
};
use frame_system::EnsureSignedBy;
use hex_literal::hex;
use node_primitives::{CurrencyId, TokenSymbol};
use node_primitives::{CurrencyId, CurrencyIdMapping, TokenSymbol};
use sp_core::{blake2_256, H256};
use sp_runtime::{
testing::Header,
Expand All @@ -57,9 +58,8 @@ pub type Amount = i128;
pub type Balance = u128;

pub type AccountId = AccountId32;
pub const BNC: CurrencyId = CurrencyId::Native(TokenSymbol::ASG);
pub const DOT: CurrencyId = CurrencyId::Token(TokenSymbol::DOT);
pub const vDOT: CurrencyId = CurrencyId::VToken(TokenSymbol::DOT);
pub const BNC: CurrencyId = CurrencyId::Native(TokenSymbol::BNC);
pub const vBNC: CurrencyId = CurrencyId::VToken(TokenSymbol::BNC);
pub const KSM: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub const vKSM: CurrencyId = CurrencyId::VToken(TokenSymbol::KSM);
pub const MOVR: CurrencyId = CurrencyId::Token(TokenSymbol::MOVR);
Expand Down Expand Up @@ -120,21 +120,26 @@ impl frame_system::Config for Runtime {
}

parameter_types! {
pub const GetNativeCurrencyId: CurrencyId = CurrencyId::Native(TokenSymbol::ASG);
pub const NativeCurrencyId: CurrencyId = CurrencyId::Native(TokenSymbol::BNC);
}

pub type AdaptedBasicCurrency =
orml_currencies::BasicCurrencyAdapter<Runtime, Balances, Amount, BlockNumber>;

impl orml_currencies::Config for Runtime {
type GetNativeCurrencyId = GetNativeCurrencyId;
type GetNativeCurrencyId = NativeCurrencyId;
type MultiCurrency = Tokens;
type NativeCurrency = AdaptedBasicCurrency;
type WeightInfo = ();
}

parameter_types! {
pub const ExistentialDeposit: Balance = 1;
// pub const NativeCurrencyId: CurrencyId = CurrencyId::Native(TokenSymbol::BNC);
// pub const RelayCurrencyId: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
pub const StableCurrencyId: CurrencyId = CurrencyId::Stable(TokenSymbol::KUSD);
// pub SelfParaId: u32 = ParachainInfo::parachain_id().into();
pub const PolkadotCurrencyId: CurrencyId = CurrencyId::Token(TokenSymbol::DOT);
}

impl pallet_balances::Config for Runtime {
Expand All @@ -150,8 +155,22 @@ impl pallet_balances::Config for Runtime {
}

orml_traits::parameter_type_with_key! {
pub ExistentialDeposits: |_currency_id: CurrencyId| -> Balance {
0
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
env_logger::try_init().unwrap_or(());

log::debug!(
"{:?}",currency_id
);
match currency_id {
&CurrencyId::Native(TokenSymbol::BNC) => 10 * milli::<Runtime>(NativeCurrencyId::get()), // 0.01 BNC
&CurrencyId::Token(TokenSymbol::KSM) => 0,
&CurrencyId::VToken(TokenSymbol::KSM) => 0,
&CurrencyId::Token(TokenSymbol::MOVR) => 1 * micro::<Runtime>(CurrencyId::Token(TokenSymbol::MOVR)), // MOVR has a decimals of 10e18
&CurrencyId::VToken(TokenSymbol::MOVR) => 1 * micro::<Runtime>(CurrencyId::Token(TokenSymbol::MOVR)), // MOVR has a decimals of 10e18
&CurrencyId::VToken(TokenSymbol::BNC) => 10 * milli::<Runtime>(NativeCurrencyId::get()), // 0.01 BNC
_ => AssetIdMaps::<Runtime>::get_currency_metadata(*currency_id)
.map_or(Balance::max_value(), |metatata| metatata.minimal_balance)
}
};
}
impl orml_tokens::Config for Runtime {
Expand Down Expand Up @@ -311,13 +330,10 @@ impl ExtBuilder {

pub fn one_hundred_for_alice_n_bob(self) -> Self {
self.balances(vec![
(ALICE, BNC, 100),
(BOB, BNC, 100),
(CHARLIE, BNC, 100),
(ALICE, DOT, 100),
(ALICE, vDOT, 400),
(ALICE, BNC, 1000000000000000000000),
(BOB, BNC, 1000000000000),
(BOB, vKSM, 1000),
(BOB, KSM, 10000000000),
(BOB, KSM, 1000000000000),
(BOB, MOVR, 1000000000000000000000),
(CHARLIE, MOVR, 100000000000000000000000),
])
Expand Down
35 changes: 29 additions & 6 deletions pallets/vtoken-minting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,32 @@

#![cfg(test)]

use crate::{mock::*, *};
use frame_support::{assert_noop, assert_ok, sp_runtime::Permill, BoundedVec};

use crate::{mock::*, *};
#[test]
fn mint_bnc() {
ExtBuilder::default().one_hundred_for_alice_n_bob().build().execute_with(|| {
assert_eq!(Tokens::total_issuance(vBNC), 0);
assert_ok!(VtokenMinting::set_minimum_mint(Origin::signed(ALICE), BNC, 200));
pub const FEE: Permill = Permill::from_percent(5);
assert_ok!(VtokenMinting::set_fees(Origin::root(), FEE, FEE));
assert_noop!(
VtokenMinting::mint(Some(BOB).into(), BNC, 100),
Error::<Runtime>::BelowMinimumMint
);
let (entrance_account, _exit_account) = VtokenMinting::get_entrance_and_exit_accounts();
assert_eq!(Balances::free_balance(&entrance_account), 0);
assert_ok!(VtokenMinting::mint(Some(BOB).into(), BNC, 100000000000));
assert_eq!(VtokenMinting::token_pool(BNC), 95000000000);
assert_eq!(VtokenMinting::minimum_mint(BNC), 200);
assert_eq!(Tokens::total_issuance(vBNC), 95000000000);

assert_eq!(Balances::free_balance(&entrance_account), 95000000000);
let fee_account: AccountId = <Runtime as Config>::FeeAccount::get();
assert_eq!(Balances::free_balance(&fee_account), 5000000000);
});
}

#[test]
fn mint() {
Expand All @@ -34,18 +57,18 @@ fn mint() {
VtokenMinting::mint(Some(BOB).into(), KSM, 100),
Error::<Runtime>::BelowMinimumMint
);
assert_ok!(VtokenMinting::mint(Some(BOB).into(), KSM, 1000));
assert_ok!(VtokenMinting::mint(Some(BOB).into(), KSM, 100000000000));
assert_ok!(VtokenMinting::mint(Some(BOB).into(), MOVR, 100000000000000000000));
assert_ok!(VtokenMinting::mint(Some(BOB).into(), MOVR, 100000000000000000000));
assert_eq!(VtokenMinting::token_pool(MOVR), 190000000000000000000);
assert_eq!(VtokenMinting::token_pool(KSM), 950);
assert_eq!(VtokenMinting::token_pool(KSM), 95000000000);
assert_eq!(VtokenMinting::minimum_mint(KSM), 200);
assert_eq!(Tokens::total_issuance(vKSM), 1950);
assert_eq!(Tokens::total_issuance(vKSM), 95000001000);

let (entrance_account, _exit_account) = VtokenMinting::get_entrance_and_exit_accounts();
assert_eq!(Tokens::free_balance(KSM, &entrance_account), 950);
assert_eq!(Tokens::free_balance(KSM, &entrance_account), 95000000000);
let fee_account: AccountId = <Runtime as Config>::FeeAccount::get();
assert_eq!(Tokens::free_balance(KSM, &fee_account), 50);
assert_eq!(Tokens::free_balance(KSM, &fee_account), 5000000000);
});
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ parameter_type_with_key! {
AssetIdMaps::<Runtime>::get_asset_metadata(AssetIds::ForeignAssetId(*foreign_asset_id)).
map_or(Balance::max_value(), |metatata| metatata.minimal_balance)
},
_ => AssetIdMaps::<Runtime>::get_asset_metadata(AssetIds::NativeAssetId(*currency_id))
_ => AssetIdMaps::<Runtime>::get_currency_metadata(*currency_id)
.map_or(Balance::max_value(), |metatata| metatata.minimal_balance)
}
};
Expand Down
3 changes: 2 additions & 1 deletion runtime/bifrost-kusama/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<T: Config> frame_support::traits::OnRuntimeUpgrade for AssetRegistryMigrati
len += items.len() as Weight;

// vToken
AssetIdMaps::<T>::register_vtoken_metadata(BNC).expect("VToken register");
AssetIdMaps::<T>::register_vtoken_metadata(KSM).expect("VToken register");
AssetIdMaps::<T>::register_vtoken_metadata(MOVR).expect("VToken register");
// vsToken
Expand Down Expand Up @@ -115,7 +116,7 @@ impl<T: Config> frame_support::traits::OnRuntimeUpgrade for AssetRegistryMigrati
fn post_upgrade() -> Result<(), &'static str> {
let count = bifrost_asset_registry::CurrencyMetadatas::<T>::iter().count();
log::info!("try-runtime::post_upgrade currency_metadatas count: {:?}", count);
assert_eq!(count, 34);
assert_eq!(count, 35);

Ok(())
}
Expand Down

0 comments on commit 2e81d8a

Please # to comment.