// An agent proposes "20x long BTC" to the Open Desk policy and reads the verdict. // Runs as written: `npm i viem` then `node agent.mjs`. Every call is an eth_call; nothing is sent. import { createPublicClient, http, defineChain, parseAbi } from "viem"; import { privateKeyToAccount, generatePrivateKey } from "viem/accounts"; const elysium = defineChain({ id: 99901, name: "Elysium", nativeCurrency: { name: "HYPE", symbol: "HYPE", decimals: 18 }, rpcUrls: { default: { http: ["https://rpc.elysium.kinetiq.xyz"] } }, }); const client = createPublicClient({ chain: elysium, transport: http() }); const POLICY = "0x729c246974B19a89a09B0457b7e1C733b4Cd1Dc4"; // Open Desk policy const IMPLEMENTATION = "0xfD0E10371fe17737512776406e33d929EdeBbDDe"; // Policy implementation const abi = parseAbi([ "struct Limits { uint32 maxLevX10; uint64 maxOrderNotionalE8; uint64 maxOpenNotionalE8; uint64 maxDailyLossE8; uint32 cooldownSecs; uint16 maxOrdersPerHour; uint16 priceBandBps; bool reduceOnly; bool paused; }", "struct Book { int64 posSzE8; uint64 markE8; int256 equityE8; uint256 otherOpenNotionalE8; uint256 lossUsedE8; uint64 lastLosingCloseAt; uint64 nowTs; uint16 ordersThisHour; }", "struct Order { bool isBuy; bool reduceOnly; uint64 sz; uint64 limitPx; uint32 leverageX10; }", "struct Proposal { uint32 asset; bool isBuy; bool reduceOnly; uint64 sz; uint64 limitPx; uint32 leverageX10; uint64 nonce; uint64 issuedAt; uint64 deadline; }", "function evaluate(Limits l, Book b, Order o) pure returns (uint8)", "function propose(Proposal p, bytes sig) returns (uint256 id, uint8 code)", "function config() view returns (uint64 maxOrderNotionalE8, uint64 maxOpenNotionalE8, uint64 maxDailyLossE8, uint32 cooldownSecs, uint16 maxOrdersPerHour, uint16 priceBandBps, bool flattenOnBreach)", "function maxLevX10(uint32 asset) view returns (uint32)", "function lossUsed() view returns (uint256)", ]); const e8 = (x) => BigInt(Math.round(x * 1e8)); const REASON = { 0: "ACCEPTED", 1: "BAD_SIGNATURE", 5: "MARKET_NOT_ALLOWED", 7: "ORDER_SIZE", 8: "LEVERAGE", 9: "OPEN_NOTIONAL", 10: "DAILY_LOSS", 14: "PRICE_BAND" }; // 1. The policy's limits, read from Elysium. BTC is HyperCore asset 0. const [cfg, levBtc, lossUsed] = await Promise.all([ client.readContract({ address: POLICY, abi, functionName: "config" }), client.readContract({ address: POLICY, abi, functionName: "maxLevX10", args: [0] }), client.readContract({ address: POLICY, abi, functionName: "lossUsed" }), ]); // 2. The BTC mark from the HyperCore info API. const mids = await fetch("https://api.hyperliquid.xyz/info", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ type: "allMids" }), }).then((r) => r.json()); const mark = Number(mids.BTC); // 3. The order: 20x long BTC on a 30 USD account, limit 0.3% through the mark. const equity = 30, leverage = 20; const order = { isBuy: true, reduceOnly: false, sz: e8((leverage * equity) / mark), limitPx: e8(mark * 1.003), leverageX10: leverage * 10 }; const limits = { maxLevX10: levBtc, maxOrderNotionalE8: cfg[0], maxOpenNotionalE8: cfg[1], maxDailyLossE8: cfg[2], cooldownSecs: cfg[3], maxOrdersPerHour: cfg[4], priceBandBps: cfg[5], reduceOnly: false, paused: false }; const now = BigInt(Math.floor(Date.now() / 1000)); const book = { posSzE8: 0n, markE8: e8(mark), equityE8: e8(equity), otherOpenNotionalE8: 0n, lossUsedE8: lossUsed, lastLosingCloseAt: 0n, nowTs: now, ordersThisHour: 0 }; // 4. The verdict from the deployed contract (eth_call, no gas). const code = await client.readContract({ address: IMPLEMENTATION, abi, functionName: "evaluate", args: [limits, book, order] }); console.log("evaluate:", code, REASON[code] ?? ""); // 5. The full agent path: sign the proposal (EIP-712) and simulate `propose`. // With a key the owner issued (setKey), the code is the policy's verdict. A fresh key gets 1, BAD_SIGNATURE. const agent = privateKeyToAccount(process.env.AGENT_KEY ?? generatePrivateKey()); const proposal = { asset: 0, isBuy: true, reduceOnly: false, sz: order.sz, limitPx: order.limitPx, leverageX10: order.leverageX10, nonce: now, issuedAt: now, deadline: now + 60n }; const signature = await agent.signTypedData({ domain: { name: "Agentisium Policy", version: "1", chainId: 99901, verifyingContract: POLICY }, types: { Proposal: [ { name: "asset", type: "uint32" }, { name: "isBuy", type: "bool" }, { name: "reduceOnly", type: "bool" }, { name: "sz", type: "uint64" }, { name: "limitPx", type: "uint64" }, { name: "leverageX10", type: "uint32" }, { name: "nonce", type: "uint64" }, { name: "issuedAt", type: "uint64" }, { name: "deadline", type: "uint64" }, ] }, primaryType: "Proposal", message: proposal, }); const { result } = await client.simulateContract({ address: POLICY, abi, functionName: "propose", args: [proposal, signature], account: agent.address }); console.log("propose (simulated):", result[1], REASON[result[1]] ?? "");