|
1 | 1 | const { shouldFail } = require('openzeppelin-test-helpers');
|
2 | 2 |
|
3 |
| -const SafeERC20Helper = artifacts.require('SafeERC20Helper'); |
| 3 | +const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock'); |
| 4 | +const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock'); |
| 5 | +const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock'); |
| 6 | +const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper'); |
4 | 7 |
|
5 | 8 | contract('SafeERC20', function () {
|
6 |
| - beforeEach(async function () { |
7 |
| - this.helper = await SafeERC20Helper.new(); |
8 |
| - }); |
9 |
| - |
10 | 9 | describe('with token that returns false on all calls', function () {
|
| 10 | + beforeEach(async function () { |
| 11 | + this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address); |
| 12 | + }); |
| 13 | + |
11 | 14 | it('reverts on transfer', async function () {
|
12 |
| - await shouldFail.reverting(this.helper.doFailingTransfer()); |
| 15 | + await shouldFail.reverting(this.wrapper.transfer()); |
13 | 16 | });
|
14 | 17 |
|
15 | 18 | it('reverts on transferFrom', async function () {
|
16 |
| - await shouldFail.reverting(this.helper.doFailingTransferFrom()); |
| 19 | + await shouldFail.reverting(this.wrapper.transferFrom()); |
17 | 20 | });
|
18 | 21 |
|
19 | 22 | it('reverts on approve', async function () {
|
20 |
| - await shouldFail.reverting(this.helper.doFailingApprove()); |
| 23 | + await shouldFail.reverting(this.wrapper.approve(0)); |
21 | 24 | });
|
22 | 25 |
|
23 | 26 | it('reverts on increaseAllowance', async function () {
|
24 |
| - await shouldFail.reverting(this.helper.doFailingIncreaseAllowance()); |
| 27 | + await shouldFail.reverting(this.wrapper.increaseAllowance(0)); |
25 | 28 | });
|
26 | 29 |
|
27 | 30 | it('reverts on decreaseAllowance', async function () {
|
28 |
| - await shouldFail.reverting(this.helper.doFailingDecreaseAllowance()); |
| 31 | + await shouldFail.reverting(this.wrapper.decreaseAllowance(0)); |
29 | 32 | });
|
30 | 33 | });
|
31 | 34 |
|
32 | 35 | describe('with token that returns true on all calls', function () {
|
33 |
| - it('doesn\'t revert on transfer', async function () { |
34 |
| - await this.helper.doSucceedingTransfer(); |
| 36 | + beforeEach(async function () { |
| 37 | + this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address); |
35 | 38 | });
|
36 | 39 |
|
37 |
| - it('doesn\'t revert on transferFrom', async function () { |
38 |
| - await this.helper.doSucceedingTransferFrom(); |
| 40 | + shouldOnlyRevertOnErrors(); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('with token that returns no boolean values', function () { |
| 44 | + beforeEach(async function () { |
| 45 | + this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address); |
39 | 46 | });
|
40 | 47 |
|
41 |
| - describe('approvals', function () { |
42 |
| - context('with zero allowance', function () { |
43 |
| - beforeEach(async function () { |
44 |
| - await this.helper.setAllowance(0); |
45 |
| - }); |
| 48 | + shouldOnlyRevertOnErrors(); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +function shouldOnlyRevertOnErrors () { |
| 53 | + it('doesn\'t revert on transfer', async function () { |
| 54 | + await this.wrapper.transfer(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('doesn\'t revert on transferFrom', async function () { |
| 58 | + await this.wrapper.transferFrom(); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('approvals', function () { |
| 62 | + context('with zero allowance', function () { |
| 63 | + beforeEach(async function () { |
| 64 | + await this.wrapper.setAllowance(0); |
| 65 | + }); |
46 | 66 |
|
47 |
| - it('doesn\'t revert when approving a non-zero allowance', async function () { |
48 |
| - await this.helper.doSucceedingApprove(100); |
49 |
| - }); |
| 67 | + it('doesn\'t revert when approving a non-zero allowance', async function () { |
| 68 | + await this.wrapper.approve(100); |
| 69 | + }); |
50 | 70 |
|
51 |
| - it('doesn\'t revert when approving a zero allowance', async function () { |
52 |
| - await this.helper.doSucceedingApprove(0); |
53 |
| - }); |
| 71 | + it('doesn\'t revert when approving a zero allowance', async function () { |
| 72 | + await this.wrapper.approve(0); |
| 73 | + }); |
54 | 74 |
|
55 |
| - it('doesn\'t revert when increasing the allowance', async function () { |
56 |
| - await this.helper.doSucceedingIncreaseAllowance(10); |
57 |
| - }); |
| 75 | + it('doesn\'t revert when increasing the allowance', async function () { |
| 76 | + await this.wrapper.increaseAllowance(10); |
| 77 | + }); |
58 | 78 |
|
59 |
| - it('reverts when decreasing the allowance', async function () { |
60 |
| - await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(10)); |
61 |
| - }); |
| 79 | + it('reverts when decreasing the allowance', async function () { |
| 80 | + await shouldFail.reverting(this.wrapper.decreaseAllowance(10)); |
62 | 81 | });
|
| 82 | + }); |
63 | 83 |
|
64 |
| - context('with non-zero allowance', function () { |
65 |
| - beforeEach(async function () { |
66 |
| - await this.helper.setAllowance(100); |
67 |
| - }); |
| 84 | + context('with non-zero allowance', function () { |
| 85 | + beforeEach(async function () { |
| 86 | + await this.wrapper.setAllowance(100); |
| 87 | + }); |
68 | 88 |
|
69 |
| - it('reverts when approving a non-zero allowance', async function () { |
70 |
| - await shouldFail.reverting(this.helper.doSucceedingApprove(20)); |
71 |
| - }); |
| 89 | + it('reverts when approving a non-zero allowance', async function () { |
| 90 | + await shouldFail.reverting(this.wrapper.approve(20)); |
| 91 | + }); |
72 | 92 |
|
73 |
| - it('doesn\'t revert when approving a zero allowance', async function () { |
74 |
| - await this.helper.doSucceedingApprove(0); |
75 |
| - }); |
| 93 | + it('doesn\'t revert when approving a zero allowance', async function () { |
| 94 | + await this.wrapper.approve(0); |
| 95 | + }); |
76 | 96 |
|
77 |
| - it('doesn\'t revert when increasing the allowance', async function () { |
78 |
| - await this.helper.doSucceedingIncreaseAllowance(10); |
79 |
| - }); |
| 97 | + it('doesn\'t revert when increasing the allowance', async function () { |
| 98 | + await this.wrapper.increaseAllowance(10); |
| 99 | + }); |
80 | 100 |
|
81 |
| - it('doesn\'t revert when decreasing the allowance to a positive value', async function () { |
82 |
| - await this.helper.doSucceedingDecreaseAllowance(50); |
83 |
| - }); |
| 101 | + it('doesn\'t revert when decreasing the allowance to a positive value', async function () { |
| 102 | + await this.wrapper.decreaseAllowance(50); |
| 103 | + }); |
84 | 104 |
|
85 |
| - it('reverts when decreasing the allowance to a negative value', async function () { |
86 |
| - await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(200)); |
87 |
| - }); |
| 105 | + it('reverts when decreasing the allowance to a negative value', async function () { |
| 106 | + await shouldFail.reverting(this.wrapper.decreaseAllowance(200)); |
88 | 107 | });
|
89 | 108 | });
|
90 | 109 | });
|
91 |
| -}); |
| 110 | +} |
0 commit comments