styling consistency

This commit is contained in:
tim
2025-10-15 11:19:36 -04:00
parent 7ac4cdc8f6
commit ead004d631
18 changed files with 275 additions and 281 deletions

View File

@@ -27,27 +27,27 @@ contract PartyPoolMintImpl is PartyPoolBase {
function initialMint(address receiver, uint256 lpTokens, int128 KAPPA) external payable native nonReentrant
returns (uint256 lpMinted) {
uint256 n = tokens.length;
uint256 n = _tokens.length;
// Check if this is initial deposit - revert if not
bool isInitialDeposit = _totalSupply == 0 || lmsr.nAssets == 0;
bool isInitialDeposit = _totalSupply == 0 || _lmsr.nAssets == 0;
require(isInitialDeposit, "initialMint: pool already initialized");
// Update cached balances for all assets
int128[] memory newQInternal = new int128[](n);
uint256[] memory depositAmounts = new uint256[](n);
for (uint i = 0; i < n; ) {
uint256 bal = IERC20(tokens[i]).balanceOf(address(this));
cachedUintBalances[i] = bal;
newQInternal[i] = _uintToInternalFloor(bal, bases[i]);
uint256 bal = IERC20(_tokens[i]).balanceOf(address(this));
_cachedUintBalances[i] = bal;
newQInternal[i] = _uintToInternalFloor(bal, _bases[i]);
depositAmounts[i] = bal;
unchecked { i++; }
}
// Initialize the stabilized LMSR state with provided kappa
lmsr.init(newQInternal, KAPPA);
_lmsr.init(newQInternal, KAPPA);
// Compute actual LP tokens to mint based on size metric (scaled)
// Compute actual LP _tokens to mint based on size metric (scaled)
if( lpTokens != 0 )
lpMinted = lpTokens;
else {
@@ -68,24 +68,24 @@ contract PartyPoolMintImpl is PartyPoolBase {
function mint(address payer, address receiver, uint256 lpTokenAmount, uint256 deadline) external payable native nonReentrant
returns (uint256 lpMinted) {
require(deadline == 0 || block.timestamp <= deadline, "mint: deadline exceeded");
uint256 n = tokens.length;
uint256 n = _tokens.length;
// Check if this is NOT initial deposit - revert if it is
bool isInitialDeposit = _totalSupply == 0 || lmsr.nAssets == 0;
bool isInitialDeposit = _totalSupply == 0 || _lmsr.nAssets == 0;
require(!isInitialDeposit, "mint: use initialMint for pool initialization");
require(lpTokenAmount > 0, "mint: zero LP amount");
// Capture old pool size metric (scaled) by computing from current balances
int128 oldTotal = _computeSizeMetric(lmsr.qInternal);
int128 oldTotal = _computeSizeMetric(_lmsr.qInternal);
uint256 oldScaled = ABDKMath64x64.mulu(oldTotal, LP_SCALE);
// Calculate required deposit amounts for the desired LP tokens
uint256[] memory depositAmounts = mintAmounts(lpTokenAmount, lmsr.nAssets, _totalSupply, cachedUintBalances);
// Calculate required deposit amounts for the desired LP _tokens
uint256[] memory depositAmounts = mintAmounts(lpTokenAmount, _lmsr.nAssets, _totalSupply, _cachedUintBalances);
// Transfer in all token amounts
for (uint i = 0; i < n; ) {
if (depositAmounts[i] > 0) {
_receiveTokenFrom(payer, tokens[i], depositAmounts[i]);
_receiveTokenFrom(payer, _tokens[i], depositAmounts[i]);
}
unchecked { i++; }
}
@@ -93,16 +93,16 @@ contract PartyPoolMintImpl is PartyPoolBase {
// 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 newBal = _cachedUintBalances[i] + depositAmounts[i];
_cachedUintBalances[i] = newBal;
newQInternal[i] = _uintToInternalFloor(newBal, _bases[i]);
unchecked { i++; }
}
// Update for proportional change
lmsr.updateForProportionalChange(newQInternal);
_lmsr.updateForProportionalChange(newQInternal);
// Compute actual LP tokens to mint based on change in size metric (scaled)
// Compute actual LP _tokens to mint based on change in size metric (scaled)
// floor truncation rounds in favor of the pool
int128 newTotal = _computeSizeMetric(newQInternal);
uint256 newScaled = ABDKMath64x64.mulu(newTotal, LP_SCALE);
@@ -131,18 +131,18 @@ contract PartyPoolMintImpl is PartyPoolBase {
return actualLpToMint;
}
/// @notice Burn LP tokens and withdraw the proportional basket to receiver.
/// @dev Payer must own or approve the LP tokens being burned. The function updates LMSR state
/// @notice Burn LP _tokens and withdraw the proportional basket to receiver.
/// @dev Payer must own or approve the LP _tokens being burned. The function updates LMSR state
/// proportionally to reflect the reduced pool size after the withdrawal.
/// @param payer address that provides the LP tokens to burn
/// @param receiver address that receives the withdrawn tokens
/// @param lpAmount amount of LP tokens to burn (proportional withdrawal)
/// @param payer address that provides the LP _tokens to burn
/// @param receiver address that receives the withdrawn _tokens
/// @param lpAmount amount of LP _tokens to burn (proportional withdrawal)
/// @param deadline timestamp after which the transaction will revert. Pass 0 to ignore.
/// @param unwrap if true and the native token is being withdrawn, it is unwraped and sent as native currency
function burn(address payer, address receiver, uint256 lpAmount, uint256 deadline, bool unwrap) external nonReentrant
returns (uint256[] memory withdrawAmounts) {
require(deadline == 0 || block.timestamp <= deadline, "burn: deadline exceeded");
uint256 n = tokens.length;
uint256 n = _tokens.length;
require(lpAmount > 0, "burn: zero lp");
uint256 supply = _totalSupply;
@@ -151,12 +151,12 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Use cached balances; assume standard ERC20 transfers without external interference
// Compute proportional withdrawal amounts for the requested LP amount (rounded down)
withdrawAmounts = burnAmounts(lpAmount, lmsr.nAssets, _totalSupply, cachedUintBalances);
withdrawAmounts = burnAmounts(lpAmount, _lmsr.nAssets, _totalSupply, _cachedUintBalances);
// Transfer underlying tokens out to receiver according to computed proportions
// 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);
_sendTokenTo(_tokens[i], receiver, withdrawAmounts[i], unwrap);
}
unchecked { i++; }
}
@@ -164,9 +164,9 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Update cached balances and internal q for all assets using computed withdrawals
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 newBal = _cachedUintBalances[i] - withdrawAmounts[i];
_cachedUintBalances[i] = newBal;
newQInternal[i] = _uintToInternalFloor(newBal, _bases[i]);
unchecked { i++; }
}
@@ -181,9 +181,9 @@ contract PartyPoolMintImpl is PartyPoolBase {
}
if (allZero) {
lmsr.deinit();
_lmsr.deinit();
} else {
lmsr.updateForProportionalChange(newQInternal);
_lmsr.updateForProportionalChange(newQInternal);
}
// Burn exactly the requested LP amount from payer (authorization via allowance)
@@ -199,9 +199,9 @@ contract PartyPoolMintImpl is PartyPoolBase {
/// @notice Calculate the proportional deposit amounts required for a given LP token amount
/// @dev Returns the minimum token amounts (rounded up) that must be supplied to receive lpTokenAmount
/// LP tokens at current pool proportions. If the pool is empty (initial deposit) returns zeros
/// because the initial deposit is handled by transferring tokens then calling mint().
/// @param lpTokenAmount The amount of LP tokens desired
/// LP _tokens at current pool proportions. If the pool is empty (initial deposit) returns zeros
/// because the initial deposit is handled by transferring _tokens then calling mint().
/// @param lpTokenAmount The amount of LP _tokens desired
/// @return depositAmounts Array of token amounts to deposit (rounded up)
function mintAmounts(uint256 lpTokenAmount,
uint256 numAssets, uint256 totalSupply, uint256[] memory cachedUintBalances) public pure
@@ -209,7 +209,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
depositAmounts = new uint256[](numAssets);
// If this is the first mint or pool is empty, return zeros
// For first mint, tokens should already be transferred to the pool
// For first mint, _tokens should already be transferred to the pool
if (totalSupply == 0 || numAssets == 0) {
return depositAmounts; // Return zeros, initial deposit handled differently
}
@@ -256,11 +256,11 @@ contract PartyPoolMintImpl is PartyPoolBase {
/// @param maxAmountIn maximum amount of token to deposit (inclusive of fee)
/// @param swapFeePpm fee in parts-per-million
/// @param lmsrState current LMSR state
/// @param bases_ scaling bases for each token
/// @param bases_ scaling _bases for each token
/// @param totalSupply_ current total LP token supply
/// @return amountInUsed actual input amount used (excluding fee)
/// @return fee fee amount charged
/// @return lpMinted LP tokens that would be minted
/// @return lpMinted LP _tokens that would be minted
function swapMintAmounts(
uint256 inputTokenIndex,
uint256 maxAmountIn,
@@ -311,7 +311,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
uint256 newScaled = ABDKMath64x64.mulu(newTotal, LP_SCALE);
if (totalSupply_ == 0) {
// If somehow supply zero (shouldn't happen as lmsr.nAssets>0), mint newScaled
// If somehow supply zero (shouldn't happen as _lmsr.nAssets>0), mint newScaled
lpMinted = newScaled;
} else {
require(oldScaled > 0, "swapMintAmounts: oldScaled zero");
@@ -331,7 +331,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
/// @dev swapMint executes as an exact-in planned swap followed by proportional scaling of qInternal.
/// The function emits SwapMint (gross, net, fee) and also emits Mint for LP issuance.
/// @param payer who transfers the input token
/// @param receiver who receives the minted LP tokens
/// @param receiver who receives the minted LP _tokens
/// @param inputTokenIndex index of the input token
/// @param maxAmountIn maximum uint token input (inclusive of fee)
/// @param deadline optional deadline
@@ -346,24 +346,24 @@ contract PartyPoolMintImpl is PartyPoolBase {
uint256 swapFeePpm,
uint256 protocolFeePpm
) external payable native nonReentrant returns (uint256 lpMinted) {
uint256 n = tokens.length;
uint256 n = _tokens.length;
require(inputTokenIndex < n, "swapMint: idx");
require(maxAmountIn > 0, "swapMint: input zero");
require(deadline == 0 || block.timestamp <= deadline, "swapMint: deadline");
require(lmsr.nAssets > 0, "swapMint: uninit pool");
require(_lmsr.nAssets > 0, "swapMint: uninit pool");
// compute fee on gross maxAmountIn to get an initial net estimate (we'll recompute based on actual used)
(, uint256 netUintGuess) = _computeFee(maxAmountIn, swapFeePpm);
// Convert the net guess to internal (floor)
int128 netInternalGuess = _uintToInternalFloor(netUintGuess, bases[inputTokenIndex]);
int128 netInternalGuess = _uintToInternalFloor(netUintGuess, _bases[inputTokenIndex]);
require(netInternalGuess > int128(0), "swapMint: input too small after fee");
// Use LMSR view to determine actual internal consumed and size-increase (ΔS) for mint
(int128 amountInInternalUsed, int128 sizeIncreaseInternal) = lmsr.swapAmountsForMint(inputTokenIndex, netInternalGuess);
(int128 amountInInternalUsed, int128 sizeIncreaseInternal) = _lmsr.swapAmountsForMint(inputTokenIndex, netInternalGuess);
// amountInInternalUsed may be <= netInternalGuess. Convert to uint (ceil) to determine actual transfer
uint256 amountInUint = _internalToUintCeil(amountInInternalUsed, bases[inputTokenIndex]);
uint256 amountInUint = _internalToUintCeil(amountInInternalUsed, _bases[inputTokenIndex]);
require(amountInUint > 0, "swapMint: input zero after internal conversion");
// Compute fee on the actual used input and total transfer amount (ceiling)
@@ -371,22 +371,22 @@ contract PartyPoolMintImpl is PartyPoolBase {
uint256 totalTransfer = amountInUint + feeUintActual;
require(totalTransfer > 0 && totalTransfer <= maxAmountIn, "swapMint: transfer exceeds max");
// Transfer tokens from payer (assume standard ERC20 without transfer fees) via helper
_receiveTokenFrom(payer, tokens[inputTokenIndex], totalTransfer);
// Transfer _tokens from payer (assume standard ERC20 without transfer fees) via helper
_receiveTokenFrom(payer, _tokens[inputTokenIndex], totalTransfer);
// Accrue protocol share (floor) from the fee on the input token
uint256 protoShare = 0;
if (protocolFeePpm > 0 && feeUintActual > 0) {
protoShare = (feeUintActual * protocolFeePpm) / 1_000_000;
if (protoShare > 0) {
protocolFeesOwed[inputTokenIndex] += protoShare;
_protocolFeesOwed[inputTokenIndex] += protoShare;
}
}
// Update cached effective balance directly: add totalTransfer minus protocol share
cachedUintBalances[inputTokenIndex] += (totalTransfer - protoShare);
_cachedUintBalances[inputTokenIndex] += (totalTransfer - protoShare);
// Compute old and new scaled size metrics to determine LP minted
int128 oldTotal = _computeSizeMetric(lmsr.qInternal);
int128 oldTotal = _computeSizeMetric(_lmsr.qInternal);
uint256 oldScaled = ABDKMath64x64.mulu(oldTotal, LP_SCALE);
int128 newTotal = oldTotal.add(sizeIncreaseInternal);
@@ -396,7 +396,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Use natural ERC20 function since base contract inherits from ERC20
uint256 currentSupply = _totalSupply;
if (currentSupply == 0) {
// If somehow supply zero (shouldn't happen as lmsr.nAssets>0), mint newScaled
// If somehow supply zero (shouldn't happen as _lmsr.nAssets>0), mint newScaled
actualLpToMint = newScaled;
} else {
uint256 delta = (newScaled > oldScaled) ? (newScaled - oldScaled) : 0;
@@ -414,11 +414,11 @@ contract PartyPoolMintImpl is PartyPoolBase {
int128[] memory newQInternal = new int128[](n);
for (uint256 idx = 0; idx < n; idx++) {
// newQInternal[idx] = qInternal[idx] * (newTotal / oldTotal)
newQInternal[idx] = lmsr.qInternal[idx].mul(newTotal).div(oldTotal);
newQInternal[idx] = _lmsr.qInternal[idx].mul(newTotal).div(oldTotal);
}
// Update cached internal and kappa via updateForProportionalChange
lmsr.updateForProportionalChange(newQInternal);
_lmsr.updateForProportionalChange(newQInternal);
// Use natural ERC20 function since base contract inherits from ERC20
_mint(receiver, actualLpToMint);
@@ -435,11 +435,11 @@ contract PartyPoolMintImpl is PartyPoolBase {
/// @notice Calculate the amounts for a burn swap operation
/// @dev This is a pure view function that computes burn swap amounts from provided state
/// @param lpAmount amount of LP tokens to burn
/// @param lpAmount amount of LP _tokens to burn
/// @param inputTokenIndex index of target asset to receive
/// @param swapFeePpm fee in parts-per-million
/// @param lmsrState current LMSR state
/// @param bases_ scaling bases for each token
/// @param bases_ scaling _bases for each token
/// @param totalSupply_ current total LP token supply
/// @return amountOut amount of target asset that would be received
function burnSwapAmounts(
@@ -467,11 +467,11 @@ contract PartyPoolMintImpl is PartyPoolBase {
require(amountOut > 0, "burnSwapAmounts: output zero");
}
/// @notice Burn LP tokens then swap the redeemed proportional basket into a single asset `inputTokenIndex` and send to receiver.
/// @dev The function burns LP tokens (authorization via allowance if needed), sends the single-asset payout and updates LMSR state.
/// @param payer who burns LP tokens
/// @notice Burn LP _tokens then swap the redeemed proportional basket into a single asset `inputTokenIndex` and send to receiver.
/// @dev The function burns LP _tokens (authorization via allowance if needed), sends the single-asset payout and updates LMSR state.
/// @param payer who burns LP _tokens
/// @param receiver who receives the single asset
/// @param lpAmount amount of LP tokens to burn
/// @param lpAmount amount of LP _tokens to burn
/// @param inputTokenIndex index of target asset to receive
/// @param deadline optional deadline
/// @param swapFeePpm fee in parts-per-million for this pool (may be used for future fee logic)
@@ -486,7 +486,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
uint256 swapFeePpm,
uint256 protocolFeePpm
) external nonReentrant returns (uint256 amountOutUint) {
uint256 n = tokens.length;
uint256 n = _tokens.length;
require(inputTokenIndex < n, "burnSwap: idx");
require(lpAmount > 0, "burnSwap: zero lp");
require(deadline == 0 || block.timestamp <= deadline, "burnSwap: deadline");
@@ -499,16 +499,16 @@ contract PartyPoolMintImpl is PartyPoolBase {
.mul(ABDKMath64x64.divu(1000000-swapFeePpm, 1000000)); // adjusted for fee
// Use LMSR view to compute single-asset payout and burned size-metric
(int128 payoutInternal, ) = lmsr.swapAmountsForBurn(inputTokenIndex, alpha);
(int128 payoutInternal, ) = _lmsr.swapAmountsForBurn(inputTokenIndex, alpha);
// Convert payoutInternal -> uint (floor) to favor pool
amountOutUint = _internalToUintFloor(payoutInternal, bases[inputTokenIndex]);
amountOutUint = _internalToUintFloor(payoutInternal, _bases[inputTokenIndex]);
require(amountOutUint > 0, "burnSwap: output zero");
// Compute gross payout (no swap fee) so we can determine token-side fee = gross - net
int128 alphaGross = ABDKMath64x64.divu(lpAmount, supply); // gross fraction (no swap fee)
(int128 payoutGrossInternal, ) = lmsr.swapAmountsForBurn(inputTokenIndex, alphaGross);
uint256 payoutGrossUint = _internalToUintFloor(payoutGrossInternal, bases[inputTokenIndex]);
(int128 payoutGrossInternal, ) = _lmsr.swapAmountsForBurn(inputTokenIndex, alphaGross);
uint256 payoutGrossUint = _internalToUintFloor(payoutGrossInternal, _bases[inputTokenIndex]);
uint256 feeTokenUint = (payoutGrossUint > amountOutUint) ? (payoutGrossUint - amountOutUint) : 0;
// Accrue protocol share (floor) from the token-side fee
@@ -516,14 +516,14 @@ contract PartyPoolMintImpl is PartyPoolBase {
if (protocolFeePpm > 0 && feeTokenUint > 0) {
protoShare = (feeTokenUint * protocolFeePpm) / 1_000_000;
if (protoShare > 0) {
protocolFeesOwed[inputTokenIndex] += protoShare;
_protocolFeesOwed[inputTokenIndex] += protoShare;
}
}
// Transfer the payout to receiver via centralized helper
_sendTokenTo(tokens[inputTokenIndex], receiver, amountOutUint, unwrap);
_sendTokenTo(_tokens[inputTokenIndex], receiver, amountOutUint, unwrap);
// Burn LP tokens from payer (authorization via allowance)
// Burn LP _tokens from payer (authorization via allowance)
if (msg.sender != payer) {
uint256 allowed = _allowances[payer][msg.sender];
_approve(payer, msg.sender, allowed - lpAmount);
@@ -533,13 +533,13 @@ contract PartyPoolMintImpl is PartyPoolBase {
// Update cached balances using computed payout and protocol fee; no on-chain reads
int128[] memory newQInternal = new int128[](n);
for (uint256 idx = 0; idx < n; idx++) {
uint256 newBal = cachedUintBalances[idx];
uint256 newBal = _cachedUintBalances[idx];
if (idx == inputTokenIndex) {
// Effective LP balance decreases by net payout and increased protocol owed
newBal = newBal - amountOutUint - protoShare;
}
cachedUintBalances[idx] = newBal;
newQInternal[idx] = _uintToInternalFloor(newBal, bases[idx]);
_cachedUintBalances[idx] = newBal;
newQInternal[idx] = _uintToInternalFloor(newBal, _bases[idx]);
}
// Emit BurnSwap with public-facing info only (do not expose ΔS or LP burned)
@@ -551,9 +551,9 @@ contract PartyPoolMintImpl is PartyPoolBase {
if (newQInternal[idx] != int128(0)) { allZero = false; break; }
}
if (allZero) {
lmsr.deinit();
_lmsr.deinit();
} else {
lmsr.updateForProportionalChange(newQInternal);
_lmsr.updateForProportionalChange(newQInternal);
}
emit IPartyPool.Burn(payer, receiver, new uint256[](n), lpAmount);