SDK/Reference

Book and Ptb

PREVIEW

Book reads the chain; Ptb builds every agent transaction. EsseyAgent exposes both as agent.book and agent.ptb.

Signatures below are copied from the source; descriptions are the source's own doc comments.

Book#

Every chain read an agent makes. All reads go to the local fullnode; nothing here signs.

TypeScript
export class Book

constructor

TypeScript
constructor(
  readonly client: SuiGrpcClient,
  readonly d: EsseyDeployment,
  readonly reader: string,
  readonly events: EventReader,
  readonly touches: TouchReader | null = null,
) {}

private t(name: string)

Book.read

The reader call every fill, borrow, refinance and liquidation PTB starts with.

TypeScript
read(tx: Transaction): TransactionResult

Book.offers

TypeScript
async offers(): Promise<OfferView[]>

Book.loans

TypeScript
async loans(): Promise<LoanView[]>

Book.deeds

TypeScript
async deeds(owner: string): Promise<{ deedId: string; loanId: string }[]>

Book.heldLoans

Loans whose deed a borrower's SUI treasury holds: its borrower::DeedKey dynamic fields, every page.

TypeScript
async heldLoans(treasuryId: string): Promise<string[]>

Book.ownPoolShares

The PoolShares its own deposits created that it still holds; a share anyone else sends it is never read.

TypeScript
async ownPoolShares(owner: string): Promise<string[]>

Book.offerOutstanding

Principal still out under an offer; its room is size minus this (treasury.move offer_outstanding).

TypeScript
offerOutstanding(treasury: string, offerId: string)

Book.borrowValue

TypeScript
borrowValue(collateral: bigint)

Book.ltvOnChain

TypeScript
ltvOnChain(collateral: bigint, debt: bigint)

Book.marketParams

(ltv_bps, liq_bps, bonus_bps), market.move params.

TypeScript
async marketParams(): Promise<{ ltvBps: number; liqBps: number; bonusBps: number }>

Book.marketLtv

TypeScript
marketLtv()

Book.quoteBps

TypeScript
quoteBps(amount: bigint)

Book.owed

TypeScript
async owed(loanId: string)

Book.underwater

Underwater at spot AND EMA for what the loan owes this second (market.move:174-179).

TypeScript
async underwater(loan: LoanView): Promise<boolean>

Book.treasuryBalance

TypeScript
treasuryBalance(treasury: string, coinType: string)

Book.mandate

The cap's rules and what the account holds, in one read: a bid or offer is sized against both (G-lender); null once revoked.

TypeScript
async mandate(
  treasury: string,
  capId: string,
): Promise<{ totalCap: bigint; outstanding: bigint; funds: bigint } | null>

Book.capLimits

treasury::limits_of for a loan-coin treasury; null once revoked.

TypeScript
async capLimits(treasury: string, capId: string)

Book.capExpiresAt

TypeScript
async capExpiresAt(treasury: string, capId: string): Promise<number | null>

Book.lenderMandates

Audit R2P-M1: each ask's LendMandate and Limits entries (treasury.move:80-101), read as the Table fields they are: two batched object reads for any number of offers, and a missing entry is null, never an abort that hides the rest.

TypeScript
async lenderMandates(asks: LenderAsk[]): Promise<(MandateView | null)[]>

Book.graceMs

TypeScript
graceMs()

Book.clockMs

TypeScript
async clockMs(): Promise<number>

Book.rooms

Rooms still open to offers, from the shared index after one bounded refresh; IndexLoading until it has caught up.

TypeScript
rooms(): Promise<OpenRoom[]>

Book.roomOffers

TypeScript
async roomOffers(roomId: string): Promise<(SealedOnChain & { ciphertext: Uint8Array })[]>

Book.coins

TypeScript
async coins(owner: string, coinType: string)

From ptb.ts#

Exported by index.ts.

Me#

cash: a hosted borrower's loan-coin treasury and its cap there; proceeds go in, repayments come out.

TypeScript
export type Me = {
  address: string;
  treasuryId: string;
  agentCapId: string;
  cash?: { treasuryId: string; capId: string };
};

PtbBook#

What the builders need of a Book; structural so the console can build the same PTBs without node:fs.

TypeScript
export type PtbBook = {
  d: { pkg: string; loanCoin: string; market: string; pool: string | null; access?: string | null };
  read(tx: Transaction): TransactionResult;
};

Ptb#

Agent PTBs. Every fill, borrow, refinance and liquidation starts with the reader call (M3/M4 hand-off).

TypeScript
export class Ptb

constructor

TypeScript
constructor(
  readonly book: PtbBook,
  readonly me: Me,
  readonly gas: "own" | "sponsored" = "sponsored",
) {}

private get d()

Ptb.post

TypeScript
post(terms: OfferTerms, nowMs: number): Transaction

Ptb.cancel

TypeScript
cancel(offerId: string): Transaction

Ptb.spend

TypeScript
spend(coinType: string, amount: bigint): Transaction

Ptb.poolDeposit

TypeScript
poolDeposit(coinIds: string[], amount: bigint): Transaction

Ptb.redeem

TypeScript
redeem(shareId: string): Transaction

Ptb.depositLoanCoin

TypeScript
depositLoanCoin(amount: bigint): Transaction

Ptb.fill

TypeScript
fill(offer: OfferView, collateral: bigint, amount: bigint): Transaction

Ptb.borrowFromPool

TypeScript
borrowFromPool(collateral: bigint, amount: bigint): Transaction

Ptb.refinance

One PTB: the old loan's collateral funds the new fill, whose cash repays the old lender (G10's shape).

TypeScript
refinance(deedId: string, loan: LoanView, offer: OfferView, amount: bigint): Transaction

Ptb.repay

TypeScript
repay(deedId: string, loan: LoanView, coinIds: string[]): Transaction

Ptb.liquidate

Audit R1T: liquidate takes owed at execution from the coin it is given, so it is given exactly the checked amount.

TypeScript
liquidate(loan: LoanView, coinIds: string[], pay: bigint): Transaction

Ptb.deskFill

Collateral from the SUI treasury into the loan; proceeds into the cash treasury; the deed stays in the SUI treasury.

TypeScript
deskFill(offer: OfferView, collateral: bigint, amount: bigint): Transaction

Ptb.deskPool

TypeScript
deskPool(collateral: bigint, amount: bigint): Transaction

Ptb.deskRepay

Exactly what is owed comes out of the cash treasury; the collateral goes home to the SUI treasury.

TypeScript
deskRepay(loan: LoanView): Transaction

Ptb.deskAddCollateral

TypeScript
deskAddCollateral(loanId: string, amount: bigint): Transaction

Ptb.deskClaim

TypeScript
deskClaim(loanId: string): Transaction

Ptb.deskRefinance

One call: the old loan's collateral funds the new fill, the old lender is repaid, the change lands in cash.

TypeScript
deskRefinance(loan: LoanView, offer: OfferView, amount: bigint): Transaction

Ptb.openRoom

TypeScript
openRoom(
  collateral: bigint,
  amount: bigint,
  maxRateBps: number,
  termMs: number,
  ctLen: number,
  closeMs: number,
  settleByMs: number,
)

Ptb.consentSealed

TypeScript
consentSealed(digest: Uint8Array): Transaction

Ptb.placeOffer

TypeScript
placeOffer(roomId: string, ciphertext: Uint8Array, commitment: Uint8Array): Transaction

Ptb.settle

TypeScript
settle(
  roomId: string,
  index: number,
  o: { rateBps: bigint; salt: Uint8Array; treasuryId: string; capId: string },
)

Ptb.addCollateral

TypeScript
addCollateral(loanId: string, collateral: bigint): Transaction

Ptb.decline

TypeScript
decline(roomId: string): Transaction

Ptb.refund

TypeScript
refund(roomId: string): Transaction

Ptb.claim

TypeScript
claim(deedId: string, loanId: string): Transaction