Skip to content

Commit

Permalink
Merge branch 'daan/feat-api_fungibles_pallet' into chungquantin/feat-…
Browse files Browse the repository at this point in the history
…transfer-decrease
  • Loading branch information
chungquantin committed Jul 23, 2024
2 parents a3443a0 + 9d2276c commit 3a96036
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pallets/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
std = [
Expand Down
60 changes: 60 additions & 0 deletions pallets/api/src/fungibles/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! Benchmarking setup for pallet-cards

use super::{AccountIdOf, AssetIdOf, Assets, AssetsInstanceOf, BalanceOf, Call, Config, Pallet};
use frame_benchmarking::{account, v2::*};
use frame_support::{
assert_ok,
traits::{
fungibles::{
approvals::{Inspect as ApprovalInspect, Mutate},
Create, Inspect,
},
Currency,
},
};
use frame_system::RawOrigin;
use sp_runtime::traits::Zero;

const SEED: u32 = 1;

#[benchmarks(
where
<pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<<T as frame_system::Config>::AccountId>>::AssetId: Zero,
)]
mod benchmarks {
use super::*;

// The worst case scenario is when the allowance is set to a value which is lower than the
// current allowance.
#[benchmark]
fn approve() -> Result<(), BenchmarkError> {
let asset = AssetIdOf::<T>::zero();
let decreased_value = <BalanceOf<T>>::from(50u32);
let min_balance = <BalanceOf<T>>::from(1u32);
let owner: AccountIdOf<T> = account("Alice", 0, SEED);
let spender: AccountIdOf<T> = account("Bob", 0, SEED);
let value = <BalanceOf<T>>::from(100u32);
T::Currency::make_free_balance_be(&owner, 100u32.into());
assert_ok!(<Assets<T> as Create<AccountIdOf<T>>>::create(
asset.clone().into(),
owner.clone(),
true,
min_balance
));
assert_ok!(<Assets<T> as Mutate<AccountIdOf<T>>>::approve(
asset.clone(),
&owner,
&spender,
value
));

#[extrinsic_call]
_(RawOrigin::Signed(owner.clone()), asset.clone(), spender.clone(), decreased_value);

assert_eq!(Assets::<T>::allowance(asset, &owner, &spender), decreased_value);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
7 changes: 5 additions & 2 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/// consistent API that adheres to standards in the smart contract space.
pub use pallet::*;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -125,14 +127,15 @@ pub mod pallet {
/// # Returns
/// Returns `Ok(())` if successful, or an error if the approval fails.
#[pallet::call_index(2)]
#[pallet::weight(T::DbWeight::get().reads(2) + AssetsWeightInfo::<T>::cancel_approval() + AssetsWeightInfo::<T>::approve_transfer())]
#[pallet::weight(T::DbWeight::get().reads(1) + AssetsWeightInfo::<T>::cancel_approval() + AssetsWeightInfo::<T>::approve_transfer())]
pub fn approve(
origin: OriginFor<T>,
id: AssetIdOf<T>,
spender: AccountIdOf<T>,
mut value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())
// To have the caller pay some fees.
.map_err(|e| e.with_weight(T::DbWeight::get().reads(1)))?;
let current_allowance = Assets::<T>::allowance(id.clone(), &who, &spender);
let spender = T::Lookup::unlookup(spender);
Expand All @@ -146,7 +149,7 @@ pub mod pallet {
value.saturating_reduce(current_allowance);
Assets::<T>::approve_transfer(origin, id, spender, value).map_err(|e| {
e.with_weight(
T::DbWeight::get().reads(2) + AssetsWeightInfo::<T>::approve_transfer(),
T::DbWeight::get().reads(1) + AssetsWeightInfo::<T>::approve_transfer(),
)
})?;
} else {
Expand Down

0 comments on commit 3a96036

Please sign in to comment.