M3c: implement LP, JIT, backrun and sandwich mechanics

This commit is contained in:
2026-10-19 20:34:48 -07:00
parent 2b18bdc580
commit c2547c33a0
+211
View File
@@ -11,6 +11,21 @@ library AMMSim {
uint256 feeBps; uint256 feeBps;
} }
struct ArbitrageResult {
bool zeroForOne;
uint256 amountIn;
uint256 amountOut;
uint256 profitQuote;
uint256 postPriceWad;
}
struct SandwichResult {
uint256 frontOut;
uint256 victimOut;
uint256 backOut;
uint256 profitToken0;
}
struct ArbitrageResult { struct ArbitrageResult {
bool zeroForOne; bool zeroForOne;
uint256 amountIn; uint256 amountIn;
@@ -95,6 +110,45 @@ library AMMSim {
} }
function lpShouldRebalance(
uint256 feeIncomeWad,
uint256 rebalanceCostWad,
uint256 ratioWad
) internal pure returns (bool) {
int256 il = impermanentLossWad(ratioWad);
uint256 loss = il < 0 ? uint256(-il) : 0;
return loss > feeIncomeWad + rebalanceCostWad;
}
function jitFeeIncomeWad(
uint256 tradeAmount,
uint256 feeBps,
uint256 jitLiquidity,
uint256 totalLiquidity
) internal pure returns (uint256) {
require(feeBps < BPS, "bad fee");
require(totalLiquidity > 0, "no liquidity");
require(jitLiquidity <= totalLiquidity, "jit share");
uint256 totalFee = tradeAmount * feeBps / BPS;
return totalFee * jitLiquidity / totalLiquidity;
}
function jitShouldProvide(
uint256 tradeAmount,
uint256 feeBps,
uint256 jitLiquidity,
uint256 totalLiquidity,
uint256 placementCost
) internal pure returns (bool) {
return jitFeeIncomeWad(
tradeAmount,
feeBps,
jitLiquidity,
totalLiquidity
) > placementCost;
}
function lpShouldRebalance( function lpShouldRebalance(
uint256 feeIncomeWad, uint256 feeIncomeWad,
uint256 rebalanceCostWad, uint256 rebalanceCostWad,
@@ -462,6 +516,163 @@ library AMMSim {
); );
} }
function _spotPriceWad(uint256 x, uint256 y)
private pure returns (uint256)
{
require(x > 0 && y > 0, "bad reserves");
return y * WAD / x;
}
function _arbCandidate(
uint256 x,
uint256 y,
uint256 amountIn,
uint256 externalPriceWad,
uint256 feeBps,
bool zeroForOne
) private pure returns (ArbitrageResult memory r) {
r.zeroForOne = zeroForOne;
r.amountIn = amountIn;
if (amountIn == 0) {
r.postPriceWad = _spotPriceWad(x, y);
return r;
}
if (zeroForOne) {
r.amountOut = amountOut(x, y, amountIn, feeBps);
uint256 postX = x + amountIn;
uint256 postY = y - r.amountOut;
r.postPriceWad = _spotPriceWad(postX, postY);
uint256 externalCost = amountIn * externalPriceWad / WAD;
if (r.amountOut > externalCost) {
r.profitQuote = r.amountOut - externalCost;
}
} else {
r.amountOut = amountOut(y, x, amountIn, feeBps);
uint256 postY = y + amountIn;
uint256 postX = x - r.amountOut;
r.postPriceWad = _spotPriceWad(postX, postY);
uint256 externalValue = r.amountOut * externalPriceWad / WAD;
if (externalValue > amountIn) {
r.profitQuote = externalValue - amountIn;
}
}
}
function _priceError(uint256 a, uint256 b)
private pure returns (uint256)
{
return a > b ? a - b : b - a;
}
function backrunToPrice(
uint256 x,
uint256 y,
uint256 externalPriceWad,
uint256 feeBps
) internal pure returns (ArbitrageResult memory best) {
require(
x > 0 && y > 0 &&
externalPriceWad > 0 &&
feeBps < BPS,
"bad arb"
);
uint256 current = _spotPriceWad(x, y);
if (current == externalPriceWad) {
best.postPriceWad = current;
return best;
}
bool zeroForOne = current > externalPriceWad;
uint256 base = zeroForOne ? x : y;
uint256 lo = 0;
uint256 hi = base;
// Expand until the post-trade price crosses the reference price.
for (uint256 i = 0; i < 24; ++i) {
ArbitrageResult memory probe = _arbCandidate(
x, y, hi, externalPriceWad, feeBps, zeroForOne
);
bool crossed = zeroForOne
? probe.postPriceWad <= externalPriceWad
: probe.postPriceWad >= externalPriceWad;
if (crossed) break;
require(hi <= type(uint256).max / 2, "arb overflow");
hi *= 2;
}
for (uint256 i = 0; i < 64 && lo < hi; ++i) {
uint256 mid = lo + (hi - lo) / 2;
ArbitrageResult memory probe = _arbCandidate(
x, y, mid, externalPriceWad, feeBps, zeroForOne
);
if (zeroForOne) {
if (probe.postPriceWad > externalPriceWad) {
lo = mid + 1;
} else {
hi = mid;
}
} else {
if (probe.postPriceWad < externalPriceWad) {
lo = mid + 1;
} else {
hi = mid;
}
}
}
best = _arbCandidate(
x, y, lo, externalPriceWad, feeBps, zeroForOne
);
if (lo > 0) {
ArbitrageResult memory alt = _arbCandidate(
x, y, lo - 1, externalPriceWad, feeBps, zeroForOne
);
if (
_priceError(alt.postPriceWad, externalPriceWad) <
_priceError(best.postPriceWad, externalPriceWad)
) {
best = alt;
}
}
}
function simulateSandwich(
uint256 x,
uint256 y,
uint256 victimDx,
uint256 attackerFrontDx,
uint256 feeBps
) internal pure returns (SandwichResult memory s) {
require(
x > 0 && y > 0 &&
victimDx > 0 && attackerFrontDx > 0 &&
feeBps < BPS,
"bad sandwich"
);
s.frontOut = amountOut(
x, y, attackerFrontDx, feeBps
);
x += attackerFrontDx;
y -= s.frontOut;
s.victimOut = amountOut(
x, y, victimDx, feeBps
);
x += victimDx;
y -= s.victimOut;
s.backOut = amountOut(
y, x, s.frontOut, feeBps
);
if (s.backOut > attackerFrontDx) {
s.profitToken0 = s.backOut - attackerFrontDx;
}
}
/// @notice Token amounts represented by concentrated liquidity L at a /// @notice Token amounts represented by concentrated liquidity L at a
/// current sqrt-price. All sqrt-prices are WAD-scaled. /// current sqrt-price. All sqrt-prices are WAD-scaled.
function concentratedAmounts( function concentratedAmounts(