Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,9 +993,7 @@ fn use_allowance<S: Storage>(
) -> StdResult<()> {
let mut allowance = read_allowance(storage, owner, spender)?;

if allowance.expiration.map(|ex| ex < env.block.time) == Some(true) && allowance.amount != 0 {
allowance.amount = 0;
write_allowance(storage, owner, spender, allowance)?;
if allowance.is_expired_at(&env.block) {
return Err(insufficient_allowance(0, amount));
Comment on lines 995 to 997
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer: This code segment was expensive and unnecessary. There's no need to actively set the amount to 0 if the allowance query would make it clear to consumers that the allowance has expired anyway (so the amount isn't important)

}
if let Some(new_allowance) = allowance.amount.checked_sub(amount) {
Expand Down Expand Up @@ -1338,7 +1336,17 @@ fn try_increase_allowance<S: Storage, A: Api, Q: Querier>(
let spender_address = deps.api.canonical_address(&spender)?;

let mut allowance = read_allowance(&deps.storage, &owner_address, &spender_address)?;
allowance.amount = allowance.amount.saturating_add(amount.u128());

// If the previous allowance has expired, reset the allowance.
// Without this users can take advantage of an expired allowance given to
// them long ago.
if allowance.is_expired_at(&env.block) {
allowance.amount = amount.u128();
allowance.expiration = None;
} else {
allowance.amount = allowance.amount.saturating_add(amount.u128());
}

if expiration.is_some() {
allowance.expiration = expiration;
}
Expand Down Expand Up @@ -1373,7 +1381,17 @@ fn try_decrease_allowance<S: Storage, A: Api, Q: Querier>(
let spender_address = deps.api.canonical_address(&spender)?;

let mut allowance = read_allowance(&deps.storage, &owner_address, &spender_address)?;
allowance.amount = allowance.amount.saturating_sub(amount.u128());

// If the previous allowance has expired, reset the allowance.
// Without this users can take advantage of an expired allowance given to
// them long ago.
if allowance.is_expired_at(&env.block) {
allowance.amount = 0;
allowance.expiration = None;
} else {
allowance.amount = allowance.amount.saturating_sub(amount.u128());
}

if expiration.is_some() {
allowance.expiration = expiration;
}
Expand Down
9 changes: 9 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ pub struct Allowance {
pub expiration: Option<u64>,
}

impl Allowance {
pub fn is_expired_at(&self, block: &cosmwasm_std::BlockInfo) -> bool {
match self.expiration {
Some(time) => block.time >= time,
None => false, // allowance has no expiration
}
}
}

pub fn read_allowance<S: Storage>(
store: &S,
owner: &CanonicalAddr,
Expand Down