From 56de7716796b4c2aa230a77915d3f6a7ac530a44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Enrique=20Mu=C3=B1oz=20Mart=C3=ADn?= Date: Tue, 16 Apr 2024 20:28:35 +0200 Subject: [PATCH] fix existential deposit with block-rewards (#1802) --- pallets/block-rewards/src/lib.rs | 18 +++++++++++------- pallets/block-rewards/src/mock.rs | 30 +++++++++++++----------------- pallets/block-rewards/src/tests.rs | 19 ++++++++----------- runtime/altair/src/lib.rs | 1 - runtime/centrifuge/src/lib.rs | 1 - runtime/common/src/lib.rs | 7 ++++++- runtime/development/src/lib.rs | 3 +-- 7 files changed, 39 insertions(+), 40 deletions(-) diff --git a/pallets/block-rewards/src/lib.rs b/pallets/block-rewards/src/lib.rs index ca63a06e08..8970b65500 100644 --- a/pallets/block-rewards/src/lib.rs +++ b/pallets/block-rewards/src/lib.rs @@ -50,7 +50,7 @@ use frame_support::{ storage::transactional, traits::{ fungible::{Inspect as FungibleInspect, Mutate as FungibleMutate}, - fungibles::Mutate, + fungibles::{Inspect, Mutate}, tokens::{Balance, Fortitude, Precision}, OneSessionHandler, }, @@ -132,9 +132,6 @@ pub mod pallet { /// Type used to handle balances. type Balance: Balance + MaxEncodedLen + FixedPointOperand + MaybeSerializeDeserialize; - #[pallet::constant] - type ExistentialDeposit: Get; - /// Type used to handle group weights. type Weight: Parameter + MaxEncodedLen + EnsureAdd + Unsigned + FixedPointOperand + Default; @@ -147,8 +144,12 @@ pub mod pallet { > + CurrencyGroupChange::CurrencyId>; /// The type used to handle currency minting and burning for collators. - type Tokens: Mutate::CurrencyId, Balance = Self::Balance> - + FungibleMutate + type Tokens: Mutate + + Inspect< + Self::AccountId, + AssetId = ::CurrencyId, + Balance = Self::Balance, + > + FungibleMutate + FungibleInspect; /// The currency type of the artificial block rewards currency. @@ -314,10 +315,13 @@ impl Pallet { /// * deposit_stake (4 reads, 4 writes): Currency, Group, StakeAccount, /// Account pub(crate) fn do_init_collator(who: &T::AccountId) -> DispatchResult { + let existential_deposit = + >::minimum_balance(T::StakeCurrencyId::get()); + >::mint_into( T::StakeCurrencyId::get(), who, - T::StakeAmount::get().saturating_add(T::ExistentialDeposit::get()), + T::StakeAmount::get().saturating_add(existential_deposit), )?; T::Rewards::deposit_stake(T::StakeCurrencyId::get(), who, T::StakeAmount::get()) } diff --git a/pallets/block-rewards/src/mock.rs b/pallets/block-rewards/src/mock.rs index 90a8dfb4bb..5d46c5fef9 100644 --- a/pallets/block-rewards/src/mock.rs +++ b/pallets/block-rewards/src/mock.rs @@ -5,7 +5,10 @@ use cfg_types::{ }; use frame_support::{ derive_impl, parameter_types, - traits::{fungibles::Inspect, tokens::WithdrawConsequence, ConstU32, OnFinalize, OnInitialize}, + traits::{ + fungibles::Inspect, tokens::WithdrawConsequence, ConstU128, ConstU32, OnFinalize, + OnInitialize, + }, PalletId, }; use frame_system::EnsureRoot; @@ -81,26 +84,24 @@ impl pallet_session::Config for Test { type WeightInfo = (); } -parameter_types! { - // the minimum fee for an anchor is 500,000ths of a CFG. - // This is set to a value so you can still get some return without getting your account removed. - pub const ExistentialDeposit: Balance = 1 * cfg_primitives::MICRO_CFG; -} +pub const BALANCE_ED: Balance = 23; +pub const REWARD_CURRENCY_ED: Balance = 42; #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)] impl pallet_balances::Config for Test { type AccountStore = System; type Balance = Balance; type DustRemoval = (); - type ExistentialDeposit = ExistentialDeposit; + type ExistentialDeposit = ConstU128; type RuntimeHoldReason = (); } orml_traits::parameter_type_with_key! { pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance { match currency_id { - CurrencyId::Native => ExistentialDeposit::get(), - _ => 1, + CurrencyId::Native => BALANCE_ED, + CurrencyId::Staking(BlockRewardsCurrency) => REWARD_CURRENCY_ED, + _ => unreachable!() } }; } @@ -188,7 +189,6 @@ impl pallet_block_rewards::Config for Test { type AuthorityId = UintAuthorityId; type Balance = Balance; type CurrencyId = CurrencyId; - type ExistentialDeposit = ExistentialDeposit; type MaxCollators = MaxCollators; type Rate = Rate; type Rewards = Rewards; @@ -207,13 +207,13 @@ pub(crate) fn assert_staked(who: &AccountId) { assert_eq!( // NOTE: This is now the ED instead of 0, as we collators need ED now. Tokens::balance(BlockRewardCurrency::get(), who), - ExistentialDeposit::get() + REWARD_CURRENCY_ED ); assert_eq!( ::Tokens::can_withdraw( ::StakeCurrencyId::get(), who, - ExistentialDeposit::get() * 2 + REWARD_CURRENCY_ED * 2 ), WithdrawConsequence::BalanceLow ); @@ -229,11 +229,7 @@ pub(crate) fn assert_not_staked(who: &AccountId, was_before: bool) { ::Tokens::balance(::StakeCurrencyId::get(), who), // NOTE: IF a collator has been staked before the system already granted them ED // of `StakeCurrency`. - if was_before { - ExistentialDeposit::get() - } else { - 0 - } + if was_before { REWARD_CURRENCY_ED } else { 0 } ); } diff --git a/pallets/block-rewards/src/tests.rs b/pallets/block-rewards/src/tests.rs index ad7e8df2c3..dccb4bfcc4 100644 --- a/pallets/block-rewards/src/tests.rs +++ b/pallets/block-rewards/src/tests.rs @@ -1,4 +1,4 @@ -use cfg_primitives::{CFG, SECONDS_PER_YEAR}; +use cfg_primitives::SECONDS_PER_YEAR; use cfg_types::{ fixed_point::Rate, tokens::{CurrencyId, StakingCurrency}, @@ -13,7 +13,7 @@ use crate::mock::*; // The Reward amount // NOTE: This value needs to be > ExistentialDeposit, otherwise the tests will // fail as it's not allowed to transfer a value below the ED threshold. -const REWARD: u128 = 100 * CFG + ExistentialDeposit::get(); +const REWARD: u128 = 100 * BALANCE_ED; #[test] fn check_special_privileges() { @@ -105,7 +105,7 @@ fn joining_leaving_collators() { >::total_issuance(CurrencyId::Staking( StakingCurrency::BlockRewards )), - ::StakeAmount::get() as u128 + ExistentialDeposit::get() + ::StakeAmount::get() as u128 + REWARD_CURRENCY_ED ); advance_session(); @@ -126,7 +126,7 @@ fn joining_leaving_collators() { >::total_issuance(CurrencyId::Staking( StakingCurrency::BlockRewards )), - ::StakeAmount::get() as u128 + ExistentialDeposit::get() + ::StakeAmount::get() as u128 + REWARD_CURRENCY_ED ); advance_session(); @@ -149,7 +149,7 @@ fn joining_leaving_collators() { >::total_issuance(CurrencyId::Staking( StakingCurrency::BlockRewards )), - 2 * ::StakeAmount::get() as u128 + 3 * ExistentialDeposit::get() + 2 * ::StakeAmount::get() as u128 + 3 * REWARD_CURRENCY_ED ); advance_session(); @@ -173,7 +173,7 @@ fn joining_leaving_collators() { >::total_issuance(CurrencyId::Staking( StakingCurrency::BlockRewards )), - 3 * ::StakeAmount::get() as u128 + 5 * ExistentialDeposit::get() + 3 * ::StakeAmount::get() as u128 + 5 * REWARD_CURRENCY_ED ); }); } @@ -223,10 +223,7 @@ fn single_claim_reward() { Balances::total_balance(&TreasuryPalletId::get().into_account_truncating()), 0 ); - assert_eq!( - Balances::total_issuance(), - REWARD + ExistentialDeposit::get() - ); + assert_eq!(Balances::total_issuance(), REWARD + BALANCE_ED); assert_eq!(Balances::free_balance(&1), REWARD); }); } @@ -244,7 +241,7 @@ fn collator_rewards_greater_than_remainder() { Balances::free_balance(&TreasuryPalletId::get().into_account_truncating()); // EPOCH 0 -> EPOCH - let total_issuance = ExistentialDeposit::get(); + let total_issuance = BALANCE_ED; assert_eq!(Balances::total_issuance(), total_issuance); MockTime::mock_now(|| SECONDS_PER_YEAR * 1000); advance_session(); diff --git a/runtime/altair/src/lib.rs b/runtime/altair/src/lib.rs index 69a540163a..789ae4a6e8 100644 --- a/runtime/altair/src/lib.rs +++ b/runtime/altair/src/lib.rs @@ -1240,7 +1240,6 @@ impl pallet_block_rewards::Config for Runtime { type AuthorityId = AuraId; type Balance = Balance; type CurrencyId = CurrencyId; - type ExistentialDeposit = ExistentialDeposit; type MaxCollators = MaxAuthorities; type Rate = Rate; type Rewards = BlockRewardsBase; diff --git a/runtime/centrifuge/src/lib.rs b/runtime/centrifuge/src/lib.rs index 722a5174be..31f5b97400 100644 --- a/runtime/centrifuge/src/lib.rs +++ b/runtime/centrifuge/src/lib.rs @@ -1254,7 +1254,6 @@ impl pallet_block_rewards::Config for Runtime { type AuthorityId = AuraId; type Balance = Balance; type CurrencyId = CurrencyId; - type ExistentialDeposit = ExistentialDeposit; type MaxCollators = MaxAuthorities; type Rate = Rate; type Rewards = BlockRewardsBase; diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 36849be304..aa542820bb 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -28,7 +28,11 @@ pub mod transfer_filter; pub mod xcm; use cfg_primitives::Balance; -use cfg_types::{fee_keys::FeeKey, pools::PoolNav, tokens::CurrencyId}; +use cfg_types::{ + fee_keys::FeeKey, + pools::PoolNav, + tokens::{CurrencyId, StakingCurrency}, +}; use orml_traits::GetByKey; use pallet_loans::entities::input::PriceCollectionInput; use pallet_pool_system::Nav; @@ -81,6 +85,7 @@ where fn get(currency_id: &CurrencyId) -> Balance { match currency_id { CurrencyId::Native => T::ExistentialDeposit::get(), + CurrencyId::Staking(StakingCurrency::BlockRewards) => T::ExistentialDeposit::get(), currency_id => orml_asset_registry::Pallet::::metadata(currency_id) .map(|metadata| metadata.existential_deposit) .unwrap_or_default(), diff --git a/runtime/development/src/lib.rs b/runtime/development/src/lib.rs index 7bb2cde05f..bb17b96c84 100644 --- a/runtime/development/src/lib.rs +++ b/runtime/development/src/lib.rs @@ -1247,7 +1247,7 @@ type CollatorSelectionUpdateOrigin = EitherOfDiverse< // Implement Collator Selection pallet configuration trait for the runtime impl pallet_collator_selection::Config for Runtime { - type Currency = Tokens; + type Currency = Balances; // should be a multiple of session or things will get inconsistent type KickThreshold = Period; type MaxCandidates = MaxCandidates; @@ -1738,7 +1738,6 @@ impl pallet_block_rewards::Config for Runtime { type AuthorityId = AuraId; type Balance = Balance; type CurrencyId = CurrencyId; - type ExistentialDeposit = ExistentialDeposit; type MaxCollators = MaxAuthorities; type Rate = Rate; type Rewards = BlockRewardsBase;