Book and Ptb
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.
export class Bookconstructor
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.
read(tx: Transaction): TransactionResultBook.offers
async offers(): Promise<OfferView[]>Book.loans
async loans(): Promise<LoanView[]>Book.deeds
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.
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.
async ownPoolShares(owner: string): Promise<string[]>Book.offerOutstanding
Principal still out under an offer; its room is size minus this (treasury.move offer_outstanding).
offerOutstanding(treasury: string, offerId: string)Book.borrowValue
borrowValue(collateral: bigint)Book.ltvOnChain
ltvOnChain(collateral: bigint, debt: bigint)Book.marketParams
(ltv_bps, liq_bps, bonus_bps), market.move params.
async marketParams(): Promise<{ ltvBps: number; liqBps: number; bonusBps: number }>Book.marketLtv
marketLtv()Book.quoteBps
quoteBps(amount: bigint)Book.owed
async owed(loanId: string)Book.underwater
Underwater at spot AND EMA for what the loan owes this second (market.move:174-179).
async underwater(loan: LoanView): Promise<boolean>Book.treasuryBalance
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.
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.
async capLimits(treasury: string, capId: string)Book.capExpiresAt
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.
async lenderMandates(asks: LenderAsk[]): Promise<(MandateView | null)[]>Book.graceMs
graceMs()Book.clockMs
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.
rooms(): Promise<OpenRoom[]>Book.roomOffers
async roomOffers(roomId: string): Promise<(SealedOnChain & { ciphertext: Uint8Array })[]>Book.coins
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.
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.
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).
export class Ptbconstructor
constructor(
readonly book: PtbBook,
readonly me: Me,
readonly gas: "own" | "sponsored" = "sponsored",
) {}
private get d()Ptb.post
post(terms: OfferTerms, nowMs: number): TransactionPtb.cancel
cancel(offerId: string): TransactionPtb.spend
spend(coinType: string, amount: bigint): TransactionPtb.poolDeposit
poolDeposit(coinIds: string[], amount: bigint): TransactionPtb.redeem
redeem(shareId: string): TransactionPtb.depositLoanCoin
depositLoanCoin(amount: bigint): TransactionPtb.fill
fill(offer: OfferView, collateral: bigint, amount: bigint): TransactionPtb.borrowFromPool
borrowFromPool(collateral: bigint, amount: bigint): TransactionPtb.refinance
One PTB: the old loan's collateral funds the new fill, whose cash repays the old lender (G10's shape).
refinance(deedId: string, loan: LoanView, offer: OfferView, amount: bigint): TransactionPtb.repay
repay(deedId: string, loan: LoanView, coinIds: string[]): TransactionPtb.liquidate
Audit R1T: liquidate takes owed at execution from the coin it is given, so it is given exactly the checked amount.
liquidate(loan: LoanView, coinIds: string[], pay: bigint): TransactionPtb.deskFill
Collateral from the SUI treasury into the loan; proceeds into the cash treasury; the deed stays in the SUI treasury.
deskFill(offer: OfferView, collateral: bigint, amount: bigint): TransactionPtb.deskPool
deskPool(collateral: bigint, amount: bigint): TransactionPtb.deskRepay
Exactly what is owed comes out of the cash treasury; the collateral goes home to the SUI treasury.
deskRepay(loan: LoanView): TransactionPtb.deskAddCollateral
deskAddCollateral(loanId: string, amount: bigint): TransactionPtb.deskClaim
deskClaim(loanId: string): TransactionPtb.deskRefinance
One call: the old loan's collateral funds the new fill, the old lender is repaid, the change lands in cash.
deskRefinance(loan: LoanView, offer: OfferView, amount: bigint): TransactionPtb.openRoom
openRoom(
collateral: bigint,
amount: bigint,
maxRateBps: number,
termMs: number,
ctLen: number,
closeMs: number,
settleByMs: number,
)Ptb.consentSealed
consentSealed(digest: Uint8Array): TransactionPtb.placeOffer
placeOffer(roomId: string, ciphertext: Uint8Array, commitment: Uint8Array): TransactionPtb.settle
settle(
roomId: string,
index: number,
o: { rateBps: bigint; salt: Uint8Array; treasuryId: string; capId: string },
)Ptb.addCollateral
addCollateral(loanId: string, collateral: bigint): TransactionPtb.decline
decline(roomId: string): TransactionPtb.refund
refund(roomId: string): TransactionPtb.claim
claim(deedId: string, loanId: string): Transaction