Skip to content

Reading Elysium

Mine

Handler.sol

183 lines · sha256 841d2fe42300ae84ce28cf5d26e7926fd5479331b99338c4a909208a0bb5cdbe · Raw

1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.26;
3
4import {Test} from "forge-std/Test.sol";
5import {PoolSwapTest} from "@uniswap/v4-core/src/test/PoolSwapTest.sol";
6import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol";
7import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
8import {KibbleToken} from "../../src/KibbleToken.sol";
9import {Emission} from "../../src/Emission.sol";
10import {Genesis} from "../../src/Genesis.sol";
11import {Miner} from "../../src/Miner.sol";
12import {Oracle} from "../../src/Oracle.sol";
13import {LpLocker} from "../../src/LpLocker.sol";
14import {KibblePool} from "../../src/libraries/KibblePool.sol";
15
16/// @notice Random protocol actions for fuzz / invariant runs.
17contract Handler is Test {
18 KibbleToken public token;
19 Emission public emission;
20 Genesis public genesis;
21 Miner public miner;
22 Oracle public oracle;
23 LpLocker public locker;
24 PoolSwapTest public swapRouter;
25
26 address[] public actors;
27 uint256 public maxLiquiditySeen;
28 uint256 public calls;
29
30 constructor(KibbleToken t, Emission e, Genesis g, Miner m, Oracle o, LpLocker l, PoolSwapTest sr) {
31 token = t;
32 emission = e;
33 genesis = g;
34 miner = m;
35 oracle = o;
36 locker = l;
37 swapRouter = sr;
38 for (uint256 i; i < 4; ++i) {
39 actors.push(address(uint160(0xA11CE0 + i)));
40 }
41 }
42
43 function actorsLength() external view returns (uint256) {
44 return actors.length;
45 }
46
47 function _actor(uint256 seed) internal view returns (address) {
48 return actors[seed % actors.length];
49 }
50
51 function _track() internal {
52 ++calls;
53 uint256 l = locker.totalLiquidity();
54 require(l >= maxLiquiditySeen, "LP liquidity decreased");
55 maxLiquiditySeen = l;
56 }
57
58 function buyRigs(uint256 seed, uint256 amount) external {
59 address u = _actor(seed);
60 amount = bound(amount, 1, 2_000);
61 vm.deal(u, u.balance + amount * 1e18);
62 vm.prank(u);
63 miner.buyRigs{value: amount * 1e18}(amount);
64 _track();
65 }
66
67 function depositPower(uint256 seed, uint256 amount) external {
68 address u = _actor(seed);
69 amount = bound(amount, 1, 500e18);
70 vm.deal(u, u.balance + amount);
71 vm.prank(u);
72 miner.depositPower{value: amount}();
73 _track();
74 }
75
76 function depositPowerKibble(uint256 seed, uint256 amount) external {
77 if (!genesis.miningStarted()) return;
78 address u = _actor(seed);
79 uint256 bal = token.balanceOf(u);
80 if (bal == 0) return;
81 amount = bound(amount, 1, bal);
82 if (amount * oracle.twap() * 10 * 60_000 / (9 * 1e18) == 0) return;
83 vm.startPrank(u);
84 token.approve(address(miner), amount);
85 miner.depositPowerKibble(amount);
86 vm.stopPrank();
87 _track();
88 }
89
90 function withdrawPower(uint256 seed, uint256 amount, bool all) external {
91 address u = _actor(seed);
92 (,,, uint256 total) = miner.tankOf(u);
93 if (all) amount = type(uint256).max;
94 else if (total == 0) return;
95 else amount = bound(amount, 1, total);
96 uint256 kibBefore = token.balanceOf(u);
97 (,, uint256 kibInTank,) = miner.tankOf(u);
98 vm.prank(u);
99 miner.withdrawPower(amount);
100 // KIBBLE never comes back above what is in the KIBBLE-backed tank
101 require(token.balanceOf(u) - kibBefore <= kibInTank, "kibble out > tank");
102 _track();
103 }
104
105 function setRigsOn(uint256 seed, uint256 count) external {
106 address u = _actor(seed);
107 uint256 rigs = miner.rigsOf(u);
108 count = bound(count, 0, rigs);
109 (,,, uint256 total) = miner.tankOf(u);
110 if (count > 0 && total * 60_000 / (count * 1e18) == 0) return;
111 vm.prank(u);
112 miner.setRigsOn(count);
113 _track();
114 }
115
116 function claim(uint256 seed) external {
117 address u = _actor(seed);
118 uint256 p = miner.pending(u);
119 vm.prank(u);
120 uint256 got = miner.claim();
121 require(got == p, "claim != pending view");
122 _track();
123 }
124
125 function roll(uint256 blocks) external {
126 blocks = bound(blocks, 1, 120_000);
127 vm.roll(block.number + blocks);
128 _track();
129 }
130
131 function checkpoint() external {
132 miner.checkpoint();
133 uint256 sum;
134 for (uint256 i; i < actors.length; ++i) {
135 sum += miner.rigsOnOf(actors[i]);
136 }
137 require(miner.onlineHashrateAccounted() == sum, "hashrate != sum rigsOn");
138 _track();
139 }
140
141 function startMining() external {
142 if (genesis.miningStarted() || miner.totalRigs() == 0) return;
143 if (block.number < genesis.preorderEndBlock()) vm.roll(genesis.preorderEndBlock());
144 genesis.startMining();
145 _track();
146 }
147
148 function flushBurn() external {
149 miner.flushBurn();
150 _track();
151 }
152
153 function settle(uint256 seed) external {
154 miner.settle(_actor(seed));
155 _track();
156 }
157
158 function marketSwap(uint256 seed, bool buyKibble, uint256 amount) external {
159 if (!genesis.miningStarted()) return;
160 address u = _actor(seed);
161 PoolSwapTest.TestSettings memory ts = PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false});
162 if (buyKibble) {
163 amount = bound(amount, 1e15, 200e18);
164 vm.deal(u, u.balance + amount);
165 vm.prank(u);
166 swapRouter.swap{value: amount}(
167 KibblePool.key(address(token)), SwapParams(true, -int256(amount), TickMath.MIN_SQRT_PRICE + 1), ts, ""
168 );
169 } else {
170 uint256 bal = token.balanceOf(u);
171 if (bal < 1e15) return;
172 amount = bound(amount, 1e15, bal);
173 vm.startPrank(u);
174 token.approve(address(swapRouter), amount);
175 swapRouter.swap(
176 KibblePool.key(address(token)), SwapParams(false, -int256(amount), TickMath.MAX_SQRT_PRICE - 1), ts, ""
177 );
178 vm.stopPrank();
179 }
180 _track();
181 }
182}
183