forked from OpenZeppelin/openzeppelin-contracts
/
SafeERC20.test.js
256 lines (213 loc) · 8.3 KB
/
SafeERC20.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const { constants, expectRevert } = require('@openzeppelin/test-helpers');
const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock');
const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
const ERC20PermitNoRevertMock = artifacts.require('ERC20PermitNoRevertMock');
const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper');
const { EIP712Domain, Permit } = require('../../../helpers/eip712');
const { fromRpcSig } = require('ethereumjs-util');
const ethSigUtil = require('eth-sig-util');
const Wallet = require('ethereumjs-wallet').default;
contract('SafeERC20', function (accounts) {
const [ hasNoCode ] = accounts;
describe('with address that has no contract code', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new(hasNoCode);
});
shouldRevertOnAllCalls('Address: call to non-contract');
});
describe('with token that returns false on all calls', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address);
});
shouldRevertOnAllCalls('SafeERC20: ERC20 operation did not succeed');
});
describe('with token that returns true on all calls', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address);
});
shouldOnlyRevertOnErrors();
});
describe('with token that returns no boolean values', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address);
});
shouldOnlyRevertOnErrors();
});
describe('with token that doesn\'t revert on invalid permit', function () {
const wallet = Wallet.generate();
const owner = wallet.getAddressString();
const spender = hasNoCode;
beforeEach(async function () {
this.token = await ERC20PermitNoRevertMock.new();
this.wrapper = await SafeERC20Wrapper.new(this.token.address);
const chainId = await this.token.getChainId();
this.data = {
primaryType: 'Permit',
types: { EIP712Domain, Permit },
domain: { name: 'ERC20PermitNoRevertMock', version: '1', chainId, verifyingContract: this.token.address },
message: { owner, spender, value: '42', nonce: '0', deadline: constants.MAX_UINT256 },
};
this.signature = fromRpcSig(ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data: this.data }));
});
it('accepts owner signature', async function () {
expect(await this.token.nonces(owner)).to.be.bignumber.equal('0');
expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal('0');
await this.wrapper.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(this.data.message.value);
});
it('revert on reused signature', async function () {
expect(await this.token.nonces(owner)).to.be.bignumber.equal('0');
await this.wrapper.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
await expectRevert(
this.wrapper.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
),
'SafeERC20: permit did not succeed',
);
expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
});
it('revert on invalid signature', async function () {
await expectRevert(
this.wrapper.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
27,
'0x71753dc5ecb5b4bfc0e3bc530d79ce5988760ed3f3a234c86a5546491f540775',
'0x0049cedee5aed990aabed5ad6a9f6e3c565b63379894b5fa8b512eb2b79e485d',
),
'SafeERC20: permit did not succeed',
);
});
it('underlying token does not revert on reused or invalid signature', async function () {
await this.token.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
await this.token.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
this.signature.v,
this.signature.r,
this.signature.s,
);
await this.token.permit(
this.data.message.owner,
this.data.message.spender,
this.data.message.value,
this.data.message.deadline,
27,
'0x71753dc5ecb5b4bfc0e3bc530d79ce5988760ed3f3a234c86a5546491f540775',
'0x0049cedee5aed990aabed5ad6a9f6e3c565b63379894b5fa8b512eb2b79e485d',
);
});
});
});
function shouldRevertOnAllCalls (reason) {
it('reverts on transfer', async function () {
await expectRevert(this.wrapper.transfer(), reason);
});
it('reverts on transferFrom', async function () {
await expectRevert(this.wrapper.transferFrom(), reason);
});
it('reverts on approve', async function () {
await expectRevert(this.wrapper.approve(0), reason);
});
it('reverts on increaseAllowance', async function () {
// [TODO] make sure it's reverting for the right reason
await expectRevert.unspecified(this.wrapper.increaseAllowance(0));
});
it('reverts on decreaseAllowance', async function () {
// [TODO] make sure it's reverting for the right reason
await expectRevert.unspecified(this.wrapper.decreaseAllowance(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 () {
await this.wrapper.approve(100);
});
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.wrapper.increaseAllowance(10);
});
it('reverts when decreasing the allowance', async function () {
await expectRevert(
this.wrapper.decreaseAllowance(10),
'SafeERC20: decreased allowance below zero',
);
});
});
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 expectRevert(
this.wrapper.approve(20),
'SafeERC20: approve from non-zero to non-zero allowance',
);
});
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.wrapper.increaseAllowance(10);
});
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 expectRevert(
this.wrapper.decreaseAllowance(200),
'SafeERC20: decreased allowance below zero',
);
});
});
});
}