git.delta.rocks / unique-network / refs/commits / a8f92f7e5279

difftreelog

feat switch `common` from `Currency` trait to `fungible::*` traits

Grigoriy Simonov2023-05-22parent: #3539f20.patch.diff
in: master

2 files changed

modifiedpallets/common/src/benchmarking.rsdiffbeforeafterboth
27 MAX_PROPERTIES_PER_ITEM,27 MAX_PROPERTIES_PER_ITEM,
28};28};
29use frame_support::{29use frame_support::{
30 traits::{Currency, Get},30 traits::{Get, fungible::Balanced, Imbalance, tokens::Precision},
31 pallet_prelude::ConstU32,31 pallet_prelude::ConstU32,
32 BoundedVec,32 BoundedVec,
33};33};
34use core::convert::TryInto;34use core::convert::TryInto;
35use sp_runtime::DispatchError;35use sp_runtime::{DispatchError, traits::Zero};
3636
37const SEED: u32 = 1;37const SEED: u32 = 1;
3838
85 ) -> Result<CollectionId, DispatchError>,85 ) -> Result<CollectionId, DispatchError>,
86 cast: impl FnOnce(CollectionHandle<T>) -> R,86 cast: impl FnOnce(CollectionHandle<T>) -> R,
87) -> Result<R, DispatchError> {87) -> Result<R, DispatchError> {
88 <T as Config>::Currency::deposit_creating(&owner.as_sub(), T::CollectionCreationPrice::get());88 let imbalance = <T as Config>::Currency::deposit(
89 &owner.as_sub(),
90 T::CollectionCreationPrice::get(),
91 Precision::Exact,
92 )?;
93 debug_assert!(imbalance.peek().is_zero());
89 let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();94 let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();
90 let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();95 let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();
91 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();96 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -64,7 +64,11 @@
 use frame_support::{
 	dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo},
 	ensure,
-	traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement},
+	traits::{
+		Get,
+		fungible::{Balanced, Debt, Inspect},
+		tokens::{Imbalance, Precision, Preservation},
+	},
 	dispatch::Pays,
 	transactional, fail,
 };
@@ -85,7 +89,7 @@
 
 pub use pallet::*;
 use sp_core::H160;
-use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
+use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, traits::Zero};
 
 #[cfg(feature = "runtime-benchmarks")]
 pub mod benchmarking;
@@ -424,7 +428,6 @@
 	use super::*;
 	use dispatch::CollectionDispatch;
 	use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion};
-	use frame_support::traits::Currency;
 	use up_data_structs::{TokenId, mapping::TokenAddressMapping};
 	use scale_info::TypeInfo;
 	use weights::WeightInfo;
@@ -440,12 +443,12 @@
 		type RuntimeEvent: IsType<<Self as frame_system::Config>::RuntimeEvent> + From<Event<Self>>;
 
 		/// Handler of accounts and payment.
-		type Currency: Currency<Self::AccountId>;
+		type Currency: Balanced<Self::AccountId> + Inspect<Self::AccountId>;
 
 		/// Set price to create a collection.
 		#[pallet::constant]
 		type CollectionCreationPrice: Get<
-			<<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,
+			<<Self as Config>::Currency as Inspect<Self::AccountId>>::Balance,
 		>;
 
 		/// Dispatcher of operations on collections.
@@ -1112,21 +1115,17 @@
 
 		// Take a (non-refundable) deposit of collection creation
 		{
-			let mut imbalance =
-				<<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();
-			imbalance.subsume(
-				<<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(
-					&T::TreasuryAccountId::get(),
-					T::CollectionCreationPrice::get(),
-				),
-			);
-			<T as Config>::Currency::settle(
-				payer.as_sub(),
-				imbalance,
-				WithdrawReasons::TRANSFER,
-				ExistenceRequirement::KeepAlive,
-			)
-			.map_err(|_| Error::<T>::NotSufficientFounds)?;
+			let mut imbalance = <Debt<T::AccountId, <T as Config>::Currency>>::zero();
+			imbalance.subsume(<T as Config>::Currency::deposit(
+				&T::TreasuryAccountId::get(),
+				T::CollectionCreationPrice::get(),
+				Precision::Exact,
+			)?);
+			let credit =
+				<T as Config>::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve)
+					.map_err(|_| Error::<T>::NotSufficientFounds)?;
+
+			debug_assert!(credit.peek().is_zero())
 		}
 
 		<CreatedCollectionCount<T>>::put(created_count);