-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathERC777.behavior.js
555 lines (461 loc) · 19 KB
/
ERC777.behavior.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
const { contract, web3 } = require('@openzeppelin/test-environment');
const {
BN, constants, expectEvent, expectRevert,
} = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const ERC777SenderRecipientMock = contract.fromArtifact('ERC777SenderRecipientMock');
function shouldBehaveLikeERC777DirectSendBurn(holder, recipient, data) {
shouldBehaveLikeERC777DirectSend(holder, recipient, data);
shouldBehaveLikeERC777DirectBurn(holder, data);
}
function shouldBehaveLikeERC777OperatorSendBurn(holder, recipient, operator, data, operatorData) {
shouldBehaveLikeERC777OperatorSend(holder, recipient, operator, data, operatorData);
shouldBehaveLikeERC777OperatorBurn(holder, operator, data, operatorData);
}
function shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, recipient, operator, data, operatorData) {
shouldBehaveLikeERC777UnauthorizedOperatorSend(holder, recipient, operator, data, operatorData);
shouldBehaveLikeERC777UnauthorizedOperatorBurn(holder, operator, data, operatorData);
}
function shouldBehaveLikeERC777DirectSend(holder, recipient, data) {
describe('direct send', () => {
context('when the sender has tokens', () => {
shouldDirectSendTokens(holder, recipient, new BN('0'), data);
shouldDirectSendTokens(holder, recipient, new BN('1'), data);
it('reverts when sending more than the balance', async function () {
const balance = await this.token.balanceOf(holder);
await expectRevert.unspecified(this.token.send(recipient, balance.addn(1), data, { from: holder }));
});
it('reverts when sending to the zero address', async function () {
await expectRevert.unspecified(this.token.send(ZERO_ADDRESS, new BN('1'), data, { from: holder }));
});
});
context('when the sender has no tokens', () => {
removeBalance(holder);
shouldDirectSendTokens(holder, recipient, new BN('0'), data);
it('reverts when sending a non-zero amount', async function () {
await expectRevert.unspecified(this.token.send(recipient, new BN('1'), data, { from: holder }));
});
});
});
}
function shouldBehaveLikeERC777OperatorSend(holder, recipient, operator, data, operatorData) {
describe('operator send', () => {
context('when the sender has tokens', async () => {
shouldOperatorSendTokens(holder, operator, recipient, new BN('0'), data, operatorData);
shouldOperatorSendTokens(holder, operator, recipient, new BN('1'), data, operatorData);
it('reverts when sending more than the balance', async function () {
const balance = await this.token.balanceOf(holder);
await expectRevert.unspecified(
this.token.operatorSend(holder, recipient, balance.addn(1), data, operatorData, { from: operator }),
);
});
it('reverts when sending to the zero address', async function () {
await expectRevert.unspecified(
this.token.operatorSend(
holder, ZERO_ADDRESS, new BN('1'), data, operatorData, { from: operator },
),
);
});
});
context('when the sender has no tokens', () => {
removeBalance(holder);
shouldOperatorSendTokens(holder, operator, recipient, new BN('0'), data, operatorData);
it('reverts when sending a non-zero amount', async function () {
await expectRevert.unspecified(
this.token.operatorSend(holder, recipient, new BN('1'), data, operatorData, { from: operator }),
);
});
it('reverts when sending from the zero address', async function () {
// This is not yet reflected in the spec
await expectRevert.unspecified(
this.token.operatorSend(
ZERO_ADDRESS, recipient, new BN('0'), data, operatorData, { from: operator },
),
);
});
});
});
}
function shouldBehaveLikeERC777UnauthorizedOperatorSend(holder, recipient, operator, data, operatorData) {
describe('operator send', () => {
it('reverts', async function () {
await expectRevert.unspecified(this.token.operatorSend(holder, recipient, new BN('0'), data, operatorData));
});
});
}
function shouldBehaveLikeERC777DirectBurn(holder, data) {
describe('direct burn', () => {
context('when the sender has tokens', () => {
shouldDirectBurnTokens(holder, new BN('0'), data);
shouldDirectBurnTokens(holder, new BN('1'), data);
it('reverts when burning more than the balance', async function () {
const balance = await this.token.balanceOf(holder);
await expectRevert.unspecified(this.token.burn(balance.addn(1), data, { from: holder }));
});
});
context('when the sender has no tokens', () => {
removeBalance(holder);
shouldDirectBurnTokens(holder, new BN('0'), data);
it('reverts when burning a non-zero amount', async function () {
await expectRevert.unspecified(this.token.burn(new BN('1'), data, { from: holder }));
});
});
});
}
function shouldBehaveLikeERC777OperatorBurn(holder, operator, data, operatorData) {
describe('operator burn', () => {
context('when the sender has tokens', async () => {
shouldOperatorBurnTokens(holder, operator, new BN('0'), data, operatorData);
shouldOperatorBurnTokens(holder, operator, new BN('1'), data, operatorData);
it('reverts when burning more than the balance', async function () {
const balance = await this.token.balanceOf(holder);
await expectRevert.unspecified(
this.token.operatorBurn(holder, balance.addn(1), data, operatorData, { from: operator }),
);
});
});
context('when the sender has no tokens', () => {
removeBalance(holder);
shouldOperatorBurnTokens(holder, operator, new BN('0'), data, operatorData);
it('reverts when burning a non-zero amount', async function () {
await expectRevert.unspecified(
this.token.operatorBurn(holder, new BN('1'), data, operatorData, { from: operator }),
);
});
it('reverts when burning from the zero address', async function () {
// This is not yet reflected in the spec
await expectRevert.unspecified(
this.token.operatorBurn(
ZERO_ADDRESS, new BN('0'), data, operatorData, { from: operator },
),
);
});
});
});
}
function shouldBehaveLikeERC777UnauthorizedOperatorBurn(holder, operator, data, operatorData) {
describe('operator burn', () => {
it('reverts', async function () {
await expectRevert.unspecified(this.token.operatorBurn(holder, new BN('0'), data, operatorData));
});
});
}
function shouldDirectSendTokens(from, to, amount, data) {
shouldSendTokens(from, null, to, amount, data, null);
}
function shouldOperatorSendTokens(from, operator, to, amount, data, operatorData) {
shouldSendTokens(from, operator, to, amount, data, operatorData);
}
function shouldSendTokens(from, operator, to, amount, data, operatorData) {
const operatorCall = operator !== null;
it(`${operatorCall ? 'operator ' : ''}can send an amount of ${amount}`, async function () {
const initialTotalSupply = await this.token.totalSupply();
const initialFromBalance = await this.token.balanceOf(from);
const initialToBalance = await this.token.balanceOf(to);
let logs;
if (!operatorCall) {
({ logs } = await this.token.send(to, amount, data, { from }));
expectEvent.inLogs(logs, 'Sent', {
operator: from,
from,
to,
amount,
data,
operatorData: null,
});
} else {
({ logs } = await this.token.operatorSend(from, to, amount, data, operatorData, { from: operator }));
expectEvent.inLogs(logs, 'Sent', {
operator,
from,
to,
amount,
data,
operatorData,
});
}
expectEvent.inLogs(logs, 'Transfer', {
from,
to,
value: amount,
});
const finalTotalSupply = await this.token.totalSupply();
const finalFromBalance = await this.token.balanceOf(from);
const finalToBalance = await this.token.balanceOf(to);
expect(finalTotalSupply).to.be.bignumber.equal(initialTotalSupply);
expect(finalToBalance.sub(initialToBalance)).to.be.bignumber.equal(amount);
expect(finalFromBalance.sub(initialFromBalance)).to.be.bignumber.equal(amount.neg());
});
}
function shouldDirectBurnTokens(from, amount, data) {
shouldBurnTokens(from, null, amount, data, null);
}
function shouldOperatorBurnTokens(from, operator, amount, data, operatorData) {
shouldBurnTokens(from, operator, amount, data, operatorData);
}
function shouldBurnTokens(from, operator, amount, data, operatorData) {
const operatorCall = operator !== null;
it(`${operatorCall ? 'operator ' : ''}can burn an amount of ${amount}`, async function () {
const initialTotalSupply = await this.token.totalSupply();
const initialFromBalance = await this.token.balanceOf(from);
let logs;
if (!operatorCall) {
({ logs } = await this.token.burn(amount, data, { from }));
expectEvent.inLogs(logs, 'Burned', {
operator: from,
from,
amount,
data,
operatorData: null,
});
} else {
({ logs } = await this.token.operatorBurn(from, amount, data, operatorData, { from: operator }));
expectEvent.inLogs(logs, 'Burned', {
operator,
from,
amount,
data,
operatorData,
});
}
expectEvent.inLogs(logs, 'Transfer', {
from,
to: ZERO_ADDRESS,
value: amount,
});
const finalTotalSupply = await this.token.totalSupply();
const finalFromBalance = await this.token.balanceOf(from);
expect(finalTotalSupply.sub(initialTotalSupply)).to.be.bignumber.equal(amount.neg());
expect(finalFromBalance.sub(initialFromBalance)).to.be.bignumber.equal(amount.neg());
});
}
function shouldBehaveLikeERC777InternalMint(recipient, operator, amount, data, operatorData) {
shouldInternalMintTokens(operator, recipient, new BN('0'), data, operatorData);
shouldInternalMintTokens(operator, recipient, amount, data, operatorData);
it('reverts when minting tokens for the zero address', async function () {
await expectRevert.unspecified(this.token.mintInternal(operator, ZERO_ADDRESS, amount, data, operatorData));
});
}
function shouldInternalMintTokens(operator, to, amount, data, operatorData) {
it(`can (internal) mint an amount of ${amount}`, async function () {
const initialTotalSupply = await this.token.totalSupply();
const initialToBalance = await this.token.balanceOf(to);
const { logs } = await this.token.mintInternal(operator, to, amount, data, operatorData);
expectEvent.inLogs(logs, 'Minted', {
operator,
to,
amount,
data,
operatorData,
});
expectEvent.inLogs(logs, 'Transfer', {
from: ZERO_ADDRESS,
to,
value: amount,
});
const finalTotalSupply = await this.token.totalSupply();
const finalToBalance = await this.token.balanceOf(to);
expect(finalTotalSupply.sub(initialTotalSupply)).to.be.bignumber.equal(amount);
expect(finalToBalance.sub(initialToBalance)).to.be.bignumber.equal(amount);
});
}
function shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData) {
context('when TokensRecipient reverts', () => {
beforeEach(async function () {
await this.tokensRecipientImplementer.setShouldRevertReceive(true);
});
it('send reverts', async function () {
await expectRevert.unspecified(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
});
it('operatorSend reverts', async function () {
await expectRevert.unspecified(
this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
);
});
it('mint (internal) reverts', async function () {
await expectRevert.unspecified(
this.token.mintInternal(operator, this.recipient, amount, data, operatorData),
);
});
});
context('when TokensRecipient does not revert', () => {
beforeEach(async function () {
await this.tokensRecipientImplementer.setShouldRevertSend(false);
});
it('TokensRecipient receives send data and is called after state mutation', async function () {
const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
const postSenderBalance = await this.token.balanceOf(this.sender);
const postRecipientBalance = await this.token.balanceOf(this.recipient);
await assertTokensReceivedCalled(
this.token,
tx,
this.sender,
this.sender,
this.recipient,
amount,
data,
null,
postSenderBalance,
postRecipientBalance,
);
});
it('TokensRecipient receives operatorSend data and is called after state mutation', async function () {
const { tx } = await this.token.operatorSend(
this.sender, this.recipient, amount, data, operatorData,
{ from: operator },
);
const postSenderBalance = await this.token.balanceOf(this.sender);
const postRecipientBalance = await this.token.balanceOf(this.recipient);
await assertTokensReceivedCalled(
this.token,
tx,
operator,
this.sender,
this.recipient,
amount,
data,
operatorData,
postSenderBalance,
postRecipientBalance,
);
});
it('TokensRecipient receives mint (internal) data and is called after state mutation', async function () {
const { tx } = await this.token.mintInternal(
operator, this.recipient, amount, data, operatorData,
);
const postRecipientBalance = await this.token.balanceOf(this.recipient);
await assertTokensReceivedCalled(
this.token,
tx,
operator,
ZERO_ADDRESS,
this.recipient,
amount,
data,
operatorData,
new BN('0'),
postRecipientBalance,
);
});
});
}
function shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData) {
context('when TokensSender reverts', () => {
beforeEach(async function () {
await this.tokensSenderImplementer.setShouldRevertSend(true);
});
it('send reverts', async function () {
await expectRevert.unspecified(sendFromHolder(this.token, this.sender, this.recipient, amount, data));
});
it('operatorSend reverts', async function () {
await expectRevert.unspecified(
this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
);
});
it('burn reverts', async function () {
await expectRevert.unspecified(burnFromHolder(this.token, this.sender, amount, data));
});
it('operatorBurn reverts', async function () {
await expectRevert.unspecified(
this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator }),
);
});
});
context('when TokensSender does not revert', () => {
beforeEach(async function () {
await this.tokensSenderImplementer.setShouldRevertSend(false);
});
it('TokensSender receives send data and is called before state mutation', async function () {
const preSenderBalance = await this.token.balanceOf(this.sender);
const preRecipientBalance = await this.token.balanceOf(this.recipient);
const { tx } = await sendFromHolder(this.token, this.sender, this.recipient, amount, data);
await assertTokensToSendCalled(
this.token,
tx,
this.sender,
this.sender,
this.recipient,
amount,
data,
null,
preSenderBalance,
preRecipientBalance,
);
});
it('TokensSender receives operatorSend data and is called before state mutation', async function () {
const preSenderBalance = await this.token.balanceOf(this.sender);
const preRecipientBalance = await this.token.balanceOf(this.recipient);
const { tx } = await this.token.operatorSend(
this.sender, this.recipient, amount, data, operatorData,
{ from: operator },
);
await assertTokensToSendCalled(
this.token,
tx,
operator,
this.sender,
this.recipient,
amount,
data,
operatorData,
preSenderBalance,
preRecipientBalance,
);
});
it('TokensSender receives burn data and is called before state mutation', async function () {
const preSenderBalance = await this.token.balanceOf(this.sender);
const { tx } = await burnFromHolder(this.token, this.sender, amount, data, { from: this.sender });
await assertTokensToSendCalled(
this.token, tx, this.sender, this.sender, ZERO_ADDRESS, amount, data, null, preSenderBalance,
);
});
it('TokensSender receives operatorBurn data and is called before state mutation', async function () {
const preSenderBalance = await this.token.balanceOf(this.sender);
const { tx } = await this.token.operatorBurn(this.sender, amount, data, operatorData, { from: operator });
await assertTokensToSendCalled(
this.token, tx, operator, this.sender, ZERO_ADDRESS, amount, data, operatorData, preSenderBalance,
);
});
});
}
function removeBalance(holder) {
beforeEach(async function () {
await this.token.burn(await this.token.balanceOf(holder), '0x', { from: holder });
expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
});
}
async function assertTokensReceivedCalled(token, txHash, operator, from, to, amount, data, operatorData, fromBalance,
toBalance = '0') {
await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensReceivedCalled', {
operator, from, to, amount, data, operatorData, token: token.address, fromBalance, toBalance,
});
}
async function assertTokensToSendCalled(token, txHash, operator, from, to, amount, data, operatorData, fromBalance,
toBalance = '0') {
await expectEvent.inTransaction(txHash, ERC777SenderRecipientMock, 'TokensToSendCalled', {
operator, from, to, amount, data, operatorData, token: token.address, fromBalance, toBalance,
});
}
async function sendFromHolder(token, holder, to, amount, data) {
if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
return token.send(to, amount, data, { from: holder });
}
// assume holder is ERC777SenderRecipientMock contract
return (await ERC777SenderRecipientMock.at(holder)).send(token.address, to, amount, data);
}
async function burnFromHolder(token, holder, amount, data) {
if ((await web3.eth.getCode(holder)).length <= '0x'.length) {
return token.burn(amount, data, { from: holder });
}
// assume holder is ERC777SenderRecipientMock contract
return (await ERC777SenderRecipientMock.at(holder)).burn(token.address, amount, data);
}
module.exports = {
shouldBehaveLikeERC777DirectSendBurn,
shouldBehaveLikeERC777OperatorSendBurn,
shouldBehaveLikeERC777UnauthorizedOperatorSendBurn,
shouldBehaveLikeERC777InternalMint,
shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook,
shouldBehaveLikeERC777SendBurnWithSendHook,
};