// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {ElysioTypes, IElysioReceiver, IElysioRouter} from "./IElysio.sol"; /// @title MockElysioRouter /// @notice For tests: records intents and answers them with scripted receipts, through the same onReceipt call /// the real router makes. No bonds, subscriptions or relayers. contract MockElysioRouter is IElysioRouter { struct Request { address consumer; ElysioTypes.Intent intent; } uint256 public nextId = 1; mapping(uint256 => Request) public requests; mapping(uint256 => ElysioTypes.Receipt) internal receipts; error UnknownIntent(uint256 intentId); error AlreadyFinal(uint256 intentId); error NotFinal(uint256 intentId); error TooEarly(uint256 intentId); function request(ElysioTypes.Intent calldata intent) external returns (uint256 intentId) { intentId = nextId++; requests[intentId] = Request(msg.sender, intent); receipts[intentId].status = ElysioTypes.Status.Open; emit IntentRequested(intentId, msg.sender, address(this), address(0), ElysioTypes.cloid(address(this), intentId), intent); } /// Answer with a status, fill size and average price (8 decimals); the rest of the receipt is zero. function answer(uint256 intentId, ElysioTypes.Status status, uint64 filledSz, uint64 avgPx) external { ElysioTypes.Receipt memory r; r.status = status; r.filledSz = filledSz; r.avgPx = avgPx; if (status == ElysioTypes.Status.Dropped) r.reason = ElysioTypes.IOC_NO_MATCH; _post(intentId, r); } /// Answer with a full receipt. function answer(uint256 intentId, ElysioTypes.Receipt calldata r) external { _post(intentId, r); } function receiptOf(uint256 intentId) external view returns (ElysioTypes.Receipt memory) { return receipts[intentId]; } function retryCallback(uint256 intentId) external { if (!ElysioTypes.isFinal(receipts[intentId].status)) revert NotFinal(intentId); _deliver(intentId); } function expire(uint256 intentId) external { Request storage q = requests[intentId]; if (q.consumer == address(0)) revert UnknownIntent(intentId); if (ElysioTypes.isFinal(receipts[intentId].status)) revert AlreadyFinal(intentId); if (block.timestamp <= q.intent.deadline + ElysioTypes.EXPIRY_GRACE) revert TooEarly(intentId); ElysioTypes.Receipt memory r; r.status = ElysioTypes.Status.Unanswered; receipts[intentId] = r; emit Expired(intentId, address(this), msg.sender); _deliver(intentId); } function _post(uint256 intentId, ElysioTypes.Receipt memory r) internal { if (requests[intentId].consumer == address(0)) revert UnknownIntent(intentId); if (ElysioTypes.isFinal(receipts[intentId].status)) revert AlreadyFinal(intentId); receipts[intentId] = r; // stored before the callback runs emit ReceiptPosted(intentId, address(this), r); if (ElysioTypes.isFinal(r.status)) _deliver(intentId); } function _deliver(uint256 intentId) internal { Request storage q = requests[intentId]; uint256 g = gasleft(); bool ok; try IElysioReceiver(q.consumer).onReceipt{gas: q.intent.callbackGas}(intentId, receipts[intentId]) { ok = true; } catch {} emit CallbackResult(intentId, ok, g - gasleft()); } }