-
Notifications
You must be signed in to change notification settings - Fork 97
Safe ERC20 permit #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ItsNickBarry
wants to merge
11
commits into
master
Choose a base branch
from
safe-erc20-permit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+319
−4
Open
Safe ERC20 permit #324
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cba3351
inherit IERC20 into IERC2612
ItsNickBarry 7dd6660
add SafeERC20.safePermit with front-run tolerance
ItsNickBarry c3632b9
refactor SafeERC20.safePermit to require exact allowance match on revert
ItsNickBarry 980cea9
remove redundant SafeERC20Test helper contract
ItsNickBarry a3fb5b6
remove todo file
ItsNickBarry 9c6b111
Merge branch 'master' into safe-erc20-permit
ItsNickBarry 0798107
add SafeERC20 unit tests
ItsNickBarry 56deb6d
inherit FungibleToken and FungibleTokenMetadata into FungibleTokenPermit
ItsNickBarry 736fd8d
Merge branch 'master' into safe-erc20-permit
ItsNickBarry 7f202e6
Merge branch 'master' into safe-erc20-permit
ItsNickBarry e8aac0c
clarify dev comment
ItsNickBarry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; | ||
| import { deployMockContract } from '@solidstate/library'; | ||
| import { $SafeERC20, $SafeERC20__factory } from '@solidstate/typechain-types'; | ||
| import { expect } from 'chai'; | ||
| import { ethers } from 'hardhat'; | ||
|
|
||
| describe('SafeERC20', () => { | ||
| let instance: $SafeERC20; | ||
| let deployer: SignerWithAddress; | ||
| let receiver: SignerWithAddress; | ||
| let spender: SignerWithAddress; | ||
|
|
||
| beforeEach(async () => { | ||
| [deployer, receiver, spender] = await ethers.getSigners(); | ||
| instance = await new $SafeERC20__factory(deployer).deploy(); | ||
| }); | ||
|
|
||
| describe('#safeTransfer', () => { | ||
| it('transfers tokens', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function transfer(address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.transfer.withArgs(receiver.address, 1n).returns(true); | ||
|
|
||
| await expect(instance.$safeTransfer(mock.address, receiver.address, 1n)) | ||
| .not.to.be.reverted; | ||
| }); | ||
|
|
||
| describe('reverts if', () => { | ||
| it('transfer returns false', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function transfer(address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.transfer.withArgs(receiver.address, 1n).returns(false); | ||
|
|
||
| await expect( | ||
| instance.$safeTransfer(mock.address, receiver.address, 1n), | ||
| ).to.be.revertedWithCustomError(instance, 'SafeERC20__OperationFailed'); | ||
| }); | ||
|
|
||
| it('transfer reverts', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function transfer(address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.transfer | ||
| .withArgs(receiver.address, 1n) | ||
| .revertsWithReason('TRANSFER_FAILED'); | ||
|
|
||
| await expect( | ||
| instance.$safeTransfer(mock.address, receiver.address, 1n), | ||
| ).to.be.revertedWith('TRANSFER_FAILED'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#safeTransferFrom', () => { | ||
| it('transfers tokens', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function transferFrom(address,address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.transferFrom | ||
| .withArgs(deployer.address, receiver.address, 1n) | ||
| .returns(true); | ||
|
|
||
| await expect( | ||
| instance.$safeTransferFrom( | ||
| mock.address, | ||
| deployer.address, | ||
| receiver.address, | ||
| 1n, | ||
| ), | ||
| ).not.to.be.reverted; | ||
| }); | ||
|
|
||
| describe('reverts if', () => { | ||
| it('transferFrom returns false', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function transferFrom(address,address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.transferFrom | ||
| .withArgs(deployer.address, receiver.address, 1n) | ||
| .returns(false); | ||
|
|
||
| await expect( | ||
| instance.$safeTransferFrom( | ||
| mock.address, | ||
| deployer.address, | ||
| receiver.address, | ||
| 1n, | ||
| ), | ||
| ).to.be.revertedWithCustomError(instance, 'SafeERC20__OperationFailed'); | ||
| }); | ||
|
|
||
| it('transferFrom reverts', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function transferFrom(address,address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.transferFrom | ||
| .withArgs(deployer.address, receiver.address, 1n) | ||
| .revertsWithReason('TRANSFER_FROM_FAILED'); | ||
|
|
||
| await expect( | ||
| instance.$safeTransferFrom( | ||
| mock.address, | ||
| deployer.address, | ||
| receiver.address, | ||
| 1n, | ||
| ), | ||
| ).to.be.revertedWith('TRANSFER_FROM_FAILED'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#safeApprove', () => { | ||
| it('approves spender', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function approve(address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.approve.withArgs(spender.address, 1n).returns(true); | ||
|
|
||
| await expect(instance.$safeApprove(mock.address, spender.address, 1n)).not | ||
| .to.be.reverted; | ||
| }); | ||
|
|
||
| describe('reverts if', () => { | ||
| it('approve returns false', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function approve(address,uint256) external returns (bool)', | ||
| ]); | ||
|
|
||
| await mock.mock.approve.withArgs(spender.address, 1n).returns(false); | ||
|
|
||
| await expect( | ||
| instance.$safeApprove(mock.address, spender.address, 1n), | ||
| ).to.be.revertedWithCustomError(instance, 'SafeERC20__OperationFailed'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#safeIncreaseAllowance', () => { | ||
| it('todo'); | ||
| }); | ||
|
|
||
| describe('#safeDecreaseAllowance', () => { | ||
| it('todo'); | ||
| }); | ||
|
|
||
| describe('#safePermit', () => { | ||
| it('executes permit', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function permit(address,address,uint256,uint256,uint8,bytes32,bytes32) external', | ||
| ]); | ||
|
|
||
| await mock.mock.permit.returns(); | ||
|
|
||
| await expect( | ||
| instance.$safePermit( | ||
| mock.address, | ||
| deployer.address, | ||
| spender.address, | ||
| 1n, | ||
| ethers.MaxUint256, | ||
| 27, | ||
| ethers.ZeroHash, | ||
| ethers.ZeroHash, | ||
| ), | ||
| ).not.to.be.reverted; | ||
| }); | ||
|
|
||
| it('tolerates front-run', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function permit(address,address,uint256,uint256,uint8,bytes32,bytes32) external', | ||
| 'function allowance(address,address) external view returns (uint256)', | ||
| ]); | ||
|
|
||
| await mock.mock.permit.reverts(); | ||
| await mock.mock.allowance | ||
| .withArgs(deployer.address, spender.address) | ||
| .returns(1n); | ||
|
|
||
| await expect( | ||
| instance.$safePermit( | ||
| mock.address, | ||
| deployer.address, | ||
| spender.address, | ||
| 1n, | ||
| ethers.MaxUint256, | ||
| 27, | ||
| ethers.ZeroHash, | ||
| ethers.ZeroHash, | ||
| ), | ||
| ).not.to.be.reverted; | ||
| }); | ||
|
|
||
| describe('reverts if', () => { | ||
| it('permit fails and allowance is insufficient', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function permit(address,address,uint256,uint256,uint8,bytes32,bytes32) external', | ||
| 'function allowance(address,address) external view returns (uint256)', | ||
| ]); | ||
|
|
||
| await mock.mock.permit.reverts(); | ||
| await mock.mock.allowance | ||
| .withArgs(deployer.address, spender.address) | ||
| .returns(0n); | ||
|
|
||
| await expect( | ||
| instance.$safePermit( | ||
| mock.address, | ||
| deployer.address, | ||
| spender.address, | ||
| 1n, | ||
| ethers.MaxUint256, | ||
| 27, | ||
| ethers.ZeroHash, | ||
| ethers.ZeroHash, | ||
| ), | ||
| ).to.be.revertedWithCustomError(instance, 'SafeERC20__PermitFailed'); | ||
| }); | ||
|
|
||
| it('permit fails and allowance is excessive', async () => { | ||
| const mock = await deployMockContract(deployer, [ | ||
| 'function permit(address,address,uint256,uint256,uint8,bytes32,bytes32) external', | ||
| 'function allowance(address,address) external view returns (uint256)', | ||
| ]); | ||
|
|
||
| await mock.mock.permit.reverts(); | ||
| await mock.mock.allowance | ||
| .withArgs(deployer.address, spender.address) | ||
| .returns(2n); | ||
|
|
||
| await expect( | ||
| instance.$safePermit( | ||
| mock.address, | ||
| deployer.address, | ||
| spender.address, | ||
| 1n, | ||
| ethers.MaxUint256, | ||
| 27, | ||
| ethers.ZeroHash, | ||
| ethers.ZeroHash, | ||
| ), | ||
| ).to.be.revertedWithCustomError(instance, 'SafeERC20__PermitFailed'); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calls can succeed without executing a permit, bypassing this check. Many tokens have non-reverting fallbacks, including WETH9. A
callto an address with no code will also returnsuccess == true.Would it make sense to check the allowance unconditionally? For example:
The downside is an extra allowance call on successful permits.