Skip to content

Commit

Permalink
fix BNC to_vtoken conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
herryho committed Nov 20, 2023
1 parent 2b5b00e commit f464ef0
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion node/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-cli"
version = "0.9.86"
version = "0.9.87"
authors = ["Liebi Technologies <[email protected]>"]
description = "Bifrost Parachain Node"
build = "build.rs"
Expand Down
2 changes: 2 additions & 0 deletions node/primitives/src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ impl CurrencyId {

pub fn to_token(&self) -> Result<Self, ()> {
match self {
Self::VToken(TokenSymbol::BNC) => Ok(Self::Native(TokenSymbol::BNC)),
Self::VToken(symbol) => Ok(Self::Token(*symbol)),
Self::VToken2(id) => Ok(Self::Token2(*id)),
_ => Err(()),
Expand All @@ -424,6 +425,7 @@ impl CurrencyId {
match self {
Self::Token(symbol) => Ok(Self::VToken(*symbol)),
Self::Token2(id) => Ok(Self::VToken2(*id)),
Self::Native(TokenSymbol::BNC) => Ok(Self::VToken(TokenSymbol::BNC)),
_ => Err(()),
}
}
Expand Down
70 changes: 70 additions & 0 deletions node/primitives/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,73 @@ fn u64_to_currency_id_should_work() {
assert_eq!(eb5, CurrencyId::try_from(0x07d5_004f_005f_0b05).unwrap());
assert_eq!(eb6, CurrencyId::try_from(0x07d6_005f_006f_0b06).unwrap());
}

#[test]
fn to_vtoken_should_work() {
let native_bnc = CurrencyId::Native(TokenSymbol::BNC);
let native_asg = CurrencyId::Native(TokenSymbol::ASG);
let vtoken_ksm = CurrencyId::VToken(TokenSymbol::KSM);
let token_ksm = CurrencyId::Token(TokenSymbol::KSM);
let stable_kusd = CurrencyId::Stable(TokenSymbol::KUSD);
let vstoken_eth = CurrencyId::VSToken(TokenSymbol::ETH);
let vsbond_ksm = CurrencyId::VSBond(TokenSymbol::KSM, 2001, 0, 1000);
let token2_dot = CurrencyId::Token2(DOT_TOKEN_ID);
let vtoken2_dot = CurrencyId::VToken2(DOT_TOKEN_ID);
let vstoken2_dot = CurrencyId::VSToken2(DOT_TOKEN_ID);
let vsbond2_dot = CurrencyId::VSBond2(DOT_TOKEN_ID, 2030, 0, 1000);
let foreign_asset = CurrencyId::ForeignAsset(1984);
let blp = CurrencyId::BLP(100);
let stable_lp_token = CurrencyId::StableLpToken(100);
let lp_token = CurrencyId::LPToken(TokenSymbol::BNC, 0, TokenSymbol::KSM, 2);

assert_eq!(native_bnc.to_vtoken(), Ok(CurrencyId::VToken(TokenSymbol::BNC)));
assert_eq!(native_asg.to_vtoken(), Err(()));
assert_eq!(vtoken_ksm.to_vtoken(), Err(()));
assert_eq!(token_ksm.to_vtoken(), Ok(CurrencyId::VToken(TokenSymbol::KSM)));
assert_eq!(stable_kusd.to_vtoken(), Err(()));
assert_eq!(vstoken_eth.to_vtoken(), Err(()));
assert_eq!(vsbond_ksm.to_vtoken(), Err(()));
assert_eq!(token2_dot.to_vtoken(), Ok(CurrencyId::VToken2(DOT_TOKEN_ID)));
assert_eq!(vtoken2_dot.to_vtoken(), Err(()));
assert_eq!(vstoken2_dot.to_vtoken(), Err(()));
assert_eq!(vsbond2_dot.to_vtoken(), Err(()));
assert_eq!(foreign_asset.to_vtoken(), Err(()));
assert_eq!(blp.to_vtoken(), Err(()));
assert_eq!(stable_lp_token.to_vtoken(), Err(()));
assert_eq!(lp_token.to_vtoken(), Err(()));
}

#[test]
fn to_token_should_work() {
let native_bnc = CurrencyId::Native(TokenSymbol::BNC);
let vtoken_bnc = CurrencyId::VToken(TokenSymbol::BNC);
let vtoken_ksm = CurrencyId::VToken(TokenSymbol::KSM);
let token_ksm = CurrencyId::Token(TokenSymbol::KSM);
let stable_kusd = CurrencyId::Stable(TokenSymbol::KUSD);
let vstoken_eth = CurrencyId::VSToken(TokenSymbol::ETH);
let vsbond_ksm = CurrencyId::VSBond(TokenSymbol::KSM, 2001, 0, 1000);
let token2_dot = CurrencyId::Token2(DOT_TOKEN_ID);
let vtoken2_dot = CurrencyId::VToken2(DOT_TOKEN_ID);
let vstoken2_dot = CurrencyId::VSToken2(DOT_TOKEN_ID);
let vsbond2_dot = CurrencyId::VSBond2(DOT_TOKEN_ID, 2030, 0, 1000);
let foreign_asset = CurrencyId::ForeignAsset(1984);
let blp = CurrencyId::BLP(100);
let stable_lp_token = CurrencyId::StableLpToken(100);
let lp_token = CurrencyId::LPToken(TokenSymbol::BNC, 0, TokenSymbol::KSM, 2);

assert_eq!(native_bnc.to_token(), Err(()));
assert_eq!(vtoken_bnc.to_token(), Ok(CurrencyId::Native(TokenSymbol::BNC)));
assert_eq!(vtoken_ksm.to_token(), Ok(CurrencyId::Token(TokenSymbol::KSM)));
assert_eq!(token_ksm.to_token(), Err(()));
assert_eq!(stable_kusd.to_token(), Err(()));
assert_eq!(vstoken_eth.to_token(), Err(()));
assert_eq!(vsbond_ksm.to_token(), Err(()));
assert_eq!(token2_dot.to_token(), Err(()));
assert_eq!(vtoken2_dot.to_token(), Ok(CurrencyId::Token2(DOT_TOKEN_ID)));
assert_eq!(vstoken2_dot.to_token(), Err(()));
assert_eq!(vsbond2_dot.to_token(), Err(()));
assert_eq!(foreign_asset.to_token(), Err(()));
assert_eq!(blp.to_token(), Err(()));
assert_eq!(stable_lp_token.to_token(), Err(()));
assert_eq!(lp_token.to_token(), Err(()));
}
6 changes: 3 additions & 3 deletions pallets/slp/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ mod parachain_staking_tests;
#[cfg(test)]
mod phala_tests;

#[cfg(test)]
pub use filecoin_tests::*;
#[cfg(test)]
pub use kusama_tests::*;
#[cfg(test)]
pub use moonriver_tests::*;
// #[cfg(test)]
// pub use parachain_staking_tests::*;
#[cfg(test)]
pub use filecoin_tests::*;
pub use parachain_staking_tests::*;
#[cfg(test)]
pub use phala_tests::*;
67 changes: 57 additions & 10 deletions pallets/slp/src/tests/parachain_staking_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
#![cfg(test)]
use crate::{
mocks::mock::*,
primitives::{OneToManyDelegationAction, OneToManyScheduledRequest},
*,
};
use frame_support::{assert_noop, assert_ok};
use parachain_staking::RoundInfo;

use crate::{
primitives::{OneToManyDelegatorStatus, OneToManyLedger},
BNC,
primitives::{
OneToManyDelegationAction, OneToManyDelegatorStatus, OneToManyLedger,
OneToManyScheduledRequest,
},
BNC, *,
};
use codec::alloc::collections::BTreeMap;
use frame_support::PalletId;
use frame_support::{assert_noop, assert_ok, PalletId};
use node_primitives::VBNC;
use parachain_staking::RoundInfo;
use sp_runtime::traits::AccountIdConversion;

#[test]
Expand Down Expand Up @@ -970,3 +968,52 @@ fn add_validator_and_remove_validator_works() {
assert_eq!(Slp::get_validators(BNC), Some(empty_bounded_vec));
});
}

#[test]
fn charge_host_fee_and_tune_vtoken_exchange_rate_works() {
let subaccount_0_location =
MultiLocation { parents: 0, interior: X1(AccountId32 { network: None, id: ALICE.into() }) };

ExtBuilder::default().build().execute_with(|| {
// environment setup
parachain_staking_setup();

// First set base vtoken exchange rate. Should be 1:1.
assert_ok!(Currencies::deposit(VBNC, &ALICE, 1000));
assert_ok!(Slp::increase_token_pool(RuntimeOrigin::signed(ALICE), BNC, 1000));

// Set the hosting fee to be 20%, and the beneficiary to be bifrost treasury account.
let pct = Permill::from_percent(20);
let treasury_account_id_32: [u8; 32] = PalletId(*b"bf/trsry").into_account_truncating();
let treasury_location = MultiLocation {
parents: 0,
interior: X1(AccountId32 { network: None, id: treasury_account_id_32 }),
};

assert_ok!(Slp::set_hosting_fees(
RuntimeOrigin::signed(ALICE),
BNC,
Some((pct, treasury_location))
));

let pct_100 = Permill::from_percent(100);
assert_ok!(Slp::set_currency_tune_exchange_rate_limit(
RuntimeOrigin::signed(ALICE),
BNC,
Some((1, pct_100))
));

// call the charge_host_fee_and_tune_vtoken_exchange_rate
assert_ok!(Slp::charge_host_fee_and_tune_vtoken_exchange_rate(
RuntimeOrigin::signed(ALICE),
BNC,
1000,
Some(subaccount_0_location)
));

// check token pool, should be 1000 + 1000 = 2000
assert_eq!(<Runtime as Config>::VtokenMinting::get_token_pool(BNC), 2000);
// check vBNC issuance, should be 1000 + 20% * 1000 = 1200
assert_eq!(Currencies::total_issuance(VBNC), 1200);
});
}
5 changes: 3 additions & 2 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("bifrost"),
impl_name: create_runtime_str!("bifrost"),
authoring_version: 1,
spec_version: 986,
spec_version: 987,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -1905,10 +1905,11 @@ pub type Migrations = migrations::Unreleased;

/// The runtime migrations per release.
pub mod migrations {
#[allow(unused)]
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = bifrost_slp::migrations::v3::SlpMigration3<Runtime>;
pub type Unreleased = ();
}

/// Executive: handles dispatch to the various modules.
Expand Down
5 changes: 3 additions & 2 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("bifrost_polkadot"),
impl_name: create_runtime_str!("bifrost_polkadot"),
authoring_version: 0,
spec_version: 986,
spec_version: 987,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -1709,10 +1709,11 @@ pub type Migrations = migrations::Unreleased;

/// The runtime migrations per release.
pub mod migrations {
#[allow(unused)]
use super::*;

/// Unreleased migrations. Add new ones here:
pub type Unreleased = bifrost_slp::migrations::v3::SlpMigration3<Runtime>;
pub type Unreleased = ();
}

/// Executive: handles dispatch to the various modules.
Expand Down

0 comments on commit f464ef0

Please sign in to comment.