Skip to content

Commit

Permalink
chore: save gas for amount=0
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Nov 7, 2023
1 parent c08207d commit 9851b65
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
12 changes: 12 additions & 0 deletions pallets/dnt-fee-controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ pub mod pallet {
conversion_rate: (U256, U256),
amount: U256,
) -> Result<(), Self::Error> {
if amount.is_zero() {
return Ok(());
}
let fee_vault = FeeVaultPrecompileAddressStorage::<T>::get().unwrap();
let mapped_amount = amount
.saturating_mul(conversion_rate.0)
Expand All @@ -124,6 +127,11 @@ pub mod pallet {
actual_amount: U256,
) -> Result<(), Self::Error> {
let over_fee = paid_amount.saturating_sub(actual_amount);

if over_fee.is_zero() {
return Ok(());
}

let mapped_amount = over_fee
.saturating_mul(conversion_rate.0)
.div_mod(conversion_rate.1)
Expand All @@ -144,6 +152,10 @@ pub mod pallet {
validator: H160,
to: Option<H160>,
) -> Result<(U256, U256), Self::Error> {
if actual_amount.is_zero() {
return Ok((0.into(), 0.into()));
}

let fee_in_user_token = actual_amount
.saturating_mul(conversion_rate.0)
.div_mod(conversion_rate.1)
Expand Down
45 changes: 26 additions & 19 deletions pallets/fee-rewards-vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,19 @@ pub mod pallet {
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config {
}
pub trait Config: frame_system::Config {}

// double map
#[pallet::storage]
#[pallet::getter(fn claimable_reward)]
pub(super) type ClaimableReward<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
H160,
Twox64Concat,
H160,
U256,
ValueQuery,
>;
pub(super) type ClaimableReward<T: Config> =
StorageDoubleMap<_, Twox64Concat, H160, Twox64Concat, H160, U256, ValueQuery>;

// map
#[pallet::storage]
#[pallet::getter(fn whitelist)]
pub(super) type Whitelist<T: Config> = StorageMap<_, Twox64Concat, H160, bool, ValueQuery>;


impl<T: Config> Pallet<T> {
pub fn is_whitelisted(address: H160) -> bool {
Self::whitelist(address)
Expand All @@ -57,24 +48,40 @@ pub mod pallet {
Self::claimable_reward(address, token)
}

pub fn add_claimable_reward(address: H160, token: H160, amount: U256) -> Result<(), &'static str> {
pub fn add_claimable_reward(
address: H160,
token: H160,
amount: U256,
) -> Result<(), &'static str> {
if amount.is_zero() {
return Ok(());
}
let current_amount = Self::claimable_reward(address, token);

let new_amount = current_amount.checked_add(amount)
let new_amount = current_amount
.checked_add(amount)
.ok_or("Overflow adding a new claimable reward")?;

ClaimableReward::<T>::insert(address, token, new_amount);
Ok(())
}

pub fn sub_claimable_reward(address: H160, token: H160, amount: U256)-> Result<(), &'static str> {

pub fn sub_claimable_reward(
address: H160,
token: H160,
amount: U256,
) -> Result<(), &'static str> {
if amount.is_zero() {
return Ok(());
}
let current_amount = Self::claimable_reward(address, token);

let new_amount = current_amount.checked_sub(amount)
let new_amount = current_amount
.checked_sub(amount)
.ok_or("Insufficient balance")?;

ClaimableReward::<T>::insert(address, token, new_amount);
Ok(())
}
}
}
}
8 changes: 8 additions & 0 deletions pallets/sponsored-transactions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ pub mod pallet {
}

fn ensure_sponsor_balance(sponsor: H160, token: H160, amount: U256) -> Result<(), ()> {
if amount.is_zero() {
return Ok(());
}

let balance = T::ERC20Manager::balance_of(token.clone(), sponsor.clone());
if balance >= amount {
Ok(())
Expand All @@ -307,6 +311,10 @@ pub mod pallet {
payee: &H160,
amount: U256,
) -> Result<(), ()> {
if amount.is_zero() {
return Ok(());
}

let actual_amount = amount
.saturating_mul(conversion_rate.0)
.div_mod(conversion_rate.1)
Expand Down

0 comments on commit 9851b65

Please sign in to comment.