-
Notifications
You must be signed in to change notification settings - Fork 8
/
erc721.js
477 lines (390 loc) · 14 KB
/
erc721.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
const erc721 = artifacts.require("erc721");
let owner,
receiver,
operator,
erc721Token;
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
const ERC165_INTERFACE_ID = '0x0000000000000000000000000000000000000000000000000000000001ffc9a7';
const ERC721_INTERFACE_ID = '0x0000000000000000000000000000000000000000000000000000000080ac58cd';
contract("ERC721", async accounts => {
beforeEach(async () => {
owner = accounts[0];
receiver = accounts[1];
operator = accounts[2];
erc721Token = await erc721.new({ from: owner });
});
it("...should register ERC165 interface.", async () => {
const has_erc165_interface = await erc721Token.supportsInterface.call(ERC165_INTERFACE_ID);
assert.ok(has_erc165_interface, "The ERC165 interface was not correctly registered.");
});
it("...should register ERC721 interface.", async () => {
const has_erc721_interface = await erc721Token.supportsInterface.call(ERC721_INTERFACE_ID);
assert.ok(has_erc721_interface, "The ERC721 interface was not correctly registered.");
});
it("...should mint token to owner.", async () => {
let balance = await erc721Token.balanceOf.call(owner);
const minter_starting_balance = balance.toString();
// TODO: weird -> we are getting the token id from the mint event parameters
// how to get the return value of mint()??
let returnData = await erc721Token.mint({ from: owner });
const tokenId_1 = returnData.logs[0].args['1'].toString();
returnData = await erc721Token.mint({ from: owner });
const tokenId_2 = returnData.logs[0].args['1'].toString();
const ownerOfToken_1 = await erc721Token.ownerOf.call(tokenId_1);
const ownerOfToken_2 = await erc721Token.ownerOf.call(tokenId_2);
balance = await erc721Token.balanceOf.call(owner);
const minter_ending_balance = balance.toString();
assert.equal(
tokenId_1,
0,
"Token ID wasn't correctly set."
);
assert.equal(
tokenId_2,
1,
"Token ID wasn't correctly set."
);
assert.equal(
minter_starting_balance,
minter_ending_balance - 2,
"Token wasn't correctly minted to owner."
);
assert.equal(
ownerOfToken_1,
owner,
"Owner of first minted token wasn't correctly set."
);
assert.equal(
ownerOfToken_2,
owner,
"Owner of second minted token wasn't correctly set."
);
});
// TODO:
it("...mint should revert if msg.sender is not owner.", async () => {
try {
await erc721Token.mint({ from: receiver });
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
'Mint by non owner did not revert.'
)
});
it("...should safeTransferFrom.", async () => {
// TODO: weird -> we are getting the token id from the mint event parameters
// how to get the return value of mint()??
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
const ownerBalanceBefore = await erc721Token.balanceOf.call(owner);
const receiverBalanceBefore = await erc721Token.balanceOf.call(receiver);
const ownerOfTokenBefore = await erc721Token.ownerOf.call(tokenId);
await erc721Token.safeTransferFrom(owner, receiver, tokenId);
const ownerBalanceAfter = await erc721Token.balanceOf.call(owner);
const receiverBalanceAfter = await erc721Token.balanceOf.call(receiver);
const ownerOfTokenAfter = await erc721Token.ownerOf.call(tokenId);
assert.equal(
ownerBalanceBefore - 1,
ownerBalanceAfter.toString(),
"Balance of owner wasn't correctly updated."
);
assert.equal(
receiverBalanceBefore.toString(),
receiverBalanceAfter - 1,
"Balance of reveiver wasn't correctly updated."
);
assert.equal(
ownerOfTokenAfter,
receiver,
"Owner of token wasn't correctly updated."
);
});
it("...should transferFrom.", async () => {
// TODO: weird -> we are getting the token id from the mint event parameters
// how to get the return value of mint()??
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
const ownerBalanceBefore = await erc721Token.balanceOf.call(owner);
const receiverBalanceBefore = await erc721Token.balanceOf.call(receiver);
const ownerOfTokenBefore = await erc721Token.ownerOf.call(tokenId);
await erc721Token.transferFrom(owner, receiver, tokenId);
const ownerBalanceAfter = await erc721Token.balanceOf.call(owner);
const receiverBalanceAfter = await erc721Token.balanceOf.call(receiver);
const ownerOfTokenAfter = await erc721Token.ownerOf.call(tokenId);
assert.equal(
ownerBalanceBefore - 1,
ownerBalanceAfter.toString(),
"Balance of owner wasn't correctly updated."
);
assert.equal(
receiverBalanceBefore.toString(),
receiverBalanceAfter - 1,
"Balance of reveiver wasn't correctly updated."
);
assert.equal(
ownerOfTokenAfter,
receiver,
"Owner of token wasn't correctly updated."
);
});
// TODO:
it("...safeTransferFrom() to contract without onERC721Received should revert.", async () => {
});
it("...safeTransferFrom() to ZERO_ADDRESS should revert.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
const result = await erc721Token.safeTransferFrom(owner, ZERO_ADDRESS, tokenId);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"SafeTransferFrom to ZERO_ADDRESS did not revert."
)
});
it("...transferFrom() to ZERO_ADDRESS should revert.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
const result = await erc721Token.transferFrom(owner, ZERO_ADDRESS, tokenId);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"TransferFrom to ZERO_ADDRESS did not revert."
)
});
it("...safeTransferFrom() should revert if 'tokenId' is not a valid NFT.", async () => {
const tokenId = -1
let revert = false;
try {
await erc721Token.safeTransferFrom(owner, receiver, tokenId);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"SafeTransferFrom of non valid NFT did not revert."
)
});
it("...transferFrom() should revert if 'tokenId' is not a valid NFT.", async () => {
const tokenId = -1
let revert = false;
try {
await erc721Token.transferFrom(owner, receiver, tokenId);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"TransferFrom of non valid NFT did not revert."
)
});
it("...safeTransferFrom() should revert if 'from' is not the owner.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
await erc721Token.safeTransferFrom(receiver, owner, tokenId);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"SafeTransferFrom by non owner did not revert."
)
});
it("...transferFrom() should revert if 'from' is not the owner.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
await erc721Token.transferFrom(receiver, owner, tokenId);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"SafeTransferFrom by non owner did not revert."
)
});
it("...balanceOf() should revert for queries about the ZERO_ADDRESS.", async () => {
let revert = false;
try {
await erc721Token.balanceOf(ZERO_ADDRESS);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"BalanceOf query about ZERO_ADDRESS did not revert."
)
});
it("...should approve() if is owner.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
await erc721Token.approve(operator, tokenId, { from: owner });
} catch (e) {
revert = e;
}
const isApproved = await erc721Token.getApproved.call(tokenId);
assert.isOk(
isApproved,
"Operator was not correctly approved."
);
assert.isNotOk(
revert,
"Operator was not correctly approved."
);
});
it("...approve() should revert if 'tokenId' is not owned by 'msg.sender'.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
await erc721Token.approve(owner, tokenId, { from: operator });
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"Approve from non owner did not revert."
)
});
it("...approve() should revert if 'tokenId' is not a valid NFT.", async () => {
let revert = false;
try {
await erc721Token.approve(owner, -1, { from: operator });
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"Approve of non valid NFT did not revert."
)
});
it("...should approve if is an authorized operator.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
await erc721Token.approve(operator, tokenId, { from: owner });
await erc721Token.approve(accounts[3], tokenId, { from: operator });
const isApproved = await erc721Token.getApproved.call(tokenId);
assert.equal(
isApproved,
accounts[3],
"Authorized operator did not correctly approve."
);
});
it("...approve should revert if 'msg.sender' is not an authorized operator.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
let revert = false;
try {
await erc721Token.approve(owner, tokenId, { from: account[3] });
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"Approve call from non authorized operator did not revert."
)
});
it("...should getApproved.", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
await erc721Token.approve(operator, tokenId, { from: owner });
const isApproved = await erc721Token.getApproved.call(tokenId);
assert.equal(
isApproved,
operator,
"Did not correctly getApproved."
);
});
it("...should setApprovalForAll.", async () => {
await erc721Token.setApprovalForAll(operator, true, { from: owner });
const isApprovedForAll = await erc721Token.isApprovedForAll.call(owner, operator);
assert.isOk(
isApprovedForAll,
"Did not setApprovalForAll."
);
});
it("...isApprovedForAll should return 'False' if address is not approvedForAll.", async () => {
const isApprovedForAll = await erc721Token.isApprovedForAll.call(owner, accounts[3]);
assert.isNotOk(
isApprovedForAll,
"Did not return 'False' for address that is not approvedForAll."
);
});
it("...getApproved should revert if 'tokenId' is not a valid NFT.", async () => {
let revert = false;
try {
await erc721Token.getApproved.call(-1);
} catch (e) {
revert = e;
}
assert.instanceOf(
revert,
Error,
"GetApproved of non valid NFT did not revert."
)
});
// Test event logging
it("...should log 'Minted' on mint().", async () => {
const res = await erc721Token.mint({ from: owner });
const log = res.logs.find(element => element.event.match('Mint'));
assert.strictEqual(log.args._to, owner);
assert.strictEqual(log.args._tokenId.toString(), "0");
})
it("...should log 'Transfer' on safeTransferFrom().", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
const res = await erc721Token.safeTransferFrom(owner, receiver, tokenId);
const log = res.logs.find(element => element.event.match('Transfer'));
assert.strictEqual(log.args._from, owner);
assert.strictEqual(log.args._to, receiver);
assert.strictEqual(log.args._tokenId.toString(), tokenId);
})
it("...should log 'Transfer' on transferFrom().", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
const res = await erc721Token.transferFrom(owner, receiver, tokenId);
const log = res.logs.find(element => element.event.match('Transfer'));
assert.strictEqual(log.args._from, owner);
assert.strictEqual(log.args._to, receiver);
assert.strictEqual(log.args._tokenId.toString(), tokenId);
})
it("...should log 'Approval' on approve().", async () => {
const returnData = await erc721Token.mint({ from: owner });
const tokenId = returnData.logs[0].args['1'].toString();
const res = await erc721Token.approve(operator, tokenId, { from: owner });
const log = res.logs.find(element => element.event.match('Approval'));
assert.strictEqual(log.args._owner, owner);
assert.strictEqual(log.args._approved, operator);
assert.strictEqual(log.args._tokenId.toString(), tokenId);
})
it("...should log 'ApprovalForAll' on setApprovalForAll().", async () => {
const res = await erc721Token.setApprovalForAll(operator, true, { from: owner });
const log = res.logs.find(element => element.event.match('ApprovalForAll'));
assert.strictEqual(log.args._owner, owner);
assert.strictEqual(log.args._operator, operator);
assert.strictEqual(log.args._approved, true);
})
});