Borrowing through the treasury desk
The borrower desk is how an agent borrows without ever holding the money. A borrower owner has two treasuries: one for SUI collateral and one for the loan coin, called the cash treasury. Collateral leaves the SUI treasury only into a loan, the loan's proceeds land in the cash treasury, and repayments are drawn from it.
The loan's LoanDeed is stored under the SUI treasury, so only the desk can spend it: the agent through its caps, or the owner through the OwnerCap. That is how a revoked or stopped agent's loans stay reachable.
Borrow rules#
Every loan the desk opens is held to the owner's borrow rules for that agent, stored on chain with the SUI treasury's cap.
| Rule | Meaning |
|---|---|
| max_rate_bps | The highest yearly rate a desk loan may open at. For a pool loan this is the pool's rate at the moment of borrowing; after that the loan floats with its pool and the rule no longer applies. |
| min_term_ms | An offer loan's shortest allowed term. Pool loans have no term. |
| latest_maturity_ms | No loan of any kind opens at or after this moment, and no offer loan may mature after it. |
| cash_id | The owner's own cash treasury, the only one a new loan's proceeds may land in. Anyone can create a treasury whose AgentCap goes to the agent's address while keeping its OwnerCap, so without the pin proceeds could be steered into a treasury the owner does not control. |
| pool_id | The one pool the agent may borrow from, or none: then every pool is refused. Pools can be created by anyone, so without the pin an agent could be steered into a pool whose rate its owner does not control. |
| allowance | What the agent may still borrow, in the loan coin. Every new loan uses it up, a refinance only by what it borrows over the old loan's debt. Nothing gives it back: not a repay, a claim, a liquidation or a default. It is not a cap on the debt outstanding, so no repay has to credit it. |
Only the owner sets them, with set_borrow_mandate; a revoke removes them. Each set replaces the allowance with the new number, which is the only way the owner tops it up.
What the desk checks, before each loan opens:
| Desk call | Checks |
|---|---|
fill | That the cash treasury receiving the proceeds is the pinned one; then as for refinance_offer. |
fill, refinance_offer | The offer's rate against max_rate_bps; its term against min_term_ms; that it is still before latest_maturity_ms and the loan would mature by then. |
borrow_pool | That the cash treasury is the pinned one; that the pool is the pinned one; the pool's rate right after the draw against max_rate_bps; that it is still before latest_maturity_ms. |
refinance_pool | The new offer as for fill, and that the pool being left is the pinned one. |
fill, borrow_pool | That the loan's amount is no more than the allowance has left; it is then subtracted. |
refinance_offer, refinance_pool | That the change, what the new loan pays out over what the old one owed, is no more than the allowance has left; it is then subtracted. |
Abort (treasury) | When |
|---|---|
ENoBorrowMandate (23) | The cap has no borrow rules: a cap without them opens no loan at all. |
ERateAboveCeiling (24) | The opening rate is above max_rate_bps. |
ETermTooShort (25) | An offer's term is under min_term_ms. |
EPastRepayBy (26) | It is at or past latest_maturity_ms, or the offer loan would mature after it. |
EPoolNotAllowed (27) | The pool is not the pinned one, or no pool is pinned. |
ECashNotAllowed (28) | The cash treasury receiving a new loan's proceeds is not the pinned one. |
EOverAllowance (29) | The new loan, or a refinance's change, is more than the allowance has left. |
Each use emits AllowanceUsed with the amount and what remains. Read what is left with borrow_allowance_of.
Repaying, adding collateral and claiming are not held to the borrow rules, so a borrower can always close out.
For a hosted borrower the rules come from its form: the maximum rate is yours, the shortest term is 6 hours, the latest maturity is the agent's repay-by time, one hour before its rules end, the pool pin is the deployment's pool for a switcher and none for a taker or shopper, the cash pin is the agent's own cash treasury, and the allowance is the amount on your form, plus 0.001 USDC for each of a switcher's two refinances (a taker or shopper gets the amount alone). The same transaction that deposits the collateral sets both caps' limits and these rules.
The owner action rules signs both caps' limits and the borrow rules again from the form and moves no money. It does not let a hosted agent borrow a second time: one that has opened a loan, a refinance included, does not borrow again. See Agent transactions.
Flow#
SUI treasury --(collateral, AgentCap draw)--> Loan --(proceeds)--> cash treasury
^ | |
| | <--(repayment draw)----+
+------(collateral home on repay)-----------+
+------(leftover after liquidation / default: claim)
LoanDeed held under the SUI treasury, keyed by loan idAgent functions#
Each takes the agent's two AgentCaps and runs the normal cap checks on every draw.
| Function | What it does |
|---|---|
fill | Borrows from an offer: draws collateral from the SUI treasury, fills, holds the deed, puts the proceeds in cash. |
borrow_pool | The same against the pool. |
repay | Draws exactly what is owed from cash; the collateral returns to the SUI treasury. The lender's payout must be delivered in the same transaction. |
repay_pool | The same for a pool loan. |
add_collateral | Tops up a loan whose deed this treasury holds. |
claim | Brings home what a liquidation, write-off or default left. |
refinance_offer | Moves an offer loan into a new offer in one call; the old lender is paid and the change lands in cash. |
refinance_pool | Moves a pool loan into an offer in one call. |
An agent repays only from the cash treasury that received the loan's proceeds.
Owner functions#
These take the SUI treasury's OwnerCap and work whether the agent is running, paused or revoked.
| Function | What it does |
|---|---|
owner_repay | Repays with any coin; the change comes back. |
owner_repay_pool | The same for a pool loan. |
owner_add_collateral | Tops up from the SUI treasury. |
owner_claim | Brings home a closed loan's leftover. |
Reading the desk#
holds(treasury, loan_id) tells whether the treasury holds a loan's deed; cash_of(treasury, loan_id) returns the cash treasury it repays from.