From f83cf9bc4a2b6706e372bf7112f00f5bfbea2ddf Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Mon, 13 May 2024 15:56:50 +0200
Subject: [PATCH] CurrencyAdapter to FungibleAdapter

---
 pallets/duniter-account/src/lib.rs   | 14 ++---
 pallets/oneshot-account/src/lib.rs   | 82 +++++++++++++---------------
 pallets/oneshot-account/src/mock.rs  | 12 ++--
 runtime/common/src/pallets_config.rs | 13 +++--
 runtime/g1/src/lib.rs                |  3 +-
 runtime/gdev/src/lib.rs              |  3 +-
 runtime/gtest/src/lib.rs             |  3 +-
 7 files changed, 61 insertions(+), 69 deletions(-)

diff --git a/pallets/duniter-account/src/lib.rs b/pallets/duniter-account/src/lib.rs
index 346dab976..2e131264a 100644
--- a/pallets/duniter-account/src/lib.rs
+++ b/pallets/duniter-account/src/lib.rs
@@ -29,8 +29,7 @@ pub use types::*;
 pub use weights::WeightInfo;
 
 use frame_support::pallet_prelude::*;
-use frame_support::traits::StoredMap;
-use frame_support::traits::{Currency, StorageVersion};
+use frame_support::traits::{fungible, fungible::Credit, IsSubType, StorageVersion, StoredMap};
 use frame_system::pallet_prelude::*;
 use pallet_quota::traits::RefundFee;
 use pallet_transaction_payment::OnChargeTransaction;
@@ -42,8 +41,8 @@ pub mod pallet {
     use super::*;
     pub type IdtyIdOf<T> = <T as pallet_identity::Config>::IdtyIndex;
     pub type CurrencyOf<T> = pallet_balances::Pallet<T>;
-    pub type BalanceOf<T> =
-        <CurrencyOf<T> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
+    type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
+    pub type BalanceOf<T> = <CurrencyOf<T> as fungible::Inspect<AccountIdOf<T>>>::Balance;
 
     /// The current storage version.
     const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
@@ -290,14 +289,15 @@ where
 // allows pay fees with quota instead of currency if available
 impl<T: Config> OnChargeTransaction<T> for Pallet<T>
 where
+    T::RuntimeCall: IsSubType<Call<T>>,
     T::InnerOnChargeTransaction: OnChargeTransaction<
         T,
-        Balance = <CurrencyOf<T> as Currency<T::AccountId>>::Balance,
-        LiquidityInfo = Option<<CurrencyOf<T> as Currency<T::AccountId>>::NegativeImbalance>,
+        Balance = BalanceOf<T>,
+        LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>,
     >,
 {
     type Balance = BalanceOf<T>;
-    type LiquidityInfo = Option<<CurrencyOf<T> as Currency<T::AccountId>>::NegativeImbalance>;
+    type LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>;
 
     fn withdraw_fee(
         who: &T::AccountId,
diff --git a/pallets/oneshot-account/src/lib.rs b/pallets/oneshot-account/src/lib.rs
index 637debf4d..0d95b4ed5 100644
--- a/pallets/oneshot-account/src/lib.rs
+++ b/pallets/oneshot-account/src/lib.rs
@@ -29,14 +29,20 @@ pub use types::*;
 pub use weights::WeightInfo;
 
 use frame_support::pallet_prelude::*;
+use frame_support::traits::fungible;
 use frame_support::traits::{
-    Currency, ExistenceRequirement, Imbalance, IsSubType, WithdrawReasons,
+    fungible::{Balanced, Credit, Inspect},
+    tokens::{Fortitude, Precision, Preservation},
+    Imbalance, IsSubType,
 };
 use frame_system::pallet_prelude::*;
 use pallet_transaction_payment::OnChargeTransaction;
 use sp_runtime::traits::{DispatchInfoOf, PostDispatchInfoOf, Saturating, StaticLookup, Zero};
 use sp_std::convert::TryInto;
 
+type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
+type BalanceOf<T> = <<T as Config>::Currency as fungible::Inspect<AccountIdOf<T>>>::Balance;
+
 #[frame_support::pallet]
 pub mod pallet {
     use super::*;
@@ -49,7 +55,7 @@ pub mod pallet {
 
     #[pallet::config]
     pub trait Config: frame_system::Config + pallet_transaction_payment::Config {
-        type Currency: Currency<Self::AccountId>;
+        type Currency: fungible::Balanced<Self::AccountId> + fungible::Mutate<Self::AccountId>;
         type InnerOnChargeTransaction: OnChargeTransaction<Self>;
         type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
         /// Type representing the weight of this pallet
@@ -60,13 +66,8 @@ pub mod pallet {
 
     #[pallet::storage]
     #[pallet::getter(fn oneshot_account)]
-    pub type OneshotAccounts<T: Config> = StorageMap<
-        _,
-        Blake2_128Concat,
-        T::AccountId,
-        <T::Currency as Currency<T::AccountId>>::Balance,
-        OptionQuery,
-    >;
+    pub type OneshotAccounts<T: Config> =
+        StorageMap<_, Blake2_128Concat, T::AccountId, BalanceOf<T>, OptionQuery>;
 
     // EVENTS //
 
@@ -77,25 +78,19 @@ pub mod pallet {
         /// A oneshot account was created.
         OneshotAccountCreated {
             account: T::AccountId,
-            balance: <T::Currency as Currency<T::AccountId>>::Balance,
+            balance: BalanceOf<T>,
             creator: T::AccountId,
         },
         /// A oneshot account was consumed.
         OneshotAccountConsumed {
             account: T::AccountId,
-            dest1: (
-                T::AccountId,
-                <T::Currency as Currency<T::AccountId>>::Balance,
-            ),
-            dest2: Option<(
-                T::AccountId,
-                <T::Currency as Currency<T::AccountId>>::Balance,
-            )>,
+            dest1: (T::AccountId, BalanceOf<T>),
+            dest2: Option<(T::AccountId, BalanceOf<T>)>,
         },
         /// A withdrawal was executed on a oneshot account.
         Withdraw {
             account: T::AccountId,
-            balance: <T::Currency as Currency<T::AccountId>>::Balance,
+            balance: BalanceOf<T>,
         },
     }
 
@@ -133,13 +128,13 @@ pub mod pallet {
         pub fn create_oneshot_account(
             origin: OriginFor<T>,
             dest: <T::Lookup as StaticLookup>::Source,
-            #[pallet::compact] value: <T::Currency as Currency<T::AccountId>>::Balance,
+            #[pallet::compact] value: BalanceOf<T>,
         ) -> DispatchResult {
             let transactor = ensure_signed(origin)?;
             let dest = T::Lookup::lookup(dest)?;
 
             ensure!(
-                value >= <T::Currency as Currency<T::AccountId>>::minimum_balance(),
+                value >= T::Currency::minimum_balance(),
                 Error::<T>::ExistentialDeposit
             );
             ensure!(
@@ -147,11 +142,12 @@ pub mod pallet {
                 Error::<T>::OneshotAccountAlreadyCreated
             );
 
-            let _ = <T::Currency as Currency<T::AccountId>>::withdraw(
+            let _ = T::Currency::withdraw(
                 &transactor,
                 value,
-                WithdrawReasons::TRANSFER,
-                ExistenceRequirement::KeepAlive,
+                Precision::Exact,
+                Preservation::Preserve,
+                Fortitude::Polite,
             )?;
             OneshotAccounts::<T>::insert(&dest, value);
             Self::deposit_event(Event::OneshotAccountCreated {
@@ -206,8 +202,9 @@ pub mod pallet {
                     creator: transactor.clone(),
                 });
             } else {
-                let _ =
-                    <T::Currency as Currency<T::AccountId>>::deposit_into_existing(&dest, value)?;
+                if frame_system::Pallet::<T>::providers(&dest) > 0 {
+                    let _ = T::Currency::deposit(&dest, value, Precision::Exact)?;
+                }
             }
             OneshotAccounts::<T>::remove(&transactor);
             Self::deposit_event(Event::OneshotAccountConsumed {
@@ -236,7 +233,7 @@ pub mod pallet {
             block_height: BlockNumberFor<T>,
             dest: Account<<T::Lookup as StaticLookup>::Source>,
             remaining_to: Account<<T::Lookup as StaticLookup>::Source>,
-            #[pallet::compact] balance: <T::Currency as Currency<T::AccountId>>::Balance,
+            #[pallet::compact] balance: BalanceOf<T>,
         ) -> DispatchResult {
             let transactor = ensure_signed(origin)?;
 
@@ -271,12 +268,12 @@ pub mod pallet {
                     Error::<T>::OneshotAccountAlreadyCreated
                 );
                 ensure!(
-                    balance1 >= <T::Currency as Currency<T::AccountId>>::minimum_balance(),
+                    balance1 >= T::Currency::minimum_balance(),
                     Error::<T>::ExistentialDeposit
                 );
             } else {
                 ensure!(
-                    !<T::Currency as Currency<T::AccountId>>::free_balance(&dest1).is_zero(),
+                    !T::Currency::balance(&dest1).is_zero(),
                     Error::<T>::DestAccountNotExist
                 );
             }
@@ -286,7 +283,7 @@ pub mod pallet {
                     Error::<T>::OneshotAccountAlreadyCreated
                 );
                 ensure!(
-                    balance2 >= <T::Currency as Currency<T::AccountId>>::minimum_balance(),
+                    balance2 >= T::Currency::minimum_balance(),
                     Error::<T>::ExistentialDeposit
                 );
                 OneshotAccounts::<T>::insert(&dest2, balance2);
@@ -296,9 +293,9 @@ pub mod pallet {
                     creator: transactor.clone(),
                 });
             } else {
-                let _ = <T::Currency as Currency<T::AccountId>>::deposit_into_existing(
-                    &dest2, balance2,
-                )?;
+                if frame_system::Pallet::<T>::providers(&dest2) > 0 {
+                    let _ = T::Currency::deposit(&dest2, balance2, Precision::Exact)?;
+                }
             }
             if dest1_is_oneshot {
                 OneshotAccounts::<T>::insert(&dest1, balance1);
@@ -308,9 +305,9 @@ pub mod pallet {
                     creator: transactor.clone(),
                 });
             } else {
-                let _ = <T::Currency as Currency<T::AccountId>>::deposit_into_existing(
-                    &dest1, balance1,
-                )?;
+                if frame_system::Pallet::<T>::providers(&dest1) > 0 {
+                    let _ = T::Currency::deposit(&dest1, balance1, Precision::Exact)?;
+                }
             }
             OneshotAccounts::<T>::remove(&transactor);
             Self::deposit_event(Event::OneshotAccountConsumed {
@@ -329,12 +326,12 @@ where
     T::RuntimeCall: IsSubType<Call<T>>,
     T::InnerOnChargeTransaction: OnChargeTransaction<
         T,
-        Balance = <T::Currency as Currency<T::AccountId>>::Balance,
-        LiquidityInfo = Option<<T::Currency as Currency<T::AccountId>>::NegativeImbalance>,
+        Balance = BalanceOf<T>,
+        LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>,
     >,
 {
-    type Balance = <T::Currency as Currency<T::AccountId>>::Balance;
-    type LiquidityInfo = Option<<T::Currency as Currency<T::AccountId>>::NegativeImbalance>;
+    type Balance = BalanceOf<T>;
+    type LiquidityInfo = Option<Credit<T::AccountId, T::Currency>>;
 
     fn withdraw_fee(
         who: &T::AccountId,
@@ -359,10 +356,7 @@ where
                         account: who.clone(),
                         balance: fee,
                     });
-                    // TODO
-                    return Ok(Some(
-                        <T::Currency as Currency<T::AccountId>>::NegativeImbalance::zero(),
-                    ));
+                    return Ok(Some(Imbalance::zero()));
                 }
             }
             Err(TransactionValidityError::Invalid(
diff --git a/pallets/oneshot-account/src/mock.rs b/pallets/oneshot-account/src/mock.rs
index 8e83c61f0..21e66ce35 100644
--- a/pallets/oneshot-account/src/mock.rs
+++ b/pallets/oneshot-account/src/mock.rs
@@ -19,7 +19,7 @@
 use crate::{self as pallet_oneshot_account};
 use frame_support::{parameter_types, traits::Everything, weights::IdentityFee};
 use frame_system as system;
-use pallet_transaction_payment::CurrencyAdapter;
+use pallet_transaction_payment::FungibleAdapter;
 use sp_core::{ConstU32, H256};
 use sp_runtime::{
     traits::{BlakeTwo256, IdentityLookup},
@@ -107,16 +107,16 @@ impl pallet_transaction_payment::Config for Test {
 }
 impl pallet_oneshot_account::Config for Test {
     type Currency = Balances;
-    type InnerOnChargeTransaction = CurrencyAdapter<Balances, HandleFees>;
+    type InnerOnChargeTransaction = FungibleAdapter<Balances, HandleFees>;
+    // TODO
     type RuntimeEvent = RuntimeEvent;
     type WeightInfo = ();
 }
 
 pub struct HandleFees;
-type NegativeImbalance = <Balances as frame_support::traits::Currency<u64>>::NegativeImbalance;
-impl frame_support::traits::OnUnbalanced<NegativeImbalance> for HandleFees {
-    fn on_nonzero_unbalanced(_amount: NegativeImbalance) {}
-}
+type Imbalance =
+    frame_support::traits::fungible::Credit<<Test as frame_system::Config>::AccountId, Balances>;
+impl frame_support::traits::OnUnbalanced<Imbalance> for HandleFees {}
 
 // Build genesis storage according to the mock runtime.
 #[allow(dead_code)]
diff --git a/runtime/common/src/pallets_config.rs b/runtime/common/src/pallets_config.rs
index 09156ab08..e0ebed7d9 100644
--- a/runtime/common/src/pallets_config.rs
+++ b/runtime/common/src/pallets_config.rs
@@ -108,7 +108,7 @@ macro_rules! pallets_config {
             type RuntimeEvent = RuntimeEvent;
             type WeightInfo = common_runtime::weights::pallet_duniter_account::WeightInfo<Runtime>;
             // does currency adapter in any case, but adds "refund with quota" feature
-            type InnerOnChargeTransaction = CurrencyAdapter<Balances, HandleFees>;
+            type InnerOnChargeTransaction = FungibleAdapter<Balances, HandleFees>;
             type Refund = Quota;
         }
 
@@ -184,6 +184,7 @@ type RuntimeFreezeReason = ();
         // Take Dust from Balances and put it in the Treasury pot
         pub struct HandleDust;
         type CreditOf = frame_support::traits::tokens::fungible::Credit<AccountId, Balances>;
+		type NegativeImbalance = <Balances as frame_support::traits::Currency<AccountId>>::NegativeImbalance;
         impl frame_support::traits::OnUnbalanced<CreditOf> for HandleDust {
             fn on_nonzero_unbalanced(amount: CreditOf) {
                 use frame_support::traits::Currency as _;
@@ -195,13 +196,13 @@ type RuntimeFreezeReason = ();
 
         // fees are moved to the treasury
         pub struct HandleFees;
-        type NegativeImbalance = <Balances as frame_support::traits::Currency<AccountId>>::NegativeImbalance;
-        impl frame_support::traits::OnUnbalanced<NegativeImbalance> for HandleFees {
-            fn on_nonzero_unbalanced(amount: NegativeImbalance) {
+        impl frame_support::traits::OnUnbalanced<CreditOf> for HandleFees {
+            fn on_nonzero_unbalanced(amount: CreditOf) {
                 use frame_support::traits::Currency as _;
-
+                use frame_support::traits::Imbalance as _;
                 // fee is moved to treasury
-                Balances::resolve_creating(&Treasury::account_id(), amount);
+                let imbalance = NegativeImbalance::new(amount.peek());
+                Balances::resolve_creating(&Treasury::account_id(), imbalance);
                 // should move the tip to author
                 // if let Some(author) = Authorship::author() {
                 //     Balances::resolve_creating(&author, amount);
diff --git a/runtime/g1/src/lib.rs b/runtime/g1/src/lib.rs
index 8129cbc2e..af09bb120 100644
--- a/runtime/g1/src/lib.rs
+++ b/runtime/g1/src/lib.rs
@@ -14,7 +14,6 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
 
-#![allow(deprecated)] // TODO
 #![cfg_attr(not(feature = "std"), no_std)]
 // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
 #![recursion_limit = "256"]
@@ -40,7 +39,7 @@ pub use pallet_identity::{IdtyStatus, IdtyValue};
 pub use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
 use pallet_session::historical as session_historical;
 pub use pallet_timestamp::Call as TimestampCall;
-use pallet_transaction_payment::CurrencyAdapter;
+use pallet_transaction_payment::FungibleAdapter;
 pub use pallet_universal_dividend;
 #[cfg(any(feature = "std", test))]
 pub use sp_runtime::BuildStorage;
diff --git a/runtime/gdev/src/lib.rs b/runtime/gdev/src/lib.rs
index c662393b4..e80000a74 100644
--- a/runtime/gdev/src/lib.rs
+++ b/runtime/gdev/src/lib.rs
@@ -14,7 +14,6 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
 
-#![allow(deprecated)] // TODO
 #![cfg_attr(not(feature = "std"), no_std)]
 // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
 #![recursion_limit = "256"]
@@ -40,7 +39,7 @@ pub use pallet_duniter_test_parameters::Parameters as GenesisParameters;
 pub use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
 use pallet_session::historical as session_historical;
 pub use pallet_timestamp::Call as TimestampCall;
-use pallet_transaction_payment::CurrencyAdapter;
+use pallet_transaction_payment::FungibleAdapter;
 pub use pallet_universal_dividend;
 #[cfg(any(feature = "std", test))]
 pub use sp_runtime::BuildStorage;
diff --git a/runtime/gtest/src/lib.rs b/runtime/gtest/src/lib.rs
index 385887b0b..9678593d8 100644
--- a/runtime/gtest/src/lib.rs
+++ b/runtime/gtest/src/lib.rs
@@ -14,7 +14,6 @@
 // You should have received a copy of the GNU Affero General Public License
 // along with Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
 
-#![allow(deprecated)] // TODO
 #![cfg_attr(not(feature = "std"), no_std)]
 // `construct_runtime!` does a lot of recursion and requires us to increase the limit to 256.
 #![recursion_limit = "256"]
@@ -39,7 +38,7 @@ pub use pallet_balances::Call as BalancesCall;
 pub use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
 use pallet_session::historical as session_historical;
 pub use pallet_timestamp::Call as TimestampCall;
-use pallet_transaction_payment::CurrencyAdapter;
+use pallet_transaction_payment::FungibleAdapter;
 pub use pallet_universal_dividend;
 #[cfg(any(feature = "std", test))]
 pub use sp_runtime::BuildStorage;
-- 
GitLab