marginal price views
This commit is contained in:
@@ -927,6 +927,42 @@ contract PartyPool is IPartyPool, ERC20, ReentrancyGuard {
|
||||
return floorValue;
|
||||
}
|
||||
|
||||
/// @notice Marginal price of `base` in terms of `quote` (p_quote / p_base) as Q64.64
|
||||
/// @dev Returns the LMSR marginal price directly (raw 64.64) for use by off-chain quoting logic.
|
||||
function price(uint256 baseTokenIndex, uint256 quoteTokenIndex) external view returns (int128) {
|
||||
uint256 n = tokens.length;
|
||||
require(baseTokenIndex < n && quoteTokenIndex < n, "price: idx");
|
||||
require(lmsr.nAssets > 0, "price: uninit");
|
||||
return lmsr.price(baseTokenIndex, quoteTokenIndex);
|
||||
}
|
||||
|
||||
/// @notice Price of one LP token denominated in `quote` asset as Q64.64
|
||||
/// @dev Computes LMSR poolPrice (quote per unit qTotal) and scales it by totalSupply() / qTotal
|
||||
/// to return price per LP token unit in quote asset (raw 64.64).
|
||||
function poolPrice(uint256 quoteTokenIndex) external view returns (int128) {
|
||||
uint256 n = tokens.length;
|
||||
require(quoteTokenIndex < n, "poolPrice: idx");
|
||||
require(lmsr.nAssets > 0, "poolPrice: uninit");
|
||||
|
||||
// price per unit of qTotal (Q64.64) from LMSR
|
||||
int128 pricePerQ = lmsr.poolPrice(quoteTokenIndex);
|
||||
|
||||
// total internal q (qTotal) as Q64.64
|
||||
int128 qTotal = _computeSizeMetric(lmsr.qInternal);
|
||||
require(qTotal > int128(0), "poolPrice: qTotal zero");
|
||||
|
||||
// totalSupply as Q64.64
|
||||
uint256 supply = totalSupply();
|
||||
require(supply > 0, "poolPrice: zero supply");
|
||||
int128 supplyQ64 = ABDKMath64x64.fromUInt(supply);
|
||||
|
||||
// factor = totalSupply / qTotal (Q64.64)
|
||||
int128 factor = supplyQ64.div(qTotal);
|
||||
|
||||
// price per LP token = pricePerQ * factor (Q64.64)
|
||||
return pricePerQ.mul(factor);
|
||||
}
|
||||
|
||||
/// @notice Helper to compute size metric (sum of all asset quantities) from internal balances
|
||||
/// @dev Returns the sum of all provided qInternal_ entries as a Q64.64 value.
|
||||
function _computeSizeMetric(int128[] memory qInternal_) private pure returns (int128) {
|
||||
|
||||
Reference in New Issue
Block a user