SKIP TO CONTENT

Demo build — mock protocol data. No chain, no ABIs yet.

  • SYNCING: ……

StockDesk.t.sol

Batched buys, the slippage guard, and pro-rata distribution by multiplier.

158 lines5.8 KBSolidity
Source of test/StockDesk.t.sol, 158 lines of Solidity
1// SPDX-License-Identifier: MIT
2pragma solidity ^0.8.24;
3 
4import {Base} from "./Base.t.sol";
5import {K401StockDesk} from "../src/K401StockDesk.sol";
6import {K401Seat6551Account} from "../src/K401Seat6551.sol";
7import {Symbol} from "../src/interfaces/IK401Interfaces.sol";
8 
9contract StockDeskTest is Base {
10 uint8 internal constant NVDA = uint8(Symbol.NVDA);
11 uint8 internal constant AAPL = uint8(Symbol.AAPL);
12 
13 function setUp() public override {
14 super.setUp();
15 _primeOracle();
16 }
17 
18 function test_hardSlippageCeilingIsAConstant() public view {
19 assertEq(stockDesk.MAX_SLIPPAGE_BPS(), 300);
20 assertEq(stockDesk.MIN_BATCH_INTERVAL(), 1 hours);
21 }
22 
23 function test_batchBuysAndIndexesToVestedSeats() public {
24 _giveSeats(alice, 2);
25 _giveSeats(bob, 1);
26 assertEq(registry.weightedMultiplier(NVDA), 3e18);
27 
28 usdg.mint(address(stockDesk), 30_000e6);
29 uint256 bought = stockDesk.executeBatch(NVDA, 30_000e6, 0);
30 assertGt(bought, 0);
31 
32 uint256[] memory aIds = k401.seatsOf(alice);
33 uint256 claimA = stockDesk.claimable(aIds[0], NVDA);
34 assertApproxEqRel(claimA, bought / 3, 0.0001e18, "pro-rata by multiplier");
35 }
36 
37 function test_batchRespectsTheOneHourInterval() public {
38 _giveSeats(alice, 1);
39 usdg.mint(address(stockDesk), 20_000e6);
40 stockDesk.executeBatch(NVDA, 5_000e6, 0);
41 
42 vm.expectRevert(K401StockDesk.BatchTooSoon.selector);
43 stockDesk.executeBatch(NVDA, 5_000e6, 0);
44 
45 // A different symbol has its own clock.
46 uint256 id = k401.seatsOf(alice)[0];
47 vm.prank(alice);
48 registry.setStocks(id, [AAPL, AAPL, AAPL], [uint16(10_000), 0, 0]);
49 stockDesk.executeBatch(AAPL, 5_000e6, 0);
50 
51 _skip(1 hours);
52 vm.prank(alice);
53 registry.setStocks(id, [NVDA, NVDA, NVDA], [uint16(10_000), 0, 0]);
54 stockDesk.executeBatch(NVDA, 5_000e6, 0);
55 }
56 
57 /// @dev Thin pool: the batch must revert and the desk must keep holding USDG.
58 function test_thinPoolHoldsUsdgRatherThanTrading() public {
59 _giveSeats(alice, 1);
60 // A batch this large against the mock pool moves price far past 300 bps.
61 usdg.mint(address(stockDesk), 4_000_000e6);
62 uint256 before = usdg.balanceOf(address(stockDesk));
63 
64 vm.expectRevert();
65 stockDesk.executeBatch(NVDA, 4_000_000e6, 0);
66 
67 assertEq(usdg.balanceOf(address(stockDesk)), before, "USDG held, not donated to MEV");
68 assertEq(nvda.balanceOf(address(stockDesk)), 0);
69 }
70 
71 function test_batchRefusesWithNoVestedSeats() public {
72 usdg.mint(address(stockDesk), 10_000e6);
73 vm.expectRevert(K401StockDesk.NoVestedSeats.selector);
74 stockDesk.executeBatch(NVDA, 10_000e6, 0);
75 }
76 
77 function test_deliverLazilyDeploysTheTbaAndCreditsLifetimeUsd() public {
78 _giveSeats(alice, 1);
79 uint256 id = k401.seatsOf(alice)[0];
80 
81 address predicted = seat6551.tbaOf(id);
82 assertEq(predicted.code.length, 0, "TBA is NOT deployed on mint");
83 assertFalse(seat6551.isDeployed(id));
84 
85 usdg.mint(address(stockDesk), 10_000e6);
86 stockDesk.executeBatch(NVDA, 10_000e6, 0);
87 
88 address tba = stockDesk.deliver(id, _syms(NVDA));
89 assertEq(tba, predicted, "deterministic address");
90 assertGt(tba.code.length, 0, "deployed on first delivery");
91 assertTrue(seat6551.isDeployed(id));
92 assertGt(nvda.balanceOf(tba), 0);
93 assertGt(registry.lifetimeStockUsd(id), 0);
94 
95 // Nothing left to deliver.
96 vm.expectRevert(K401StockDesk.NothingToDeliver.selector);
97 stockDesk.deliver(id, _syms(NVDA));
98 }
99 
100 function test_clockedInSeatsEarnNoStock() public {
101 _giveSeats(alice, 1);
102 _giveSeats(bob, 1);
103 uint256 aliceId = k401.seatsOf(alice)[0];
104 uint256 bobId = k401.seatsOf(bob)[0];
105 
106 vm.prank(alice);
107 staking.clockIn(_ids(aliceId));
108 
109 usdg.mint(address(stockDesk), 10_000e6);
110 uint256 bought = stockDesk.executeBatch(NVDA, 10_000e6, 0);
111 
112 assertEq(stockDesk.claimable(aliceId, NVDA), 0, "ON_THE_CLOCK Seats do not earn stock");
113 assertApproxEqRel(stockDesk.claimable(bobId, NVDA), bought, 0.0001e18, "bob takes the whole batch");
114 }
115 
116 function test_higherTierEarnsMoreStock() public {
117 _fundNoSeats(alice, 100_000e18);
118 uint256 aliceId = _giveSeatNfts(alice, 1)[0];
119 _giveSeats(bob, 1);
120 uint256 bobId = k401.seatsOf(bob)[0];
121 
122 vm.prank(alice);
123 registry.upgradeTier(aliceId); // 1.00x -> 1.25x
124 assertEq(registry.weightedMultiplier(NVDA), 2.25e18);
125 
126 usdg.mint(address(stockDesk), 22_500e6);
127 stockDesk.executeBatch(NVDA, 22_500e6, 0);
128 
129 
130 uint256 a = stockDesk.claimable(aliceId, NVDA);
131 uint256 b = stockDesk.claimable(bobId, NVDA);
132 assertApproxEqRel(a, (b * 125) / 100, 0.001e18, "tier 2 earns 1.25x tier 1");
133 }
134 
135 /// @dev A tier change must not retroactively re-price already accrued stock.
136 function test_settleOnTierChangeDoesNotRewriteHistory() public {
137 _fundNoSeats(alice, 100_000e18);
138 uint256 id = _giveSeatNfts(alice, 1)[0];
139 
140 usdg.mint(address(stockDesk), 20_000e6);
141 stockDesk.executeBatch(NVDA, 10_000e6, 0);
142 uint256 beforeUpgrade = stockDesk.claimable(id, NVDA);
143 
144 vm.prank(alice);
145 registry.upgradeTier(id);
146 
147 assertEq(stockDesk.claimable(id, NVDA), beforeUpgrade, "prior accrual is frozen at the old multiplier");
148 }
149 
150 function test_symbolBoundsAreEnforced() public {
151 usdg.mint(address(stockDesk), 1_000e6);
152 vm.expectRevert(K401StockDesk.BadSymbol.selector);
153 stockDesk.executeBatch(uint8(Symbol.USDG), 1_000e6, 0);
154 vm.expectRevert(K401StockDesk.BadSymbol.selector);
155 stockDesk.executeBatch(200, 1_000e6, 0);
156 }
157}
158 

Click any line number to deep-link to it — the target line highlights on load.