From d0e85b9c79f8d9c46eeaace26fefbecc42e94d23 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Tue, 16 Apr 2024 14:28:19 +0300 Subject: [PATCH] Improve handling unbalanced funds --- crates/humanode-runtime/src/currency_swap.rs | 4 +-- crates/humanode-runtime/src/lib.rs | 2 +- crates/pallet-pot/src/lib.rs | 34 +++++++++++++------- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/crates/humanode-runtime/src/currency_swap.rs b/crates/humanode-runtime/src/currency_swap.rs index 8298a6f9b..bef19cd33 100644 --- a/crates/humanode-runtime/src/currency_swap.rs +++ b/crates/humanode-runtime/src/currency_swap.rs @@ -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, EvmToNativeSwapBridgePot, >; pub type TreasuryPotProxy = primitives_currency_swap_proxy::SwapUnbalanced< EvmToNativeProxyConfig, - TreasuryPot, + pallet_pot::DepositUnbalancedCurrency, EvmToNativeSwapBridgePot, >; diff --git a/crates/humanode-runtime/src/lib.rs b/crates/humanode-runtime/src/lib.rs index 97718fc8f..0b4f1f278 100644 --- a/crates/humanode-runtime/src/lib.rs +++ b/crates/humanode-runtime/src/lib.rs @@ -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; + type DustRemoval = pallet_pot::DepositUnbalancedFungible; type ExistentialDeposit = ConstU128<500>; type AccountStore = System; type MaxLocks = ConstU32<50>; diff --git a/crates/pallet-pot/src/lib.rs b/crates/pallet-pot/src/lib.rs index d309be84e..07a10950e 100644 --- a/crates/pallet-pot/src/lib.rs +++ b/crates/pallet-pot/src/lib.rs @@ -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}; @@ -197,30 +200,37 @@ impl, I: 'static> Pallet { } } -impl, I: 'static> OnUnbalanced> for Pallet { +/// Handle unbalanced funds by depositing them into this pot. +/// +/// Implementation for [`Currency`]. +pub struct DepositUnbalancedCurrency(PhantomData<(T, I)>); + +impl, I: 'static> OnUnbalanced> + for DepositUnbalancedCurrency +{ fn on_nonzero_unbalanced(amount: NegativeImbalanceOf) { 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::::account_id(), amount); - Self::deposit_event(Event::Deposit { + Pallet::::deposit_event(Event::Deposit { value: numeric_amount, }); } } -/// A helper to handle `OnUnbalanced` implementation over `CreditOf` -/// to avoid conflicting implmentation. -pub struct OnUnbalancedOverCredit(Pallet); +/// Handle unbalanced funds by depositing them into this pot. +/// +/// Implementation for [`Fungible`]. +pub struct DepositUnbalancedFungible(PhantomData<(T, I)>); -impl, I: 'static> OnUnbalanced> for OnUnbalancedOverCredit { +impl, I: 'static> OnUnbalanced> for DepositUnbalancedFungible { fn on_nonzero_unbalanced(amount: CreditOf) { let numeric_amount = amount.peek(); // Pot account already exists. let _ = T::Currency::resolve(&Pallet::::account_id(), amount); - T::Currency::done_deposit(&Pallet::::account_id(), numeric_amount); Pallet::::deposit_event(Event::Deposit { value: numeric_amount,