Docs

Developers

SDK quickstart

@mortiseagentvault/sdk is the TypeScript client for both sides of a vault: root user operations for the owner, and actions, simulation and execution for the agent. The deployed addresses are built in.

Install

npm install @mortiseagentvault/sdk viem

It needs Node.js 20 or later and is one bundled module with no runtime dependencies. Its types expect the viem clients you create yourself. viem pins ws 8.21.0, which npm audit reports; the SDK's bundle contains no ws code. To move your project to the patched release, add this to its package.json:

"overrides": { "ws": "^8.23.1" }

Create a vault and add an agent

The vault's address is known before it exists. The first root user operation deploys it and applies the settings in one step. This example gives an agent 30 days, the WETH adapter and a cap of 0.1 ETH a day.

owner.ts
import { createPublicClient, http, parseEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import {
  ETH, robinhood, mortise, markets, gate, singleKeyRoot, ecdsaRootSigner,
  predictVault, buildRootOp, sendUserOpToBundler,
} from "@mortiseagentvault/sdk";

const client = createPublicClient({ chain: robinhood, transport: http(RPC_URL) });
const owner = privateKeyToAccount(OWNER_KEY);
const root = singleKeyRoot(owner.address);
const vault = await predictVault(client, root, 0n);
// Send the vault a little ETH for gas (or use a paymaster), then:

const day = 86_400n;
const until = (await client.getBlock()).timestamp + 30n * day;
const op = await buildRootOp(
  client,
  vault,
  {
    kind: "execute",
    calls: [
      gate.setAgent(AGENT, until),
      gate.setAdapterAccess(AGENT, mortise.weth, true),
      gate.setMarket(mortise.weth, markets.weth(), true),
      gate.setCap(AGENT, ETH, parseEther("0.1"), day),
    ],
  },
  { root, signers: [ecdsaRootSigner(0, owner)], create: { salt: 0n }, bundlerUrl: BUNDLER_URL },
);
const result = await sendUserOpToBundler(client, BUNDLER_URL, op);

sendUserOpToBundler sends through any ERC-4337 bundler for EntryPoint v0.7. sendUserOpDirect sends it straight to the EntryPoint from an ordinary account instead.

Act as the agent

The agent uses its own key, sends transactions to the Gate and pays its own gas. It never holds the vault's funds or its root keys.

agent.ts
import { parseEther } from "viem";
import {
  ETH, mortise, actions, simulateActions, executeActions, permissions, mortiseErrorName,
} from "@mortiseagentvault/sdk";

const wrap = [actions.weth({ wrap: true, amount: parseEther("0.01") })];
try {
  await simulateActions(client, agent.address, vault, wrap);
} catch (err) {
  console.log(mortiseErrorName(err)); // e.g. "CapExceeded", "MarketNotAllowed", "NotAgent"
}
const receipt = await executeActions(client, agentWallet, vault, wrap);
const left = await permissions(client, vault, agent.address, { adapters: [mortise.weth], tokens: [ETH] });

actions.* encodes the input of every adapter: Uniswap v3 and v4 swaps and positions, Morpho Blue, ERC-4626, Pons, Flap, Across, WETH, listed transfers and family transfers. markets.* gives the market id the owner allows for each of them.

Errors

Contract errors decode by name with mortiseErrorName and decodeMortiseError. The full list, with causes, is in Errors and refusals.