Skip to content

DexShareResolver ​

Git Source

Inherits:Error

Title: DexShareResolver

Reusable abstract for resolving DEX LP shares to USD-denominated prices. Intended for vault oracles, auth contracts, limit handlers, or any consumer that needs to value DEX collateral/debt shares in USD. Uses the USD Oracle as common denominator: each token's reserves are valued in USD independently, eliminating the need for conversion price oracles, colDebt bridge oracles, or result scaling.

Peg buffer (pegBufferPpm_): optional, same semantics as V1 DexReservesFromLiquidityPeg. Pass 0 for unbuffered reserves (true economic value) — use this for limit handlers and any consumer that must not discount reserves. Vault oracles (T2–T4) pass a protocol-fixed buffer from VaultOracleBase (0.5% operate / 0.1% liquidate). Any other pegBufferPpm_ must be < 1e6. eMode_: forwarded to IUSDOracle.getPriceDetailedView when valuing reserves; vault oracles supply immutable E_MODE from VaultOracleBase.

Constants ​

X64 ​

solidity
uint256 private constant X64 = 0xffffffffffffffff

X128 ​

solidity
uint256 private constant X128 = 0xffffffffffffffffffffffffffffffff

DEX_SHARE_DECIMALS ​

Fluid DEX LP shares use 18 decimals; returned as uint256 to avoid widening at every call site.

solidity
uint256 internal constant DEX_SHARE_DECIMALS = 18

PEG_BUFFER_SCALE ​

Denominator for peg buffer (parts per million). pegBufferPpm_ of 10_000 = 1%.

solidity
uint256 internal constant PEG_BUFFER_SCALE = 1e6

SHARE_PRICE_USD_SCALE ​

Converts pool total USD into per-share USD at oracle precision (1e27). Reserves are normalized to 1e12; oracle prices are 1e27; totalValueUsd_ is ~1e39. DEX share amounts are 1e18. We want USD per 1e18 shares also at 1e27: totalValueUsd_ * SHARE_PRICE_USD_SCALE / totalShares_ → 1e39 * 1e6 / 1e18 = 1e27. Same numeric value as PEG_BUFFER_SCALE but a different semantic (not ppm).

solidity
uint256 private constant SHARE_PRICE_USD_SCALE = 1e6

LIQUIDITY ​

solidity
IFluidStorageReadable internal constant LIQUIDITY =
    IFluidStorageReadable(0x52Aa899454998Be5b000Ad077a46Bbe360F4e497)

Functions ​

_resolveColShare ​

Resolves the USD price per one DEX collateral share (1e18).

Reads raw collateral reserves from Liquidity, values them in USD via the oracle, divides by total supply shares. Returns price in 1e27 precision; second return is DEX_SHARE_DECIMALS (18) as uint256.

solidity
function _resolveColShare(
    address usdOracle_,
    uint256 eMode_,
    bool isOperate_,
    DexParams memory d_,
    uint256 pegBufferPpm_,
    bool isRaw_
) internal view returns (uint256 priceUsd_, uint256 decimals_);

Parameters

NameTypeDescription
usdOracle_address
eMode_uint256
isOperate_bool
d_DexParams
pegBufferPpm_uint256Peg buffer in parts per million; 0 = none. Collateral reserves are scaled by (1e6 - pegBufferPpm_) / 1e6 before valuation.
isRaw_bool

_resolveDebtShare ​

Resolves the USD price per one DEX debt share (1e18).

Same approach as collateral shares but reads borrow reserves and total borrow shares. Second return is DEX_SHARE_DECIMALS (18) as uint256.

solidity
function _resolveDebtShare(
    address usdOracle_,
    uint256 eMode_,
    bool isOperate_,
    DexParams memory d_,
    uint256 pegBufferPpm_,
    bool isRaw_
) internal view returns (uint256 priceUsd_, uint256 decimals_);

Parameters

NameTypeDescription
usdOracle_address
eMode_uint256
isOperate_bool
d_DexParams
pegBufferPpm_uint256Peg buffer in parts per million; 0 = none. Debt reserves are scaled by (1e6 + pegBufferPpm_) / 1e6 before valuation.
isRaw_bool

_applyPegBufferToReserves ​

V1 DexReservesFromLiquidityPeg: collateral reduces, debt increases by pegBufferPpm_ / 1e6.

solidity
function _applyPegBufferToReserves(
    uint256 token0Reserves_,
    uint256 token1Reserves_,
    uint256 pegBufferPpm_,
    bool isCollateralSide_
) internal pure returns (uint256 out0_, uint256 out1_);

_computeTotalValueUsd ​

Computes total USD value of token0 + token1 reserves (each already at 1e12 precision). Returns result in ~1e39 range (1e12 * 1e27).

solidity
function _computeTotalValueUsd(
    address usdOracle_,
    uint256 eMode_,
    bool isOperate_,
    bool isCollateral_,
    address token0_,
    address token1_,
    uint256 token0Reserves_,
    uint256 token1Reserves_,
    bool isRaw_
) private view returns (uint256 totalValueUsd_);

_usdPriceDetailedView ​

solidity
function _usdPriceDetailedView(
    address usdOracle_,
    address token_,
    uint256 eMode_,
    bool isOperate_,
    bool isCollateral_,
    bool isRaw_
) private view returns (uint256 price_, uint8 decimals_, uint8 tokenType_);

_getLiquidityCollateral ​

Reads a token's collateral (supply) amount from Liquidity, adjusted for exchange price. Normalized to 1e12 using the token's precision scalers.

solidity
function _getLiquidityCollateral(
    bytes32 supplySlot_,
    bytes32 exchangePriceSlot_,
    uint256 numPrecision_,
    uint256 denPrecision_
) private view returns (uint256 tokenSupply_);

_getLiquidityDebt ​

Reads a token's debt (borrow) amount from Liquidity, adjusted for exchange price. Normalized to 1e12 using the token's precision scalers.

solidity
function _getLiquidityDebt(
    bytes32 borrowSlot_,
    bytes32 exchangePriceSlot_,
    uint256 numPrecision_,
    uint256 denPrecision_
) private view returns (uint256 debtAmount_);

Structs ​

DexParams ​

Parameters for DEX share resolution. Passed by the vault oracle which caches them as immutables.

solidity
struct DexParams {
    address dexPool;
    address token0;
    address token1;
    bytes32 supplyToken0Slot;
    bytes32 supplyToken1Slot;
    bytes32 borrowToken0Slot;
    bytes32 borrowToken1Slot;
    bytes32 exchangePriceToken0Slot;
    bytes32 exchangePriceToken1Slot;
    uint256 token0NumeratorPrecision;
    uint256 token0DenominatorPrecision;
    uint256 token1NumeratorPrecision;
    uint256 token1DenominatorPrecision;
}