Lens.t.sol
The read aggregator returns what the frontend expects.
129 lines4.5 KBSolidity
| 1 | // SPDX-License-Identifier: MIT |
| 2 | pragma solidity ^0.8.24; |
| 3 | |
| 4 | import {Base} from "./Base.t.sol"; |
| 5 | import {K401Lens} from "../src/K401Lens.sol"; |
| 6 | import {Symbol, SeatMode} from "../src/interfaces/IK401Interfaces.sol"; |
| 7 | |
| 8 | contract LensTest is Base { |
| 9 | uint8 internal constant NVDA = uint8(Symbol.NVDA); |
| 10 | |
| 11 | function test_getProtocolStatsBeforeTheOracleIsLive() public view { |
| 12 | K401Lens.ProtocolStats memory s = lens.getProtocolStats(); |
| 13 | assertFalse(s.oracleOk, "stale oracle surfaces as a flag, not a revert"); |
| 14 | assertEq(s.marketPrice, 0); |
| 15 | assertEq(s.nav, 1.2e18); |
| 16 | assertEq(s.rfv, 1.2e18); |
| 17 | assertEq(s.totalSupply, INITIAL_SUPPLY); |
| 18 | assertEq(s.index, 1e18); |
| 19 | } |
| 20 | |
| 21 | function test_getProtocolStatsWhenLive() public { |
| 22 | _primeOracle(); |
| 23 | _giveSeats(alice, 10); |
| 24 | uint256[] memory ids = k401.seatsOf(alice); |
| 25 | vm.prank(alice); |
| 26 | staking.clockIn(_ids(ids[0], ids[1])); |
| 27 | |
| 28 | K401Lens.ProtocolStats memory s = lens.getProtocolStats(); |
| 29 | assertTrue(s.oracleOk); |
| 30 | assertApproxEqRel(s.marketPrice, 2e18, 0.01e18); |
| 31 | assertEq(s.nav, 1.2e18); |
| 32 | assertEq(s.rfv, 1.2e18); |
| 33 | assertApproxEqRel(s.premium, 1.666e18, 0.01e18); |
| 34 | assertGt(s.epochRate, 0); |
| 35 | assertGt(s.apy, 0); |
| 36 | assertEq(s.seatCount, 10); |
| 37 | assertEq(s.treasuryUsdg, 1_200_000e18); |
| 38 | assertEq(s.treasuryEquityUsd, 0); |
| 39 | assertEq(s.nextEpochTs, distributor.nextEpochTs()); |
| 40 | assertEq(s.totalStaked, 2e18); |
| 41 | } |
| 42 | |
| 43 | function test_getSeatsReturnsTheFullPerSeatShape() public { |
| 44 | _primeOracle(); |
| 45 | _giveSeats(alice, 3); |
| 46 | uint256[] memory ids = k401.seatsOf(alice); |
| 47 | vm.prank(alice); |
| 48 | staking.clockIn(_ids(ids[0])); |
| 49 | |
| 50 | usdg.mint(address(stockDesk), 10_000e6); |
| 51 | stockDesk.executeBatch(NVDA, 10_000e6, 0); |
| 52 | stockDesk.deliver(ids[1], _syms(NVDA)); |
| 53 | |
| 54 | K401Lens.Seat[] memory seats = lens.getSeats(alice); |
| 55 | assertEq(seats.length, 3); |
| 56 | |
| 57 | for (uint256 i; i < seats.length; ++i) { |
| 58 | assertEq(seats[i].tier, 1); |
| 59 | assertEq(seats[i].multiplier, 1e18); |
| 60 | assertEq(seats[i].tbaAddress, seat6551.tbaOf(seats[i].tokenId)); |
| 61 | assertEq(seats[i].stockWeightsBps[0], 10_000); |
| 62 | assertEq(seats[i].stockSymbols[0], NVDA); |
| 63 | } |
| 64 | |
| 65 | // The clocked-in Seat. |
| 66 | K401Lens.Seat memory clocked = lens.getSeat(ids[0]); |
| 67 | assertEq(uint8(clocked.mode), uint8(SeatMode.ON_THE_CLOCK)); |
| 68 | assertTrue(clocked.locked); |
| 69 | |
| 70 | // The Seat that received equity. |
| 71 | K401Lens.Seat memory withStock = lens.getSeat(ids[1]); |
| 72 | assertTrue(withStock.tbaDeployed); |
| 73 | assertEq(withStock.holdings.length, 1); |
| 74 | assertEq(withStock.holdings[0].symbol, NVDA); |
| 75 | assertGt(withStock.holdings[0].qty, 0); |
| 76 | assertGt(withStock.holdings[0].usdValue, 0); |
| 77 | assertGt(withStock.lifetimeStockUsd, 0); |
| 78 | } |
| 79 | |
| 80 | function test_getBondMarkets() public { |
| 81 | _primeOracle(); |
| 82 | vm.startPrank(owner); |
| 83 | bonds.createMarket(address(usdg), false, 500, 100); |
| 84 | bonds.createMarket(address(pair), true, 750, 50); |
| 85 | vm.stopPrank(); |
| 86 | |
| 87 | K401Lens.BondMarket[] memory m = lens.getBondMarkets(); |
| 88 | assertEq(m.length, 2); |
| 89 | |
| 90 | assertEq(m[0].id, 0); |
| 91 | assertEq(m[0].principal, address(usdg)); |
| 92 | assertFalse(m[0].isLp); |
| 93 | assertTrue(m[0].active); |
| 94 | assertTrue(m[0].priceOk); |
| 95 | assertEq(m[0].discountBps, 500); |
| 96 | assertEq(m[0].vestingDays, 5); |
| 97 | assertEq(m[0].capacityLeft, (k401.totalSupply() * 100) / 10_000); |
| 98 | assertGe(m[0].price, treasury.nav()); |
| 99 | |
| 100 | assertTrue(m[1].isLp); |
| 101 | assertEq(m[1].discountBps, 750); |
| 102 | } |
| 103 | |
| 104 | function test_getLeaderboard() public { |
| 105 | _giveSeats(alice, 5); |
| 106 | _giveSeats(bob, 2); |
| 107 | |
| 108 | uint256[] memory aIds = k401.seatsOf(alice); |
| 109 | vm.prank(alice); |
| 110 | registry.fuse(_ids(aIds[0], aIds[1], aIds[2])); // one tier-2 Seat |
| 111 | |
| 112 | address[] memory candidates = new address[](3); |
| 113 | candidates[0] = alice; |
| 114 | candidates[1] = bob; |
| 115 | candidates[2] = carol; |
| 116 | |
| 117 | K401Lens.LeaderboardRow[] memory rows = lens.getLeaderboard(candidates); |
| 118 | assertEq(rows.length, 3); |
| 119 | assertEq(rows[0].account, alice); |
| 120 | assertEq(rows[0].seats, 3); |
| 121 | // tiers 2,1,1 -> average 1.33 |
| 122 | assertEq(rows[0].avgTierX100, 133); |
| 123 | assertEq(rows[1].seats, 2); |
| 124 | assertEq(rows[1].avgTierX100, 100); |
| 125 | assertEq(rows[2].seats, 0); |
| 126 | assertEq(rows[2].avgTierX100, 0); |
| 127 | } |
| 128 | } |
| 129 |
Click any line number to deep-link to it — the target line highlights on load.