Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
use forceApprove in safeIncreaseAllowance and safeDecreaseAllowance
  • Loading branch information
Amxx committed May 17, 2023
commit a3b6035f89199c7f79de990fe8e57886a94cf45e
4 changes: 2 additions & 2 deletions contracts/token/ERC20/utils/SafeERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ library SafeERC20 {
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
forceApprove(token, spender, oldAllowance + value);
}

/**
Expand All @@ -70,7 +70,7 @@ library SafeERC20 {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
forceApprove(token, spender, oldAllowance - value);
}
}

Expand Down
13 changes: 8 additions & 5 deletions test/token/ERC20/utils/SafeERC20.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,20 @@ contract('SafeERC20', function (accounts) {
it('safeApprove can update approval to zero', async function () {
await this.mock.$safeApprove(this.token.address, spender, 0);
});

it('safeApprove can increase approval', async function () {
await expectRevert(this.mock.$safeIncreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');

it('safeIncreaseAllowance works', async function () {
this.mock.$safeIncreaseAllowance(this.token.address, spender, 10);
expect(this.token.allowance(this.mock.address, spender, 90));
});

it('safeApprove can decrease approval', async function () {
await expectRevert(this.mock.$safeDecreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
it('safeDecreaseAllowance works', async function () {
this.mock.$safeDecreaseAllowance(this.token.address, spender, 10);
expect(this.token.allowance(this.mock.address, spender, 110));
});

it('forceApprove works', async function () {
await this.mock.$forceApprove(this.token.address, spender, 200);
expect(this.token.allowance(this.mock.address, spender, 200));
});
});
});
Expand Down