Skip to content

Commit

Permalink
Bifrost v0.16.1 (#1576)
Browse files Browse the repository at this point in the history
* Bifrost v0.16.1

* Modify the commission amount to be calculated based on the corresponding vToken quantity.

* fix clippy

* fix clippy

* change div to checked_div method
  • Loading branch information
SunTiebing authored Jan 2, 2025
1 parent e9d83a6 commit 623db31
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 14 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 = "bifrost-cli"
version = "0.16.0"
version = "0.16.1"
authors = ["Liebi Technologies <[email protected]>"]
description = "Bifrost Parachain Node"
build = "build.rs"
Expand Down
34 changes: 31 additions & 3 deletions pallets/channel-commission/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern crate alloc;
use alloc::{vec, vec::Vec};
use bifrost_primitives::{
CurrencyId, CurrencyIdExt, SlpHostingFeeProvider, VTokenMintRedeemProvider,
VtokenMintingInterface,
};
use frame_support::{pallet_prelude::*, PalletId};
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -88,26 +89,46 @@ pub mod pallet {
type NameLengthLimit: Get<u32>;
/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
/// The interface to call VtokenMinting module functions.
type VtokenMintingInterface: VtokenMintingInterface<
AccountIdOf<Self>,
CurrencyId,
BalanceOf<Self>,
>;
}

#[pallet::error]
pub enum Error<T> {
/// Overflow error, indicating that a mathematical operation exceeded the allowed numeric range.
Overflow,
/// Error indicating that the provided channel name exceeds the maximum allowed length.
ChannelNameTooLong,
/// Conversion error, indicating a failure during a type conversion operation.
ConversionError,
/// Error indicating that the specified channel does not exist in storage.
ChannelNotExist,
/// Transfer error, indicating that a fund transfer operation has failed.
TransferError,
/// Error indicating that the vToken is not configured for commission calculations.
VtokenNotConfiguredForCommission,
/// Invalid commission rate, indicating that the provided commission rate is out of range or malformed.
InvalidCommissionRate,
/// Error indicating that the commission token has already been set and cannot be reconfigured.
CommissionTokenAlreadySet,
/// Invalid vToken, indicating that the provided vToken is invalid or unrecognized.
InvalidVtoken,
/// Error indicating that no changes were made during a modification operation.
/// This means that a modification request was issued but did not result in any actual changes.
NoChangesMade,
/// Represents an error that occurs when a division operation encounters a divisor of zero.
/// Error indicating a division operation encountered a divisor of zero.
/// This is a critical error, as division by zero is undefined and cannot be performed.
DivisionByZero,
/// Error indicating that the removal operation was not completed successfully.
/// Error indicating that the removal operation was not successfully completed.
/// This means an attempt to remove a resource or record did not succeed.
RemovalNotComplete,
/// Error indicating a failure during token-to-vToken conversion via exchange rate calculation.
/// This can occur when the conversion formula encounters an unexpected condition or invalid input.
TokenToVtokenConversionFailed,
}

#[pallet::event]
Expand Down Expand Up @@ -1027,14 +1048,21 @@ impl<T: Config> SlpHostingFeeProvider<CurrencyId, BalanceOf<T>, AccountIdOf<T>>
.to_vtoken()
.map_err(|_| Error::<T>::ConversionError)?;

let vtoken_amount = T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount(
staking_token,
vtoken,
amount,
)
.map_err(|_| Error::<T>::TokenToVtokenConversionFailed)?;

// If the vtoken is configured for commission, record the hosting fee
if let Some(commission_token) = CommissionTokens::<T>::get(vtoken) {
PeriodTotalCommissions::<T>::mutate(
commission_token,
|total_commission| -> Result<(), Error<T>> {
let sum_up_amount = total_commission
.1
.checked_add(&amount)
.checked_add(&vtoken_amount)
.ok_or(Error::<T>::Overflow)?;

total_commission.1 = sum_up_amount;
Expand Down
73 changes: 68 additions & 5 deletions pallets/channel-commission/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
#![cfg(test)]
#![allow(non_upper_case_globals)]

use crate as bifrost_channel_commission;
use crate::mock::sp_api_hidden_includes_construct_runtime::hidden_include::traits::OnInitialize;
use bifrost_primitives::{
currency::{ASG, BNC, KSM},
CommissionPalletId, CurrencyId,
CommissionPalletId, CurrencyId, MockXcmTransfer, MoonbeamChainId, RedeemType,
VTokenSupplyProvider, VtokenMintingInterface,
};
use frame_support::dispatch::DispatchResultWithPostInfo;
use frame_support::{
derive_impl, ord_parameter_types, parameter_types, traits::Nothing, BoundedVec, PalletId,
};
use frame_support::{derive_impl, ord_parameter_types, parameter_types, traits::Nothing, PalletId};
use frame_system::EnsureSignedBy;
use sp_core::ConstU32;
use sp_runtime::{traits::AccountIdConversion, AccountId32, BuildStorage};

use crate as bifrost_channel_commission;
use sp_runtime::{traits::AccountIdConversion, AccountId32, BuildStorage, DispatchError};

pub type BlockNumber = u64;
pub type Amount = i128;
Expand Down Expand Up @@ -142,6 +145,66 @@ impl bifrost_channel_commission::Config for Runtime {
type ClearingDuration = ClearingDuration;
type NameLengthLimit = NameLengthLimit;
type BlockNumberProvider = System;
type VtokenMintingInterface = SimpleVtokenMintingInterface;
}

pub struct SimpleVtokenMintingInterface;

impl VtokenMintingInterface<AccountId, CurrencyId, Balance> for SimpleVtokenMintingInterface {
fn mint(
_exchanger: AccountId,
_token_id: CurrencyId,
_token_amount: Balance,
_remark: BoundedVec<u8, ConstU32<32>>,
_channel_id: Option<u32>,
) -> Result<Balance, DispatchError> {
unimplemented!("method do not need to be implemented yet");
}

fn redeem(
_exchanger: AccountId,
_vtoken_id: CurrencyId,
_vtoken_amount: Balance,
) -> DispatchResultWithPostInfo {
unimplemented!("method do not need to be implemented yet");
}

fn slpx_redeem(
_exchanger: AccountId,
_vtoken_id: CurrencyId,
_vtoken_amount: Balance,
_redeem: RedeemType<AccountId>,
) -> DispatchResultWithPostInfo {
unimplemented!("method do not need to be implemented yet");
}

fn get_v_currency_amount_by_currency_amount(
_token_id: CurrencyId,
_vtoken_id: CurrencyId,
token_amount: Balance,
) -> Result<Balance, DispatchError> {
Ok(token_amount.checked_div(2u64).unwrap())
}

fn get_currency_amount_by_v_currency_amount(
_token_id: CurrencyId,
_vtoken_id: CurrencyId,
_vtoken_amount: Balance,
) -> Result<Balance, DispatchError> {
unimplemented!("method do not need to be implemented yet");
}

fn get_token_pool(_currency_id: CurrencyId) -> Balance {
unimplemented!("method do not need to be implemented yet");
}

fn get_minimums_redeem(_vtoken_id: CurrencyId) -> Balance {
unimplemented!("method do not need to be implemented yet");
}

fn get_moonbeam_parachain_id() -> u32 {
unimplemented!("method do not need to be implemented yet");
}
}

pub struct ExtBuilder {
Expand Down
4 changes: 2 additions & 2 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("bifrost"),
impl_name: create_runtime_str!("bifrost"),
authoring_version: 1,
spec_version: 16000,
spec_version: 16001,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -1764,6 +1764,7 @@ impl bifrost_channel_commission::Config for Runtime {
type ClearingDuration = ClearingDuration;
type NameLengthLimit = NameLengthLimit;
type BlockNumberProvider = System;
type VtokenMintingInterface = VtokenMinting;
}

impl bifrost_vbnc_convert::Config for Runtime {
Expand Down Expand Up @@ -2026,7 +2027,6 @@ pub mod migrations {
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_channel_commission::migrations::v1::MigrateToV1<Runtime>,
);
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/bifrost-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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: 16000,
spec_version: 16001,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -1594,6 +1594,7 @@ impl bifrost_channel_commission::Config for Runtime {
type ClearingDuration = ClearingDuration;
type NameLengthLimit = NameLengthLimit;
type BlockNumberProvider = System;
type VtokenMintingInterface = VtokenMinting;
}

impl bifrost_clouds_convert::Config for Runtime {
Expand Down Expand Up @@ -1927,7 +1928,6 @@ pub mod migrations {
pub type Unreleased = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
bifrost_channel_commission::migrations::v1::MigrateToV1<Runtime>,
);
}

Expand Down

0 comments on commit 623db31

Please sign in to comment.