1
1
const { parseEther } = require ( "@ethersproject/units" )
2
- const { formatBytes32String, toUtf8Bytes } = require ( "@ethersproject/strings" )
3
2
const { id } = require ( "@ethersproject/hash" )
4
3
const { expect } = require ( "chai" )
5
4
const { ethers } = require ( "hardhat" )
6
5
6
+ // "err" as bytes, induces a simulated error in MockRecipient.sol and MockRecipientReturnBool.sol
7
+ const errData = "0x657272"
8
+
7
9
describe ( "DATAv2" , ( ) => {
8
10
it ( "transferAndCall triggers ERC677 callback" , async ( ) => {
9
11
const [ signer , minter ] = await ethers . getSigners ( )
@@ -13,47 +15,39 @@ describe("DATAv2", () => {
13
15
await recipient . deployed ( )
14
16
15
17
const MockRecipientNotERC677Receiver = await ethers . getContractFactory ( "MockRecipientNotERC677Receiver" )
16
- const recipient2 = await MockRecipientNotERC677Receiver . deploy ( )
17
- await recipient2 . deployed ( )
18
+ const nonReceiverRecipient = await MockRecipientNotERC677Receiver . deploy ( )
19
+ await nonReceiverRecipient . deployed ( )
18
20
19
21
const MockRecipientReturnBool = await ethers . getContractFactory ( "MockRecipientReturnBool" )
20
- const recipient3 = await MockRecipientReturnBool . deploy ( )
21
- await recipient3 . deployed ( )
22
-
22
+ const returnBoolRecipient = await MockRecipientReturnBool . deploy ( )
23
+ await returnBoolRecipient . deployed ( )
23
24
24
25
const DATAv2 = await ethers . getContractFactory ( "DATAv2" )
25
26
const token = await DATAv2 . deploy ( )
26
27
await token . deployed ( )
27
28
28
29
await expect ( token . grantRole ( id ( "MINTER_ROLE" ) , minter . address ) ) . to . emit ( token , "RoleGranted" )
29
- await expect ( token . connect ( minter ) . mint ( signer . address , parseEther ( "10" ) ) ) . to . emit ( token , "Transfer(address,address,uint256)" )
30
+ await expect ( token . connect ( minter ) . mint ( signer . address , parseEther ( "10" ) ) ) . to . emit ( token , "Transfer(address,address,uint256)" )
30
31
31
- //"err" as bytes
32
- const errData = "0x657272"
33
- //revert in callback should revert transferAndCall
32
+ // revert in callback => should revert transferAndCall
34
33
await expect ( token . transferAndCall ( recipient . address , parseEther ( "1" ) , errData ) ) . to . be . reverted
35
- //no callback should revert transferAndCall
36
- await expect ( token . transferAndCall ( recipient2 . address , parseEther ( "1" ) , "0x" ) ) . to . be . reverted
37
-
38
- //call to contract that implements ERC677Receiver reverts if onTokensReceived = false
39
- //await expect().to.be.reverted
40
-
41
- //call to contract that implements ERC677Receiver calls callback
42
- let before = await recipient . txCount ( )
43
- await token . transferAndCall ( recipient . address , parseEther ( "1" ) , "0x6c6f6c" )
44
- let after = await recipient . txCount ( )
45
- expect ( after ) . to . equal ( before . add ( 1 ) )
46
34
35
+ // no callback => should revert transferAndCall
36
+ await expect ( token . transferAndCall ( nonReceiverRecipient . address , parseEther ( "1" ) , "0x" ) ) . to . be . reverted
47
37
48
- before = await recipient3 . txCount ( )
38
+ // contract that implements ERC677Receiver executes the callback
39
+ const txsBefore = await recipient . txCount ( )
40
+ await token . transferAndCall ( recipient . address , parseEther ( "1" ) , "0x6c6f6c" )
41
+ const txsAfter = await recipient . txCount ( )
49
42
50
- //if callback returns true or false, shouldnt revert
51
- await token . transferAndCall ( recipient3 . address , parseEther ( "1" ) , errData )
52
- await token . transferAndCall ( recipient3 . address , parseEther ( "1" ) , "0x" )
43
+ // callback returns true or false but doesn't revert => should NOT revert
44
+ const txsBeforeBool = await returnBoolRecipient . txCount ( )
45
+ await token . transferAndCall ( returnBoolRecipient . address , parseEther ( "1" ) , errData )
46
+ await token . transferAndCall ( returnBoolRecipient . address , parseEther ( "1" ) , "0x" )
47
+ const txsAfterBool = await returnBoolRecipient . txCount ( )
53
48
54
- after = await recipient3 . txCount ( )
55
- expect ( after ) . to . equal ( before . add ( 2 ) )
56
-
49
+ expect ( txsAfter ) . to . equal ( txsBefore . add ( 1 ) )
50
+ expect ( txsAfterBool ) . to . equal ( txsBeforeBool . add ( 2 ) )
57
51
} )
58
52
59
53
it ( "transferAndCall just does normal transfer for non-contract accounts" , async ( ) => {
0 commit comments