Skip to main content

Lending oracles

YieldBasis provides two lending-oracle families for pricing yb-LP (and staked gauge shares) as collateral in external lending protocols:

  • YBLendingOracle + YBPriceProxy: Stateless, live unwind valuation suitable for protocols that want instantaneous liquidation pricing or that apply their own smoothing.
  • YBLendingOracleLL + YBLendingOracleLLFactory + YBLendingOracleLLUSD: Stateful, flash-proof EMA-smoothed valuation built specifically for Curve LlamaLend (price() / price_w()).
YieldBasis lending oracle valuation paths for YBLendingOracle and YBLendingOracleLLYieldBasis lending oracle valuation paths for YBLendingOracle and YBLendingOracleLL

Comparing the two oracle families​

PropertyYBLendingOracle / YBPriceProxyYBLendingOracleLL / YBLendingOracleLLUSD
StatefulnessStateless singleton + per-market YBPriceProxy clonesPer-market EIP-1167 clone holding shared EMA state + YBLendingOracleLLUSD forwarder
SmoothingRelies on Curve price_oracle() EMA; AMM equity and virtual_price read liveFlash-proof EMA on the fundamental LT-to-asset rate; live shift from (A, price_oracle / price_scale)
LlamaLend price_w()View-only (price() on YBPriceProxy)Full price() / price_w() (asset) and price_usd() / price_usd_w() (USD)
Staked gauge pricingBuilt-in (staked_price_in_asset, staked_price_in_usd)Unstaked yb-LP only
DeploymentPermissionless YBLendingOracle.create_oracles(market_id)Permissionless YBLendingOracleLLFactory.create_oracles(market_id)
Output scaling1e18 per yb-LP (or per gauge share)1e18 per yb-LP

1. Live unwind pricing (YBLendingOracle and YBPriceProxy)​

YBLendingOracle is a stateless singleton bound to Factory and a YBPriceProxy implementation. Anyone can call create_oracles(market_id) (idempotent) to spawn a pair of minimal-proxy price() forwarders: usd_oracle(market_id) and asset_oracle(market_id).

Interface​

interface IYBLendingOracle {
function create_oracles(uint256 market_id) external returns (address usd_oracle, address asset_oracle);
function usd_oracle(uint256 market_id) external view returns (address);
function asset_oracle(uint256 market_id) external view returns (address);

function price_in_asset(address lt, bool use_balances) external view returns (uint256);
function price_in_usd(address lt, bool use_balances) external view returns (uint256);
function staked_price_in_asset(address lt, bool use_balances) external view returns (uint256);
function staked_price_in_usd(address lt, bool use_balances) external view returns (uint256);
}

interface IYBPriceProxy {
function oracle() external view returns (address);
function lt() external view returns (address);
function in_usd() external view returns (bool);
function price() external view returns (uint256);
}

Valuation math​

On the normal tradable path (x0 solvable, not amm.is_killed(), and use_balances == false):

  1. Live curve read (_live_curve): Reads (A, virtual_price, D) from the Curve Twocrypto pool. If an A ramp has not yet been observed by a trade (last_timestamp < block.timestamp and future_A_gamma_time > last_timestamp), it re-solves D via pool.MATH().newton_D(...) at the current A and recomputes virtual_price.
  2. LP price at price_scale vs. price_oracle:
    • lp_price_ps = 2 * virtual_price * isqrt(price_scale * 1e18) // 1e18
    • lp_price_oracle = portfolio_value(A, price_oracle / price_scale) * D // pool_supply
  3. Arbitrage-bounded leveraged equity:
    • Let r=lp_price_oracle/lp_price_psr = \text{lp\_price\_oracle} / \text{lp\_price\_ps} and r=isqrt(1036⋅lp_price_oracle/lp_price_ps)\sqrt{r} = \text{isqrt}(10^{36} \cdot \text{lp\_price\_oracle} / \text{lp\_price\_ps}).
    • Arbitrage moves AMM toward rr until it reaches SQRT_P_MAX_DEBT (926776695296636881 ≈0.9268\approx 0.9268) or SQRT_P_MIN_DEBT (1457106781186547524 ≈1.4571\approx 1.4571), yielding endpoint e\sqrt{e}.
    • At fixed leverage L=2L = 2: factor=L⋅(re+e)2L−1,yb_oracle_total=max⁡ ⁣(0,  x0⋅(factor−1))\text{factor} = \frac{L \cdot \left(\frac{r}{\sqrt{e}} + \sqrt{e}\right)}{2L - 1}, \qquad \text{yb\_oracle\_total} = \max\!\left(0,\; x_0 \cdot (\text{factor} - 1)\right)
    • When arbitrage is unconstrained (e=r\sqrt{e} = \sqrt{r}), this simplifies to x0⋅(43r−1)x_0 \cdot \left(\frac{4}{3}\sqrt{r} - 1\right).
  4. Fresh LT fee and supply adjustment: Replicates LT._calculate_values in-contract to obtain (lv_total, lv_admin, lt_supply, staked_tokens) and returns: price_in_usd=yb_oracle_total⋅lv_totalmax⁡(lv_admin,0)+lv_total⋅1018lt_supply\text{price\_in\_usd} = \frac{\text{yb\_oracle\_total} \cdot \text{lv\_total}}{\max(\text{lv\_admin}, 0) + \text{lv\_total}} \cdot \frac{10^{18}}{\text{lt\_supply}} price_in_asset divides price_in_usd by price_oracle * agg.price() // 1e18.
  5. Staked gauge scaling: staked_price_in_asset and staked_price_in_usd multiply the yb-LP price by (staker_balance + 1) * 1e18 // (gauge.totalSupply() + 1).

Fallback path (killed, untradable, or use_balances = true)​

When AMM.is_killed() is true, x0 is unsolvable (debt / collateral_value > 9/16), or the caller sets use_balances = true, the oracle switches to raw balance valuation:

equity=max⁡ ⁣(0,  collateral⋅lp_price_oracle⋅agg_price−debt)\text{equity} = \max\!\left(0,\; \text{collateral} \cdot \text{lp\_price\_oracle} \cdot \text{agg\_price} - \text{debt}\right)

If AMM is not killed, equity is scaled by lv.total / (max(lv.admin, 0) + lv.total) using cached lt.liquidity(). If AMM is killed, the admin fee buffer is not withheld—matching LT.emergency_withdraw—and the result is divided by lt.totalSupply().

2. EMA-smoothed pricing for LlamaLend (YBLendingOracleLL)​

YBLendingOracleLL prevents same-block wash-trade manipulation of AMM equity and virtual_price by decomposing the yb-LP price into two factors:

price_asset=fundamental_ema⋅po_shift,price_usd=price_asset⋅(price_oracle⋅agg_price)\text{price\_asset} = \text{fundamental\_ema} \cdot \text{po\_shift}, \qquad \text{price\_usd} = \text{price\_asset} \cdot (\text{price\_oracle} \cdot \text{agg\_price})
  • fundamental (EMA-smoothed): The asset-denominated yb-LP conversion rate at price == price_scale, computed from x0 / (2L - 1), fresh LT fee accounting, and lt_supply. It absorbs every manipulable level input (AMM wash-trade fees and pool virtual_price).
  • po_shift (applied live): The leverage shift from price_scale to price_oracle, which is a pure function of (A, price_oracle / price_scale) (plus the safe-debt arbitrage endpoint outside the safe band). Because price_oracle is the Curve pool's own EMA, po_shift tracks market moves and hits 0 at the insolvency boundary without lag.

How the flash-proof EMA advances​

On every price_w() or price_usd_w() checkpoint with elapsed time Δt=block.timestamp−ema_ts>0\Delta t = \text{block.timestamp} - \text{ema\_ts} > 0:

α=exp⁡ ⁣(−Δtema_time),emanew=fundamental_last⋅(1−α)+emaold⋅α\alpha = \exp\!\left(-\frac{\Delta t}{\text{ema\_time}}\right), \qquad \text{ema}_{\text{new}} = \text{fundamental\_last} \cdot (1 - \alpha) + \text{ema}_{\text{old}} \cdot \alpha

The EMA blends fundamental_last (recorded on the previous checkpoint in an earlier block) and never the current-block fundamental. It then stores the current block's fundamental into fundamental_last for the next checkpoint. Its half-life is ema_time⋅ln⁡(2)\text{ema\_time} \cdot \ln(2).

Shared EMA across asset and USD denominators​

YBLendingOracleLLFactory.create_oracles(market_id) deploys two contracts per market:

  1. asset_oracle: a YBLendingOracleLL EIP-1167 clone that owns the EMA state and exposes price() / price_w() in the underlying asset and price_usd() / price_usd_w() in USD.
  2. usd_oracle: a YBLendingOracleLLUSD blueprint instance whose price() / price_w() forward directly to asset_oracle.price_usd() / asset_oracle.price_usd_w().

Checkpointing either contract advances the same underlying EMA. The DAO can retune ema_time per market via YBLendingOracleLLFactory.set_ema_time(market_id, ema_time) (bounded by 0 < ema_time <= 10**9).

interface IYBLendingOracleLLFactory {
function create_oracles(uint256 market_id) external returns (address usd_oracle, address asset_oracle);
function usd_oracle(uint256 market_id) external view returns (address);
function asset_oracle(uint256 market_id) external view returns (address);
function default_ema_time() external view returns (uint256);
}

interface ILlamaLendOracle {
function price() external view returns (uint256);
function price_w() external returns (uint256);
}

3. Decimals and scaling​

Both oracle families always return prices scaled to 1e18, representing the value of 1e18 base units (1.0 yb-LP, since LT.decimals() == 18):

  • USD price (1e18): 100_000 * 1e18 means 1.0 yb-LP is worth \$100,000.
  • Asset price (1e18): 1.05 * 1e18 means 1.0 yb-LP is worth 1.05 whole units of asset_token (for example, 1.05 WBTC or 1.05 WETH), regardless of whether asset_token has 8 or 18 decimals.

If your lending protocol prices collateral against a debt token with debt_decimals != 18, rescale the oracle output accordingly when converting collateral value into debt token units.

4. Integration cautions​

  • Read-only reentrancy: Both YBLendingOracle and YBLendingOracleLL probe AMM.check_nonreentrant() via staticcall (reverting with "AMM reentrancy" if the lock is held) and read @nonreentrant Curve pool views (price_oracle(), price_scale()). Do not call these oracles from inside a reentrant callback of AMM or the Curve pool.
  • Insolvency returns 0, not a revert: If leveraged equity is wiped out (factor <= 1e18 or coll_value <= debt), both oracles return 0 rather than underflowing so liquidators are never bricked. Ensure your lending market handles a 0 price cleanly.
  • Seed and checkpoint YBLendingOracleLL: Before the first price_w() / price_usd_w() call, fundamental_ema == 0 and price() falls back to the unsmoothed fundamental. Seed the clone by calling price_w() once after deployment, and ensure active markets call price_w() on borrow/repay/liquidate so dt stays small.
  • Staked shares vs. unstaked yb-LP: YBLendingOracleLL prices unstaked yb-LP only. If your protocol accepts LiquidityGauge shares as collateral, either use YBLendingOracle.staked_price_in_usd / staked_price_in_asset or multiply the YBLendingOracleLL price by LiquidityGauge.previewRedeem(1e18) / 1e18.