removed console logs
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user