removed console logs
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import {PartyPool} from "./PartyPool.sol";
|
||||
import {PartyPoolSwapMintImpl} from "./PartyPoolSwapMintImpl.sol";
|
||||
import {PartyPoolMintImpl} from "./PartyPoolMintImpl.sol";
|
||||
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
|
||||
import {PartyPlanner} from "./PartyPlanner.sol";
|
||||
import {PartyPool} from "./PartyPool.sol";
|
||||
import {PartyPoolMintImpl} from "./PartyPoolMintImpl.sol";
|
||||
import {PartyPoolSwapImpl} from "./PartyPoolSwapImpl.sol";
|
||||
|
||||
library Deploy {
|
||||
|
||||
function newPartyPlanner() internal returns (PartyPlanner) {
|
||||
return new PartyPlanner(
|
||||
new PartyPoolSwapMintImpl(),
|
||||
new PartyPoolSwapImpl(),
|
||||
new PartyPoolMintImpl(),
|
||||
0, // protocolFeePpm = 0 for deploy helper
|
||||
address(0) // protocolFeeAddress = address(0) for deploy helper
|
||||
@@ -43,7 +43,7 @@ library Deploy {
|
||||
protocolFeePpm,
|
||||
protocolAddr,
|
||||
_stable,
|
||||
new PartyPoolSwapMintImpl(),
|
||||
new PartyPoolSwapImpl(),
|
||||
new PartyPoolMintImpl()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -120,6 +120,6 @@ interface IPartyPlanner {
|
||||
function mintImpl() external view returns (PartyPoolMintImpl);
|
||||
|
||||
/// @notice Address of the SwapMint implementation contract used by all pools created by this factory
|
||||
function swapMintImpl() external view returns (PartyPoolSwapMintImpl);
|
||||
function swapMintImpl() external view returns (PartyPoolSwapImpl);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
|
||||
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
|
||||
import {IERC20Metadata} from "../lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
|
||||
|
||||
/// @title PartyPool - LMSR-backed multi-asset pool with LP ERC20 token
|
||||
/// @notice A multi-asset liquidity pool backed by the LMSRStabilized pricing model.
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "forge-std/console2.sol";
|
||||
import "@abdk/ABDKMath64x64.sol";
|
||||
import {ABDKMath64x64} from "../lib/abdk-libraries-solidity/ABDKMath64x64.sol";
|
||||
|
||||
/// @notice Stabilized LMSR library with incremental exp(z) caching for gas efficiency.
|
||||
/// - Stores b (64.64), M (shift), Z = sum exp(z_i), z[i] = (q_i / b) - M
|
||||
@@ -41,21 +40,11 @@ library LMSRStabilized {
|
||||
}
|
||||
|
||||
int128 total = _computeSizeMetric(s.qInternal);
|
||||
console2.log("total (internal 64.64)");
|
||||
console2.logInt(total);
|
||||
require(total > int128(0), "LMSR: total zero");
|
||||
|
||||
console2.log("LMSR.init: start");
|
||||
console2.log("nAssets", s.nAssets);
|
||||
console2.log("qInternal.length", s.qInternal.length);
|
||||
|
||||
// Set kappa directly (caller provides kappa)
|
||||
s.kappa = kappa;
|
||||
console2.log("kappa (64x64)");
|
||||
console2.logInt(s.kappa);
|
||||
require(s.kappa > int128(0), "LMSR: kappa>0");
|
||||
|
||||
console2.log("LMSR.init: done");
|
||||
}
|
||||
|
||||
/* --------------------
|
||||
@@ -167,49 +156,31 @@ library LMSRStabilized {
|
||||
// push the marginal price p_i/p_j beyond the limit; if so, truncate `a`.
|
||||
// Marginal price ratio evolves as r(t) = r0 * exp(t/b) (since e_i multiplies by exp(t/b))
|
||||
if (limitPrice > int128(0)) {
|
||||
console2.log("\n=== LimitPrice Logic Debug ===");
|
||||
console2.log("Received limitPrice (64x64):");
|
||||
console2.logInt(limitPrice);
|
||||
|
||||
console2.log("Current price ratio r0 (e_i/e_j, 64x64):");
|
||||
console2.logInt(r0);
|
||||
|
||||
// r0 must be positive; if r0 == 0 then no risk of exceeding limit by increasing r.
|
||||
require(r0 >= int128(0), "LMSR: r0<0");
|
||||
if (r0 == int128(0)) {
|
||||
console2.log("r0 == 0 (input asset has zero weight), no limit truncation needed");
|
||||
// console2.log("r0 == 0 (input asset has zero weight), no limit truncation needed");
|
||||
} else {
|
||||
// If limitPrice <= current price, we revert (caller must choose a limit > current price to allow any fill)
|
||||
if (limitPrice <= r0) {
|
||||
console2.log("Limit price is <= current price: reverting");
|
||||
revert("LMSR: limitPrice <= current price");
|
||||
}
|
||||
|
||||
// Compute a_limit directly from ln(limit / r0): a_limit = b * ln(limit / r0)
|
||||
int128 ratioLimitOverR0 = limitPrice.div(r0);
|
||||
console2.log("limitPrice/r0 (64x64):");
|
||||
console2.logInt(ratioLimitOverR0);
|
||||
require(ratioLimitOverR0 > int128(0), "LMSR: ratio<=0");
|
||||
|
||||
int128 aLimitOverB = _ln(ratioLimitOverR0); // > 0
|
||||
console2.log("ln(limitPrice/r0) (64x64):");
|
||||
console2.logInt(aLimitOverB);
|
||||
|
||||
// aLimit = b * aLimitOverB
|
||||
int128 aLimit64 = b.mul(aLimitOverB);
|
||||
console2.log("aLimit in 64x64 format:");
|
||||
console2.logInt(aLimit64);
|
||||
|
||||
// If computed aLimit is less than the requested a, use the truncated value.
|
||||
if (aLimit64 < a) {
|
||||
console2.log("TRUNCATING: a reduced from 64.64 value");
|
||||
console2.logInt(a);
|
||||
console2.log("to 64.64 value");
|
||||
console2.logInt(aLimit64);
|
||||
amountIn = aLimit64; // Store the truncated input amount
|
||||
a = aLimit64; // Use truncated amount for calculations
|
||||
} else {
|
||||
console2.log("Not truncating: aLimit64 >= a");
|
||||
// console2.log("Not truncating: aLimit64 >= a");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,56 +190,24 @@ library LMSRStabilized {
|
||||
// Protect exp from enormous inputs (consistent with recenter thresholds)
|
||||
require(aOverB <= EXP_LIMIT, "LMSR: a/b too large (would overflow exp)");
|
||||
|
||||
console2.log("\n=== AmountOut Calculation Debug ===");
|
||||
console2.log("Input amount (64.64):");
|
||||
console2.logInt(a);
|
||||
console2.log("a/b (64x64):");
|
||||
console2.logInt(aOverB);
|
||||
|
||||
// Use the closed-form fee-free formula:
|
||||
// y = b * ln(1 + r0 * (1 - exp(-a/b)))
|
||||
console2.log("r0_for_calc (e_i/e_j):");
|
||||
console2.logInt(r0);
|
||||
|
||||
int128 expNeg = _exp(aOverB.neg()); // exp(-a/b)
|
||||
console2.log("exp(-a/b):");
|
||||
console2.logInt(expNeg);
|
||||
|
||||
int128 oneMinusExpNeg = ONE.sub(expNeg);
|
||||
console2.log("1 - exp(-a/b):");
|
||||
console2.logInt(oneMinusExpNeg);
|
||||
|
||||
int128 inner = ONE.add(r0.mul(oneMinusExpNeg));
|
||||
console2.log("inner = 1 + r0 * (1 - exp(-a/b)):");
|
||||
console2.logInt(inner);
|
||||
|
||||
// If inner <= 0 then cap output to the current balance q_j (cannot withdraw more than q_j)
|
||||
if (inner <= int128(0)) {
|
||||
console2.log("WARNING: inner <= 0, capping output to balance q_j");
|
||||
int128 qj64 = qInternal[j];
|
||||
console2.log("Capped output (64.64):");
|
||||
console2.logInt(qj64);
|
||||
return (amountIn, qj64);
|
||||
}
|
||||
|
||||
int128 lnInner = _ln(inner);
|
||||
console2.log("ln(inner):");
|
||||
console2.logInt(lnInner);
|
||||
|
||||
int128 b_lnInner = b.mul(lnInner);
|
||||
console2.log("b*ln(inner):");
|
||||
console2.logInt(b_lnInner);
|
||||
|
||||
amountOut = b_lnInner;
|
||||
console2.log("amountOut = b*ln(inner):");
|
||||
console2.logInt(amountOut);
|
||||
|
||||
console2.log("amountOut (final 64.64 amount):");
|
||||
console2.logInt(amountOut);
|
||||
|
||||
// Safety check
|
||||
if (amountOut <= 0) {
|
||||
console2.log("WARNING: x64 <= 0, returning 0");
|
||||
return (0, 0);
|
||||
}
|
||||
}
|
||||
@@ -334,60 +273,34 @@ library LMSRStabilized {
|
||||
// Compute r0 = exp((q_i - q_j) / b) directly using invB
|
||||
int128 r0 = _exp(qInternal[i].sub(qInternal[j]).mul(invB));
|
||||
|
||||
console2.log("\n=== Max Input/Output Calculation ===");
|
||||
console2.log("Limit price (64x64):");
|
||||
console2.logInt(limitPrice);
|
||||
console2.log("Current price ratio r0 (e_i/e_j, 64x64):");
|
||||
console2.logInt(r0);
|
||||
|
||||
// Mirror swapAmountsForExactInput behavior: treat invalid r0 as an error condition.
|
||||
// Revert if r0 is non-positive (no finite trade under a price limit).
|
||||
require(r0 > int128(0), "LMSR: r0<=0");
|
||||
|
||||
// If current price already exceeds or equals limit, revert the same way swapAmountsForExactInput does.
|
||||
if (r0 >= limitPrice) {
|
||||
console2.log("Limit price is <= current price: reverting");
|
||||
revert("LMSR: limitPrice <= current price");
|
||||
}
|
||||
|
||||
// Calculate the price change factor: limitPrice/r0
|
||||
int128 priceChangeFactor = limitPrice.div(r0);
|
||||
console2.log("Price change factor (limitPrice/r0):");
|
||||
console2.logInt(priceChangeFactor);
|
||||
|
||||
// ln(priceChangeFactor) gives us the maximum allowed delta in the exponent
|
||||
int128 maxDeltaExponent = _ln(priceChangeFactor);
|
||||
console2.log("Max delta exponent ln(priceChangeFactor):");
|
||||
console2.logInt(maxDeltaExponent);
|
||||
|
||||
// Maximum input capable of reaching the price limit:
|
||||
// x_max = b * ln(limitPrice / r0)
|
||||
int128 amountInMax = b.mul(maxDeltaExponent);
|
||||
console2.log("Max input to reach limit (64.64):");
|
||||
console2.logInt(amountInMax);
|
||||
|
||||
// The maximum output y corresponding to that input:
|
||||
// y = b * ln(1 + (e_i/e_j) * (1 - exp(-x_max/b)))
|
||||
int128 expTerm = ONE.sub(_exp(maxDeltaExponent.neg()));
|
||||
console2.log("1 - exp(-maxDeltaExponent):");
|
||||
console2.logInt(expTerm);
|
||||
|
||||
int128 innerTerm = r0.mul(expTerm);
|
||||
console2.log("e_i/e_j * expTerm:");
|
||||
console2.logInt(innerTerm);
|
||||
|
||||
int128 lnTerm = _ln(ONE.add(innerTerm));
|
||||
console2.log("ln(1 + innerTerm):");
|
||||
console2.logInt(lnTerm);
|
||||
|
||||
int128 maxOutput = b.mul(lnTerm);
|
||||
console2.log("Max output (b * lnTerm):");
|
||||
console2.logInt(maxOutput);
|
||||
|
||||
// Current balance of asset j (in 64.64)
|
||||
int128 qj64 = qInternal[j];
|
||||
console2.log("Current j balance (64.64):");
|
||||
console2.logInt(qj64);
|
||||
|
||||
// Initialize outputs to the computed maxima
|
||||
amountIn = amountInMax;
|
||||
@@ -395,7 +308,6 @@ library LMSRStabilized {
|
||||
|
||||
// If the calculated maximum output exceeds the balance, cap output and solve for input.
|
||||
if (maxOutput > qj64) {
|
||||
console2.log("Max output exceeds balance, capping to balance");
|
||||
amountOut = qj64;
|
||||
|
||||
// Solve inverse relation for input given capped output:
|
||||
@@ -405,19 +317,12 @@ library LMSRStabilized {
|
||||
// a = -b * ln( (r0 + 1 - E) / r0 ) = b * ln( r0 / (r0 + 1 - E) )
|
||||
int128 E = _exp(amountOut.mul(invB)); // exp(y/b)
|
||||
int128 rhs = r0.add(ONE).sub(E); // r0 + 1 - E
|
||||
console2.log("E = exp(y/b):");
|
||||
console2.logInt(E);
|
||||
console2.log("rhs = r0 + 1 - E:");
|
||||
console2.logInt(rhs);
|
||||
|
||||
// If rhs <= 0 due to numerical issues, fall back to amountInMax
|
||||
if (rhs <= int128(0)) {
|
||||
console2.log("Numerical issue solving inverse; using amountInMax as fallback");
|
||||
amountIn = amountInMax;
|
||||
} else {
|
||||
amountIn = b.mul(_ln(r0.div(rhs)));
|
||||
console2.log("Computed input required for capped output (64.64):");
|
||||
console2.logInt(amountIn);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -796,19 +701,9 @@ library LMSRStabilized {
|
||||
require(amountIn > int128(0), "LMSR: amountIn <= 0");
|
||||
require(amountOut > int128(0), "LMSR: amountOut <= 0");
|
||||
|
||||
console2.log("\n=== Applying Swap ===");
|
||||
console2.log("Input asset:", i);
|
||||
console2.log("Output asset:", j);
|
||||
console2.log("Amount in (64.64):");
|
||||
console2.logInt(amountIn);
|
||||
console2.log("Amount out (64.64):");
|
||||
console2.logInt(amountOut);
|
||||
|
||||
// Update internal balances
|
||||
s.qInternal[i] = s.qInternal[i].add(amountIn);
|
||||
s.qInternal[j] = s.qInternal[j].sub(amountOut);
|
||||
|
||||
console2.log("=== Swap Applied (qInternal updated) ===\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -819,27 +714,19 @@ library LMSRStabilized {
|
||||
function updateForProportionalChange(State storage s, int128[] memory newQInternal) internal {
|
||||
require(newQInternal.length == s.nAssets, "LMSR: length mismatch");
|
||||
|
||||
console2.log("LMSR.updateForProportionalChange: start");
|
||||
|
||||
// Compute new total for validation
|
||||
int128 newTotal = _computeSizeMetric(newQInternal);
|
||||
console2.log("new total");
|
||||
console2.logInt(newTotal);
|
||||
|
||||
require(newTotal > int128(0), "LMSR: new total zero");
|
||||
|
||||
// With kappa formulation, b automatically scales with pool size
|
||||
int128 newB = s.kappa.mul(newTotal);
|
||||
console2.log("new effective b");
|
||||
console2.logInt(newB);
|
||||
|
||||
// Update the cached qInternal with new values
|
||||
for (uint i = 0; i < s.nAssets; ) {
|
||||
s.qInternal[i] = newQInternal[i];
|
||||
unchecked { i++; }
|
||||
}
|
||||
|
||||
console2.log("LMSR.updateForProportionalChange: end");
|
||||
}
|
||||
|
||||
/// @notice Price-share of asset i: exp(z_i) / Z (64.64)
|
||||
@@ -1064,8 +951,6 @@ library LMSRStabilized {
|
||||
/// @notice De-initialize the LMSR state when the entire pool is drained.
|
||||
/// This resets the state so the pool can be re-initialized by init(...) on next mint.
|
||||
function deinit(State storage s) internal {
|
||||
console2.log("LMSR.deinit: resetting state");
|
||||
|
||||
// Reset core state
|
||||
s.nAssets = 0;
|
||||
s.kappa = int128(0);
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "forge-std/console2.sol";
|
||||
import "@abdk/ABDKMath64x64.sol";
|
||||
import "./LMSRStabilized.sol";
|
||||
import {ABDKMath64x64} from "../lib/abdk-libraries-solidity/ABDKMath64x64.sol";
|
||||
import {LMSRStabilized} from "./LMSRStabilized.sol";
|
||||
|
||||
/// @notice Specialized functions for the 2-asset stablecoin case
|
||||
library LMSRStabilizedBalancedPair {
|
||||
@@ -40,7 +39,6 @@ library LMSRStabilizedBalancedPair {
|
||||
|
||||
// If not exactly a two-asset pool, fall back to the general routine.
|
||||
if (s.nAssets != 2) {
|
||||
console2.log('balanced2: fallback nAssets!=n2');
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
@@ -48,7 +46,6 @@ library LMSRStabilizedBalancedPair {
|
||||
int128 b = LMSRStabilized._computeB(s);
|
||||
// Guard: if b not positive, fallback to exact implementation (will revert there if necessary)
|
||||
if (!(b > int128(0))) {
|
||||
console2.log("balanced2: fallback b<=0");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
int128 invB = ABDKMath64x64.div(ONE, b);
|
||||
@@ -58,8 +55,6 @@ library LMSRStabilizedBalancedPair {
|
||||
|
||||
// If a positive limitPrice is given, attempt a 2-asset near-parity polynomial solution
|
||||
if (limitPrice > int128(0)) {
|
||||
console2.log("balanced2: handling limitPrice via small-delta approx");
|
||||
|
||||
// Approximate r0 = exp(delta) using Taylor: 1 + δ + δ^2/2 + δ^3/6
|
||||
int128 delta_sq = delta.mul(delta);
|
||||
int128 delta_cu = delta_sq.mul(delta);
|
||||
@@ -68,19 +63,13 @@ library LMSRStabilizedBalancedPair {
|
||||
.add(delta_sq.div(ABDKMath64x64.fromUInt(2)))
|
||||
.add(delta_cu.div(ABDKMath64x64.fromUInt(6)));
|
||||
|
||||
console2.log("r0_approx:");
|
||||
console2.logInt(r0_approx);
|
||||
|
||||
// If limitPrice <= r0 (current price) we must revert (same semantic as original)
|
||||
if (limitPrice <= r0_approx) {
|
||||
console2.log("balanced2: limitPrice <= r0_approx -> revert");
|
||||
revert("LMSR: limitPrice <= current price");
|
||||
}
|
||||
|
||||
// Ratio = limitPrice / r0_approx
|
||||
int128 ratio = limitPrice.div(r0_approx);
|
||||
console2.log("limitPrice/r0_approx:");
|
||||
console2.logInt(ratio);
|
||||
|
||||
// x = ratio - 1; use Taylor for ln(1+x) when |x| is small
|
||||
int128 x = ratio.sub(ONE);
|
||||
@@ -90,7 +79,6 @@ library LMSRStabilizedBalancedPair {
|
||||
int128 X_MAX = ABDKMath64x64.divu(1, 10); // 0.1
|
||||
if (absX > X_MAX) {
|
||||
// Too large to safely approximate; fall back to exact computation
|
||||
console2.log("balanced2: fallback limitPrice ratio too far from 1");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
@@ -101,63 +89,34 @@ library LMSRStabilizedBalancedPair {
|
||||
.sub(x_sq.div(ABDKMath64x64.fromUInt(2)))
|
||||
.add(x_cu.div(ABDKMath64x64.fromUInt(3)));
|
||||
|
||||
console2.log("lnRatioApprox (64x64):");
|
||||
console2.logInt(lnRatioApprox);
|
||||
|
||||
// aLimitOverB = ln(limitPrice / r0) approximated
|
||||
int128 aLimitOverB = lnRatioApprox;
|
||||
|
||||
// Must be > 0; otherwise fall back
|
||||
if (!(aLimitOverB > int128(0))) {
|
||||
console2.log("balanced2: fallback non-positive aLimitOverB");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
// aLimit = b * aLimitOverB (in Q64.64)
|
||||
int128 aLimit64 = b.mul(aLimitOverB);
|
||||
console2.log("aLimit64 (64x64):");
|
||||
console2.logInt(aLimit64);
|
||||
|
||||
// If computed aLimit is less than requested a, use the truncated value.
|
||||
if (aLimit64 < a) {
|
||||
console2.log("balanced2: truncating input a to aLimit64 due to limitPrice");
|
||||
console2.log("original a:");
|
||||
console2.logInt(a);
|
||||
console2.log("truncated aLimit64:");
|
||||
console2.logInt(aLimit64);
|
||||
a = aLimit64;
|
||||
} else {
|
||||
console2.log("balanced2: limitPrice does not truncate input");
|
||||
// console2.log("balanced2: limitPrice does not truncate input");
|
||||
}
|
||||
|
||||
// Note: after potential truncation we continue with the polynomial approximation below
|
||||
}
|
||||
|
||||
// Debug: entry trace
|
||||
console2.log("balanced2: enter");
|
||||
console2.log("i", i);
|
||||
console2.log("j", j);
|
||||
console2.log("nAssets", s.nAssets);
|
||||
console2.log("a (64x64):");
|
||||
console2.logInt(a);
|
||||
console2.log("b (64x64):");
|
||||
console2.logInt(b);
|
||||
console2.log("invB (64x64):");
|
||||
console2.logInt(invB);
|
||||
|
||||
// Small-signal delta already computed above; reuse it
|
||||
int128 absDelta = delta >= int128(0) ? delta : delta.neg();
|
||||
|
||||
console2.log("delta (q_i - q_j)/b:");
|
||||
console2.logInt(delta);
|
||||
console2.log("absDelta:");
|
||||
console2.logInt(absDelta);
|
||||
|
||||
// Allow balanced pools only: require |delta| <= 1% (approx ln(1.01) ~ 0.00995; we use conservative 0.01)
|
||||
int128 DELTA_MAX = ABDKMath64x64.divu(1, 100); // 0.01
|
||||
if (absDelta > DELTA_MAX) {
|
||||
// Not balanced within 1% -> use exact routine
|
||||
console2.log("balanced2: fallback delta too large");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
@@ -165,18 +124,13 @@ library LMSRStabilizedBalancedPair {
|
||||
int128 u = a.mul(invB);
|
||||
if (u <= int128(0)) {
|
||||
// Non-positive input -> behave like exact implementation (will revert if invalid)
|
||||
console2.log("balanced2: fallback u<=0");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
console2.log("u = a/b (64x64):");
|
||||
console2.logInt(u);
|
||||
|
||||
// Restrict to a conservative polynomial radius for accuracy; fallback otherwise.
|
||||
// We choose u <= 0.5 (0.5 in Q64.64) as safe for cubic approximation in typical parameters.
|
||||
int128 U_MAX = ABDKMath64x64.divu(1, 2); // 0.5
|
||||
if (u > U_MAX) {
|
||||
console2.log("balanced2: fallback u too large");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
@@ -200,39 +154,26 @@ library LMSRStabilizedBalancedPair {
|
||||
if (u <= U_TIER1) {
|
||||
// Cheap quadratic ln(1+X) ≈ X - X^2/2
|
||||
lnApprox = X.sub(X2.div(ABDKMath64x64.fromUInt(2)));
|
||||
console2.log("balanced2: using tier1 quadratic approx");
|
||||
} else if (u <= U_MAX_LOCAL) {
|
||||
// Secondary cubic correction: ln(1+X) ≈ X - X^2/2 + X^3/3
|
||||
int128 X3 = X2.mul(X);
|
||||
lnApprox = X.sub(X2.div(ABDKMath64x64.fromUInt(2))).add(X3.div(ABDKMath64x64.fromUInt(3)));
|
||||
console2.log("balanced2: using tier2 cubic approx");
|
||||
} else {
|
||||
// u beyond allowed range - fallback
|
||||
console2.log("balanced2: fallback u too large for approximation");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
console2.log("lnApprox (64x64):");
|
||||
console2.logInt(lnApprox);
|
||||
|
||||
int128 approxOut = b.mul(lnApprox);
|
||||
|
||||
console2.log("approxOut (64x64):");
|
||||
console2.logInt(approxOut);
|
||||
|
||||
// Safety sanity: approximation must be > 0
|
||||
if (approxOut <= int128(0)) {
|
||||
console2.log("balanced2: fallback approxOut <= 0");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
// Cap to available j balance: if approximated output exceeds q_j, it's likely approximation break;
|
||||
// fall back to the exact solver to handle capping/edge cases.
|
||||
int128 qj64 = s.qInternal[j];
|
||||
console2.log("qj64 (64x64):");
|
||||
console2.logInt(qj64);
|
||||
if (approxOut >= qj64) {
|
||||
console2.log("balanced2: fallback approxOut >= qj");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
@@ -240,15 +181,8 @@ library LMSRStabilizedBalancedPair {
|
||||
amountIn = a;
|
||||
amountOut = approxOut;
|
||||
|
||||
console2.log("balanced2: returning approx results");
|
||||
console2.log("amountIn (64x64):");
|
||||
console2.logInt(amountIn);
|
||||
console2.log("amountOut (64x64):");
|
||||
console2.logInt(amountOut);
|
||||
|
||||
// Final guard: ensure output is sensible and not NaN-like (rely on positivity checks above)
|
||||
if (amountOut < int128(0)) {
|
||||
console2.log("balanced2: fallback final guard amountOut<0");
|
||||
return LMSRStabilized.swapAmountsForExactInput(s, i, j, a, limitPrice);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "./IPartyPlanner.sol";
|
||||
import "./PartyPool.sol";
|
||||
import "./LMSRStabilized.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import {PartyPoolSwapMintImpl} from "./PartyPoolSwapMintImpl.sol";
|
||||
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
|
||||
import {SafeERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import {IPartyPlanner} from "./IPartyPlanner.sol";
|
||||
import {LMSRStabilized} from "./LMSRStabilized.sol";
|
||||
import {PartyPool} from "./PartyPool.sol";
|
||||
import {PartyPoolMintImpl} from "./PartyPoolMintImpl.sol";
|
||||
import {PartyPoolSwapImpl} from "./PartyPoolSwapImpl.sol";
|
||||
|
||||
/// @title PartyPlanner
|
||||
/// @notice Factory contract for creating and tracking PartyPool instances
|
||||
@@ -20,8 +20,8 @@ contract PartyPlanner is IPartyPlanner {
|
||||
function mintImpl() external view returns (PartyPoolMintImpl) { return MINT_IMPL; }
|
||||
|
||||
/// @notice Address of the SwapMint implementation contract used by all pools created by this factory
|
||||
PartyPoolSwapMintImpl private immutable SWAP_MINT_IMPL;
|
||||
function swapMintImpl() external view returns (PartyPoolSwapMintImpl) { return SWAP_MINT_IMPL; }
|
||||
PartyPoolSwapImpl private immutable SWAP_MINT_IMPL;
|
||||
function swapMintImpl() external view returns (PartyPoolSwapImpl) { return SWAP_MINT_IMPL; }
|
||||
|
||||
/// @notice Protocol fee share (ppm) applied to fees collected by pools created by this planner
|
||||
uint256 private immutable PROTOCOL_FEE_PPM;
|
||||
@@ -43,7 +43,7 @@ contract PartyPlanner is IPartyPlanner {
|
||||
/// @param _protocolFeePpm protocol fee share (ppm) to be used for pools created by this planner
|
||||
/// @param _protocolFeeAddress recipient address for protocol fees for pools created by this planner (may be address(0))
|
||||
constructor(
|
||||
PartyPoolSwapMintImpl _swapMintImpl,
|
||||
PartyPoolSwapImpl _swapMintImpl,
|
||||
PartyPoolMintImpl _mintImpl,
|
||||
uint256 _protocolFeePpm,
|
||||
address _protocolFeeAddress
|
||||
@@ -97,7 +97,7 @@ contract PartyPlanner is IPartyPlanner {
|
||||
PROTOCOL_FEE_PPM,
|
||||
PROTOCOL_FEE_ADDRESS,
|
||||
_stable,
|
||||
PartyPoolSwapMintImpl(SWAP_MINT_IMPL),
|
||||
PartyPoolSwapImpl(SWAP_MINT_IMPL),
|
||||
MINT_IMPL
|
||||
);
|
||||
|
||||
|
||||
@@ -6,15 +6,14 @@ import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20
|
||||
import {SafeERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import {Address} from "../lib/openzeppelin-contracts/contracts/utils/Address.sol";
|
||||
import {ReentrancyGuard} from "../lib/openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol";
|
||||
import {ERC20External} from "./ERC20External.sol";
|
||||
import {IPartyFlashCallback} from "./IPartyFlashCallback.sol";
|
||||
import {IPartyPool} from "./IPartyPool.sol";
|
||||
import {LMSRStabilized} from "./LMSRStabilized.sol";
|
||||
import {LMSRStabilizedBalancedPair} from "./LMSRStabilizedBalancedPair.sol";
|
||||
import {PartyPoolBase} from "./PartyPoolBase.sol";
|
||||
import {PartyPoolMintImpl} from "./PartyPoolMintImpl.sol";
|
||||
import {PartyPoolSwapMintImpl} from "./PartyPoolSwapMintImpl.sol";
|
||||
import {ERC20External} from "./ERC20External.sol";
|
||||
|
||||
import {PartyPoolSwapImpl} from "./PartyPoolSwapImpl.sol";
|
||||
|
||||
/// @title PartyPool - LMSR-backed multi-asset pool with LP ERC20 token
|
||||
/// @notice A multi-asset liquidity pool backed by the LMSRStabilized pricing model.
|
||||
@@ -67,8 +66,8 @@ contract PartyPool is PartyPoolBase, ERC20External, IPartyPool {
|
||||
function mintImpl() external view returns (PartyPoolMintImpl) { return MINT_IMPL; }
|
||||
|
||||
/// @notice Address of the SwapMint implementation contract for delegatecall
|
||||
PartyPoolSwapMintImpl private immutable SWAP_MINT_IMPL;
|
||||
function swapMintImpl() external view returns (PartyPoolSwapMintImpl) { return SWAP_MINT_IMPL; }
|
||||
PartyPoolSwapImpl private immutable SWAP_IMPL;
|
||||
function swapMintImpl() external view returns (PartyPoolSwapImpl) { return SWAP_IMPL; }
|
||||
|
||||
|
||||
/// @inheritdoc IPartyPool
|
||||
@@ -104,7 +103,7 @@ contract PartyPool is PartyPoolBase, ERC20External, IPartyPool {
|
||||
uint256 protocolFeePpm_, // NEW: protocol share of fees (ppm)
|
||||
address protocolFeeAddress_, // NEW: recipient for collected protocol tokens
|
||||
bool stable_,
|
||||
PartyPoolSwapMintImpl swapMintImpl_,
|
||||
PartyPoolSwapImpl swapMintImpl_,
|
||||
PartyPoolMintImpl mintImpl_
|
||||
) ERC20External(name_, symbol_) {
|
||||
require(tokens_.length > 1, "Pool: need >1 asset");
|
||||
@@ -120,7 +119,7 @@ contract PartyPool is PartyPoolBase, ERC20External, IPartyPool {
|
||||
PROTOCOL_FEE_PPM = protocolFeePpm_;
|
||||
PROTOCOL_FEE_ADDRESS = protocolFeeAddress_;
|
||||
IS_STABLE_PAIR = stable_ && tokens_.length == 2;
|
||||
SWAP_MINT_IMPL = swapMintImpl_;
|
||||
SWAP_IMPL = swapMintImpl_;
|
||||
MINT_IMPL = mintImpl_;
|
||||
|
||||
uint256 n = tokens_.length;
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "@abdk/ABDKMath64x64.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "./PartyPoolBase.sol";
|
||||
import "./LMSRStabilized.sol";
|
||||
import {IPartyPool} from "./IPartyPool.sol";
|
||||
import {ABDKMath64x64} from "../lib/abdk-libraries-solidity/ABDKMath64x64.sol";
|
||||
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
|
||||
import {SafeERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import {LMSRStabilized} from "./LMSRStabilized.sol";
|
||||
import {PartyPoolBase} from "./PartyPoolBase.sol";
|
||||
|
||||
/// @title PartyPoolSwapMintImpl - Implementation contract for swapMint and burnSwap functions
|
||||
/// @notice This contract contains the swapMint and burnSwap implementation that will be called via delegatecall
|
||||
/// @dev This contract inherits from PartyPoolBase to access storage and internal functions
|
||||
contract PartyPoolSwapMintImpl is PartyPoolBase {
|
||||
contract PartyPoolSwapImpl is PartyPoolBase {
|
||||
using ABDKMath64x64 for int128;
|
||||
using LMSRStabilized for LMSRStabilized.State;
|
||||
using SafeERC20 for IERC20;
|
||||
Reference in New Issue
Block a user