/
Challenge3.t.sol
105 lines (84 loc) · 4.29 KB
/
Challenge3.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
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "forge-std/console.sol";
import "forge-std/Test.sol";
import {LendingPool} from "../src/3_LendingPool/LendingPool.sol";
import {Create2Deployer} from "../src/3_LendingPool/Create2Deployer.sol";
import {CreateDeployer} from "../src/3_LendingPool/CreateDeployer.sol";
import {LendingHack} from "../src/3_LendingPool/LendingHack.sol";
import {USDC} from "../src/3_LendingPool/USDC.sol";
import {LendExGovernor} from "../src/3_LendingPool/LendExGovernor.sol";
/*////////////////////////////////////////////////////////////
// DEFINE ANY NECESSARY CONTRACTS HERE //
// If you need a contract for your hack, define it below //
////////////////////////////////////////////////////////////*/
/*////////////////////////////////////////////////////////////
// TEST CONTRACT //
////////////////////////////////////////////////////////////*/
contract Challenge3Test is Test {
USDC public usdc;
uint256 public usdcAmount = 100000;
LendExGovernor public lendExGovernor;
CreateDeployer public createDeployer;
Create2Deployer public create2Deployer;
LendingPool public lendingPool;
LendingHack public lendingHack;
address public hacker = makeAddr("hacker");
address public governanceOwner = makeAddr("governanceOwner");
function setUp() public {
/* set up actions */
usdc = new USDC(usdcAmount);
usdc.transfer(governanceOwner, usdcAmount);
/* sending transactions as hacker */
// deploying the LendingPool contract
vm.startPrank(hacker);
create2Deployer = new Create2Deployer();
createDeployer = CreateDeployer(create2Deployer.deploy());
lendingPool = LendingPool(createDeployer.deploy(true, address(usdc)));
vm.stopPrank();
/* sending transactions as the governance owner */
// deploying the LendingPool contract
vm.startPrank(governanceOwner);
lendExGovernor = new LendExGovernor(address(usdc));
lendExGovernor.addContract(address(lendingPool));
usdc.transfer(address(lendExGovernor), usdcAmount);
// funding the LendingPool contract with USDC
lendExGovernor.fundLendingPool(address(lendingPool), usdcAmount);
vm.stopPrank();
assertEq(
lendExGovernor.getPoolName(address(lendingPool)),
"LendingPool V1",
"lendingPool contract has the original name"
);
vm.startPrank(hacker);
/*//////////////////////////////////////////////////////////////////////
// Add your hack below! //
// Hack part #1 //
// NOTE!!! Foundry has a bug //
// anything selfdestruct() related has to be in the setUp() function //
// //
// terminal command to run the speciffic test: //
// forge test --match-contract Challenge3Test -vvvv //
//////////////////////////////////////////////////////////////////////*/
//====================================================================//
vm.stopPrank();
}
function testExploit() public {
vm.startPrank(hacker);
/*//////////////////////////////////////////////////////////////
// Add your hack below! //
// Hack part #2 //
// Please implement necessary changes also to LendingHack.sol //
// terminal command to run the specific test: //
// forge test --match-contract Challenge3Test -vvvv //
//////////////////////////////////////////////////////////////*/
//=============================================================//
vm.stopPrank();
assertEq(
lendExGovernor.getPoolName(address(lendingPool)),
"LendingPool hack",
"lendingPool contract address now gives a different name"
);
assertEq(usdc.balanceOf(hacker), usdcAmount, "Hacker has to steal all the stablecoin tokens");
}
}