// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; // Create a policy from the Agentisium PolicyFactory on Elysium (99901) and check verdicts. // The caller of `create` becomes the policy owner: here, this contract. interface IPolicyFactory { struct Config { uint64 maxOrderNotionalE8; // USD, 8 decimals uint64 maxOpenNotionalE8; uint64 maxDailyLossE8; uint32 cooldownSecs; // after a losing close uint16 maxOrdersPerHour; uint16 priceBandBps; // limit price distance from the mark bool flattenOnBreach; } function create(address coreAccount, Config calldata cfg, uint32[] calldata markets, uint32[] calldata levsX10) external payable returns (address policy); } interface IPolicy { function preview(uint32 asset, bool isBuy, uint64 sz, uint64 limitPx, uint32 leverageX10) external view returns (uint8); function proposalCount() external view returns (uint256); function verdicts(uint256 id) external view returns (address key, uint32 asset, bool isBuy, uint8 code, uint64 sz, uint64 limitPx, uint64 at, uint256 intentId, uint8 status); function setKey(address key, uint64 expiry) external; } contract PolicyUser { IPolicyFactory public constant FACTORY = IPolicyFactory(0xd98fC54553881d0d430EA422C5747fC90B97f8Ee); uint8 public constant ACCEPTED = 0; uint8 public constant LEVERAGE = 8; address public policy; /// BTC (asset 0) and ETH (asset 1) at 3x, 30 USD per order, 90 USD open, 2 USD daily loss. function open(address coreAccount, address agentKey) external payable returns (address) { uint32[] memory markets = new uint32[](2); uint32[] memory levs = new uint32[](2); (markets[0], markets[1], levs[0], levs[1]) = (0, 1, 30, 30); IPolicyFactory.Config memory cfg = IPolicyFactory.Config({ maxOrderNotionalE8: 30e8, maxOpenNotionalE8: 90e8, maxDailyLossE8: 2e8, cooldownSecs: 60, maxOrdersPerHour: 40, priceBandBps: 200, flattenOnBreach: true }); policy = FACTORY.create{value: msg.value}(coreAccount, cfg, markets, levs); IPolicy(policy).setKey(agentKey, uint64(block.timestamp + 30 days)); return policy; } /// What the policy would answer right now, with no signature and no state change. function wouldAccept(uint32 asset, bool isBuy, uint64 sz, uint64 limitPx, uint32 leverageX10) external view returns (bool, uint8) { uint8 code = IPolicy(policy).preview(asset, isBuy, sz, limitPx, leverageX10); return (code == ACCEPTED, code); } /// The code of the latest recorded verdict (0 = accepted, 8 = leverage, 17 = accepted, not routed). function lastVerdict(address p) external view returns (uint256 id, uint8 code) { id = IPolicy(p).proposalCount(); if (id > 0) (, , , code, , , , , ) = IPolicy(p).verdicts(id); } }