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.pull/1658/head
parent
0dded493a0
commit
41aa39afbc
@ -1,91 +1,110 @@ |
|||||||
const { shouldFail } = require('openzeppelin-test-helpers'); |
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 () { |
contract('SafeERC20', function () { |
||||||
beforeEach(async function () { |
|
||||||
this.helper = await SafeERC20Helper.new(); |
|
||||||
}); |
|
||||||
|
|
||||||
describe('with token that returns false on all calls', function () { |
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 () { |
it('reverts on transfer', async function () { |
||||||
await shouldFail.reverting(this.helper.doFailingTransfer()); |
await shouldFail.reverting(this.wrapper.transfer()); |
||||||
}); |
}); |
||||||
|
|
||||||
it('reverts on transferFrom', async function () { |
it('reverts on transferFrom', async function () { |
||||||
await shouldFail.reverting(this.helper.doFailingTransferFrom()); |
await shouldFail.reverting(this.wrapper.transferFrom()); |
||||||
}); |
}); |
||||||
|
|
||||||
it('reverts on approve', async function () { |
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 () { |
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 () { |
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 () { |
describe('with token that returns true on all calls', function () { |
||||||
it('doesn\'t revert on transfer', async function () { |
beforeEach(async function () { |
||||||
await this.helper.doSucceedingTransfer(); |
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address); |
||||||
}); |
}); |
||||||
|
|
||||||
it('doesn\'t revert on transferFrom', async function () { |
shouldOnlyRevertOnErrors(); |
||||||
await this.helper.doSucceedingTransferFrom(); |
}); |
||||||
|
|
||||||
|
describe('with token that returns no boolean values', function () { |
||||||
|
beforeEach(async function () { |
||||||
|
this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address); |
||||||
}); |
}); |
||||||
|
|
||||||
describe('approvals', function () { |
shouldOnlyRevertOnErrors(); |
||||||
context('with zero allowance', function () { |
}); |
||||||
beforeEach(async function () { |
}); |
||||||
await this.helper.setAllowance(0); |
|
||||||
}); |
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 () { |
it('doesn\'t revert when approving a non-zero allowance', async function () { |
||||||
await this.helper.doSucceedingApprove(100); |
await this.wrapper.approve(100); |
||||||
}); |
}); |
||||||
|
|
||||||
it('doesn\'t revert when approving a zero allowance', async function () { |
it('doesn\'t revert when approving a zero allowance', async function () { |
||||||
await this.helper.doSucceedingApprove(0); |
await this.wrapper.approve(0); |
||||||
}); |
}); |
||||||
|
|
||||||
it('doesn\'t revert when increasing the allowance', async function () { |
it('doesn\'t revert when increasing the allowance', async function () { |
||||||
await this.helper.doSucceedingIncreaseAllowance(10); |
await this.wrapper.increaseAllowance(10); |
||||||
}); |
}); |
||||||
|
|
||||||
it('reverts when decreasing the allowance', async function () { |
it('reverts when decreasing the allowance', async function () { |
||||||
await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(10)); |
await shouldFail.reverting(this.wrapper.decreaseAllowance(10)); |
||||||
}); |
|
||||||
}); |
}); |
||||||
|
}); |
||||||
|
|
||||||
context('with non-zero allowance', function () { |
context('with non-zero allowance', function () { |
||||||
beforeEach(async function () { |
beforeEach(async function () { |
||||||
await this.helper.setAllowance(100); |
await this.wrapper.setAllowance(100); |
||||||
}); |
}); |
||||||
|
|
||||||
it('reverts when approving a non-zero allowance', async function () { |
it('reverts when approving a non-zero allowance', async function () { |
||||||
await shouldFail.reverting(this.helper.doSucceedingApprove(20)); |
await shouldFail.reverting(this.wrapper.approve(20)); |
||||||
}); |
}); |
||||||
|
|
||||||
it('doesn\'t revert when approving a zero allowance', async function () { |
it('doesn\'t revert when approving a zero allowance', async function () { |
||||||
await this.helper.doSucceedingApprove(0); |
await this.wrapper.approve(0); |
||||||
}); |
}); |
||||||
|
|
||||||
it('doesn\'t revert when increasing the allowance', async function () { |
it('doesn\'t revert when increasing the allowance', async function () { |
||||||
await this.helper.doSucceedingIncreaseAllowance(10); |
await this.wrapper.increaseAllowance(10); |
||||||
}); |
}); |
||||||
|
|
||||||
it('doesn\'t revert when decreasing the allowance to a positive value', async function () { |
it('doesn\'t revert when decreasing the allowance to a positive value', async function () { |
||||||
await this.helper.doSucceedingDecreaseAllowance(50); |
await this.wrapper.decreaseAllowance(50); |
||||||
}); |
}); |
||||||
|
|
||||||
it('reverts when decreasing the allowance to a negative value', async function () { |
it('reverts when decreasing the allowance to a negative value', async function () { |
||||||
await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(200)); |
await shouldFail.reverting(this.wrapper.decreaseAllowance(200)); |
||||||
}); |
|
||||||
}); |
}); |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
}); |
} |
||||||
|
Loading…
Reference in new issue