// SPDX-License-Identifier: MIT pragma solidity ^0.8.26; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {FullMath} from "@uniswap/v4-core/src/libraries/FullMath.sol"; import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol"; import {KibbleToken} from "./KibbleToken.sol"; import {Emission} from "./Emission.sol"; import {Genesis} from "./Genesis.sol"; import {Oracle} from "./Oracle.sol"; import {Zapper} from "./Zapper.sol"; import {Burner} from "./Burner.sol"; import {EmissionMath} from "./libraries/EmissionMath.sol"; import {ExpiryBitmap} from "./libraries/ExpiryBitmap.sol"; import {ChainBlock} from "./libraries/ChainBlock.sol"; import {KibbleConstants as K} from "./libraries/KibbleConstants.sol"; /// @title Miner /// @notice Rig balances, on/off state, power tanks, reward accounting, claims (docs §3, §4, §11, §12, §13). /// /// @dev Time. All accounting runs in "mining time" t = block - block0 (0 before block 0). Preordered rigs can be /// powered and switched on before block 0, but mining time stays 0 until block 0, so nothing is drawn or mined /// before it (docs §6). /// /// Power units. Tanks are stored in units of wei × 60,000, so one rig drains exactly 1e18 units per block /// (1/60,000 HYPE) and a full era is exactly 3.6 HYPE per rig. Withdrawals floor back to wei. /// /// Two sub-tanks. `hypeUnits` (deposited in HYPE) and `kibbleUnits` (deposited in KIBBLE, credited at /// KIBBLE × TWAP × 10/9, docs §4) with `kibbleAmount`, the KIBBLE backing `kibbleUnits`. Consuming x units /// of the KIBBLE sub-tank burns kibbleAmount × x / kibbleUnits KIBBLE, so unused KIBBLE power is returned at /// the deposit rate (docs §13). Several KIBBLE deposits are pooled at their weighted average rate. /// Consumption order: KIBBLE-backed power first, then HYPE-backed. Withdrawal order: HYPE-backed first. /// /// Rewards (docs §12). accRewardPerHash += reward(segment) × 1e36 / onlineHashrate, per era segment. /// Blocks with zero online hashrate add nothing and mint nothing. /// /// Expiry buckets (docs §12). A wallet with r rigs on and U units has an empty block E = t + U / (r × 1e18). /// r is registered in bucket E. When accounting advances, buckets are processed in increasing E: rewards are /// accrued up to E, accRewardPerHash is snapshotted for E, then r leaves the online total. Non-empty buckets /// are indexed by a 4-level bitmap (ExpiryBitmap) so empty block ranges are skipped in O(1). /// Cost: O(number of distinct non-empty buckets crossed), about 30k gas each. If many buckets accumulated /// without any interaction, `checkpointPartial(maxBuckets)` advances in bounded steps. /// /// Burns are deferred: consumed HYPE / KIBBLE accrue in `pendingBurnHypeUnits` / `pendingBurnKibble` and /// `flushBurn()` (public) sends them to the `Burner`. contract Miner is ReentrancyGuard { using SafeERC20 for IERC20; using ExpiryBitmap for ExpiryBitmap.Bitmap; // ------------------------------------------------------------------ errors error ZeroAmount(); error WrongPayment(); error TooManyRigs(); error EmptyTank(); error InsufficientPower(); error MiningNotStarted(); error NoTwap(); error TransferFailed(); // ------------------------------------------------------------------ events (docs §11) event RigsBought(address user, uint256 amount, uint256 hypePaid); event PowerDeposited(address user, uint256 hypeEquivalent, bool inKibble); event PowerWithdrawn(address user, uint256 hypeEquivalent); event RigsSwitched(address user, uint256 rigsOn); event TankEmpty(address user, uint256 blockNumber); event Claimed(address user, uint256 amount); uint256 public constant ACC_PRECISION = 1e36; uint256 internal constant UNITS_PER_WEI = K.DRAW_DIVISOR; uint256 internal constant UNITS_PER_RIG_BLOCK = K.UNITS_PER_RIG_BLOCK; /// @dev Packed in 4 slots. Bounds: rigs < 2^64 (HYPE supply), power units < 2^128 (> 5e9 HYPE), /// KIBBLE < 2^128 (> max supply), mining time < 2^64. Writes go through SafeCast. struct Account { uint64 rigs; uint64 rigsOn; // configured on-count; online while expiry > now uint64 expiry; // mining-time empty block, valid while rigsOn > 0 uint64 lastT; uint128 hypeUnits; uint128 kibbleUnits; uint128 kibbleAmount; uint128 pendingReward; uint256 accSnapshot; } KibbleToken public immutable kibble; Emission public immutable emission; Genesis public immutable genesis; Oracle public immutable oracle; Zapper public immutable zapper; Burner public immutable burner; mapping(address => Account) internal _accounts; /// @notice Total rigs ever bought. uint256 public totalRigs; /// @notice Online hashrate as of `lastAccountedT` (use `totalOnlineHashrate()` for the live value). uint256 public onlineHashrateAccounted; /// @notice Reward per unit of hashrate, scaled by ACC_PRECISION, as of `lastAccountedT`. uint256 public accRewardPerHash; /// @notice Mining time up to which global accounting is processed. uint256 public lastAccountedT; /// @notice Emission distributed to online hashrate so far, scaled by ACC_PRECISION. uint256 public totalAllocatedScaled; /// @notice KIBBLE claimed so far. uint256 public totalClaimed; /// @notice Hashrate expiring at each mining-time block. mapping(uint256 => uint256) public expiringHashrate; /// @notice accRewardPerHash at each processed expiry block. mapping(uint256 => uint256) public accAtExpiry; ExpiryBitmap.Bitmap internal _expiries; /// @notice Consumed HYPE power waiting to be sent to the Burner, in power units (wei × 60,000). uint256 public pendingBurnHypeUnits; /// @notice Consumed KIBBLE power waiting to be burned. uint256 public pendingBurnKibble; /// @notice Sum of all HYPE-backed tanks, power units (as of each wallet's last settlement). uint256 public totalHypeUnits; /// @notice Sum of all KIBBLE held for KIBBLE-backed tanks (as of each wallet's last settlement). uint256 public totalKibbleBacking; uint256 internal _block0; uint256 internal _announcedEra; constructor( KibbleToken kibble_, Emission emission_, Genesis genesis_, Oracle oracle_, Zapper zapper_, Burner burner_ ) { kibble = kibble_; emission = emission_; genesis = genesis_; oracle = oracle_; zapper = zapper_; burner = burner_; } // ================================================================== rigs (docs §3) /// @notice Buys `amount` rigs for `amount` × 1 HYPE (docs §3, §11). Before block 0 the HYPE goes to `Genesis` /// (seeds the pool at block 0); after block 0 it is zapped into locked LP by `Zapper`. /// Rigs are non-transferable and non-refundable. function buyRigs(uint256 amount) external payable nonReentrant { if (amount == 0) revert ZeroAmount(); if (msg.value != amount * K.RIG_PRICE) revert WrongPayment(); _sync(); Account storage acct = _accounts[msg.sender]; acct.rigs = SafeCast.toUint64(acct.rigs + amount); totalRigs += amount; emit RigsBought(msg.sender, amount, msg.value); if (genesis.miningStarted()) { zapper.zap{value: msg.value}(); } else { genesis.depositPreorder{value: msg.value}(); } } // ================================================================== power (docs §4) /// @notice Loads the caller's tank with `msg.value` HYPE (docs §4, §11). function depositPower() external payable nonReentrant { if (msg.value == 0) revert ZeroAmount(); uint256 nowT = _sync(); Account storage a = _accounts[msg.sender]; _settle(msg.sender, a, nowT); uint256 r = _unregister(a); uint256 units = msg.value * UNITS_PER_WEI; a.hypeUnits = SafeCast.toUint128(a.hypeUnits + units); totalHypeUnits += units; _register(a, r, nowT); emit PowerDeposited(msg.sender, msg.value, false); } /// @notice Loads the caller's tank with KIBBLE, credited at KIBBLE × TWAP / 0.9 (docs §4, §11). Reverts before /// block 0 (no TWAP). Needs a KIBBLE approval. function depositPowerKibble(uint256 kibbleAmount) external nonReentrant { if (kibbleAmount == 0) revert ZeroAmount(); if (!genesis.miningStarted()) revert MiningNotStarted(); uint256 nowT = _sync(); uint256 price = oracle.twap(); if (price == 0) revert NoTwap(); uint256 units = FullMath.mulDiv(kibbleAmount, price * K.KIBBLE_CREDIT_NUM * UNITS_PER_WEI, K.KIBBLE_CREDIT_DEN * 1e18); if (units == 0) revert ZeroAmount(); IERC20(address(kibble)).safeTransferFrom(msg.sender, address(this), kibbleAmount); Account storage a = _accounts[msg.sender]; _settle(msg.sender, a, nowT); uint256 r = _unregister(a); a.kibbleUnits = SafeCast.toUint128(a.kibbleUnits + units); a.kibbleAmount = SafeCast.toUint128(a.kibbleAmount + kibbleAmount); totalKibbleBacking += kibbleAmount; _register(a, r, nowT); emit PowerDeposited(msg.sender, units / UNITS_PER_WEI, true); } /// @notice Withdraws unused power worth `hypeEquivalent` wei, each part in the asset it was deposited in /// (docs §4, §11, §13). HYPE-backed power is withdrawn first, then KIBBLE-backed power at its deposit /// rate. Pass type(uint256).max to withdraw everything. If what is left cannot power the rigs that are /// on for one more block, they switch off (docs §13). function withdrawPower(uint256 hypeEquivalent) external nonReentrant { if (hypeEquivalent == 0) revert ZeroAmount(); uint256 nowT = _sync(); Account storage a = _accounts[msg.sender]; _settle(msg.sender, a, nowT); uint256 r = _unregister(a); uint256 hypeOut; uint256 kibbleOut; uint256 eqOut; if (hypeEquivalent == type(uint256).max) { uint256 hu = a.hypeUnits; hypeOut = hu / UNITS_PER_WEI; pendingBurnHypeUnits += hu % UNITS_PER_WEI; // sub-wei dust cannot be paid out: it is burned totalHypeUnits -= hu; eqOut = hypeOut + a.kibbleUnits / UNITS_PER_WEI; kibbleOut = a.kibbleAmount; a.hypeUnits = 0; a.kibbleUnits = 0; a.kibbleAmount = 0; totalKibbleBacking -= kibbleOut; } else { uint256 fromHype = a.hypeUnits / UNITS_PER_WEI; if (fromHype > hypeEquivalent) fromHype = hypeEquivalent; a.hypeUnits = uint128(a.hypeUnits - fromHype * UNITS_PER_WEI); totalHypeUnits -= fromHype * UNITS_PER_WEI; hypeOut = fromHype; uint256 restUnits = (hypeEquivalent - fromHype) * UNITS_PER_WEI; if (restUnits > 0) { uint256 ku = a.kibbleUnits; if (restUnits > ku) revert InsufficientPower(); if (ku - restUnits < UNITS_PER_WEI) restUnits = ku; // sub-wei remainder: withdraw the whole sub-tank kibbleOut = restUnits == ku ? a.kibbleAmount : FullMath.mulDiv(a.kibbleAmount, restUnits, ku); a.kibbleUnits = uint128(ku - restUnits); a.kibbleAmount = uint128(a.kibbleAmount - kibbleOut); totalKibbleBacking -= kibbleOut; } eqOut = hypeEquivalent; } if (r > 0 && !_register(a, r, nowT)) emit RigsSwitched(msg.sender, 0); emit PowerWithdrawn(msg.sender, eqOut); if (kibbleOut > 0) IERC20(address(kibble)).safeTransfer(msg.sender, kibbleOut); if (hypeOut > 0) { (bool ok,) = msg.sender.call{value: hypeOut}(""); if (!ok) revert TransferFailed(); } } // ================================================================== mining (docs §4, §11) /// @notice Sets how many of the caller's rigs are on (docs §4, §11). Reverts if `count` exceeds owned rigs, /// or if `count` > 0 and the tank cannot power `count` rigs for one block (docs §13 "switch on with an /// empty tank reverts"). function setRigsOn(uint256 count) external nonReentrant { Account storage a = _accounts[msg.sender]; if (count > a.rigs) revert TooManyRigs(); uint256 nowT = _sync(); _settle(msg.sender, a, nowT); _unregister(a); if (count > 0 && !_register(a, count, nowT)) revert EmptyTank(); emit RigsSwitched(msg.sender, count); } /// @notice Mints the caller's pending KIBBLE to the caller (docs §5 "claimable at any time, no fee, no lock"). function claim() external nonReentrant returns (uint256 amount) { uint256 nowT = _sync(); Account storage a = _accounts[msg.sender]; _settle(msg.sender, a, nowT); amount = a.pendingReward; if (amount == 0) return 0; a.pendingReward = 0; totalClaimed += amount; emission.mint(msg.sender, amount); emit Claimed(msg.sender, amount); } /// @notice Advances global accounting to the current block, processing every expiry bucket (docs §12). /// Anyone can call. function checkpoint() external nonReentrant { _sync(); } /// @notice Advances global accounting processing at most `maxBuckets` expiry buckets. Gas-bounded escape hatch /// if very many buckets accumulated without interaction. Returns true when fully caught up. function checkpointPartial(uint256 maxBuckets) external nonReentrant returns (bool done) { oracle.update(); done = _advance(_nowT(), maxBuckets); _syncEra(); } /// @notice Settles `user` (rewards, power drawn, tank-empty detection) so its consumed power can be burned. /// Anyone can call; it changes nothing the user could not observe through the views. function settle(address user) external nonReentrant { uint256 nowT = _sync(); _settle(user, _accounts[user], nowT); } /// @notice Sends consumed power to the `Burner` and triggers the burn (docs §4, §9). Anyone can call. function flushBurn() external nonReentrant { _sync(); uint256 hypeWei = pendingBurnHypeUnits / UNITS_PER_WEI; pendingBurnHypeUnits -= hypeWei * UNITS_PER_WEI; uint256 kib = pendingBurnKibble; pendingBurnKibble = 0; if (kib > 0) IERC20(address(kibble)).safeTransfer(address(burner), kib); if (hypeWei > 0) { (bool ok,) = address(burner).call{value: hypeWei}(""); if (!ok) revert TransferFailed(); } burner.burn(); } // ================================================================== internal accounting function _currentBlock0() internal returns (uint256 b0) { b0 = _block0; if (b0 == 0) { b0 = genesis.block0(); if (b0 != 0) _block0 = b0; } } function _nowT() internal returns (uint256) { uint256 b0 = _currentBlock0(); return b0 == 0 ? 0 : ChainBlock.number() - b0; } function _sync() internal returns (uint256 nowT) { oracle.update(); nowT = _nowT(); _advance(nowT, type(uint256).max); _syncEra(); } function _syncEra() internal { if (_block0 == 0) return; uint256 e = EmissionMath.eraAt(lastAccountedT); if (e > _announcedEra) { _announcedEra = e; emission.sync(); } } /// @dev Processes expiry buckets in (lastAccountedT, toT] in order, then accrues up to toT. function _advance(uint256 toT, uint256 maxBuckets) internal returns (bool done) { uint256 t = lastAccountedT; if (toT <= t) return true; uint256 h = onlineHashrateAccounted; uint256 acc = accRewardPerHash; uint256 allocated; uint256 processed; while (true) { (uint256 k, bool found) = _expiries.nextSet(t + 1, toT); if (!found) { (uint256 dAcc, uint256 dAlloc) = EmissionMath.accrue(t, toT, h, ACC_PRECISION); acc += dAcc; allocated += dAlloc; t = toT; done = true; break; } if (processed == maxBuckets) break; (uint256 dAcc2, uint256 dAlloc2) = EmissionMath.accrue(t, k, h, ACC_PRECISION); acc += dAcc2; allocated += dAlloc2; accAtExpiry[k] = acc; h -= expiringHashrate[k]; delete expiringHashrate[k]; _expiries.clear(k); t = k; ++processed; } lastAccountedT = t; onlineHashrateAccounted = h; accRewardPerHash = acc; totalAllocatedScaled += allocated; } /// @dev Brings `a` to mining time nowT (global accounting must already be at nowT). function _settle(address user, Account storage a, uint256 nowT) internal { uint256 r = a.rigsOn; if (r > 0) { uint256 e = a.expiry; bool expired = e <= nowT; uint256 end = expired ? e : nowT; uint256 accEnd = expired ? accAtExpiry[e] : accRewardPerHash; a.pendingReward = SafeCast.toUint128(a.pendingReward + r * (accEnd - a.accSnapshot) / ACC_PRECISION); _consume(a, r * (end - a.lastT) * UNITS_PER_RIG_BLOCK); if (expired) { a.rigsOn = 0; emit TankEmpty(user, _block0 + e); } } a.lastT = SafeCast.toUint64(nowT); a.accSnapshot = accRewardPerHash; } /// @dev Draws `units` from the tank, KIBBLE-backed power first. function _consume(Account storage a, uint256 units) internal { if (units == 0) return; uint256 ku = a.kibbleUnits; uint256 fromK = units < ku ? units : ku; if (fromK > 0) { uint256 kb = fromK == ku ? a.kibbleAmount : FullMath.mulDiv(a.kibbleAmount, fromK, ku); a.kibbleUnits = uint128(ku - fromK); a.kibbleAmount = uint128(a.kibbleAmount - kb); totalKibbleBacking -= kb; pendingBurnKibble += kb; } uint256 rest = units - fromK; if (rest > 0) { a.hypeUnits = uint128(a.hypeUnits - rest); totalHypeUnits -= rest; pendingBurnHypeUnits += rest; } } /// @dev Removes a settled, online account from the online total and its bucket. Returns its previous rigsOn. function _unregister(Account storage a) internal returns (uint256 r) { r = a.rigsOn; if (r == 0) return 0; uint256 e = a.expiry; uint256 left = expiringHashrate[e] - r; expiringHashrate[e] = left; if (left == 0) _expiries.clear(e); onlineHashrateAccounted -= r; a.rigsOn = 0; } /// @dev Puts `r` rigs online if the tank can power them for at least one block. Returns false (rigs off) if not. function _register(Account storage a, uint256 r, uint256 nowT) internal returns (bool) { if (r == 0) return true; uint256 n = (a.hypeUnits + a.kibbleUnits) / (r * UNITS_PER_RIG_BLOCK); if (n == 0) { a.rigsOn = 0; return false; } uint256 e = nowT + n; uint256 prev = expiringHashrate[e]; expiringHashrate[e] = prev + r; if (prev == 0) _expiries.set(e); onlineHashrateAccounted += r; a.rigsOn = uint64(r); // r <= rigs < 2^64 a.expiry = SafeCast.toUint64(e); return true; } // ================================================================== views (docs §14) /// @notice Current block number used by the protocol (Elysium L2 block, see ChainBlock). function currentBlock() external view returns (uint256) { return ChainBlock.number(); } /// @notice Block 0 (0 during preorder). function block0() public view returns (uint256) { uint256 b0 = _block0; return b0 != 0 ? b0 : genesis.block0(); } /// @notice Current mining time (blocks since block 0). function miningTime() public view returns (uint256) { uint256 b0 = block0(); return b0 == 0 ? 0 : ChainBlock.number() - b0; } /// @dev Simulates global accounting up to `target` (>= lastAccountedT) without writing. function _simulate(uint256 target) internal view returns (uint256 acc, uint256 h, uint256 allocated) { uint256 t = lastAccountedT; acc = accRewardPerHash; h = onlineHashrateAccounted; allocated = totalAllocatedScaled; if (target <= t) return (acc, h, allocated / ACC_PRECISION); while (true) { (uint256 k, bool found) = _expiries.nextSet(t + 1, target); uint256 stop = found ? k : target; (uint256 dAcc, uint256 dAlloc) = EmissionMath.accrue(t, stop, h, ACC_PRECISION); acc += dAcc; allocated += dAlloc; t = stop; if (!found) break; h -= expiringHashrate[k]; } allocated /= ACC_PRECISION; } /// @notice Live total online hashrate (docs §14), expired tanks removed. function totalOnlineHashrate() external view returns (uint256 h) { (, h,) = _simulate(miningTime()); } /// @notice Emission distributed to online hashrate as of `lastAccountedT` (upper bound of claimed + pending). function totalAllocated() external view returns (uint256) { return totalAllocatedScaled / ACC_PRECISION; } /// @notice Emission distributed to online hashrate up to now. function totalAllocatedNow() external view returns (uint256 allocated) { (,, allocated) = _simulate(miningTime()); } /// @notice Rigs owned by `user`. function rigsOf(address user) external view returns (uint256) { return _accounts[user].rigs; } /// @notice Rigs of `user` currently on (0 once the tank ran dry). function rigsOnOf(address user) public view returns (uint256) { Account storage a = _accounts[user]; if (a.rigsOn == 0 || a.expiry <= miningTime()) return 0; return a.rigsOn; } /// @notice Pending KIBBLE of `user` (docs §12), live. function pending(address user) external view returns (uint256) { Account storage a = _accounts[user]; uint256 p = a.pendingReward; uint256 r = a.rigsOn; if (r == 0) return p; uint256 nowT = miningTime(); uint256 e = a.expiry; uint256 accEnd; if (e <= lastAccountedT) accEnd = accAtExpiry[e]; else (accEnd,,) = _simulate(e < nowT ? e : nowT); return p + r * (accEnd - a.accSnapshot) / ACC_PRECISION; } /// @notice Live tank of `user`, all in wei (docs §14 "Tank level"). `kibbleBackedHype` is the HYPE-equivalent of /// the KIBBLE-backed sub-tank, `kibbleAmount` the KIBBLE it returns on withdrawal. function tankOf(address user) public view returns (uint256 hypeBacked, uint256 kibbleBackedHype, uint256 kibbleAmount, uint256 total) { Account storage a = _accounts[user]; uint256 hu = a.hypeUnits; uint256 ku = a.kibbleUnits; kibbleAmount = a.kibbleAmount; uint256 r = a.rigsOn; if (r > 0) { uint256 nowT = miningTime(); uint256 end = a.expiry < nowT ? a.expiry : nowT; uint256 drain = r * (end - a.lastT) * UNITS_PER_RIG_BLOCK; uint256 fromK = drain < ku ? drain : ku; if (fromK > 0) { kibbleAmount -= fromK == ku ? kibbleAmount : FullMath.mulDiv(kibbleAmount, fromK, ku); ku -= fromK; } hu -= drain - fromK; } hypeBacked = hu / UNITS_PER_WEI; kibbleBackedHype = ku / UNITS_PER_WEI; total = (hu + ku) / UNITS_PER_WEI; } /// @notice Block at which the tank of `user` runs dry (docs §12), 0 if no rig is on or mining has not started /// (see `blocksLeftOf` during preorder). function emptyBlockOf(address user) external view returns (uint256) { if (rigsOnOf(user) == 0) return 0; uint256 b0 = block0(); if (b0 == 0) return 0; return b0 + _accounts[user].expiry; } /// @notice Blocks of mining left before the tank of `user` runs dry at the current rigs on (0 if off). function blocksLeftOf(address user) external view returns (uint256) { if (rigsOnOf(user) == 0) return 0; return _accounts[user].expiry - miningTime(); } /// @notice Full account of `user` in one call. function accountOf(address user) external view returns ( uint256 rigs, uint256 rigsOn, uint256 hypeBacked, uint256 kibbleBackedHype, uint256 kibbleAmount, uint256 expiry, uint256 lastT, uint256 storedPending ) { Account storage a = _accounts[user]; (hypeBacked, kibbleBackedHype, kibbleAmount,) = tankOf(user); return (a.rigs, rigsOnOf(user), hypeBacked, kibbleBackedHype, kibbleAmount, a.expiry, a.lastT, a.pendingReward); } /// @notice Power drawn per rig per block, as the exact ratio numerator / denominator in wei (1e18 / 60,000). function powerDrawPerRigPerBlock() external pure returns (uint256 numerator, uint256 denominator) { return (1e18, K.DRAW_DIVISOR); } /// @notice Production cost in HYPE per KIBBLE (1e18 scaled), docs §7: /// power per rig per block × online hashrate / block reward, with exact ratios. /// During preorder it uses era 1 and the rigs currently switched on (the block 0 calibration). function productionCost() external view returns (uint256) { uint256 nowT = miningTime(); (, uint256 h,) = _simulate(nowT); uint256 em = EmissionMath.eraEmission(EmissionMath.eraAt(nowT)); if (em == 0) return 0; // (h × 1e18 / 60,000) / (em / ERA_LENGTH) × 1e18 return FullMath.mulDiv(h * 1e18, 1e18 * K.ERA_LENGTH, K.DRAW_DIVISOR * em); } }