-
Notifications
You must be signed in to change notification settings - Fork 9
/
ERC777.js
445 lines (345 loc) · 17.9 KB
/
ERC777.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
const { accounts, contract, web3 } = require('@openzeppelin/test-environment');
const {
BN, expectEvent, expectRevert, singletons,
} = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const {
shouldBehaveLikeERC20,
} = require('./ERC20.behavior');
const {
shouldBehaveLikeERC777DirectSendBurn,
shouldBehaveLikeERC777OperatorSendBurn,
shouldBehaveLikeERC777UnauthorizedOperatorSendBurn,
shouldBehaveLikeERC777InternalMint,
shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook,
shouldBehaveLikeERC777SendBurnWithSendHook,
} = require('./ERC777.behavior');
const ERC777Mock = contract.fromArtifact('ERC777Mock');
const DawnTokenProxy = contract.fromArtifact('DawnTokenProxy');
const ERC777SenderRecipientMock = contract.fromArtifact('ERC777SenderRecipientMock');
describe('ERC777', () => {
const [registryFunder, holder, defaultOperatorA, defaultOperatorB, newOperator, anyone, proxyOwner, proxyDeployer] = accounts;
const initialSupply = new BN('10000');
const name = 'ERC777Test';
const symbol = '777T';
const data = web3.utils.sha3('OZ777TestData');
const operatorData = web3.utils.sha3('OZ777TestOperatorData');
const defaultOperators = [defaultOperatorA, defaultOperatorB];
beforeEach(async function () {
this.erc1820 = await singletons.ERC1820Registry(registryFunder);
});
context('with default operators', () => {
beforeEach(async function () {
// this.token = await ERC777.new(holder, initialSupply, name, symbol, defaultOperators);
this.token = await ERC777Mock.new();
const initializeData = Buffer.from('');
const proxyContract = await DawnTokenProxy.new(this.token.address, proxyOwner, initializeData, { from: proxyDeployer });
// Route all token calls to go through the proxy contract
this.token = await ERC777Mock.at(proxyContract.address);
await this.token.initializeDawnForConformanceTest(holder, initialSupply, name, symbol, defaultOperators);
});
shouldBehaveLikeERC20('ERC777', initialSupply, holder, anyone, defaultOperatorA);
it.skip('does not emit AuthorizedOperator events for default operators', async function () {
expectEvent.not.inConstructor(this.token, 'AuthorizedOperator'); // This helper needs to be implemented
});
describe('basic information', () => {
it('returns the name', async function () {
expect(await this.token.name()).to.equal(name);
});
it('returns the symbol', async function () {
expect(await this.token.symbol()).to.equal(symbol);
});
it('returns a granularity of 1', async function () {
expect(await this.token.granularity()).to.be.bignumber.equal('1');
});
it('returns the default operators', async function () {
expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
});
it('default operators are operators for all accounts', async function () {
for (const operator of defaultOperators) {
expect(await this.token.isOperatorFor(operator, anyone)).to.equal(true);
}
});
it('returns the total supply', async function () {
expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
});
it('returns 18 when decimals is called', async function () {
expect(await this.token.decimals()).to.be.bignumber.equal('18');
});
it('the ERC777Token interface is registered in the registry', async function () {
expect(await this.erc1820.getInterfaceImplementer(this.token.address, web3.utils.soliditySha3('ERC777Token')))
.to.equal(this.token.address);
});
it('the ERC20Token interface is registered in the registry', async function () {
expect(await this.erc1820.getInterfaceImplementer(this.token.address, web3.utils.soliditySha3('ERC20Token')))
.to.equal(this.token.address);
});
});
describe('balanceOf', () => {
context('for an account with no tokens', () => {
it('returns zero', async function () {
expect(await this.token.balanceOf(anyone)).to.be.bignumber.equal('0');
});
});
context('for an account with tokens', () => {
it('returns their balance', async function () {
expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply);
});
});
});
context('with no ERC777TokensSender and no ERC777TokensRecipient implementers', () => {
describe('send/burn', () => {
shouldBehaveLikeERC777DirectSendBurn(holder, anyone, data);
context('with self operator', () => {
shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, holder, data, operatorData);
});
context('with first default operator', () => {
shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, defaultOperatorA, data, operatorData);
});
context('with second default operator', () => {
shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, defaultOperatorB, data, operatorData);
});
context('before authorizing a new operator', () => {
shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, anyone, newOperator, data, operatorData);
});
context('with new authorized operator', () => {
beforeEach(async function () {
await this.token.authorizeOperator(newOperator, { from: holder });
});
shouldBehaveLikeERC777OperatorSendBurn(holder, anyone, newOperator, data, operatorData);
context('with revoked operator', () => {
beforeEach(async function () {
await this.token.revokeOperator(newOperator, { from: holder });
});
shouldBehaveLikeERC777UnauthorizedOperatorSendBurn(holder, anyone, newOperator, data, operatorData);
});
});
});
describe('mint (internal)', () => {
const to = anyone;
const amount = new BN('5');
context('with default operator', () => {
const operator = defaultOperatorA;
shouldBehaveLikeERC777InternalMint(to, operator, amount, data, operatorData);
});
context('with non operator', () => {
const operator = newOperator;
shouldBehaveLikeERC777InternalMint(to, operator, amount, data, operatorData);
});
});
});
describe('operator management', () => {
it('accounts are their own operator', async function () {
expect(await this.token.isOperatorFor(holder, holder)).to.equal(true);
});
it('reverts when self-authorizing', async function () {
await expectRevert(
this.token.authorizeOperator(holder, { from: holder }), 'ERC777: authorizing self as operator',
);
});
it('reverts when self-revoking', async function () {
await expectRevert(
this.token.revokeOperator(holder, { from: holder }), 'ERC777: revoking self as operator',
);
});
it('non-operators can be revoked', async function () {
expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
const { logs } = await this.token.revokeOperator(newOperator, { from: holder });
expectEvent.inLogs(logs, 'RevokedOperator', { operator: newOperator, tokenHolder: holder });
expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
});
it('non-operators can be authorized', async function () {
expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
const { logs } = await this.token.authorizeOperator(newOperator, { from: holder });
expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: newOperator, tokenHolder: holder });
expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(true);
});
describe('new operators', () => {
beforeEach(async function () {
await this.token.authorizeOperator(newOperator, { from: holder });
});
it('are not added to the default operators list', async function () {
expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
});
it('can be re-authorized', async function () {
const { logs } = await this.token.authorizeOperator(newOperator, { from: holder });
expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: newOperator, tokenHolder: holder });
expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(true);
});
it('can be revoked', async function () {
const { logs } = await this.token.revokeOperator(newOperator, { from: holder });
expectEvent.inLogs(logs, 'RevokedOperator', { operator: newOperator, tokenHolder: holder });
expect(await this.token.isOperatorFor(newOperator, holder)).to.equal(false);
});
});
describe('default operators', () => {
it('can be re-authorized', async function () {
const { logs } = await this.token.authorizeOperator(defaultOperatorA, { from: holder });
expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: defaultOperatorA, tokenHolder: holder });
expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(true);
});
it('can be revoked', async function () {
const { logs } = await this.token.revokeOperator(defaultOperatorA, { from: holder });
expectEvent.inLogs(logs, 'RevokedOperator', { operator: defaultOperatorA, tokenHolder: holder });
expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(false);
});
it('cannot be revoked for themselves', async function () {
await expectRevert(
this.token.revokeOperator(defaultOperatorA, { from: defaultOperatorA }),
'ERC777: revoking self as operator',
);
});
context('with revoked default operator', () => {
beforeEach(async function () {
await this.token.revokeOperator(defaultOperatorA, { from: holder });
});
it('default operator is not revoked for other holders', async function () {
expect(await this.token.isOperatorFor(defaultOperatorA, anyone)).to.equal(true);
});
it('other default operators are not revoked', async function () {
expect(await this.token.isOperatorFor(defaultOperatorB, holder)).to.equal(true);
});
it('default operators list is not modified', async function () {
expect(await this.token.defaultOperators()).to.deep.equal(defaultOperators);
});
it('revoked default operator can be re-authorized', async function () {
const { logs } = await this.token.authorizeOperator(defaultOperatorA, { from: holder });
expectEvent.inLogs(logs, 'AuthorizedOperator', { operator: defaultOperatorA, tokenHolder: holder });
expect(await this.token.isOperatorFor(defaultOperatorA, holder)).to.equal(true);
});
});
});
});
describe('send and receive hooks', () => {
const amount = new BN('1');
const operator = defaultOperatorA;
// sender and recipient are stored inside 'this', since in some tests their addresses are determined dynamically
describe('tokensReceived', () => {
beforeEach(function () {
this.sender = holder;
});
context('with no ERC777TokensRecipient implementer', () => {
context('with contract recipient', () => {
beforeEach(async function () {
this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
this.recipient = this.tokensRecipientImplementer.address;
// Note that tokensRecipientImplementer doesn't implement the recipient interface for the recipient
});
it('send reverts', async function () {
await expectRevert(
this.token.send(this.recipient, amount, data, { from: holder }),
'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
);
});
it('operatorSend reverts', async function () {
await expectRevert(
this.token.operatorSend(this.sender, this.recipient, amount, data, operatorData, { from: operator }),
'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
);
});
it('mint (internal) reverts', async function () {
await expectRevert(
this.token.mintInternal(operator, this.recipient, amount, data, operatorData),
'ERC777: token recipient contract has no implementer for ERC777TokensRecipient',
);
});
it('(ERC20) transfer succeeds', async function () {
await this.token.transfer(this.recipient, amount, { from: holder });
});
it('(ERC20) transferFrom succeeds', async function () {
const approved = anyone;
await this.token.approve(approved, amount, { from: this.sender });
await this.token.transferFrom(this.sender, this.recipient, amount, { from: approved });
});
});
});
context('with ERC777TokensRecipient implementer', () => {
context('with contract as implementer for an externally owned account', () => {
beforeEach(async function () {
this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
this.recipient = anyone;
await this.tokensRecipientImplementer.recipientFor(this.recipient);
await this.erc1820.setInterfaceImplementer(
this.recipient,
web3.utils.soliditySha3('ERC777TokensRecipient'), this.tokensRecipientImplementer.address,
{ from: this.recipient },
);
});
shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
});
context('with contract as implementer for another contract', () => {
beforeEach(async function () {
this.recipientContract = await ERC777SenderRecipientMock.new();
this.recipient = this.recipientContract.address;
this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
await this.tokensRecipientImplementer.recipientFor(this.recipient);
await this.recipientContract.registerRecipient(this.tokensRecipientImplementer.address);
});
shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
});
context('with contract as implementer for itself', () => {
beforeEach(async function () {
this.tokensRecipientImplementer = await ERC777SenderRecipientMock.new();
this.recipient = this.tokensRecipientImplementer.address;
await this.tokensRecipientImplementer.recipientFor(this.recipient);
});
shouldBehaveLikeERC777SendBurnMintInternalWithReceiveHook(operator, amount, data, operatorData);
});
});
});
describe('tokensToSend', () => {
beforeEach(function () {
this.recipient = anyone;
});
context('with a contract as implementer for an externally owned account', () => {
beforeEach(async function () {
this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
this.sender = holder;
await this.tokensSenderImplementer.senderFor(this.sender);
await this.erc1820.setInterfaceImplementer(
this.sender,
web3.utils.soliditySha3('ERC777TokensSender'), this.tokensSenderImplementer.address,
{ from: this.sender },
);
});
shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
});
context('with contract as implementer for another contract', () => {
beforeEach(async function () {
this.senderContract = await ERC777SenderRecipientMock.new();
this.sender = this.senderContract.address;
this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
await this.tokensSenderImplementer.senderFor(this.sender);
await this.senderContract.registerSender(this.tokensSenderImplementer.address);
// For the contract to be able to receive tokens (that it can later send), it must also implement the
// recipient interface.
await this.senderContract.recipientFor(this.sender);
await this.token.send(this.sender, amount, data, { from: holder });
});
shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
});
context('with a contract as implementer for itself', () => {
beforeEach(async function () {
this.tokensSenderImplementer = await ERC777SenderRecipientMock.new();
this.sender = this.tokensSenderImplementer.address;
await this.tokensSenderImplementer.senderFor(this.sender);
// For the contract to be able to receive tokens (that it can later send), it must also implement the
// recipient interface.
await this.tokensSenderImplementer.recipientFor(this.sender);
await this.token.send(this.sender, amount, data, { from: holder });
});
shouldBehaveLikeERC777SendBurnWithSendHook(operator, amount, data, operatorData);
});
});
});
});
context('with no default operators', () => {
beforeEach(async function () {
this.token = await ERC777Mock.new();
await this.token.initializeDawnForConformanceTest(holder, initialSupply, name, symbol, []);
});
it('default operators list is empty', async function () {
expect(await this.token.defaultOperators()).to.deep.equal([]);
});
});
});