-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add no-return-data ERC20 support to SafeERC20. (#1655)
* Add no-return-data ERC20 support to SafeERC20. * Add changelog entry. * Replace abi.encodeWithSignature for encodeWithSelector. * Remove SafeERC20 test code duplication. * Replace assembly for abi.decode. * Fix linter errors.
- Loading branch information
Showing
4 changed files
with
136 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,91 +1,110 @@ | ||
const { shouldFail } = require('openzeppelin-test-helpers'); | ||
|
||
const SafeERC20Helper = artifacts.require('SafeERC20Helper'); | ||
const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock'); | ||
const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock'); | ||
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock'); | ||
const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper'); | ||
|
||
contract('SafeERC20', function () { | ||
beforeEach(async function () { | ||
this.helper = await SafeERC20Helper.new(); | ||
}); | ||
|
||
describe('with token that returns false on all calls', function () { | ||
beforeEach(async function () { | ||
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address); | ||
}); | ||
|
||
it('reverts on transfer', async function () { | ||
await shouldFail.reverting(this.helper.doFailingTransfer()); | ||
await shouldFail.reverting(this.wrapper.transfer()); | ||
}); | ||
|
||
it('reverts on transferFrom', async function () { | ||
await shouldFail.reverting(this.helper.doFailingTransferFrom()); | ||
await shouldFail.reverting(this.wrapper.transferFrom()); | ||
}); | ||
|
||
it('reverts on approve', async function () { | ||
await shouldFail.reverting(this.helper.doFailingApprove()); | ||
await shouldFail.reverting(this.wrapper.approve(0)); | ||
}); | ||
|
||
it('reverts on increaseAllowance', async function () { | ||
await shouldFail.reverting(this.helper.doFailingIncreaseAllowance()); | ||
await shouldFail.reverting(this.wrapper.increaseAllowance(0)); | ||
}); | ||
|
||
it('reverts on decreaseAllowance', async function () { | ||
await shouldFail.reverting(this.helper.doFailingDecreaseAllowance()); | ||
await shouldFail.reverting(this.wrapper.decreaseAllowance(0)); | ||
}); | ||
}); | ||
|
||
describe('with token that returns true on all calls', function () { | ||
it('doesn\'t revert on transfer', async function () { | ||
await this.helper.doSucceedingTransfer(); | ||
beforeEach(async function () { | ||
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address); | ||
}); | ||
|
||
it('doesn\'t revert on transferFrom', async function () { | ||
await this.helper.doSucceedingTransferFrom(); | ||
shouldOnlyRevertOnErrors(); | ||
}); | ||
|
||
describe('with token that returns no boolean values', function () { | ||
beforeEach(async function () { | ||
this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address); | ||
}); | ||
|
||
describe('approvals', function () { | ||
context('with zero allowance', function () { | ||
beforeEach(async function () { | ||
await this.helper.setAllowance(0); | ||
}); | ||
shouldOnlyRevertOnErrors(); | ||
}); | ||
}); | ||
|
||
function shouldOnlyRevertOnErrors () { | ||
it('doesn\'t revert on transfer', async function () { | ||
await this.wrapper.transfer(); | ||
}); | ||
|
||
it('doesn\'t revert on transferFrom', async function () { | ||
await this.wrapper.transferFrom(); | ||
}); | ||
|
||
describe('approvals', function () { | ||
context('with zero allowance', function () { | ||
beforeEach(async function () { | ||
await this.wrapper.setAllowance(0); | ||
}); | ||
|
||
it('doesn\'t revert when approving a non-zero allowance', async function () { | ||
await this.helper.doSucceedingApprove(100); | ||
}); | ||
it('doesn\'t revert when approving a non-zero allowance', async function () { | ||
await this.wrapper.approve(100); | ||
}); | ||
|
||
it('doesn\'t revert when approving a zero allowance', async function () { | ||
await this.helper.doSucceedingApprove(0); | ||
}); | ||
it('doesn\'t revert when approving a zero allowance', async function () { | ||
await this.wrapper.approve(0); | ||
}); | ||
|
||
it('doesn\'t revert when increasing the allowance', async function () { | ||
await this.helper.doSucceedingIncreaseAllowance(10); | ||
}); | ||
it('doesn\'t revert when increasing the allowance', async function () { | ||
await this.wrapper.increaseAllowance(10); | ||
}); | ||
|
||
it('reverts when decreasing the allowance', async function () { | ||
await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(10)); | ||
}); | ||
it('reverts when decreasing the allowance', async function () { | ||
await shouldFail.reverting(this.wrapper.decreaseAllowance(10)); | ||
}); | ||
}); | ||
|
||
context('with non-zero allowance', function () { | ||
beforeEach(async function () { | ||
await this.helper.setAllowance(100); | ||
}); | ||
context('with non-zero allowance', function () { | ||
beforeEach(async function () { | ||
await this.wrapper.setAllowance(100); | ||
}); | ||
|
||
it('reverts when approving a non-zero allowance', async function () { | ||
await shouldFail.reverting(this.helper.doSucceedingApprove(20)); | ||
}); | ||
it('reverts when approving a non-zero allowance', async function () { | ||
await shouldFail.reverting(this.wrapper.approve(20)); | ||
}); | ||
|
||
it('doesn\'t revert when approving a zero allowance', async function () { | ||
await this.helper.doSucceedingApprove(0); | ||
}); | ||
it('doesn\'t revert when approving a zero allowance', async function () { | ||
await this.wrapper.approve(0); | ||
}); | ||
|
||
it('doesn\'t revert when increasing the allowance', async function () { | ||
await this.helper.doSucceedingIncreaseAllowance(10); | ||
}); | ||
it('doesn\'t revert when increasing the allowance', async function () { | ||
await this.wrapper.increaseAllowance(10); | ||
}); | ||
|
||
it('doesn\'t revert when decreasing the allowance to a positive value', async function () { | ||
await this.helper.doSucceedingDecreaseAllowance(50); | ||
}); | ||
it('doesn\'t revert when decreasing the allowance to a positive value', async function () { | ||
await this.wrapper.decreaseAllowance(50); | ||
}); | ||
|
||
it('reverts when decreasing the allowance to a negative value', async function () { | ||
await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(200)); | ||
}); | ||
it('reverts when decreasing the allowance to a negative value', async function () { | ||
await shouldFail.reverting(this.wrapper.decreaseAllowance(200)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |