KibbleBase.sol
121 lines · sha256 bf55d20ec0397872a833238b51937fee2365c8cb09474f94affbb7d3a81d42e6 · Raw
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.26; |
| 3 | |
| 4 | import {Test} from "forge-std/Test.sol"; |
| 5 | import {PoolManager} from "@uniswap/v4-core/src/PoolManager.sol"; |
| 6 | import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; |
| 7 | import {PoolSwapTest} from "@uniswap/v4-core/src/test/PoolSwapTest.sol"; |
| 8 | import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; |
| 9 | import {SwapParams} from "@uniswap/v4-core/src/types/PoolOperation.sol"; |
| 10 | import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; |
| 11 | import {KibbleDeployer} from "../../script/KibbleDeployer.sol"; |
| 12 | import {KibbleToken} from "../../src/KibbleToken.sol"; |
| 13 | import {Emission} from "../../src/Emission.sol"; |
| 14 | import {Genesis} from "../../src/Genesis.sol"; |
| 15 | import {Miner} from "../../src/Miner.sol"; |
| 16 | import {Zapper} from "../../src/Zapper.sol"; |
| 17 | import {Burner} from "../../src/Burner.sol"; |
| 18 | import {Oracle} from "../../src/Oracle.sol"; |
| 19 | import {LpLocker} from "../../src/LpLocker.sol"; |
| 20 | import {KibblePool} from "../../src/libraries/KibblePool.sol"; |
| 21 | |
| 22 | /// @notice Deploys a real v4 PoolManager and the full KIBBLE system. |
| 23 | abstract contract KibbleBase is Test { |
| 24 | uint256 internal constant END = 1_000; |
| 25 | uint256 internal constant ERA = 216_000; |
| 26 | |
| 27 | PoolManager internal pm; |
| 28 | PoolSwapTest internal swapRouter; |
| 29 | KibbleToken internal token; |
| 30 | Emission internal emission; |
| 31 | Genesis internal genesis; |
| 32 | Miner internal miner; |
| 33 | Zapper internal zapper; |
| 34 | Burner internal burner; |
| 35 | Oracle internal oracle; |
| 36 | LpLocker internal locker; |
| 37 | |
| 38 | address internal alice = makeAddr("alice"); |
| 39 | address internal bob = makeAddr("bob"); |
| 40 | address internal carol = makeAddr("carol"); |
| 41 | |
| 42 | function setUp() public virtual { |
| 43 | vm.roll(10); |
| 44 | pm = new PoolManager(address(this)); |
| 45 | swapRouter = new PoolSwapTest(IPoolManager(address(pm))); |
| 46 | KibbleDeployer.System memory s = KibbleDeployer.deploy(IPoolManager(address(pm)), END, address(this)); |
| 47 | token = s.token; |
| 48 | emission = s.emission; |
| 49 | genesis = s.genesis; |
| 50 | miner = s.miner; |
| 51 | zapper = s.zapper; |
| 52 | burner = s.burner; |
| 53 | oracle = s.oracle; |
| 54 | locker = s.lpLocker; |
| 55 | } |
| 56 | |
| 57 | // ------------------------------------------------------------ helpers |
| 58 | |
| 59 | function _buy(address user, uint256 n) internal { |
| 60 | vm.deal(user, user.balance + n * 1e18); |
| 61 | vm.prank(user); |
| 62 | miner.buyRigs{value: n * 1e18}(n); |
| 63 | } |
| 64 | |
| 65 | function _power(address user, uint256 amount) internal { |
| 66 | vm.deal(user, user.balance + amount); |
| 67 | vm.prank(user); |
| 68 | miner.depositPower{value: amount}(); |
| 69 | } |
| 70 | |
| 71 | function _on(address user, uint256 n) internal { |
| 72 | vm.prank(user); |
| 73 | miner.setRigsOn(n); |
| 74 | } |
| 75 | |
| 76 | function _claim(address user) internal returns (uint256) { |
| 77 | vm.prank(user); |
| 78 | return miner.claim(); |
| 79 | } |
| 80 | |
| 81 | function _start() internal { |
| 82 | if (block.number < END) vm.roll(END); |
| 83 | genesis.startMining(); |
| 84 | } |
| 85 | |
| 86 | function _roll(uint256 blocks) internal { |
| 87 | vm.roll(block.number + blocks); |
| 88 | } |
| 89 | |
| 90 | function _key() internal view returns (PoolKey memory) { |
| 91 | return KibblePool.key(address(token)); |
| 92 | } |
| 93 | |
| 94 | /// @dev Market swap by a third party. zeroForOne = sell HYPE for KIBBLE. |
| 95 | function _marketSwap(address trader, bool zeroForOne, uint256 amountIn) internal { |
| 96 | PoolSwapTest.TestSettings memory ts = PoolSwapTest.TestSettings({takeClaims: false, settleUsingBurn: false}); |
| 97 | SwapParams memory p = SwapParams({ |
| 98 | zeroForOne: zeroForOne, |
| 99 | amountSpecified: -int256(amountIn), |
| 100 | sqrtPriceLimitX96: zeroForOne ? TickMath.MIN_SQRT_PRICE + 1 : TickMath.MAX_SQRT_PRICE - 1 |
| 101 | }); |
| 102 | vm.startPrank(trader); |
| 103 | if (zeroForOne) { |
| 104 | vm.deal(trader, trader.balance + amountIn); |
| 105 | swapRouter.swap{value: amountIn}(_key(), p, ts, ""); |
| 106 | } else { |
| 107 | token.approve(address(swapRouter), amountIn); |
| 108 | swapRouter.swap(_key(), p, ts, ""); |
| 109 | } |
| 110 | vm.stopPrank(); |
| 111 | } |
| 112 | |
| 113 | /// @dev Standard launch: `rigs` preordered by alice, powered for one full era, all on, mining started. |
| 114 | function _launch(uint256 rigs) internal { |
| 115 | _buy(alice, rigs); |
| 116 | _power(alice, rigs * 36e17); |
| 117 | _on(alice, rigs); |
| 118 | _start(); |
| 119 | } |
| 120 | } |
| 121 |