From 79ce20d453bd65177edb096a7aeac4ee0083c628 Mon Sep 17 00:00:00 2001 From: tin-snowflake <56880684+chungquantin@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:22:15 +0800 Subject: [PATCH] fix decrease zero value from allowance --- pallets/api/src/fungibles/mod.rs | 13 ++++++++++--- pallets/api/src/fungibles/tests.rs | 1 - 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 214ccd69..2804e7af 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -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::::allowance(id.clone(), &who, &spender); + let current_allowance = AssetsOf::::allowance(id.clone(), &who, &spender); let spender = T::Lookup::unlookup(spender); let id: AssetIdParameterOf = 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::::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::::approve_transfer(origin, id, spender, current_allowance)?; + AssetsOf::::approve_transfer( + origin, + id, + spender, + current_allowance.saturating_sub(value), + )?; Ok(Some(Self::weight_approve(1, 1)).into()) } } diff --git a/pallets/api/src/fungibles/tests.rs b/pallets/api/src/fungibles/tests.rs index f2f4a4ba..5a4f2bc7 100644 --- a/pallets/api/src/fungibles/tests.rs +++ b/pallets/api/src/fungibles/tests.rs @@ -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;