price() fixes; sepolia redeploy

This commit is contained in:
tim
2025-11-25 23:11:38 -04:00
parent 2efcd4c0d3
commit cce229b99c
29 changed files with 140 additions and 57 deletions

View File

@@ -751,18 +751,18 @@ library LMSRStabilized {
return e_i_acc.div(Z);
}
/// @notice Marginal price of `base` in terms of `quote` (p_quote / p_base) as Q64.64
/// @dev Returns exp((q_quote - q_base) / b). Indices must be valid and b > 0.
/// @notice Infinitesimal out-per-in marginal price for swap base->quote (p_base / p_quote) as Q64.64
/// @dev Returns exp((q_base - q_quote) / b). Indices must be valid and b > 0.
function price(State storage s, uint256 baseTokenIndex, uint256 quoteTokenIndex) internal view returns (int128) {
return price(s.kappa, s.qInternal, baseTokenIndex, quoteTokenIndex);
}
/// @notice Pure version: Marginal price of `base` in terms of `quote` (p_quote / p_base) as Q64.64
/// @dev Returns exp((q_quote - q_base) / b). Indices must be valid and b > 0.
/// @notice Pure version: Infinitesimal out-per-in marginal price for swap base->quote (p_base / p_quote) as Q64.64
/// @dev Returns exp((q_base - q_quote) / b). Indices must be valid and b > 0.
/// @param kappa Liquidity parameter κ (64.64 fixed point)
/// @param qInternal Cached internal balances in 64.64 fixed-point format
/// @param baseTokenIndex Index of base token
/// @param quoteTokenIndex Index of quote token
/// @param baseTokenIndex Index of base (input) token
/// @param quoteTokenIndex Index of quote (output) token
/// @return Price in 64.64 fixed-point format
function price(int128 kappa, int128[] memory qInternal, uint256 baseTokenIndex, uint256 quoteTokenIndex) internal pure returns (int128) {
int128 sizeMetric = _computeSizeMetric(qInternal);
@@ -772,9 +772,8 @@ library LMSRStabilized {
// Use reciprocal of b to avoid repeated divisions
int128 invB = ABDKMath64x64.div(ONE, b);
// Marginal price p_quote / p_base = exp((q_quote - q_base) / b)
return _exp(qInternal[quoteTokenIndex].sub(qInternal[baseTokenIndex]).mul(invB));
// Marginal price p_base / p_quote = exp((q_base - q_quote) / b)
return _exp(qInternal[baseTokenIndex].sub(qInternal[quoteTokenIndex]).mul(invB));
}
/// @notice Price of one unit of the LP size-metric (S = sum q_i) denominated in `quote` asset (Q64.64)

View File

@@ -1,7 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.30;
import "forge-std/console2.sol";
import {ABDKMath64x64} from "../lib/abdk-libraries-solidity/ABDKMath64x64.sol";
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {IPartyPool} from "./IPartyPool.sol";
@@ -36,30 +35,32 @@ contract PartyInfo is PartyPoolHelpers, IPartyInfo {
// Current marginal prices
//
/// @notice Marginal price of `base` denominated in `quote` as Q64.64.
/// @dev Returns the LMSR marginal price p_quote / p_base in ABDK 64.64 fixed-point format.
/// Useful for off-chain quoting; raw 64.64 value is returned (no scaling to token units).
/// @param baseTokenIndex index of the base asset (e.g., ETH)
/// @param quoteTokenIndex index of the quote asset (e.g., USD)
/// @return price Q64.64 value equal to quote per base (p_quote / p_base)
/// @notice Infinitesimal out-per-in marginal price for swap base->quote as Q64.64 (j per i).
/// @dev Returns p_base / p_quote in ABDK 64.64 format, scaled to external units by (denom_quote / denom_base).
/// This aligns with the swap kernel so that, fee-free, avg(out/in) ≤ price(base, quote) for exact-in trades.
/// @param baseTokenIndex index of the input (base) asset
/// @param quoteTokenIndex index of the output (quote) asset
/// @return price Q64.64 value equal to out-per-in (j per i), scaled to token units
function price(IPartyPool pool, uint256 baseTokenIndex, uint256 quoteTokenIndex) external view returns (int128) {
LMSRStabilized.State memory lmsr = pool.LMSR();
uint256 nAssets = lmsr.qInternal.length;
require(nAssets > 0, "price: uninit");
require(baseTokenIndex < nAssets && quoteTokenIndex < nAssets, "price: idx");
// Directly get p_base / p_quote (i.e., p_i / p_j) internally
int128 internalPrice = LMSRStabilized.price(pool.kappa(), lmsr.qInternal, baseTokenIndex, quoteTokenIndex);
// Convert to external units
// Convert to external units: multiply by denom_quote / denom_base
uint256 bd = pool.denominators()[baseTokenIndex];
uint256 qd = pool.denominators()[quoteTokenIndex];
return internalPrice.mul(ABDKMath64x64.divu(qd, bd));
}
/// @notice Price of one LP token denominated in `quote` as Q64.64.
/// @dev Computes LMSR poolPrice (quote per unit internal qTotal) and scales it to LP units:
/// returns price_per_LP = poolPrice_quote * (totalSupply() / qTotal) in ABDK 64.64 format.
/// The returned value is raw Q64.64 and represents quote units per one LP token unit.
/// @notice Price of one LP token denominated in `quote` as Q64.64 (external quote units per LP).
/// @dev Let P_S^quote be the LMSR pool price "quote per unit of internal S = sum q_i" (Q64.64, internal quote units).
/// We convert to external quote per LP by:
/// price_per_LP = P_S^quote * (denom_quote) * (S_internal / totalSupply)
/// where denom_quote converts internal quote to external units, and S_internal/totalSupply maps per-S to per-LP.
/// @param quoteTokenIndex index of the quote asset in which to denominate the LP price
/// @return price Q64.64 value equal to quote per LP token unit
/// @return price Q64.64 value equal to external quote units per one LP token unit
function poolPrice(IPartyPool pool, uint256 quoteTokenIndex) external view returns (int128) {
LMSRStabilized.State memory lmsr = pool.LMSR();
uint256 nAssets = lmsr.qInternal.length;