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()).
Comparing the two oracle families
| Property | YBLendingOracle / YBPriceProxy | YBLendingOracleLL / YBLendingOracleLLUSD |
|---|---|---|
| Statefulness | Stateless singleton + per-market YBPriceProxy clones | Per-market EIP-1167 clone holding shared EMA state + YBLendingOracleLLUSD forwarder |
| Smoothing | Relies on Curve price_oracle() EMA; AMM equity and virtual_price read live | Flash-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 pricing | Built-in (staked_price_in_asset, staked_price_in_usd) | Unstaked yb-LP only |
| Deployment | Permissionless YBLendingOracle.create_oracles(market_id) | Permissionless YBLendingOracleLLFactory.create_oracles(market_id) |
| Output scaling | 1e18 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):
- Live curve read (
_live_curve): Reads(A, virtual_price, D)from the Curve Twocrypto pool. If anAramp has not yet been observed by a trade (last_timestamp < block.timestampandfuture_A_gamma_time > last_timestamp), it re-solvesDviapool.MATH().newton_D(...)at the currentAand recomputesvirtual_price. - LP price at
price_scalevs.price_oracle:lp_price_ps = 2 * virtual_price * isqrt(price_scale * 1e18) // 1e18lp_price_oracle = portfolio_value(A, price_oracle / price_scale) * D // pool_supply
- Arbitrage-bounded leveraged equity:
- Let and .
- Arbitrage moves
AMMtoward until it reachesSQRT_P_MAX_DEBT(926776695296636881) orSQRT_P_MIN_DEBT(1457106781186547524), yielding endpoint . - At fixed leverage :
- When arbitrage is unconstrained (), this simplifies to .
- Fresh
LTfee and supply adjustment: ReplicatesLT._calculate_valuesin-contract to obtain(lv_total, lv_admin, lt_supply, staked_tokens)and returns:price_in_assetdividesprice_in_usdbyprice_oracle * agg.price() // 1e18. - Staked gauge scaling:
staked_price_in_assetandstaked_price_in_usdmultiply theyb-LPprice 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:
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:
fundamental(EMA-smoothed): The asset-denominatedyb-LPconversion rate atprice == price_scale, computed fromx0 / (2L - 1), freshLTfee accounting, andlt_supply. It absorbs every manipulable level input (AMMwash-trade fees and poolvirtual_price).po_shift(applied live): The leverage shift fromprice_scaletoprice_oracle, which is a pure function of(A, price_oracle / price_scale)(plus the safe-debt arbitrage endpoint outside the safe band). Becauseprice_oracleis the Curve pool's own EMA,po_shifttracks market moves and hits0at the insolvency boundary without lag.
How the flash-proof EMA advances
On every price_w() or price_usd_w() checkpoint with elapsed time :
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 .
Shared EMA across asset and USD denominators
YBLendingOracleLLFactory.create_oracles(market_id) deploys two contracts per market:
asset_oracle: aYBLendingOracleLLEIP-1167 clone that owns the EMA state and exposesprice()/price_w()in the underlying asset andprice_usd()/price_usd_w()in USD.usd_oracle: aYBLendingOracleLLUSDblueprint instance whoseprice()/price_w()forward directly toasset_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 * 1e18means1.0 yb-LPis worth\$100,000. - Asset price (
1e18):1.05 * 1e18means1.0 yb-LPis worth1.05whole units ofasset_token(for example,1.05 WBTCor1.05 WETH), regardless of whetherasset_tokenhas 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
YBLendingOracleandYBLendingOracleLLprobeAMM.check_nonreentrant()via staticcall (reverting with"AMM reentrancy"if the lock is held) and read@nonreentrantCurve pool views (price_oracle(),price_scale()). Do not call these oracles from inside a reentrant callback ofAMMor the Curve pool. - Insolvency returns
0, not a revert: If leveraged equity is wiped out (factor <= 1e18orcoll_value <= debt), both oracles return0rather than underflowing so liquidators are never bricked. Ensure your lending market handles a0price cleanly. - Seed and checkpoint
YBLendingOracleLL: Before the firstprice_w()/price_usd_w()call,fundamental_ema == 0andprice()falls back to the unsmoothedfundamental. Seed the clone by callingprice_w()once after deployment, and ensure active markets callprice_w()on borrow/repay/liquidate sodtstays small. - Staked shares vs. unstaked
yb-LP:YBLendingOracleLLprices unstakedyb-LPonly. If your protocol acceptsLiquidityGaugeshares as collateral, either useYBLendingOracle.staked_price_in_usd/staked_price_in_assetor multiply theYBLendingOracleLLprice byLiquidityGauge.previewRedeem(1e18) / 1e18.