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. The SDK derives it and, signed by the owner, creates and funds it:
import { readFileSync } from "node:fs";
import { Connection, Keypair } from "@solana/web3.js";
import { SolanaChannelBackend, vaultAddress } from "@im/sdk/solana";
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 vault = vaultAddress(ownerKey.publicKey); // derived with the devnet build's test mint by default
const ownerBackend = new SolanaChannelBackend({ connection, signer: ownerKey, vault });
await ownerBackend.initVault(); // init_vault; does nothing if the vault already exists
await ownerBackend.deposit(5_000_000n); // 5 test USDC from the owner's associated token account
console.log("vault:", vault.toBase58());
Remember:
- deposit with the
depositinstruction (ownerBackend.deposit); tokens sent straight to the vault's token account are never credited and cannot be withdrawn; - simulate
init_vaultand 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;
- on devnet the SDK builds instructions from the deployed program's IDL (
programs/idl/deployed/im_channels.json), which can differ from the tracked IDL atprograms/idl/im_channels.json(#126).
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. This continues the code from step 1:
import { ChannelWallet, type Route } from "@im/sdk";
declare const route: Route; // a verified route, from DiscoveryClient
const agentKey = loadKey(process.env.DIM_AGENT_KEYPAIR ?? "");
// Owner, once: write the Mandate.
const ownerWallet = new ChannelWallet({ backend: ownerBackend });
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 });
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
Tmust be in the future; after it, the seller stops serving. GraceG(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, amount) adds amount from the vault's free balance to that seller's open channel in place (the program's top_up). The service end and grace stay the same, and under a Mandate the amount counts against the Mandate's period budget like a new channel would. To change the service end, wallet.openSuccessor(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.