Agent SDK

Spend from the agent's own process.

The agent never needs this website. It holds the job's agent key and talks to NevfourJobPurse on Elysium directly. These are the three calls.

With viem (Node 20+)

import { createPublicClient, createWalletClient, http, parseEther, stringToHex } from "viem";
import { privateKeyToAccount } from "viem/accounts";

const purse = "0x… (NevfourJobPurse, published here once deployed)";
const job = 1n;                       // the id the operator gave the agent
const provider = "0x…";               // must be on the job's allow-list
const amount = parseEther("0.02");    // HYPE

// 1. Before starting: does the whole estimate fit?
const [accept] = await pub.readContract({ address: purse, abi, functionName: "canAccept", args: [job, parseEther("0.06")] });
if (!accept) return skip();

// 2. Before each purchase: what would happen? 0 = pay, limit bits = held, other bits = refused
const bits = await pub.readContract({ address: purse, abi, functionName: "checkSpend", args: [job, agent.address, provider, amount] });

// 3. Pay the provider straight from the job's escrow
await wallet.writeContract({
  address: purse, abi, functionName: "spend",
  args: [job, provider, amount, 1 /* 0 data, 1 compute, 2 other */, stringToHex("gpu run 1", { size: 32 })],
});

canAccept(job, estimate) view

(accept, headroom, balance, marginIfEstimate). Accept when the estimate fits under the cap minus what is already spent.

checkSpend(job, agent, provider, amount) view

Reason bits for this exact spend. 0 means it pays; only limit bits means it would be held; anything else reverts.

spend(job, provider, amount, kind, memo) write

Agent key only. Pays the provider from the escrow, or records SpendHeld and returns (false, reasons) without moving funds.

The full example in the repository

examples/agent.mjs: npm run agent -- check 0.02 0xProvider reads only; spend needs AGENT_PRIVATE_KEY in the environment (never commit it).

// An agent paying for its own data and compute from a NEV4 job, outside any browser.
//
//   NEVFOUR_PURSE=0x… NEVFOUR_JOB=1 npm run agent -- check 0.02 0xProvider
//   NEVFOUR_PURSE=0x… NEVFOUR_JOB=1 AGENT_PRIVATE_KEY=0x… npm run agent -- spend 0.02 0xProvider compute "gpu run 1"
//
// "check" only reads: the job, canAccept for the amount, and what checkSpend says the spend would do.
// "spend" asks checkSpend first and sends spend() only when the answer is "pay" or "hold" (a held spend is recorded on chain,
// moves no funds, and tells the operator the agent hit a limit). The key must be the job's agent key; it can pay only the
// job's allow-listed providers, inside the cap, before the deadline. Export AGENT_PRIVATE_KEY in your shell or a secret
// manager; never commit it.
import { createPublicClient, createWalletClient, defineChain, formatEther, http, parseEther, stringToHex } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { readFileSync } from "node:fs";

const ART = JSON.parse(readFileSync(new URL("../src/lib/contracts/artifacts.json", import.meta.url), "utf8")).contracts.NevfourJobPurse;
const abi = ART.abi;

const elysium = defineChain({
  id: 99901,
  name: "Elysium",
  nativeCurrency: { name: "HYPE", symbol: "HYPE", decimals: 18 },
  rpcUrls: { default: { http: [process.env.ELYSIUM_RPC_URL || "https://rpc.elysium.kinetiq.xyz"] } },
});

const REASONS = ["no such job", "job closed", "past the deadline", "not the agent key", "provider not allowed", "zero amount", "over the cap", "escrow too low", "over the per-spend limit"];
const HOLD_MASK = 16 | 64 | 128 | 256;
const words = (bits) => REASONS.filter((_, i) => bits & (1 << i));
const KINDS = { data: 0, compute: 1, other: 2 };

const purse = process.env.NEVFOUR_PURSE;
const jobId = BigInt(process.env.NEVFOUR_JOB || "0");
const [mode = "check", amountText = "0", provider, kindText = "data", memo = ""] = process.argv.slice(2);
if (!/^0x[0-9a-fA-F]{40}$/.test(purse || "") || jobId < 1n || !/^0x[0-9a-fA-F]{40}$/.test(provider || "")) {
  console.error("usage: NEVFOUR_PURSE=0x… NEVFOUR_JOB=<id> npm run agent -- check|spend <HYPE amount> <provider 0x…> [data|compute|other] [memo]");
  process.exit(2);
}
const amount = parseEther(amountText);
const pub = createPublicClient({ chain: elysium, transport: http() });

const job = await pub.readContract({ address: purse, abi, functionName: "getJob", args: [jobId] });
const [accept, headroom, balance] = await pub.readContract({ address: purse, abi, functionName: "canAccept", args: [jobId, amount] });
console.log(`job ${jobId}: cap ${formatEther(job.cap)} spent ${formatEther(job.spent)} headroom ${formatEther(headroom)} escrow ${formatEther(balance)} HYPE`);
console.log(`canAccept(${amountText}) = ${accept ? "accept" : "skip"}`);

const key = process.env.AGENT_PRIVATE_KEY;
const account = key ? privateKeyToAccount(key) : null;
const from = account ? account.address : job.agent;
const bits = Number(await pub.readContract({ address: purse, abi, functionName: "checkSpend", args: [jobId, from, provider, amount] }));
const outcome = bits === 0 ? "pay" : (bits & ~HOLD_MASK) === 0 ? "hold" : "revert";
console.log(`checkSpend from ${from}: ${outcome}${bits ? ` (${words(bits).join("; ")})` : ""}`);

if (mode !== "spend") process.exit(0);
if (!account) {
  console.error("spend needs AGENT_PRIVATE_KEY in the environment");
  process.exit(2);
}
if (outcome === "revert") {
  console.error("not sent: the purse would refuse this call");
  process.exit(1);
}
const memo32 = stringToHex(memo.slice(0, 31), { size: 32 });
const wallet = createWalletClient({ account, chain: elysium, transport: http() });
const hash = await wallet.writeContract({ address: purse, abi, functionName: "spend", args: [jobId, provider, amount, KINDS[kindText] ?? 2, memo32] });
const receipt = await pub.waitForTransactionReceipt({ hash });
console.log(`${outcome === "pay" ? "paid" : "held"}: ${hash} (block ${receipt.blockNumber}, status ${receipt.status})`);

Any other language

The ABI is in src/lib/contracts/artifacts.json. Chain id 99901, RPC https://rpc.elysium.kinetiq.xyz, gas in HYPE. Read with eth_call, write with a signed EIP-1559 transaction from the agent key. Watch your job with eth_getLogs filtered on topic1 = the job id.