From a4c24e56f56b582bb5546dbabac0e724f3588e10 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Mon, 30 Dec 2024 20:16:08 +0800 Subject: [PATCH 1/5] Bifrost v0.16.1 --- Cargo.lock | 2 +- node/cli/Cargo.toml | 2 +- runtime/bifrost-kusama/src/lib.rs | 3 +-- runtime/bifrost-polkadot/src/lib.rs | 3 +-- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7ffe33728..157b60a7bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -853,7 +853,7 @@ dependencies = [ [[package]] name = "bifrost-cli" -version = "0.16.0" +version = "0.16.1" dependencies = [ "bifrost-primitives", "bifrost-service", diff --git a/node/cli/Cargo.toml b/node/cli/Cargo.toml index 6b240bcde6..ed7e548bce 100644 --- a/node/cli/Cargo.toml +++ b/node/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bifrost-cli" -version = "0.16.0" +version = "0.16.1" authors = ["Liebi Technologies "] description = "Bifrost Parachain Node" build = "build.rs" diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index efa7bcd179..bba061f53c 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -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, @@ -2026,7 +2026,6 @@ pub mod migrations { pub type Unreleased = ( // permanent migration, do not remove pallet_xcm::migration::MigrateToLatestXcmVersion, - bifrost_channel_commission::migrations::v1::MigrateToV1, ); } diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index 10ae22c4f7..0ee845c4f5 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -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, @@ -1927,7 +1927,6 @@ pub mod migrations { pub type Unreleased = ( // permanent migration, do not remove pallet_xcm::migration::MigrateToLatestXcmVersion, - bifrost_channel_commission::migrations::v1::MigrateToV1, ); } From eb14dee0e8407eca828d9f80e9030034f63072e8 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Tue, 31 Dec 2024 13:57:35 +0800 Subject: [PATCH 2/5] Modify the commission amount to be calculated based on the corresponding vToken quantity. --- pallets/channel-commission/src/lib.rs | 34 ++++++++++-- pallets/channel-commission/src/mock.rs | 74 ++++++++++++++++++++++++-- runtime/bifrost-kusama/src/lib.rs | 1 + runtime/bifrost-polkadot/src/lib.rs | 1 + 4 files changed, 102 insertions(+), 8 deletions(-) diff --git a/pallets/channel-commission/src/lib.rs b/pallets/channel-commission/src/lib.rs index 12a5688fc8..709f237a37 100644 --- a/pallets/channel-commission/src/lib.rs +++ b/pallets/channel-commission/src/lib.rs @@ -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::*; @@ -88,26 +89,46 @@ pub mod pallet { type NameLengthLimit: Get; /// The current block number provider. type BlockNumberProvider: BlockNumberProvider>; + /// The interface to call VtokenMinting module functions. + type VtokenMintingInterface: VtokenMintingInterface< + AccountIdOf, + CurrencyId, + BalanceOf, + >; } #[pallet::error] pub enum Error { + /// 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] @@ -1027,6 +1048,13 @@ impl SlpHostingFeeProvider, AccountIdOf> .to_vtoken() .map_err(|_| Error::::ConversionError)?; + let vtoken_amount = T::VtokenMintingInterface::get_v_currency_amount_by_currency_amount( + staking_token, + vtoken, + amount, + ) + .map_err(|_| Error::::TokenToVtokenConversionFailed)?; + // If the vtoken is configured for commission, record the hosting fee if let Some(commission_token) = CommissionTokens::::get(vtoken) { PeriodTotalCommissions::::mutate( @@ -1034,7 +1062,7 @@ impl SlpHostingFeeProvider, AccountIdOf> |total_commission| -> Result<(), Error> { let sum_up_amount = total_commission .1 - .checked_add(&amount) + .checked_add(&vtoken_amount) .ok_or(Error::::Overflow)?; total_commission.1 = sum_up_amount; diff --git a/pallets/channel-commission/src/mock.rs b/pallets/channel-commission/src/mock.rs index 09616e4a77..e99c6ba9ef 100644 --- a/pallets/channel-commission/src/mock.rs +++ b/pallets/channel-commission/src/mock.rs @@ -19,17 +19,21 @@ #![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::Scale; +use sp_runtime::{traits::AccountIdConversion, AccountId32, BuildStorage, DispatchError}; pub type BlockNumber = u64; pub type Amount = i128; @@ -142,6 +146,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 for SimpleVtokenMintingInterface { + fn mint( + _exchanger: AccountId, + _token_id: CurrencyId, + _token_amount: Balance, + _remark: BoundedVec>, + _channel_id: Option, + ) -> Result { + 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, + ) -> 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 { + Ok(token_amount.mul(2)) + } + + fn get_currency_amount_by_v_currency_amount( + _token_id: CurrencyId, + _vtoken_id: CurrencyId, + _vtoken_amount: Balance, + ) -> Result { + 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 { diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index bba061f53c..82b176821f 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -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 { diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index 0ee845c4f5..4f624df241 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -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 { From 30250a065877345ebaba80cb58c6ab2731873367 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Tue, 31 Dec 2024 14:29:20 +0800 Subject: [PATCH 3/5] fix clippy --- pallets/channel-commission/src/mock.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pallets/channel-commission/src/mock.rs b/pallets/channel-commission/src/mock.rs index e99c6ba9ef..8726ef7713 100644 --- a/pallets/channel-commission/src/mock.rs +++ b/pallets/channel-commission/src/mock.rs @@ -19,6 +19,7 @@ #![cfg(test)] #![allow(non_upper_case_globals)] +use std::ops::Div; use crate as bifrost_channel_commission; use crate::mock::sp_api_hidden_includes_construct_runtime::hidden_include::traits::OnInitialize; use bifrost_primitives::{ @@ -184,7 +185,7 @@ impl VtokenMintingInterface for SimpleVtokenMint _vtoken_id: CurrencyId, token_amount: Balance, ) -> Result { - Ok(token_amount.mul(2)) + Ok(token_amount.div(2u64)) } fn get_currency_amount_by_v_currency_amount( From c30a470689ee4addb806a30f085a846084a98813 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Tue, 31 Dec 2024 14:31:38 +0800 Subject: [PATCH 4/5] fix clippy --- pallets/channel-commission/src/mock.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/channel-commission/src/mock.rs b/pallets/channel-commission/src/mock.rs index 8726ef7713..90270b949b 100644 --- a/pallets/channel-commission/src/mock.rs +++ b/pallets/channel-commission/src/mock.rs @@ -19,7 +19,6 @@ #![cfg(test)] #![allow(non_upper_case_globals)] -use std::ops::Div; use crate as bifrost_channel_commission; use crate::mock::sp_api_hidden_includes_construct_runtime::hidden_include::traits::OnInitialize; use bifrost_primitives::{ @@ -35,6 +34,7 @@ use frame_system::EnsureSignedBy; use sp_core::ConstU32; use sp_runtime::traits::Scale; use sp_runtime::{traits::AccountIdConversion, AccountId32, BuildStorage, DispatchError}; +use std::ops::Div; pub type BlockNumber = u64; pub type Amount = i128; From ba4740b205b3595a5c8c85421b2ffdd510e1b949 Mon Sep 17 00:00:00 2001 From: SunTiebing <1045060705@qq.com> Date: Tue, 31 Dec 2024 16:08:13 +0800 Subject: [PATCH 5/5] change div to checked_div method --- pallets/channel-commission/src/mock.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pallets/channel-commission/src/mock.rs b/pallets/channel-commission/src/mock.rs index 90270b949b..6a9b86b4f6 100644 --- a/pallets/channel-commission/src/mock.rs +++ b/pallets/channel-commission/src/mock.rs @@ -32,9 +32,7 @@ use frame_support::{ }; use frame_system::EnsureSignedBy; use sp_core::ConstU32; -use sp_runtime::traits::Scale; use sp_runtime::{traits::AccountIdConversion, AccountId32, BuildStorage, DispatchError}; -use std::ops::Div; pub type BlockNumber = u64; pub type Amount = i128; @@ -185,7 +183,7 @@ impl VtokenMintingInterface for SimpleVtokenMint _vtoken_id: CurrencyId, token_amount: Balance, ) -> Result { - Ok(token_amount.div(2u64)) + Ok(token_amount.checked_div(2u64).unwrap()) } fn get_currency_amount_by_v_currency_amount(