SDK/Getting started

Quickstart

TESTNET

install both packages from the package.json in Install, including the underscore override. The full flow list for an agent is Hand this to your agent. The steps below run on Sui testnet against the hosted API's ids; the same calls run on a localnet with network: "localnet" and a local deployment record.

You need: the deployment record (GET /v1/config, saved as is), an agent key file, and an account the owner has already created for that agent's address. The account ids (treasuryId, agentCapId, and a borrower's cash treasury) come from GET /v1/agent/setup once its state is ready, which is what EsseyService.setup() returns. The owner's setup is EsseyOwner or Owner actions.

1. Connect#

TypeScript
import { connect, deploymentOf, EsseyAgent, EsseyService, loadAgentKey } from "@esseyonsui/sui-sdk";

const API = "https://sui-agent-prototype-production.up.railway.app";
const chain = await connect({ network: "testnet" });
const deployment = deploymentOf(await (await fetch(`${API}/v1/config`)).json());

connect takes localnet (a local host only) or testnet (Sui's testnet hosts only) — Essey has no mainnet deployment — reads the chain id and refuses one that does not match the network; a localnet or testnet connection refuses a node that answers with the mainnet chain id. deploymentOf checks the record has chainId, pkg, loanCoin, reader, feed, market and a pool that is an id or null.

2. Load the agent's key#

TypeScript
const signer = loadAgentKey("/path/to/agent.env", "testnet");

The key file holds SUI_NETWORK, SUI_ADDRESS and SUI_PRIVATE_KEY lines and must be mode 600. It is refused if its SUI_NETWORK tag is not the network asked for, if its address does not match its key, or, on testnet, if its address holds a balance on mainnet. createAgentKeyFile(path, network) writes a new one and returns its address — the address the owner creates the account for.

3. Start the agent#

TypeScript
const service = new EsseyService(API, signer);
await service.signIn();
const setup = await service.setup(); // GET /v1/agent/setup; poll until state is "ready" and account is set
const agent = await EsseyAgent.start({
  chain,
  deployment,
  signer,
  account: setup.account, // { treasuryId, agentCapId }; a borrower also has cash: { treasuryId, capId }
  gas: "address-balance", // faucet SUI funds the address balance. "coins" needs the coin object fundGas creates
});

treasuryId and agentCapId are not in GET /v1/config. They are on GET /v1/agent/setup once state is ready: setup.account.treasuryId and setup.account.agentCapId. A borrower also uses setup.account.cash.treasuryId and setup.account.cash.capId. The owner record from EsseyOwner names that cash cap cash.agentCapId. AgentAccount names the same id cash.capId. EsseyService.setup() is that GET and returns the same object, or null when this key has no setup. After POST /v1/byo/claim the state is claimed and account is still null. Poll GET /v1/agent/setup (the agent session) until state is ready, which is when the owner's create has landed. Do not start the agent on the null account.

start refuses a deployment for another chain (NETWORK_REFUSED) and runs the key-separation check; if the check fails it throws instead of returning an agent that would sign. The public Sui testnet faucet credits the address balance, which is gas "address-balance" (the default when gas is unset). fundGas creates a SUI coin object, which is what gas "coins" spends: one coin of at least 50,000,000 MIST, or NO_COINS before signing. Faucet SUI is not that coin.

4. Read#

TypeScript
const now = await agent.book.clockMs();
const offers = (await agent.book.offers())
  .filter((o) => o.expiresAtMs > now)
  .sort((a, b) => a.rateBps - b.rateBps);
const SUI = 1_000_000_000n;
const value = await agent.book.borrowValue(25n * SUI);            // what 25 SUI is worth at the oracle price, in loan-coin units; not the borrowable amount
const ltvCap = await agent.book.marketLtv();                       // the market's LTV ceiling, bps
const ltv = await agent.book.ltvOnChain(25n * SUI, 5n * 1_000_000n); // the LTV the chain computes for this collateral and debt, bps
const funds = await agent.book.treasuryBalance(treasuryId, deployment.loanCoin); // what the treasury holds of that coin
const mandate = await agent.book.mandate(treasuryId, agentCapId); // a lender's { totalCap, outstanding, funds }; null once revoked

borrowValue is the collateral's value at the lower of the oracle's spot and EMA price (market::borrow_value), not what the agent may borrow. Size a borrow so that ltvOnChain(collateral, amount) stays under marketLtv() and, for an offer, under the offer's maxLtvBps (offer::fill abort 6, EOverLtv). treasuryBalance and mandate take two arguments each.

5a. Lend#

TypeScript
const USDC = 1_000_000n;
const posted = await agent.postOffer({
  rateBps: 800,
  maxLtvBps: 4_000,
  minFill: 1n * USDC,
  maxFill: 10n * USDC,
  size: 10n * USDC,
  termMs: 3 * 86_400_000,
  expiryMs: 86_400_000,
});
if (!posted.ok) console.log(posted.abort);

The owner's mandate is enforced by the chain: in the localnet run, an offer at 100 bps under a 300 bps mandate floor landed as a refusal, treasury::assert_offer_terms abort 17 (ERateBelowFloor).

5b. Borrow and repay#

A borrower agent borrows through the treasury desk: collateral comes from its SUI treasury, the loan coin lands in its cash treasury, and the repayment is drawn from there.

TypeScript
const borrowed = await agent.borrowFromOffer(offers[0].id, 25n * SUI, 5n * USDC);
const [loan] = (await agent.myLoans()).filter((l) => l.state === "open");
const repaid = await agent.repay(loan.id);

Desk borrows are held to the borrow rules the owner set: the localnet run's fill at 800 bps, over a 700 bps rule, landed as treasury::ERateAboveCeiling (24). On testnet the same refusal, and aborts 23 and 25–28, landed through the SDK in one run against the earlier package 0x628b…6579. The live package is 0x6786…61b4 (see the Status table).

The owner's preset decides which pool a desk may borrow from. A switcher's rules pin the deployment's pool, so agent.borrowFromPool is for a switcher. A taker borrows from open offers with agent.borrowFromOffer, and a shopper through its sealed room; their rules pin no pool. A desk borrows only from the pool its rules pin, so borrowFromPool from either aborts treasury::assert_pool 27 EPoolNotAllowed. The rules each preset writes are in Agent transactions.

Results#

Every signing method simulates first and throws WOULD_ABORT before signing when the node foresees a Move abort (err.abort names it). What is signed returns a Landed: ok, the real digest, and on a refusal an abort naming the Move module, function, code and constant. See Errors.