-
Notifications
You must be signed in to change notification settings - Fork 2
/
Reentrance.t.sol
36 lines (30 loc) · 1.21 KB
/
Reentrance.t.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import "../src/10-Reentrancy/ReentranceFactory.sol";
import "../src/10-Reentrancy/ReentranceAttacker.sol";
import "../src/Ethernaut.sol";
contract ReentranceTest is Test {
Ethernaut ethernaut;
address eoaAddress = address(100);
function setUp() public {
ethernaut = new Ethernaut();
vm.deal(eoaAddress, 5 ether);
}
function testReentranceHack() public {
// Level setup
ReentranceFactory reentranceFactory = new ReentranceFactory();
ethernaut.registerLevel(reentranceFactory);
vm.startPrank(eoaAddress);
address levelAddress = ethernaut.createLevelInstance{value: 1_500_000_000 gwei}(reentranceFactory); // 1.5 ether
Reentrance ethernautReentrance = Reentrance(payable(levelAddress));
// Level attack
ReentranceAttacker attacker = new ReentranceAttacker(levelAddress);
bool success = attacker.attack{value: 1 ether}();
assertTrue(success);
// Level submission
bool levelSuccessfullyPassed = ethernaut.submitLevelInstance(payable(levelAddress));
vm.stopPrank();
assert(levelSuccessfullyPassed);
}
}