market
essey_sui::market — risk terms for collateral C against loan coin L, one market per pair. Values are in L's smallest unit: value = amount × price × 10^dec(L) / (10^dec(C) × 10^expo), rounded down. The oracle and price object are fixed at creation; the loan-to-value limit, liquidation line and bonus change only through propose and, two days later, commit.
Publishing the package shares a Registry and sends a MarketAdminCap to the publisher.
Admin functions — need &MarketAdminCap#
The cap can only create markets and propose, or cancel, parameter changes that anyone commits after two days; it cannot move funds from treasuries, pools or loans, touch sealed bids, change the oracle or change the code. Lowering the liquidation line does affect open loans: in two steps it can make every loan liquidatable, with two days' public notice each time, the lender made whole and the borrower losing at most the bonus. See What the MarketAdminCap can and cannot do.
public fun create<C, L, W>(_admin: &MarketAdminCap, registry: &mut Registry, c_currency: &Currency<C>, l_currency: &Currency<L>, feed_id: ID, ltv_bps: u64, liq_bps: u64, bonus_bps: u64, ctx: &mut TxContext)Shares a market for the pair, bound to oracle witness W and price object feed_id. EMarketExists if the pair has one; EBadParams unless ltv_bps > 0, both coins have at most 36 decimals, and the parameters are valid (below).
public fun propose<C, L>(_admin: &MarketAdminCap, m: &mut Market<C, L>, ltv_bps: u64, liq_bps: u64, bonus_bps: u64, clock: &Clock)
public fun cancel<C, L>(_admin: &MarketAdminCap, m: &mut Market<C, L>)propose stages new parameters, effective two days later. cancel drops a staged proposal (ENoPending).
Valid parameters: liq_bps ≤ 9,000, bonus_bps ≤ 1,500, and, unless ltv_bps is 0, ltv_bps + 2,000 ≤ liq_bps.
Anyone#
public fun commit<C, L>(m: &mut Market<C, L>, clock: &Clock)Applies a staged proposal once its two days have passed (ENoPending, ETimelock). EDeprecationOrder blocks a change to a liquidation line under 2,000 unless the market was already closed to new loans (ltv_bps 0).
Price math#
| Function | What it does |
|---|---|
assert_reading(m, r) | The Reading came from the market's oracle and price object, else EWrongFeed. |
borrow_value(m, r, amount): u64 | Collateral value at the lower of spot and EMA. |
ltv_bps(m, r, amount, debt): u64 | Debt over borrow value, rounded up; saturates at 100,000,000 bps, including for worthless collateral. |
is_underwater(m, r, amount, debt): bool | Over the liquidation line at spot and at EMA. |
insolvency_floor(m, r, amount, debt): (bool, u64) | Worth less than the debt at both prices, and the higher of the two values. |
collateral_for(m, r, value): u64 | Units of collateral worth value at spot, rounded down. |
Views#
params(m) → (ltv_bps, liq_bps, bonus_bps), ltv_limit(m), bonus_bps(m), pending(m) → (has_pending, ltv, liq, bonus, effective_at_ms), oracle_of(m) → (oracle, feed_id), decimals(m) → (c, l), timelock_ms().
Events and abort codes#
MarketCreated, ParamsProposed, ParamsCommitted, ProposalCancelled. Aborts: 1 EMarketExists, 2 EBadParams, 3 ENoPending, 4 ETimelock, 5 EWrongFeed, 6 EDeprecationOrder.