Skip to content

Commit

Permalink
Improve handling unbalanced funds
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Apr 16, 2024
1 parent 1620fdb commit 0c90e23
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
10 changes: 6 additions & 4 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ impl pallet_balances::Config for Runtime {
type ReserveIdentifier = [u8; 8];
/// The type for recording an account's balance.
type Balance = Balance;
type DustRemoval = pallet_pot::OnUnbalancedOverCredit<Self, PotInstanceTreasury>;
type DustRemoval = pallet_pot::DepositUnbalancedFungible<Self, PotInstanceTreasury>;
type ExistentialDeposit = ConstU128<500>;
type AccountStore = System;
type MaxLocks = ConstU32<50>;
Expand Down Expand Up @@ -671,7 +671,7 @@ impl pallet_evm_balances::Config for Runtime {
type Balance = Balance;
type ExistentialDeposit = ConstU128<1>;
type AccountStore = EvmSystem;
type DustRemoval = currency_swap::TreasuryPotProxy;
type DustRemoval = pallet_pot::DepositUnbalancedCurrency<Self, currency_swap::TreasuryPotProxy>;
}

impl pallet_currency_swap::Config for Runtime {
Expand Down Expand Up @@ -706,8 +706,10 @@ impl pallet_evm::Config for Runtime {
type PrecompilesValue = PrecompilesValue;
type ChainId = EthereumChainId;
type BlockGasLimit = BlockGasLimit;
type OnChargeTransaction =
fixed_supply::EvmTransactionCharger<EvmBalances, currency_swap::FeesPotProxy>;
type OnChargeTransaction = fixed_supply::EvmTransactionCharger<
EvmBalances,
pallet_pot::DepositUnbalancedCurrency<Self, currency_swap::FeesPotProxy>,
>;
type OnCreate = ();
type FindAuthor = find_author::FindAuthorTruncated<
find_author::FindAuthorFromSession<find_author::FindAuthorBabe, BabeId>,
Expand Down
34 changes: 22 additions & 12 deletions crates/pallet-pot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::traits::{
fungible::{Balanced, Credit, Inspect},
Imbalance, OnUnbalanced, StorageVersion,
use frame_support::{
pallet_prelude::*,
traits::{
fungible::{Balanced, Credit, Inspect},
Currency, Imbalance, OnUnbalanced, StorageVersion,
},
PalletId,
};
use frame_support::{pallet_prelude::*, traits::Currency, PalletId};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::{AccountIdConversion, CheckedSub, MaybeDisplay, Saturating};

Expand Down Expand Up @@ -197,30 +200,37 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
}
}

impl<T: Config<I>, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>> for Pallet<T, I> {
/// Handle unbalanced funds by depositing them into this pot.
///
/// Implementation for [`Currency`].
pub struct DepositUnbalancedCurrency<T, I>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>>
for DepositUnbalancedCurrency<T, I>
{
fn on_nonzero_unbalanced(amount: NegativeImbalanceOf<T, I>) {
let numeric_amount = amount.peek();

// Must resolve into existing but better to be safe.
T::Currency::resolve_creating(&Self::account_id(), amount);
T::Currency::resolve_creating(&Pallet::<T, I>::account_id(), amount);

Self::deposit_event(Event::Deposit {
Pallet::<T, I>::deposit_event(Event::Deposit {
value: numeric_amount,
});
}
}

/// A helper to handle `OnUnbalanced` implementation over `CreditOf`
/// to avoid conflicting implmentation.
pub struct OnUnbalancedOverCredit<T, I>(Pallet<T, I>);
/// Handle unbalanced funds by depositing them into this pot.
///
/// Implementation for [`Fungible`].
pub struct DepositUnbalancedFungible<T, I>(PhantomData<(T, I)>);

impl<T: Config<I>, I: 'static> OnUnbalanced<CreditOf<T, I>> for OnUnbalancedOverCredit<T, I> {
impl<T: Config<I>, I: 'static> OnUnbalanced<CreditOf<T, I>> for DepositUnbalancedFungible<T, I> {
fn on_nonzero_unbalanced(amount: CreditOf<T, I>) {
let numeric_amount = amount.peek();

// Pot account already exists.
let _ = T::Currency::resolve(&Pallet::<T, I>::account_id(), amount);
T::Currency::done_deposit(&Pallet::<T, I>::account_id(), numeric_amount);

Pallet::<T, I>::deposit_event(Event::Deposit {
value: numeric_amount,
Expand Down

0 comments on commit 0c90e23

Please # to comment.