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 d0e85b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/humanode-runtime/src/currency_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ impl primitives_currency_swap_proxy::Config for EvmToNativeProxyConfig {

pub type FeesPotProxy = primitives_currency_swap_proxy::SwapUnbalanced<
EvmToNativeProxyConfig,
FeesPot,
pallet_pot::DepositUnbalancedCurrency<Self, FeesPot>,
EvmToNativeSwapBridgePot,
>;

pub type TreasuryPotProxy = primitives_currency_swap_proxy::SwapUnbalanced<
EvmToNativeProxyConfig,
TreasuryPot,
pallet_pot::DepositUnbalancedCurrency<Self, TreasuryPot>,
EvmToNativeSwapBridgePot,
>;
2 changes: 1 addition & 1 deletion 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
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`].

Check failure on line 225 in crates/pallet-pot/src/lib.rs

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04 / doc

unresolved link to `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 d0e85b9

Please sign in to comment.