// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; import {AsyncClient} from "./AsyncClient.sol"; import {AsyncTypes, IAsyncRouter} from "./IAsync.sol"; interface IStateView { function getSlot0(bytes32 poolId) external view returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee); } /// @title CoreArb (template) /// @notice An AsyncClient that acts when the Elysium pool and the HyperCore book disagree on HYPE, and books /// the HyperCore leg only from its receipt. `act(coreBid, coreAsk)` reads the Elysium USDC/WHYPE v4 pool price on /// chain; when HyperCore's bid is above it by more than `minEdgeBps` it sells on HyperCore, when HyperCore's /// ask is below it it buys, each as an IOC whose limit is the Elysium price less the edge, so the order can only /// fill while the dislocation is still there. `onReceipt` books what really filled: position, cash and the edge /// captured against the Elysium price seen at request time. Filled, Partial, Dropped and Unanswered all land. /// The Elysium leg (the swap that closes the loop on the pool) is the builder's to add next to `act`; this /// template keeps the HyperCore side exact. One open intent at a time. Prices E8 (USDC per HYPE), sizes E8. contract CoreArb is AsyncClient { IStateView public immutable stateView; bytes32 public immutable poolId; // USDC (currency0, 6 dec) / WHYPE (currency1, 18 dec) uint32 public immutable asset; // HyperCore HYPE/USDC spot: 10000 + 107 uint16 public immutable minEdgeBps; uint64 public immutable sz; // E8 HYPE per leg uint256 public openIntent; // 0 when none uint64 public pxAtRequest; // Elysium price when the open intent was sent bool public sellAtRequest; int256 public position; // E8 HYPE, from receipts only int256 public cash; // E8 USDC, from receipts only (fees included) uint256 public edgeCaptured; // E8 USDC, filled x |avg - Elysium price at request| uint256 public legs; event Acted(uint256 indexed intentId, bool sellOnCore, uint64 elysiumPx, uint64 coreBid, uint64 coreAsk, uint64 limitPx); event Booked(uint256 indexed intentId, AsyncTypes.Status status, uint64 filledSz, uint64 avgPx, int256 position, uint256 edgeCaptured); error Busy(); error NoDislocation(uint64 elysiumPx, uint64 coreBid, uint64 coreAsk); constructor(IAsyncRouter router_, address owner_, IStateView sv, bytes32 poolId_, uint32 asset_, uint16 minEdgeBps_, uint64 sz_) AsyncClient(router_, owner_) { stateView = sv; poolId = poolId_; asset = asset_; minEdgeBps = minEdgeBps_; sz = sz_; } /// @notice USDC per HYPE on the Elysium pool, E8. sqrtPriceX96 prices WHYPE-raw per USDC-raw, so /// px = 1e12 / p (decimals) x 1e8 (E8) = 1e20 / (sqrtP / 2^96)^2, in two steps to stay inside 256 bits. function elysiumPx() public view returns (uint64) { (uint160 s,,,) = stateView.getSlot0(poolId); return uint64(((uint256(1e20) << 96) / s << 96) / s); } /// @notice Sends the HyperCore leg if HyperCore's touch (read off the book by the caller) disagrees with the /// Elysium pool by more than `minEdgeBps`. The caller's prices only pick the side; the limit comes from Elysium. function act(uint64 coreBid, uint64 coreAsk, uint64 ttl) external onlyAsyncOwner returns (uint256 id) { if (openIntent != 0) revert Busy(); uint64 e = elysiumPx(); bool sell; uint64 limit; if (uint256(coreBid) * 10_000 > uint256(e) * (10_000 + minEdgeBps)) { sell = true; // HyperCore pays more than Elysium: sell there, no lower than Elysium + edge limit = uint64(uint256(e) * (10_000 + minEdgeBps) / 10_000); } else if (uint256(coreAsk) * (10_000 + minEdgeBps) < uint256(e) * 10_000) { limit = uint64(uint256(e) * 10_000 / (10_000 + minEdgeBps)); // HyperCore is cheaper: buy, no higher than Elysium - edge } else { revert NoDislocation(e, coreBid, coreAsk); } id = _request( AsyncTypes.Intent({ asset: asset, isBuy: !sell, reduceOnly: false, tif: AsyncTypes.Tif.Ioc, sz: sz, limitPx: limit, deadline: uint64(block.timestamp + ttl), callbackGas: 150_000 }) ); openIntent = id; pxAtRequest = e; sellAtRequest = sell; emit Acted(id, sell, e, coreBid, coreAsk, limit); } function _onReceipt(uint256 id, AsyncTypes.Receipt calldata r) internal override { if (id != openIntent) return; // only the leg we sent openIntent = 0; legs++; if (r.filledSz > 0) { int256 q = int256(uint256(r.filledSz)); int256 notional = q * int256(uint256(r.avgPx)) / 1e8; if (sellAtRequest) { position -= q; cash += notional; edgeCaptured += uint256(r.filledSz) * (r.avgPx - pxAtRequest) / 1e8; } else { position += q; cash -= notional; edgeCaptured += uint256(r.filledSz) * (pxAtRequest - r.avgPx) / 1e8; } cash -= int256(r.fee); } emit Booked(id, r.status, r.filledSz, r.avgPx, position, edgeCaptured); } }