From f12ddb3c4272b1308f189108f6e80f7249b0a759 Mon Sep 17 00:00:00 2001 From: toints Date: Tue, 2 Jul 2024 00:15:20 +0800 Subject: [PATCH] fixed: fixed ratio sum > 100% --- node/src/chain_spec.rs | 13 ++-- pallets/liquidation/src/lib.rs | 126 ++++++++++++++++++------------- pallets/liquidation/src/mock.rs | 24 +++--- pallets/liquidation/src/tests.rs | 8 +- 4 files changed, 95 insertions(+), 76 deletions(-) diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index 48466e3..67bd207 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -4,10 +4,7 @@ use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; use sc_service::ChainType; use serde::{Deserialize, Serialize}; use sp_core::{sr25519, Pair, Public, H160, U256}; -use sp_runtime::{ - traits::{IdentifyAccount, Verify}, - Perbill, -}; +use sp_runtime::traits::{IdentifyAccount, Verify}; use sp_std::marker::PhantomData; use std::{collections::BTreeMap, str::FromStr}; @@ -292,10 +289,10 @@ fn testnet_genesis( }, "liquidation":{ "adminKey": Some(root.clone()), - "systemRatio": Perbill::from_percent(20), - "treasuryRatio": Perbill::from_percent(33), - "operationRatio": Perbill::from_percent(25), - "collatorRatio":Perbill::from_percent(22), + "systemRatio": 20_000_0000, + "treasuryRatio": 33_000_0000, + "operationRatio": 25_000_0000, + "collatorRatio": 22_000_0000, "minLiquidationThreshold": 20_000_000_000_000_000u128, "profitDistributionCycle": 10, } diff --git a/pallets/liquidation/src/lib.rs b/pallets/liquidation/src/lib.rs index 13536c5..f8cab12 100644 --- a/pallets/liquidation/src/lib.rs +++ b/pallets/liquidation/src/lib.rs @@ -21,7 +21,7 @@ use mp_system::BASE_ACCOUNT; pub use pallet::*; use sp_runtime::{ traits::{StaticLookup, Zero}, - AccountId32, Perbill, Saturating, + AccountId32, Percent, Saturating, }; use sp_std::{prelude::*, sync::Arc, vec}; @@ -38,6 +38,7 @@ type BalanceOf = pub type Balance = u128; pub const PARACHAIN_TO_RELAYCHAIN_UNIT: u128 = 1_000_000; +pub const PERCENT_UNIT: u128 = 1_000_0000; #[frame_support::pallet] pub mod pallet { @@ -120,19 +121,19 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn system_ratio)] - pub type SystemRatio = StorageValue<_, Perbill, ValueQuery>; + pub type SystemRatio = StorageValue<_, u32, ValueQuery>; #[pallet::storage] #[pallet::getter(fn treasury_ratio)] - pub type TreasuryRatio = StorageValue<_, Perbill, ValueQuery>; + pub type TreasuryRatio = StorageValue<_, u32, ValueQuery>; #[pallet::storage] #[pallet::getter(fn operation_ratio)] - pub type OperationRatio = StorageValue<_, Perbill, ValueQuery>; + pub type OperationRatio = StorageValue<_, u32, ValueQuery>; #[pallet::storage] #[pallet::getter(fn collator_ratio)] - pub type CollatorRatio = StorageValue<_, Perbill, ValueQuery>; + pub type CollatorRatio = StorageValue<_, u32, ValueQuery>; #[pallet::storage] #[pallet::getter(fn min_liquidation_threshold)] @@ -152,10 +153,10 @@ pub mod pallet { pub struct GenesisConfig { /// The `AccountId` of the admin key. pub admin_key: Option, - pub system_ratio: Perbill, - pub treasury_ratio: Perbill, - pub operation_ratio: Perbill, - pub collator_ratio: Perbill, + pub system_ratio: u32, + pub treasury_ratio: u32, + pub operation_ratio: u32, + pub collator_ratio: u32, pub min_liquidation_threshold: Balance, pub profit_distribution_cycle: BlockNumberFor, } @@ -168,7 +169,7 @@ pub mod pallet { + self.treasury_ratio + self.operation_ratio + self.collator_ratio - <= Perbill::one(), + <= 100 * (PERCENT_UNIT as u32), "Ratio sum must be <= 100%" ); assert!( @@ -218,16 +219,16 @@ pub mod pallet { SetAdmin(T::AccountId), /// Set system ratio - SystemRatioSet(Perbill), + SystemRatioSet(u32), /// Set treasury ratio - TreasuryRatioSet(Perbill), + TreasuryRatioSet(u32), /// Set operation ratio - OperationRatioSet(Perbill), + OperationRatioSet(u32), ///Set collator ratio - CollatorRatioSet(Perbill), + CollatorRatioSet(u32), /// Set min liquidation threshold MinLiquidationThresholdSet(Balance), @@ -477,9 +478,10 @@ pub mod pallet { let treasury_ratio = TreasuryRatio::::get(); let operation_ratio = OperationRatio::::get(); - let treasury_amount = treasury_ratio * total_profit / PARACHAIN_TO_RELAYCHAIN_UNIT; - let operation_amount = operation_ratio * total_profit; - let system_amount = system_ratio * total_profit; + let treasury_amount = (treasury_ratio as u128) / PERCENT_UNIT * total_profit + / PARACHAIN_TO_RELAYCHAIN_UNIT; + let operation_amount = (operation_ratio as u128) / PERCENT_UNIT * total_profit; + let system_amount = (system_ratio as u128) / PERCENT_UNIT * total_profit; let total_collators_profit = total_profit.saturating_sub(treasury_amount + operation_amount + system_amount); @@ -504,7 +506,7 @@ pub mod pallet { transfers.push((operation_account, operation_account_profit)); for (collator, collator_cost) in CollatorRealGasCosts::::iter() { - let collator_ratio = Perbill::from_rational(collator_cost, total_cost); + let collator_ratio = Percent::from_rational(collator_cost, total_cost); let collator_profit = collator_ratio * total_collators_profit; let collator_addr_profit = @@ -597,20 +599,22 @@ pub mod pallet { #[pallet::call_index(1)] #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] - pub fn set_system_ratio( - origin: OriginFor, - ratio: Perbill, - ) -> DispatchResultWithPostInfo { + pub fn set_system_ratio(origin: OriginFor, ratio: u32) -> DispatchResultWithPostInfo { ensure_root_or_admin::(origin)?; let treasury_ratio = TreasuryRatio::::get(); let operation_ratio = OperationRatio::::get(); let collator_ratio = CollatorRatio::::get(); - ensure!( - ratio + treasury_ratio + operation_ratio + collator_ratio <= Perbill::one(), - Error::::InvalidRatio - ); + let total_ratio = treasury_ratio + ratio + operation_ratio + collator_ratio; + log::info!("1 +++++++++ set system ratio, total ratio:{:?}, treasury_ratio:{:?}, operation_ratio:{:?}, collator_ratio:{:?}, system_ratio:{:?}", + total_ratio, treasury_ratio, operation_ratio, collator_ratio, ratio); + ensure_total_ratio_not_exceed_one::( + ratio, + treasury_ratio, + operation_ratio, + collator_ratio, + )?; SystemRatio::::put(ratio); Self::deposit_event(Event::SystemRatioSet(ratio)); @@ -619,20 +623,23 @@ pub mod pallet { #[pallet::call_index(2)] #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] - pub fn set_treasury_ratio( - origin: OriginFor, - ratio: Perbill, - ) -> DispatchResultWithPostInfo { + pub fn set_treasury_ratio(origin: OriginFor, ratio: u32) -> DispatchResultWithPostInfo { ensure_root_or_admin::(origin)?; let system_ratio = SystemRatio::::get(); let operation_ratio = OperationRatio::::get(); let collator_ratio = CollatorRatio::::get(); - ensure!( - system_ratio + ratio + operation_ratio + collator_ratio <= Perbill::one(), - Error::::InvalidRatio - ); + let total_ratio = system_ratio + ratio + operation_ratio + collator_ratio; + log::info!("2 =========== set treasury ratio, total ratio:{:?}, system_ratio:{:?}, operation_ratio:{:?}, collator_ratio:{:?}, treasury_ratio:{:?}", + total_ratio, system_ratio, operation_ratio, collator_ratio, ratio); + + ensure_total_ratio_not_exceed_one::( + system_ratio, + ratio, + operation_ratio, + collator_ratio, + )?; TreasuryRatio::::put(ratio); Self::deposit_event(Event::TreasuryRatioSet(ratio)); @@ -641,20 +648,22 @@ pub mod pallet { #[pallet::call_index(3)] #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] - pub fn set_operation_ratio( - origin: OriginFor, - ratio: Perbill, - ) -> DispatchResultWithPostInfo { + pub fn set_operation_ratio(origin: OriginFor, ratio: u32) -> DispatchResultWithPostInfo { ensure_root_or_admin::(origin)?; let system_ratio = SystemRatio::::get(); let treasury_ratio = TreasuryRatio::::get(); let collator_ratio = CollatorRatio::::get(); - ensure!( - system_ratio + treasury_ratio + ratio + collator_ratio <= Perbill::one(), - Error::::InvalidRatio - ); + let total_ratio = system_ratio + treasury_ratio + ratio + collator_ratio; + log::info!("3 -+-+-+-+-+ set operation ratio, total ratio:{:?}, system_ratio:{:?}, treasury_ratio:{:?}, collator_ratio:{:?}, operation_ratio:{:?}", + total_ratio, system_ratio, treasury_ratio, collator_ratio, ratio); + ensure_total_ratio_not_exceed_one::( + system_ratio, + treasury_ratio, + ratio, + collator_ratio, + )?; OperationRatio::::put(ratio); Self::deposit_event(Event::OperationRatioSet(ratio)); @@ -663,20 +672,22 @@ pub mod pallet { #[pallet::call_index(4)] #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))] - pub fn set_collator_ratio( - origin: OriginFor, - ratio: Perbill, - ) -> DispatchResultWithPostInfo { + pub fn set_collator_ratio(origin: OriginFor, ratio: u32) -> DispatchResultWithPostInfo { crate::pallet::ensure_root_or_admin::(origin)?; - let system_ratio = crate::pallet::SystemRatio::::get(); - let treasury_ratio = crate::pallet::TreasuryRatio::::get(); + let system_ratio = SystemRatio::::get(); + let treasury_ratio = TreasuryRatio::::get(); let operation_ratio = OperationRatio::::get(); - ensure!( - system_ratio + treasury_ratio + ratio + operation_ratio <= Perbill::one(), - Error::::InvalidRatio - ); + let total_ratio = system_ratio + treasury_ratio + ratio + operation_ratio; + log::info!("4. *********** set collator ratio, total ratio:{:?}, system_ratio:{:?}, treasury_ratio:{:?}, operation_ratio:{:?}, collator_ratio:{:?}", + total_ratio, system_ratio, treasury_ratio, operation_ratio, ratio); + ensure_total_ratio_not_exceed_one::( + system_ratio, + treasury_ratio, + operation_ratio, + ratio, + )?; CollatorRatio::::put(ratio); Self::deposit_event(Event::CollatorRatioSet(ratio)); @@ -723,4 +734,15 @@ pub mod pallet { _ => Err(Error::::RequireAdmin.into()), } } + + fn ensure_total_ratio_not_exceed_one( + system_ratio: u32, + treasury_ratio: u32, + operation_ratio: u32, + collator_ratio: u32, + ) -> DispatchResult { + let total_ratio = system_ratio + treasury_ratio + operation_ratio + collator_ratio; + ensure!((total_ratio as u128) <= 100 * PERCENT_UNIT, Error::::InvalidRatio); + Ok(()) + } } diff --git a/pallets/liquidation/src/mock.rs b/pallets/liquidation/src/mock.rs index a4940ce..89cbb1d 100644 --- a/pallets/liquidation/src/mock.rs +++ b/pallets/liquidation/src/mock.rs @@ -308,10 +308,10 @@ where pub struct ExtBuilder { existential_deposit: u128, - system_ratio: Perbill, - treasury_ratio: Perbill, - operation_ratio: Perbill, - collator_ratio: Perbill, + system_ratio: u32, + treasury_ratio: u32, + operation_ratio: u32, + collator_ratio: u32, min_liquidation_threshold: Balance, profit_distribution_cycle: u64, } @@ -320,10 +320,10 @@ impl Default for ExtBuilder { fn default() -> Self { Self { existential_deposit: 1, - system_ratio: Perbill::from_percent(20), - treasury_ratio: Perbill::from_percent(33), - operation_ratio: Perbill::from_percent(25), - collator_ratio: Perbill::from_percent(22), + system_ratio: 20_000_0000, + treasury_ratio: 33_000_0000, + operation_ratio: 25_000_0000, + collator_ratio: 22_000_0000, min_liquidation_threshold: MILLIUNIT * 20, profit_distribution_cycle: 10, } @@ -336,22 +336,22 @@ impl ExtBuilder { self } - pub fn system_ratio(mut self, ratio: Perbill) -> Self { + pub fn system_ratio(mut self, ratio: u32) -> Self { self.system_ratio = ratio; self } - pub fn treasury_ratio(mut self, ratio: Perbill) -> Self { + pub fn treasury_ratio(mut self, ratio: u32) -> Self { self.treasury_ratio = ratio; self } - pub fn operation_ratio(mut self, ratio: Perbill) -> Self { + pub fn operation_ratio(mut self, ratio: u32) -> Self { self.operation_ratio = ratio; self } - pub fn collator_ratio(mut self, ratio: Perbill) -> Self { + pub fn collator_ratio(mut self, ratio: u32) -> Self { self.collator_ratio = ratio; self } diff --git a/pallets/liquidation/src/tests.rs b/pallets/liquidation/src/tests.rs index b88b9ec..9e5b1c8 100644 --- a/pallets/liquidation/src/tests.rs +++ b/pallets/liquidation/src/tests.rs @@ -7,10 +7,10 @@ use sp_runtime::traits::Zero; fn distribute_profit_should_work() { ExtBuilder::default() .existential_deposit(100) - .system_ratio(Perbill::from_percent(20)) - .treasury_ratio(Perbill::from_percent(33)) - .operation_ratio(Perbill::from_percent(25)) - .collator_ratio(Perbill::from_percent(22)) + .system_ratio(20_000_0000) + .treasury_ratio(33_000_0000) + .operation_ratio(25_000_0000) + .collator_ratio(22_000_0000) .min_liquidation_threshold(20_000_000_000) .profit_distribution_cycle(10) .build()