diff --git a/core/src/economy/sims/amm/src/AMMSim.sol b/core/src/economy/sims/amm/src/AMMSim.sol index a9781ea..b26c7eb 100644 --- a/core/src/economy/sims/amm/src/AMMSim.sol +++ b/core/src/economy/sims/amm/src/AMMSim.sol @@ -44,4 +44,143 @@ library AMMSim { uint256 loss = il < 0 ? uint256(-il) : 0; return loss > feeIncomeWad; } + + + /// @notice Token amounts represented by concentrated liquidity L at a + /// current sqrt-price. All sqrt-prices are WAD-scaled. + function concentratedAmounts( + uint256 liquidity, + uint256 sqrtLowerWad, + uint256 sqrtPriceWad, + uint256 sqrtUpperWad + ) internal pure returns (uint256 amount0, uint256 amount1) { + require( + liquidity > 0 && + sqrtLowerWad > 0 && + sqrtLowerWad < sqrtUpperWad, + "bad range" + ); + + if (sqrtPriceWad <= sqrtLowerWad) { + amount0 = + liquidity * + (sqrtUpperWad - sqrtLowerWad) / + sqrtLowerWad * + WAD / + sqrtUpperWad; + return (amount0, 0); + } + + if (sqrtPriceWad >= sqrtUpperWad) { + amount1 = + liquidity * + (sqrtUpperWad - sqrtLowerWad) / + WAD; + return (0, amount1); + } + + amount0 = + liquidity * + (sqrtUpperWad - sqrtPriceWad) / + sqrtPriceWad * + WAD / + sqrtUpperWad; + amount1 = + liquidity * + (sqrtPriceWad - sqrtLowerWad) / + WAD; + } + + function _amountOutOrZero( + uint256 x, + uint256 y, + uint256 dx, + uint256 feeBps + ) private pure returns (uint256) { + if (dx == 0) return 0; + return amountOut(x, y, dx, feeBps); + } + + function _marginalOutWad( + uint256 x, + uint256 y, + uint256 dx, + uint256 feeBps + ) private pure returns (uint256) { + require(x > 0 && y > 0 && feeBps < BPS, "bad pool"); + uint256 gamma = BPS - feeBps; + uint256 dxEff = dx * gamma / BPS; + uint256 denom = x + dxEff; + + // d(amountOut)/d(raw input), WAD-scaled. Keeping the divisions late + // preserves enough precision for the router without floating point. + return y * x * gamma * WAD / BPS / denom / denom; + } + + /// @notice Split one exact-input swap across two constant-product pools. + /// The optimum is where their marginal outputs equalize; binary search is + /// deterministic because each pool's marginal output is monotone. + function bestSplitTwoPools( + uint256 xA, + uint256 yA, + uint256 xB, + uint256 yB, + uint256 dx, + uint256 feeABps, + uint256 feeBBps + ) internal pure returns (uint256 splitA, uint256 totalOut) { + require( + xA > 0 && yA > 0 && xB > 0 && yB > 0 && + dx > 0 && feeABps < BPS && feeBBps < BPS, + "bad route" + ); + + uint256 lo = 0; + uint256 hi = dx; + for (uint256 i = 0; i < 64 && lo < hi; ++i) { + uint256 mid = lo + (hi - lo) / 2; + uint256 marginalA = _marginalOutWad( + xA, yA, mid, feeABps + ); + uint256 marginalB = _marginalOutWad( + xB, yB, dx - mid, feeBBps + ); + if (marginalA > marginalB) { + lo = mid + 1; + } else { + hi = mid; + } + } + + uint256 candidate = lo; + uint256 outCandidate = + _amountOutOrZero(xA, yA, candidate, feeABps) + + _amountOutOrZero(xB, yB, dx - candidate, feeBBps); + + splitA = candidate; + totalOut = outCandidate; + + if (candidate > 0) { + uint256 alt = candidate - 1; + uint256 outAlt = + _amountOutOrZero(xA, yA, alt, feeABps) + + _amountOutOrZero(xB, yB, dx - alt, feeBBps); + if (outAlt > totalOut) { + splitA = alt; + totalOut = outAlt; + } + } + + uint256 onlyA = amountOut(xA, yA, dx, feeABps); + if (onlyA > totalOut) { + splitA = dx; + totalOut = onlyA; + } + + uint256 onlyB = amountOut(xB, yB, dx, feeBBps); + if (onlyB > totalOut) { + splitA = 0; + totalOut = onlyB; + } + } }