// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {ElysioTypes, IElysioRouter} from "./IElysio.sol"; import {ElysioClient} from "./ElysioClient.sol"; /// Uniswap v4 StateView (read-only lens on the PoolManager). interface IStateView { function getSlot0(bytes32 poolId) external view returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee); } /// 512-bit multiply then divide, exact (no intermediate overflow). library FullMath { function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) { unchecked { uint256 prod0; uint256 prod1; assembly { let mm := mulmod(a, b, not(0)) prod0 := mul(a, b) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } if (prod1 == 0) { require(denominator > 0); return prod0 / denominator; } require(denominator > prod1); uint256 remainder; assembly { remainder := mulmod(a, b, denominator) prod1 := sub(prod1, gt(remainder, prod0)) prod0 := sub(prod0, remainder) } uint256 twos = (0 - denominator) & denominator; assembly { denominator := div(denominator, twos) prod0 := div(prod0, twos) twos := add(div(sub(0, twos), twos), 1) } prod0 |= prod1 * twos; uint256 inv = (3 * denominator) ^ 2; inv *= 2 - denominator * inv; inv *= 2 - denominator * inv; inv *= 2 - denominator * inv; inv *= 2 - denominator * inv; inv *= 2 - denominator * inv; inv *= 2 - denominator * inv; result = prod0 * inv; } } } /// @title CoreArb /// @notice Template for Elysium <-> HyperCore arbitrage on HYPE. act() compares HyperCore's touch (passed in by /// the caller) with the Elysium pool price read on chain. The caller's prices only pick the side: the IOC's /// limit is always the Elysium price plus or minus minEdgeBps, so it can fill only while the dislocation /// is still there. onReceipt books the HyperCore leg from the receipt alone. The Elysium leg (the swap /// that closes the loop) is yours to add next to act(). contract CoreArb is ElysioClient { using FullMath for uint256; uint32 public constant HYPE_USDC = 10107; IStateView public immutable stateView; bytes32 public immutable poolId; bool public immutable hypeIsToken0; uint8 public immutable hypeDecimals; uint8 public immutable usdcDecimals; uint16 public minEdgeBps; // e.g. 30 = 0.30% uint64 public legSz; // 8 decimals, size of each HyperCore leg uint256 public openIntent; // one leg at a time; 0 when free struct Leg { bool isBuy; uint64 elysiumPx; // 8 decimals, seen at request } mapping(uint256 => Leg) public legs; int256 public position; // HYPE, 8 decimals int256 public cash; // USDC, 8 decimals, net of fees int256 public edgeCaptured; // USDC, 8 decimals, against the Elysium price seen at request error LegOpen(uint256 intentId); error NoEdge(uint64 coreBid, uint64 coreAsk, uint64 elysiumPx); event Acted(uint256 indexed intentId, bool isBuy, uint64 sz, uint64 limitPx, uint64 elysiumPx); event Booked(uint256 indexed intentId, ElysioTypes.Status status, uint64 filledSz, uint64 avgPx, int256 edge); constructor( IElysioRouter router, IStateView stateView_, bytes32 poolId_, bool hypeIsToken0_, uint8 hypeDecimals_, uint8 usdcDecimals_, uint16 minEdgeBps_, uint64 legSz_ ) ElysioClient(router, msg.sender) { stateView = stateView_; poolId = poolId_; hypeIsToken0 = hypeIsToken0_; hypeDecimals = hypeDecimals_; usdcDecimals = usdcDecimals_; minEdgeBps = minEdgeBps_; legSz = legSz_; } function setParams(uint16 minEdgeBps_, uint64 legSz_) external onlyElysioOwner { minEdgeBps = minEdgeBps_; legSz = legSz_; } /// Price of one HYPE in USDC on the Elysium pool, 8 decimals. function elysiumPriceE8() public view returns (uint64) { (uint160 s,,,) = stateView.getSlot0(poolId); uint256 oneHype = 10 ** hypeDecimals; uint256 usdcRaw; // USDC base units for one HYPE if (s <= type(uint128).max) { uint256 ratioX192 = uint256(s) * s; // token1 per token0, Q192 usdcRaw = hypeIsToken0 ? ratioX192.mulDiv(oneHype, 1 << 192) : uint256(1 << 192).mulDiv(oneHype, ratioX192); } else { uint256 ratioX128 = uint256(s).mulDiv(s, 1 << 64); usdcRaw = hypeIsToken0 ? ratioX128.mulDiv(oneHype, 1 << 128) : uint256(1 << 128).mulDiv(oneHype, ratioX128); } return uint64(usdcDecimals <= 8 ? usdcRaw * 10 ** (8 - usdcDecimals) : usdcRaw / 10 ** (usdcDecimals - 8)); } /// coreBid / coreAsk: HyperCore's touch as the caller sees it (8 decimals). They choose the side only. function act(uint64 coreBid, uint64 coreAsk, uint64 ttl) external onlyElysioOwner returns (uint256 intentId) { if (openIntent != 0) revert LegOpen(openIntent); uint64 p = elysiumPriceE8(); uint64 edge = uint64(uint256(p) * minEdgeBps / 10_000); bool isBuy; uint64 limit; if (coreBid > p + edge) { (isBuy, limit) = (false, p + edge); // HyperCore pays more than Elysium: sell there } else if (coreAsk + edge < p) { (isBuy, limit) = (true, p - edge); // HyperCore is cheaper than Elysium: buy there } else { revert NoEdge(coreBid, coreAsk, p); } intentId = _request(ElysioTypes.Intent({ asset: HYPE_USDC, isBuy: isBuy, reduceOnly: false, tif: ElysioTypes.Tif.Ioc, sz: legSz, limitPx: limit, deadline: uint64(block.timestamp + ttl), callbackGas: 200_000 })); openIntent = intentId; legs[intentId] = Leg(isBuy, p); emit Acted(intentId, isBuy, legSz, limit, p); } function _onReceipt(uint256 intentId, ElysioTypes.Receipt calldata r) internal override { if (openIntent == intentId) openIntent = 0; Leg memory leg = legs[intentId]; int256 edge; if (r.status == ElysioTypes.Status.Filled || r.status == ElysioTypes.Status.Partial) { int256 sz = int256(uint256(r.filledSz)); int256 notional = sz * int256(uint256(r.avgPx)) / 1e8; int256 vsElysium = (int256(uint256(leg.elysiumPx)) - int256(uint256(r.avgPx))) * sz / 1e8; if (leg.isBuy) { position += sz; cash -= notional + r.fee; edge = vsElysium; // bought below the Elysium price } else { position -= sz; cash += notional - r.fee; edge = -vsElysium; // sold above the Elysium price } edgeCaptured += edge; } // Dropped and Unanswered book nothing; the slot is free again either way. emit Booked(intentId, r.status, r.filledSz, r.avgPx, edge); } }