treasury
essey_sui::treasury — treasuries, their owner and agent capabilities, limits, lend mandates and sealed-room consents. See Treasury & OwnerCap.
Setup#
public fun create<T>(ctx: &mut TxContext): OwnerCapCreates and shares an empty treasury; returns its OwnerCap. Needs nothing.
public fun new_with_cap<T>(agent: address, per_tx: u64, per_day: u64, expires_at_ms: u64, recipients: vector<address>, clock: &Clock, ctx: &mut TxContext): (Treasury<T>, OwnerCap, ID)Creates a treasury, mints one AgentCap to agent with these limits, and returns the treasury unshared, its OwnerCap and the AgentCap's ID, so the same transaction can set rules and deposit. Checks the limits as mint_cap does (EBadLimits, EBadRecipients). The treasury has no drop and no store: the transaction fails unless share consumes it.
Shares the treasury and emits TreasuryCreated. Mints no OwnerCap.
public fun deposit<T>(t: &mut Treasury<T>, c: Coin<T>)Anyone. Aborts EZeroAmount on an empty coin.
Owner functions — need &OwnerCap of this treasury#
Each aborts EWrongTreasury if the OwnerCap belongs to another treasury.
public fun mint_cap<T>(o: &OwnerCap, t: &mut Treasury<T>, agent: address, per_tx: u64, per_day: u64, expires_at_ms: u64, recipients: vector<address>, clock: &Clock, ctx: &mut TxContext)Mints an AgentCap to agent. per_tx above zero and at most per_day, expiry in the future, one to eight recipients; else EBadLimits or EBadRecipients.
public fun set_limits<T>(o: &OwnerCap, t: &mut Treasury<T>, cap_id: ID, per_tx: u64, per_day: u64, expires_at_ms: u64, recipients: vector<address>, clock: &Clock)Replaces an agent's limits; the 24-hour spend history is kept. Same checks; ECapRevoked for a revoked cap.
public fun set_mandate<T>(o: &OwnerCap, t: &mut Treasury<T>, cap_id: ID, total_cap: u64, min_fill: u64, max_fill: u64, min_rate_bps: u64, max_duration_ms: u64, taker_kind: u8, takers: vector<address>)Creates or replaces an agent's lend mandate; a replacement keeps outstanding and the collateral set. EBadMandate unless 0 < min_fill ≤ max_fill ≤ total_cap, min_rate_bps < 100,000, max_duration_ms > 0, taker_kind ≤ 2, and a takers list of 1–64 only for kind 1.
public fun set_collateral<T, C>(o: &OwnerCap, t: &mut Treasury<T>, cap_id: ID, m: &Market<C, T>, max_ltv_bps: u64)Allows collateral C up to max_ltv_bps, which must not exceed the market's limit; 0 removes it. At most eight types. ENoMandate, EBadMandate.
public fun set_borrow_mandate<T>(o: &OwnerCap, t: &mut Treasury<T>, cap_id: ID, max_rate_bps: u64, min_term_ms: u64, latest_maturity_ms: u64, pool_id: Option<ID>, cash_id: ID, allowance: u64)Creates or replaces a borrower cap's BorrowMandate: the highest rate a desk loan may open at, an offer loan's shortest term, the latest moment a loan may open or an offer loan may mature, the one pool the cap may borrow from (none refuses every pool), the one cash treasury a new loan's proceeds may land in, and the allowance: what the agent may still borrow, in the loan coin. ECapRevoked for a revoked cap. A cap without one opens no loan through the desk.
A set replaces the allowance outright; that is the only way it goes back up. Every new desk loan uses it up (a refinance only its extra), and nothing gives it back: it is not a debt cap, so no repay touches it. Emits BorrowMandateSet with the new allowance.
public fun revoke<T>(o: &OwnerCap, t: &mut Treasury<T>, cap_id: ID)Removes the agent's limits, lend mandate and borrow rules. ECapRevoked if already revoked.
public fun withdraw<T>(o: &OwnerCap, t: &mut Treasury<T>, amount: u64, ctx: &mut TxContext): Coin<T>EZeroAmount, EInsufficientFunds.
public fun receive<T, O: key + store>(o: &OwnerCap, t: &mut Treasury<T>, item: Receiving<O>): OTakes out an object sent to the treasury's address, such as a LenderClaim.
Agent functions — need &AgentCap of this treasury#
public fun spend<T>(cap: &AgentCap, t: &mut Treasury<T>, amount: u64, recipient: address, clock: &Clock, ctx: &mut TxContext)Pays recipient from the treasury. Checks, in order: EWrongTreasury, ECapRevoked, ECapExpired, EZeroAmount, EOverPerTx, EOverPerDay, ERecipientNotAllowed, EInsufficientFunds.
public fun consent_sealed<T>(cap: &AgentCap, t: &mut Treasury<T>, digest: vector<u8>, clock: &Clock)Records the agent's consent to one sealed bid (a 32-byte digest from loan_room::consent_of). Cap must be live; EBadConsent for a wrong length or a digest already used.
public fun withdraw_consent<T>(cap: &AgentCap, t: &mut Treasury<T>, digest: vector<u8>)Removes the agent's own consent; ENoConsent if it is not this cap's.
Views#
| Function | Returns |
|---|---|
balance<T>(t): u64 | Funds held. |
mandate_of<T>(t, cap_id) | (total_cap, outstanding, min_fill, max_fill, min_rate_bps, max_duration_ms, taker_kind); ENoMandate if none. |
borrow_mandate_of<T>(t, cap_id) | (max_rate_bps, min_term_ms, latest_maturity_ms, pool_id); ENoBorrowMandate if none. |
borrow_cash_of<T>(t, cap_id): ID | The pinned cash treasury; ENoBorrowMandate if none. |
borrow_allowance_of<T>(t, cap_id): u64 | What the agent may still borrow, in the loan coin; ENoBorrowMandate if none. |
max_ltv_of<T, C>(t, cap_id): u64 | The cap's ceiling for collateral C. |
offer_outstanding<T>(t, offer_id): u64 | Principal out on an offer. |
has_consent<T>(t, digest): bool | |
has_mandate<T>(t, cap_id): bool | |
limits_of<T>(t, cap_id, clock) | (per_tx, per_day, expires_at_ms, spent in the 24 h window); ECapRevoked if gone. |
recipients_of<T>(t, cap_id) | The allowed recipients. |
is_active<T>(t, cap_id, clock): bool | Limits present and not expired. |
cap_treasury(cap): ID, owner_treasury(o): ID | The treasury a cap belongs to. |
taker_any(), taker_list(), taker_sealed_room() | 0, 1, 2. |
Package-only#
draw funds a loan from a lender's treasury under its mandate; repay returns principal to the mandate and the offer and never aborts on accounting; draw_for_loan is the borrower desk's only way to move funds out with an AgentCap; assert_borrow is the desk's check of a loan's rate and timing against the cap's borrow rules before it opens, assert_pool its check that a pool is the pinned one, and assert_cash its check that the proceeds go to the pinned cash treasury; use_allowance subtracts a new loan's amount from the allowance, aborts EOverAllowance when it is more than is left, and emits AllowanceUsed { treasury_id, cap_id, amount, remaining }.
Abort codes#
| Code | Name | Code | Name |
|---|---|---|---|
| 1 | EWrongTreasury | 12 | EBadMandate |
| 2 | ECapRevoked | 13 | EOverTotalCap |
| 3 | ECapExpired | 14 | EFillOutOfBounds |
| 4 | EOverPerTx | 15 | ECollateralNotAllowed |
| 5 | EOverPerDay | 16 | EOverLtv |
| 6 | ERecipientNotAllowed | 17 | ERateBelowFloor |
| 7 | EBadLimits | 18 | EDurationNotAllowed |
| 8 | EZeroAmount | 19 | ETakerNotAllowed |
| 9 | EInsufficientFunds | 20 | EOfferExpiry |
| 10 | EBadRecipients | 21 | EBadConsent |
| 11 | ENoMandate | 22 | ENoConsent |
| 23 | ENoBorrowMandate | ||
| 24 | ERateAboveCeiling | ||
| 25 | ETermTooShort | ||
| 26 | EPastRepayBy | ||
| 27 | EPoolNotAllowed | ||
| 28 | ECashNotAllowed | ||
| 29 | EOverAllowance |
assert_borrow aborts 23 when the cap has no borrow rules, 24 when the opening rate is above max_rate_bps, 26 when it is already at or past latest_maturity_ms, and for an offer loan 25 when the term is under min_term_ms and 26 when the loan would mature after latest_maturity_ms. The rate is checked only at opening: a pool loan floats with its pool afterwards. assert_pool aborts 27 unless the pool is the pinned one, and assert_cash aborts 28 unless the cash treasury is the pinned one (both abort 23 if there are no rules). use_allowance aborts 29 when a loan asks for more than the allowance has left.