mint & burn streamlining

This commit is contained in:
tim
2025-12-01 16:26:58 -04:00
parent 4e56f54f27
commit 6795886eab
6 changed files with 35 additions and 47 deletions

View File

@@ -91,19 +91,15 @@ contract PartyPoolMintImpl is PartyPoolBase {
uint256[] memory depositAmounts = mintAmounts(lpTokenAmount, _totalSupply, _cachedUintBalances);
// Transfer in all token amounts
for (uint i = 0; i < n; ) {
if (depositAmounts[i] > 0) {
_receiveTokenFrom(payer, _tokens[i], depositAmounts[i]);
}
unchecked { i++; }
}
// Update cached balances and internal q for all assets using depositAmounts
int128[] memory newQInternal = new int128[](n);
for (uint i = 0; i < n; ) {
uint256 newBal = _cachedUintBalances[i] + depositAmounts[i];
_cachedUintBalances[i] = newBal;
newQInternal[i] = _uintToInternalFloor(newBal, _bases[i]);
uint256 amount = depositAmounts[i];
if (amount > 0) {
_receiveTokenFrom(payer, _tokens[i], amount);
uint256 newBal = _cachedUintBalances[i] + amount;
_cachedUintBalances[i] = newBal;
newQInternal[i] = _uintToInternalFloor(newBal, _bases[i]);
}
unchecked { i++; }
}
@@ -128,11 +124,6 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Ensure the calculated LP amount is not too different from requested
require(actualLpToMint > 0, "mint: zero LP minted");
// Allow actual amount to be at most 0.00001% less than requested
// This accounts for rounding in deposit calculations
uint256 minAcceptable = lpTokenAmount * 99_999 / 100_000;
require(actualLpToMint >= minAcceptable, "mint: insufficient LP minted");
_mint(receiver, actualLpToMint);
emit IPartyPool.Mint(payer, receiver, depositAmounts, actualLpToMint);
@@ -162,33 +153,23 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Compute proportional withdrawal amounts for the requested LP amount (rounded down)
withdrawAmounts = burnAmounts(lpAmount, _totalSupply, _cachedUintBalances);
// Transfer underlying _tokens out to receiver according to computed proportions
for (uint i = 0; i < n; ) {
if (withdrawAmounts[i] > 0) {
_sendTokenTo(_tokens[i], receiver, withdrawAmounts[i], unwrap);
}
unchecked { i++; }
}
// Update cached balances and internal q for all assets using computed withdrawals
bool allZero = true;
int128[] memory newQInternal = new int128[](n);
for (uint i = 0; i < n; ) {
uint256 newBal = _cachedUintBalances[i] - withdrawAmounts[i];
_cachedUintBalances[i] = newBal;
newQInternal[i] = _uintToInternalFloor(newBal, _bases[i]);
uint256 amount = withdrawAmounts[i];
if (amount > 0) {
_sendTokenTo(_tokens[i], receiver, amount, unwrap);
uint256 newBal = _cachedUintBalances[i] - amount;
_cachedUintBalances[i] = newBal;
newQInternal[i] = _uintToInternalFloor(newBal, _bases[i]);
if (newQInternal[i] != int128(0))
allZero = false;
}
unchecked { i++; }
}
// Apply proportional update or deinitialize if drained
bool allZero = true;
for (uint i = 0; i < n; ) {
if (newQInternal[i] != int128(0)) {
allZero = false;
break;
}
unchecked { i++; }
}
if (allZero) {
_lmsr.deinit();
} else {
@@ -226,6 +207,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Compute mint ratio in Q64.64: ratio = lpTokenAmount / totalSupply
int128 ratio = ABDKMath64x64.divu(lpTokenAmount, totalSupply);
require(ratio > 0, 'mint too small');
// depositAmount_i = ceil(ratio * currentBalance_i)
for (uint i = 0; i < numAssets; i++) {
@@ -248,11 +230,17 @@ contract PartyPoolMintImpl is PartyPoolBase {
}
int128 ratio = ABDKMath64x64.divu(lpTokenAmount, totalSupply);
require(ratio > 0, 'burn too small: tiny input');
bool nonZero = false;
for (uint i = 0; i < numAssets; i++) {
uint256 currentBalance = cachedUintBalances[i];
withdrawAmounts[i] = ratio.mulu(currentBalance);
uint256 amount = ratio.mulu(currentBalance);
withdrawAmounts[i] = amount;
if (amount > 0)
nonZero = true;
}
require(nonZero, 'burn too small: no output');
return withdrawAmounts;
}