Skip to content

Commit

Permalink
fix decrease zero value from allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Jul 28, 2024
1 parent 8a69845 commit 79ce20d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 10 additions & 3 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,25 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(T::DbWeight::get().reads(1)))?;
let mut current_allowance = AssetsOf::<T>::allowance(id.clone(), &who, &spender);
let current_allowance = AssetsOf::<T>::allowance(id.clone(), &who, &spender);
let spender = T::Lookup::unlookup(spender);
let id: AssetIdParameterOf<T> = id.into();

current_allowance.saturating_reduce(value);
if value == Zero::zero() {
return Ok(Some(Self::weight_approve(0, 0)).into());
}
// Cancel the aproval and set the new value if `current_allowance` is more than zero
AssetsOf::<T>::cancel_approval(origin.clone(), id.clone(), spender.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
if value.is_zero() {
return Ok(Some(Self::weight_approve(0, 1)).into());
}
AssetsOf::<T>::approve_transfer(origin, id, spender, current_allowance)?;
AssetsOf::<T>::approve_transfer(
origin,
id,
spender,
current_allowance.saturating_sub(value),
)?;
Ok(Some(Self::weight_approve(1, 1)).into())
}
}
Expand Down
1 change: 0 additions & 1 deletion pallets/api/src/fungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use frame_support::{
assert_ok,
traits::fungibles::{approvals::Inspect, metadata::Inspect as MetadataInspect},
};
use sp_runtime::{DispatchError, ModuleError};

const ASSET: u32 = 42;

Expand Down

0 comments on commit 79ce20d

Please sign in to comment.