// SPDX-License-Identifier: MIT pragma solidity 0.8.28; /// @title PriceBand /// @notice A HyperEVM contract as people write it today: reads mark, oracle and best bid/offer through L1Read, /// refuses to act when mark and oracle drift apart, and places an IOC order through CoreWriter. contract PriceBand { // ---- addresses: the only lines that change between HyperEVM and Elysium ---- address constant MARK_PX = 0x080687d0B2db46033615FD2F4d27fe37bCAB0806; address constant ORACLE_PX = 0x080790fa5D297F2B619EC09d5E73E4fE081e0807; address constant L1_BLOCK_NUMBER = 0x080993bD400Eb8e00D64407014540ace3C3f0809; address constant BBO = 0x080e076612C2845af543CC842Bb4B2A856e4080e; address constant CORE_WRITER = 0x33337bcD680354087Bd66999EbE4e1b33b613333; // --------------------------------------------------------------------------- struct Bbo { uint64 bid; uint64 ask; } event Checked(uint32 perp, uint64 mark, uint64 oracle, uint64 bid, uint64 ask, uint64 l1Block, bool inBand); event Placed(uint32 perp, bool isBuy, uint64 limitPx, uint64 sz); function markPx(uint32 perp) public view returns (uint64) { (bool ok, bytes memory r) = MARK_PX.staticcall(abi.encode(perp)); require(ok, "markPx"); return abi.decode(r, (uint64)); } function oraclePx(uint32 perp) public view returns (uint64) { (bool ok, bytes memory r) = ORACLE_PX.staticcall(abi.encode(perp)); require(ok, "oraclePx"); return abi.decode(r, (uint64)); } function bbo(uint32 asset) public view returns (Bbo memory) { (bool ok, bytes memory r) = BBO.staticcall(abi.encode(asset)); require(ok, "bbo"); return abi.decode(r, (Bbo)); } function l1BlockNumber() public view returns (uint64) { (bool ok, bytes memory r) = L1_BLOCK_NUMBER.staticcall(abi.encode()); require(ok, "l1BlockNumber"); return abi.decode(r, (uint64)); } /// @notice In band when mark is within `maxBps` of oracle. function check(uint32 perp, uint16 maxBps) public view returns (uint64 mark, uint64 oracle, uint64 bid, uint64 ask, uint64 l1Block, bool inBand) { mark = markPx(perp); oracle = oraclePx(perp); Bbo memory b = bbo(perp); (bid, ask) = (b.bid, b.ask); l1Block = l1BlockNumber(); uint256 d = mark > oracle ? mark - oracle : oracle - mark; inBand = d * 10_000 <= uint256(oracle) * maxBps; } /// @notice Places an IOC limit order through CoreWriter when the market is in band. /// @param limitPx and sz are CoreWriter units (1e8 x human value) function placeIfInBand(uint32 perp, uint16 maxBps, bool isBuy, uint64 limitPx, uint64 sz) external { (uint64 mark, uint64 oracle, uint64 bid, uint64 ask, uint64 l1Block, bool inBand) = check(perp, maxBps); emit Checked(perp, mark, oracle, bid, ask, l1Block, inBand); require(inBand, "out of band"); bytes memory action = abi.encode(perp, isBuy, limitPx, sz, false, uint8(3), uint128(0)); (bool ok,) = CORE_WRITER.call( abi.encodeWithSignature("sendRawAction(bytes)", abi.encodePacked(uint8(1), uint24(1), action)) ); require(ok, "sendRawAction"); emit Placed(perp, isBuy, limitPx, sz); } }