// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {Read, Stamp, ICoreReader, ICoreResultReceiver} from "./CoreWire.sol"; /// Foundry mocks written to Kinetiq's documented ElysiumCoreWriter model (bind, registrar, trade-only agent, /// intents, asynchronous callbacks), with Corewire's function names. An adapter maps them to Kinetiq's names once /// the ABI is published. Not for production. /// Answers reads like CoreWire does after a bridge delivery. contract MockEL1Read { mapping(bytes32 => bytes) public values; struct Pending { address requester; Read[] reads; uint32 callbackGas; } Pending[] internal pending; uint64 public hcBlock = 1_237_000_000; function set(address precompile, uint32 index, uint64 value) external { values[keccak256(abi.encode(precompile, abi.encode(index)))] = abi.encode(value); } function setRaw(address target, bytes calldata data, bytes calldata value) external { values[keccak256(abi.encode(target, data))] = value; } function requestRead(Read[] calldata reads, uint32 callbackGas) external payable returns (uint256 id) { id = pending.length; pending.push(); Pending storage p = pending[id]; p.requester = msg.sender; p.callbackGas = callbackGas; for (uint256 i; i < reads.length; ++i) p.reads.push(reads[i]); } /// Deliver request `id` now (the bridge takes about 8 s live). function answer(uint256 id) external { Pending storage p = pending[id]; bool[] memory ok = new bool[](p.reads.length); bytes[] memory res = new bytes[](p.reads.length); for (uint256 i; i < p.reads.length; ++i) { res[i] = values[keccak256(abi.encode(p.reads[i].target, p.reads[i].data))]; ok[i] = res[i].length > 0; } hcBlock += 10; ICoreReader(p.requester).onCoreRead{gas: p.callbackGas}(id, ok, res, Stamp(hcBlock, uint64(block.number), uint64(block.timestamp))); } /// Synchronous read, as the native precompile will answer after the upgrade. function readNow(address target, bytes calldata data) external view returns (bytes memory) { return values[keccak256(abi.encode(target, data))]; } } /// The order lane: bind, intents, registrar results. contract MockElysiumCoreWriter { struct Binding { address hcAccount; address registrar; } mapping(address => Binding) public bindings; struct Intent { address binder; uint32 asset; bool isBuy; uint64 limitPx; uint64 sz; uint32 callbackGas; } Intent[] public intents; event IntentPlaced(uint256 indexed id, address indexed binder, uint32 asset, bool isBuy, uint64 limitPx, uint64 sz); function bind(address hcAccount, address registrar) external { bindings[msg.sender] = Binding(hcAccount, registrar); } function placeOrder(uint32 asset, bool isBuy, uint64 limitPx, uint64 sz, uint8, bool, uint16, uint32 callbackGas) external payable returns (uint256 id) { require(bindings[msg.sender].hcAccount != address(0), "not bound"); id = intents.length; intents.push(Intent(msg.sender, asset, isBuy, limitPx, sz, callbackGas)); emit IntentPlaced(id, msg.sender, asset, isBuy, limitPx, sz); } /// The registrar's answer. Filled at the limit by default. function fill(uint256 id, uint64 filledSz, uint64 avgPx) external { Intent storage it = intents[id]; ICoreResultReceiver(it.binder).onCoreResult{gas: it.callbackGas}(id, 2, uint64(id + 1), filledSz, avgPx, uint64(block.timestamp), false); } function reject(uint256 id) external { Intent storage it = intents[id]; ICoreResultReceiver(it.binder).onCoreResult{gas: it.callbackGas}(id, 4, 0, 0, 0, uint64(block.timestamp), false); } }