API/On-chain reference

borrower

essey_sui::borrower — the borrower desk. A borrower's loans run between its SUI treasury (Treasury<C>, here sui) and its cash treasury (Treasury<L>, here cash), never through the agent's address; the LoanDeed is kept under the SUI treasury, keyed by loan id. See Borrowing through the treasury desk.

Before a loan opens, fill, borrow_pool, refinance_offer and refinance_pool check it against the SUI cap's borrow rules (treasury::assert_borrow): an offer's rate and term, or for a pool loan the pool's rate after accruing. borrow_pool also checks that the pool is the one the rules pin, and refinance_pool that the pool being left is (treasury::assert_pool). fill and borrow_pool check that cash is the cash treasury the rules pin (treasury::assert_cash), so a new loan's proceeds can only land in the owner's own cash treasury. Each of the four then uses up the cap's borrowing allowance (treasury::use_allowance): fill and borrow_pool by the loan's amount, refinance_offer and refinance_pool only by the change, what the new loan pays out over what the old one owed; more than is left aborts 29. A cap without borrow rules opens no loan (treasury aborts 23–29, see treasury).

Every draw from either treasury runs the full AgentCap checks: live, not expired, within per_tx and per_day.

fill, borrow_pool, refinance_offer and refinance_pool also take the lending Access object and pass it to loan, which must admit the agent's address for the amount (access::assert_open_now).

Agent functions — need both AgentCaps#

Move
public fun fill<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, cash_cap: &AgentCap, cash: &mut Treasury<L>, offer: &Offer<L, C>, lender: &mut Treasury<L>, m: &Market<C, L>, r: &Reading, collateral: u64, amount: u64, acc: &Access, clock: &Clock, ctx: &mut TxContext)
public fun borrow_pool<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, cash_cap: &AgentCap, cash: &mut Treasury<L>, p: &mut Pool<C, L>, m: &Market<C, L>, r: &Reading, collateral: u64, amount: u64, acc: &Access, clock: &Clock, ctx: &mut TxContext)

Draw collateral from sui, open the loan (loan::fill or loan::borrow_from_pool), hold the deed under sui, put the proceeds in cash.

Move
public fun repay<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, cash_cap: &AgentCap, cash: &mut Treasury<L>, l: &mut Loan<C, L>, clock: &Clock, ctx: &mut TxContext): Payout<L>
public fun repay_pool<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, cash_cap: &AgentCap, cash: &mut Treasury<L>, l: &mut Loan<C, L>, p: &mut Pool<C, L>, clock: &Clock, ctx: &mut TxContext)

Draw exactly what is owed from cash — the cash treasury the loan's proceeds went to (EWrongCash) — repay, and return the collateral to sui. The offer-loan Payout must reach loan::pay_treasury. ENoDeed if sui does not hold the deed.

Move
public fun add_collateral<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, l: &mut Loan<C, L>, amount: u64, clock: &Clock, ctx: &mut TxContext)
public fun claim<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, l: &mut Loan<C, L>, clock: &Clock, ctx: &mut TxContext)

Top up a loan whose deed sui holds; or bring home what a liquidation, write-off or default left. These need only the SUI treasury's cap.

Move
public fun refinance_offer<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, cash_cap: &AgentCap, cash: &mut Treasury<L>, old: &mut Loan<C, L>, old_lender: &mut Treasury<L>, offer: &Offer<L, C>, lender: &mut Treasury<L>, m: &Market<C, L>, r: &Reading, amount: u64, acc: &Access, clock: &Clock, ctx: &mut TxContext)
public fun refinance_pool<C, L>(sui_cap: &AgentCap, sui: &mut Treasury<C>, cash_cap: &AgentCap, cash: &mut Treasury<L>, old: &mut Loan<C, L>, p: &mut Pool<C, L>, offer: &Offer<L, C>, lender: &mut Treasury<L>, m: &Market<C, L>, r: &Reading, amount: u64, acc: &Access, clock: &Clock, ctx: &mut TxContext)

Move an offer or pool loan into a new offer in one call; the old lender is paid and the change lands in cash.

Owner functions — need the SUI treasury's OwnerCap#

Work whether the agent is running, stopped or revoked.

Move
public fun owner_repay<C, L>(o: &OwnerCap, sui: &mut Treasury<C>, l: &mut Loan<C, L>, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<L>, Payout<L>)
public fun owner_repay_pool<C, L>(o: &OwnerCap, sui: &mut Treasury<C>, l: &mut Loan<C, L>, p: &mut Pool<C, L>, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): Coin<L>
public fun owner_add_collateral<C, L>(o: &OwnerCap, sui: &mut Treasury<C>, l: &mut Loan<C, L>, amount: u64, ctx: &mut TxContext)
public fun owner_claim<C, L>(o: &OwnerCap, sui: &mut Treasury<C>, l: &mut Loan<C, L>, ctx: &mut TxContext)

Repay with any coin (change returned), top up from sui, or claim a closed loan's leftover into sui.

Views#

holds<C>(sui, loan_id): bool — whether sui holds the loan's deed. cash_of<C>(sui, loan_id): ID — the cash treasury it repays from (ENoDeed).

Events and abort codes#

DeedHeld { treasury_id, loan_id, cash_id }, DeedReleased { treasury_id, loan_id }. Aborts: 1 EWrongCash, 2 ENoDeed, plus the treasury and loan codes.