API/Endpoints

Agent transactions

Every on-chain change to a hosted agent is an owner transaction: the server builds it, you sign it, the server executes it with the operator paying gas.

Build#

POST/v1/agents/:id/tx→ 201
FieldTypeNotes
actionstringOne of the actions below.
formobjectFor actions that apply rules.
amountdecimal stringFor money actions.
loanIdstringFor loan actions: 0x and 64 hex digits.
JSON
{
  "ok": true,
  "txId": "9f2c…(32 hex)",
  "action": "set-mandate",
  "bytes": "AAACAAgA…",
  "expiresAtMs": 1792900120000
}

bytes is the base64 transaction. You have 120 seconds to execute it.

The operator pays the network fee for owner transactions, by default 0.5 SUI a day for all owners together. When less than a quarter of that is left, only revoke, withdraw, claim-default, repay, claim and add-collateral are built; once it is spent, the build or the execute answers 429 SPONSOR_BUDGET_SPENT.

Sign#

Sign the decoded bytes exactly as given:

TypeScript
const { signature } = await keypair.signTransaction(fromBase64(bytes));

Execute#

POST/v1/agents/:id/tx/:txId

With { "signature": "…" } → { ok, digest, agent }.

A built transaction can be executed once; a failed attempt uses it up, so build again.

ErrorStatusWhen
NO_SUCH_TX404Unknown, already used, or for another agent.
TX_EXPIRED410Older than 120 seconds.
TX_FAILED502The chain rejected it; the body carries digest.
OWNERCAP_NOT_IN_WALLET502After create, an OwnerCap did not reach your wallet.
AGENTCAP_NOT_AT_AGENT502After create, an AgentCap did not reach the agent.
LOAN_OPEN409A borrower revoke, re-checked just before it executes.
SPONSOR_BUDGET_SPENT429The operator's network fees for today are used up.

Lender actions#

ActionBodyWhat it doesRefusals
create{form, amount?}One transaction: creates the treasury and the agent's cap, sets the mandate and the SUI ceiling, deposits amount if given, then shares the treasury; the OwnerCap comes to you.TREASURY_EXISTS, CREATE_IN_FLIGHT, CREATE_UNRESOLVED, WALLET_SHORT 409; BAD_FORM, BAD_AMOUNT, OVER_BUDGET_CAP 400
set-mandate{form, amount?}Sets the mandate and the SUI ceiling, extends the cap if the end date changed, and deposits amount if given.NEEDS_TREASURY 409, BAD_FORM 400, plus the deposit refusals
deposit{amount}Adds USDC to the treasury.BAD_AMOUNT 400, OVER_BUDGET_CAP 400; a wallet short of the USDC: 400, with a message that starts Insufficient balance of <coin type> for owner <address>. Required: <base units>
withdraw{amount?}Sends USDC to you; without amount, the whole balance.NOTHING_TO_WITHDRAW 409, BAD_AMOUNT 400, OVER_BALANCE 400
revoke—Revokes the agent's cap.—
claim-default{loanId}Takes the LenderClaim out of your treasury and collects the overdue loan's collateral to your wallet.BAD_LOAN_ID 400; NOT_THIS_LENDERS_LOAN, LOAN_NOT_OPEN, NOT_DUE 409

After a revoke, only withdraw and claim-default are allowed; anything else returns 409 REVOKED. A withdraw without amount from a revoked treasury that is already empty returns 409 NOTHING_TO_WITHDRAW; with an amount, it returns 400 OVER_BALANCE. rules is a borrower action: on a lender it returns 400 UNKNOWN_ACTION, revoked or not.

Setup is one signature: create with the form and the first amount. set-mandate and deposit edit and top up later.

A second create build replaces an unsigned one still pending for the agent. While one is executing, another is refused with CREATE_IN_FLIGHT. If a create was submitted but never recorded (a lost connection, a restart), the next create build or execute reads it from the chain first: landed, it is adopted and the call returns TREASURY_EXISTS; not yet visible, CREATE_UNRESOLVED until its signed bytes can no longer land. WALLET_SHORT carries wallet: {coin, holds, needs} in base units.

Borrower actions#

ActionBodyWhat it doesRefusals
create{form}One transaction: creates the SUI treasury and the cash treasury, each with the agent's cap, writes the borrow rules onto the SUI cap pinned to the new cash treasury, deposits the collateral (plus spare SUI) and the USDC float, then shares both; both OwnerCaps come to you.TREASURY_EXISTS, CREATE_IN_FLIGHT, CREATE_UNRESOLVED, WALLET_SHORT 409; BAD_FORM 400
deposit{form}Re-sets both caps to the form, writes the borrow rules onto the SUI cap (set_borrow_mandate: your maximum rate, a 6-hour shortest term, latest maturity = repay-by, the deployment's pool pinned for a switcher, none otherwise, your cash treasury pinned as the only place proceeds may land, and the borrowing allowance: the form's amount, plus 0.001 USDC per refinance for a switcher), and deposits the collateral (plus spare SUI) and the USDC float. The form sets the SUI cap's per-transaction and per-day limits to the collateral plus the spare SUI, so a second borrow that draws more collateral within 24 hours is refused in treasury::charge as abort 5 (EOverPerDay) before the allowance (abort 29) is reached.NEEDS_TREASURY 409, BAD_FORM 400
rules{form}Re-sets both caps to the form and writes the borrow rules again with a fresh allowance from the form. Moves no money. A hosted agent that has opened a loan does not borrow again, even with a fresh allowance.NEEDS_TREASURY 409, BAD_FORM 400, REVOKED 409
deposit{amount}Without a form: USDC into the cash treasury only, for example to fund a repay.BAD_AMOUNT 400
withdraw—Sends spare SUI to you, and cash only above what open loans still need (their owed, plus the cash float on the form). With no open loan, both treasuries empty.NOTHING_TO_WITHDRAW 409, LOAN_OPEN 409
revoke—Revokes both caps. Building it pauses the agent; if you do not sign, the agent resumes after the transaction expires.LOAN_OPEN 409
repay{loanId}Withdraws what is owed plus a small margin from cash, repays, and returns the change to cash.BAD_LOAN 400, NO_SUCH_LOAN 404, RATE_UNREAD, CASH_SHORT 409
claim{loanId}Brings a closed loan's leftover collateral into the SUI treasury.NOTHING_TO_CLAIM 409
add-collateral{loanId, amount}Moves amount SUI from the SUI treasury into the loan.BAD_AMOUNT 400, OVER_BALANCE 400

After a revoke, withdraw, repay, claim and add-collateral still work. RATE_UNREAD is a pool loan whose live rate could not be read, so the repay amount cannot be set.

The borrower form#

The borrower form is the form object on create, deposit and rules, and on POST /v1/borrow/preview. Amounts are decimal strings. There is no separate amount on a borrower create: the collateral and the USDC float are fields of the form. Hosted limits are 6 decimals on the loan coin and a budget of 100 USDC (maxBudget 100000000). A cap may last 30 days (maxCapLifetimeMs).

FieldTypeRequiredDefaultUnitsValidation
namestringyes——^[a-z0-9][a-z0-9-]{0,62}$: 1–63 characters, a letter or digit first, then lowercase letters, digits and dashes.
presetstringyes——taker, switcher or shopper. switcher needs a pool on this market. A hosted shopper cannot be launched (ROOM_NEEDS_PASSKEY); a self-run one can.
collateralstringyes—SUI, at most 9 decimalsAbove zero.
amountstringyes—loan coin (USDC here), at most 6 decimalsAbove zero, at most 100.
maxRateBpsnumberyes—basis points a yearInteger, 1 to 100000.
repayBeforeHoursnumberyes—hoursInteger, 0 to 720. 0 is accepted. The lead written on chain is the greater of this many hours and 1 hour.
cashFloatstringyes—loan coin"0" or a positive amount, at most 6 decimals. This is the USDC deposited for interest.
expiresAtMsnumberyes—Unix millisecondsAt least 62 minutes ahead and at most 30 days ahead. Also refused when interest at the rate bound could more than double the loan before repay: end the rules sooner.
autoTopUpbooleannooff—Must be a boolean when present. Absent, or any value other than true, is off.
topUpSuistringno"0"SUI, at most 9 decimals"0" or a positive amount. Absent is zero spare SUI.
autoClaimbooleannoon—Must be a boolean when present. Absent is on. Only false turns it off.

A working create body. Replace expiresAtMs with a time 62 minutes to 30 days ahead (Date.now() + 3 * 86400000 is inside that window):

JSON
{
  "action": "create",
  "form": {
    "name": "desk-taker",
    "preset": "taker",
    "collateral": "1",
    "amount": "1",
    "maxRateBps": 800,
    "repayBeforeHours": 6,
    "cashFloat": "0.05",
    "expiresAtMs": 1793500000000,
    "autoTopUp": false,
    "topUpSui": "0.1",
    "autoClaim": true
  }
}

BAD_FORM is 400 and includes errors: [{ field, message }], one entry per field that failed.

The same form is what a self-run owner signs: owner.sign(id, "create", { form }), with no amount. After that call lands, the agent's GET /v1/agent/setup moves from claimed to ready and account carries treasuryId, agentCapId and cash. Poll it; EsseyService.setup() is that GET.

A borrower agent then borrows from its own key. Collateral is base units of SUI drawn from the SUI treasury (account.treasuryId / account.agentCapId). The loan coin is credited to the cash treasury (account.cash.treasuryId / account.cash.capId). borrowFromPool throws NO_POOL when the deployment has no pool. repay draws what is owed from the cash treasury.

TypeScript
const setup = await service.setup(); // poll until setup.state === "ready"
const agent = await EsseyAgent.start({
  chain,
  deployment,
  signer: agentKey,
  account: setup!.account!, // { treasuryId, agentCapId, cash: { treasuryId, capId } }
  gas: "address-balance",
});
const SUI = 1_000_000_000n;
const USDC = 1_000_000n;
await agent.borrowFromOffer(offerId, 1n * SUI, 1n * USDC);
await agent.borrowFromPool(1n * SUI, 1n * USDC);
const [loan] = (await agent.myLoans()).filter((l) => l.state === "open");
await agent.repay(loan.id);

The owner walkthrough is Approve from a wallet key. The desk calls are EsseyAgent.

Example: lender setup#

Shell
# 1. build create
curl -s -X POST http://127.0.0.1:8790/v1/agents/$AGENT/tx \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{\"action\": \"create\", \"form\": $FORM}"
# 2. sign .bytes, then execute
curl -s -X POST http://127.0.0.1:8790/v1/agents/$AGENT/tx/$TXID \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{\"signature\": \"$SIG\"}"
# 3. the same two calls for set-mandate with the first deposit
curl -s -X POST http://127.0.0.1:8790/v1/agents/$AGENT/tx \
  -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
  -d "{\"action\": \"set-mandate\", \"form\": $FORM, \"amount\": \"20\"}"

with, for example:

JSON
{
  "name": "my-lender", "preset": "steady",
  "totalBudget": "20", "minLoan": "5", "maxLoan": "15",
  "minRateBps": 800, "maxLtvBps": 5000,
  "termsDays": [1], "expiresAtMs": 1793500000000
}

expiresAtMs must be a time in the future and within 30 days.