@@ -993,9 +993,7 @@ fn use_allowance<S: Storage>(
993
993
) -> StdResult < ( ) > {
994
994
let mut allowance = read_allowance ( storage, owner, spender) ?;
995
995
996
- if allowance. expiration . map ( |ex| ex < env. block . time ) == Some ( true ) && allowance. amount != 0 {
997
- allowance. amount = 0 ;
998
- write_allowance ( storage, owner, spender, allowance) ?;
996
+ if allowance. is_expired_at ( & env. block ) {
999
997
return Err ( insufficient_allowance ( 0 , amount) ) ;
1000
998
}
1001
999
if let Some ( new_allowance) = allowance. amount . checked_sub ( amount) {
@@ -1338,7 +1336,17 @@ fn try_increase_allowance<S: Storage, A: Api, Q: Querier>(
1338
1336
let spender_address = deps. api . canonical_address ( & spender) ?;
1339
1337
1340
1338
let mut allowance = read_allowance ( & deps. storage , & owner_address, & spender_address) ?;
1341
- allowance. amount = allowance. amount . saturating_add ( amount. u128 ( ) ) ;
1339
+
1340
+ // If the previous allowance has expired, reset the allowance.
1341
+ // Without this users can take advantage of an expired allowance given to
1342
+ // them long ago.
1343
+ if allowance. is_expired_at ( & env. block ) {
1344
+ allowance. amount = amount. u128 ( ) ;
1345
+ allowance. expiration = None ;
1346
+ } else {
1347
+ allowance. amount = allowance. amount . saturating_add ( amount. u128 ( ) ) ;
1348
+ }
1349
+
1342
1350
if expiration. is_some ( ) {
1343
1351
allowance. expiration = expiration;
1344
1352
}
@@ -1373,7 +1381,17 @@ fn try_decrease_allowance<S: Storage, A: Api, Q: Querier>(
1373
1381
let spender_address = deps. api . canonical_address ( & spender) ?;
1374
1382
1375
1383
let mut allowance = read_allowance ( & deps. storage , & owner_address, & spender_address) ?;
1376
- allowance. amount = allowance. amount . saturating_sub ( amount. u128 ( ) ) ;
1384
+
1385
+ // If the previous allowance has expired, reset the allowance.
1386
+ // Without this users can take advantage of an expired allowance given to
1387
+ // them long ago.
1388
+ if allowance. is_expired_at ( & env. block ) {
1389
+ allowance. amount = 0 ;
1390
+ allowance. expiration = None ;
1391
+ } else {
1392
+ allowance. amount = allowance. amount . saturating_sub ( amount. u128 ( ) ) ;
1393
+ }
1394
+
1377
1395
if expiration. is_some ( ) {
1378
1396
allowance. expiration = expiration;
1379
1397
}
0 commit comments