DIM · Dagon Inference Marketplace

Funding a vault, a Mandate and a channel

Funding in DIM flows in three steps: the owner deposits USDC into a vault, optionally writes a Mandate that lets an agent key spend within limits, and each channel then locks a budget from the vault for one seller. Only the last step is per seller, and none of them happens per request.

1. Create and fund the vault

The vault is a program-derived address from your owner key and the program's pinned mint:

import { PublicKey } from "@solana/web3.js";
import { DEFAULT_PROGRAM_ID } from "@im/sdk";

const programId = new PublicKey(DEFAULT_PROGRAM_ID);
const owner = new PublicKey(process.env.DIM_OWNER ?? "");
const mint = new PublicKey("2LYaM1PTo4WwkoUx87E4pV39vWPDsohWVLyjUyA4MLUu"); // the default devnet build's test mint
const [vault] = PublicKey.findProgramAddressSync([new TextEncoder().encode("vault"), owner.toBytes(), mint.toBytes()], programId);
console.log("vault:", vault.toBase58());

Create it with init_vault and fund it with deposit(amount), both signed by the owner. The SDK does not build these two instructions; build them from the tracked IDL at programs/idl/im_channels.json. The repository's swarm demo submits both in one transaction (demo/swarm/src/validator.ts). Remember:

  • deposit with the deposit instruction; tokens sent straight to the vault's token account are never credited and cannot be withdrawn;
  • simulate init_vault and set the compute limit to the simulated units plus 10%, or use the program's documented fixed limit of 200,000;
  • the vault cannot be closed, so you create it once per owner and mint.

2. Decide who signs: owner or agent

Owner-signed channels are simplest: the owner key opens channels directly and the wallet has no Mandate. Use them for your own scripts and tests.

Agent channels under a Mandate keep the owner key out of the agent. The owner signs a Mandate once; the agent then opens channels with its own key, and the program checks every open against the Mandate.

import { readFileSync } from "node:fs";
import { Connection, Keypair } from "@solana/web3.js";
import { ChannelWallet, type Route } from "@im/sdk";
import { SolanaChannelBackend } from "@im/sdk/solana";

declare const route: Route; // a verified route, from DiscoveryClient

const connection = new Connection(process.env.SOLANA_RPC_URL ?? "https://api.devnet.solana.com", "confirmed");
const loadKey = (path: string) => Keypair.fromSecretKey(Uint8Array.from(JSON.parse(readFileSync(path, "utf8"))));
const ownerKey = loadKey(process.env.DIM_OWNER_KEYPAIR ?? "");
const agentKey = loadKey(process.env.DIM_AGENT_KEYPAIR ?? "");
const vaultAddress = process.env.DIM_VAULT ?? "";

// Owner, once: write the Mandate.
const ownerWallet = new ChannelWallet({ backend: new SolanaChannelBackend({ connection, signer: ownerKey, vault: vaultAddress }) });
const now = Math.floor(Date.now() / 1000);
const created = await ownerWallet.createMandate({
  agentKey: agentKey.publicKey.toBase58(),
  nonce: 1n,
  budgetPerPeriod: 20_000_000n, // 20 USDC of new channels per day
  periodSeconds: 86_400,
  perRequestCap: 50_000n,
  maxGraceSeconds: 3_600,
  sellers: [{ seller_settler: route.settler, seller_payout_token: route.payoutToken }],
  expiry: now + 7 * 86_400,
});

// Agent process: load the Mandate from chain and spend inside it.
const agentBackend = new SolanaChannelBackend({ connection, signer: agentKey, vault: vaultAddress });
const mandate = await agentBackend.readMandate(created.address);
const agentWallet = new ChannelWallet({ backend: agentBackend, mandates: [mandate] });
await agentWallet.open(route, { budget: 2_000_000n, serviceEnd: now + 86_400, graceSeconds: 3_600 });

For a sub-agent, pass the whole chain, leaf first: mandates: [child, parent]. See budgets for agent swarms.

3. Size the channel

A channel's budget is the most it can ever pay that seller. Pick it from three numbers:

  • Concurrency × cap. Each request in flight reserves its full cap until its receipt arrives. Ten parallel requests with a 20,000 micro-USDC cap need 200,000 of headroom.
  • Unvouchered backlog. Charges you have not yet paid by voucher also count against the budget. The SDK vouchers every 32 receipts or about 10 ms by default, so this stays small.
  • How long you will use it. Service end T must be in the future; after it, the seller stops serving. Grace G (1 second to 30 days) is how long the seller has to redeem after closing begins.

Under a Mandate, the channel must also fit the Mandate: allowed seller pair, T ≤ expiry, G ≤ max_grace_secs, and the period budget must have room for the full channel budget.

4. Top up or roll over

When a channel runs low, wallet.topUp(route, terms) opens a successor channel with the next epoch; new requests use it, and the old one keeps its receipts and vouchers until you close and finalize it. The program also has a top_up instruction that adds budget to an open channel in place; the SDK does not use it today.