loan
essey_sui::loan — loans, deeds and lender claims. A loan is a shared object holding its own collateral; whoever holds its LoanDeed repays, refinances and takes any surplus. See Loans.
Open#
public fun fill<C, L>(offer: &Offer<L, C>, t: &mut Treasury<L>, m: &Market<C, L>, r: &Reading, collateral: Coin<C>, amount: u64, acc: &Access, clock: &Clock, ctx: &mut TxContext): (LoanDeed, Coin<L>)Needs: the lending Access object, which must admit the signer for amount (access::assert_open_now); the signer is the borrower. Checks a non-zero amount and collateral, the market's loan-to-value limit at the lower of spot and EMA (EOverMarketLtv), the offer, then the lender's mandate. Sets maturity to now plus the offer's term, mints the LenderClaim to the lender's treasury, shares the loan.
public fun borrow_from_pool<C, L>(p: &mut Pool<C, L>, m: &Market<C, L>, r: &Reading, collateral: Coin<C>, amount: u64, acc: &Access, clock: &Clock, ctx: &mut TxContext): (LoanDeed, Coin<L>)Needs: the Access object, as for fill. Same market check; the pool must hold the cash (pool::EInsufficientCash). The loan opens at the pool's current index and floats.
Repay#
public fun repay<C, L>(deed: LoanDeed, loan: &mut Loan<C, L>, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<C>, Coin<L>, Payout<L>)Needs: the LoanDeed (by value; it is spent). Offer loans only (ENotOfferLoan). Never price-gated. Takes what is owed (EUnderpaid if short), returns the collateral, the change, and a Payout that must reach pay_treasury in the same transaction.
public fun repay_pool<C, L>(deed: LoanDeed, loan: &mut Loan<C, L>, p: &mut Pool<C, L>, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<C>, Coin<L>)Needs: the LoanDeed. Pool loans only, against their own pool (ENotPoolLoan, EWrongLender). Settles into the pool.
public fun pay_treasury<L>(payout: Payout<L>, t: &mut Treasury<L>)Delivers an offer loan's payment to its own lender's treasury (EWrongLender otherwise) and returns the principal to the lender's mandate and offer.
public fun add_collateral<C, L>(loan: &mut Loan<C, L>, c: Coin<C>)Needs: nothing; anyone may top up an open loan. ENotOpen, EZeroAmount.
Close without the borrower#
public fun liquidate<C, L>(loan: &mut Loan<C, L>, m: &Market<C, L>, r: &Reading, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<C>, Coin<L>, Payout<L>)
public fun liquidate_pool<C, L>(loan: &mut Loan<C, L>, p: &mut Pool<C, L>, m: &Market<C, L>, r: &Reading, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<C>, Coin<L>)Needs: nothing. The loan must be open and over the line at spot and EMA (EHealthy otherwise). Pays the whole debt; seizes collateral worth debt × (1 + bonus) at spot, capped at what the loan holds; the rest stays for claim.
public fun write_off<C, L>(loan: &mut Loan<C, L>, m: &Market<C, L>, r: &Reading, recovered: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<C>, Payout<L>)
public fun write_off_pool<C, L>(loan: &mut Loan<C, L>, p: &mut Pool<C, L>, m: &Market<C, L>, r: &Reading, recovered: Coin<L>, clock: &Clock, ctx: &mut TxContext): Coin<C>Needs: nothing. The collateral must be worth less than the debt at both prices (ESolvent); recovered at least the higher value (EBelowFloor) and at most the debt (EOverOwed). Takes all the collateral.
public fun claim_default<C, L>(claim: LenderClaim, loan: &mut Loan<C, L>, t: &mut Treasury<L>, m: &Market<C, L>, r: &Reading, clock: &Clock, ctx: &mut TxContext): Coin<C>Needs: the loan's LenderClaim (by value; EWrongClaim). Open loan (ENotOpen), at least maturity + 300 s (ENotDue), the loan's own lender treasury (EWrongLender). Seizes collateral worth owed × (1 + bonus) at a fresh price, capped; the rest stays for claim.
public fun claim<C, L>(deed: LoanDeed, loan: &mut Loan<C, L>, ctx: &mut TxContext): Coin<C>Needs: the LoanDeed. After a liquidation, write-off or default (ENotClosed); takes all remaining collateral.
Refinance#
public fun begin_refi<C, L>(deed: LoanDeed, loan: &mut Loan<C, L>, ctx: &mut TxContext): (RefiTicket, Coin<C>)
public fun finish_refi<C, L>(ticket: RefiTicket, loan: &mut Loan<C, L>, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): (Coin<L>, Payout<L>)
public fun finish_refi_pool<C, L>(ticket: RefiTicket, loan: &mut Loan<C, L>, p: &mut Pool<C, L>, payment: Coin<L>, clock: &Clock, ctx: &mut TxContext): Coin<L>Needs: the LoanDeed. begin_refi spends the deed and lends the collateral to the transaction; the RefiTicket has no abilities, so the transaction aborts unless finish_refi or finish_refi_pool repays this loan in full (ETicketMismatch for another loan).
Views#
| Function | Returns |
|---|---|
owed<C, L>(loan, clock): u64 | An offer loan's debt now. |
owed_in_pool<C, L>(loan, p, clock): u64 | A pool loan's debt now. |
info<C, L>(loan) | (market, collateral, principal, rate_bps, start_s, lender_kind, lender_id, cap_id, claim_id, state, maturity_ms) |
deed_loan(deed): ID | |
claim_info(c) | (loan, treasury, cap, principal, rate_bps) |
ticket_loan(t): ID | |
grace_ms(): u64 | 300,000 |
States: 0 OPEN, 1 REPAID, 2 LIQUIDATED, 3 WRITTEN_OFF, 4 REFINANCING, 5 CLAIMED, 6 DEFAULTED. Lender kinds: 0 treasury (offer loan), 1 pool.
Events#
LoanOpened, LoanRepaid, CollateralAdded, LoanLiquidated, LoanWrittenOff, LoanDefaulted, SurplusClaimed, RefiBegun.
Abort codes#
1 EWrongDeed, 2 ENotOpen, 3 EOverMarketLtv, 4 EUnderpaid, 5 EHealthy, 6 ESolvent, 7 EBelowFloor, 8 EOverOwed, 9 EWrongLender, 10 EZeroAmount, 12 ETicketMismatch, 13 ENotOfferLoan, 17 ENotClosed, 19 ENotPoolLoan, 20 ENotDue, 21 EWrongClaim.