additive fees; burnSwapAmounts fix

This commit is contained in:
tim
2025-11-04 16:58:16 -04:00
parent 590acdd4dc
commit dc2e186331
10 changed files with 103 additions and 100 deletions

View File

@@ -519,7 +519,7 @@ library LMSRStabilized {
State storage s,
uint256 i,
int128 alpha
) internal view returns (int128 amountOut, int128 amountIn) {
) internal view returns (int128 amountIn, int128 amountOut) {
return swapAmountsForBurn(s.kappa, s.qInternal, i, alpha);
}
@@ -536,14 +536,14 @@ library LMSRStabilized {
/// @param qInternal Cached internal balances in 64.64 fixed-point format
/// @param i Index of output asset
/// @param alpha Proportional share to burn (0 < alpha <= 1)
/// @return amountOut Amount of asset i received (in 64.64 fixed-point)
/// @return amountIn LP size-metric redeemed (alpha * S)
/// @return amountOut Amount of asset i received (in 64.64 fixed-point)
function swapAmountsForBurn(
int128 kappa,
int128[] memory qInternal,
uint256 i,
int128 alpha
) internal pure returns (int128 amountOut, int128 amountIn) {
) internal pure returns (int128 amountIn, int128 amountOut) {
require(alpha > int128(0) && alpha <= ONE, "LMSR: alpha");
int128 sizeMetric = _computeSizeMetric(qInternal);
@@ -566,10 +566,10 @@ library LMSRStabilized {
}
// Start totalOut with direct portion of asset i redeemed
int128 totalOut = alpha.mul(qInternal[i]);
amountOut = alpha.mul(qInternal[i]);
// Track whether any non-zero contribution was produced
bool anyNonZero = (totalOut > int128(0));
bool anyNonZero = (amountOut > int128(0));
// For each asset j != i, swap the withdrawn a_j := alpha * q_j into i
for (uint256 j = 0; j < n; ) {
@@ -621,7 +621,7 @@ library LMSRStabilized {
// Update q_local: pool receives amountInUsed on asset j, and loses qLocal[i]
qLocal[j] = qLocal[j].add(amountInUsed);
// subtract capped output from qLocal[i] (becomes zero)
totalOut = totalOut.add(qLocal[i]);
amountOut = amountOut.add(qLocal[i]);
qLocal[i] = int128(0);
anyNonZero = true;
unchecked { j++; }
@@ -632,7 +632,7 @@ library LMSRStabilized {
// Update q_local accordingly: pool receives aj on j, and loses y on i
qLocal[j] = qLocal[j].add(aj);
qLocal[i] = qLocal[i].sub(y);
totalOut = totalOut.add(y);
amountOut = amountOut.add(y);
anyNonZero = true;
}
}
@@ -640,12 +640,9 @@ library LMSRStabilized {
}
// If no asset contributed (totalOut == 0) treat as no-trade and revert
if (!anyNonZero || totalOut <= int128(0)) {
if (!anyNonZero || amountOut <= int128(0)) {
revert("LMSR: zero output");
}
amountOut = totalOut;
return (amountOut, amountIn);
}