burnAmounts

This commit is contained in:
tim
2025-10-01 15:17:48 -04:00
parent 40e1d25e72
commit 3a5f0842b3
3 changed files with 31 additions and 79 deletions

View File

@@ -43,7 +43,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
uint256 oldScaled = ABDKMath64x64.mulu(oldTotal, LP_SCALE);
// Calculate required deposit amounts for the desired LP tokens
uint256[] memory depositAmounts = _mintAmounts(lpTokenAmount, lmsr.nAssets, totalSupply());
uint256[] memory depositAmounts = mintAmounts(lpTokenAmount, lmsr.nAssets, totalSupply());
// Transfer in all token amounts
for (uint i = 0; i < n; ) {
@@ -101,7 +101,8 @@ contract PartyPoolMintImpl is PartyPoolBase {
/// @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.
function burn(address payer, address receiver, uint256 lpAmount, uint256 deadline) external {
function burn(address payer, address receiver, uint256 lpAmount, uint256 deadline) external
returns (uint256[] memory withdrawAmounts) {
require(deadline == 0 || block.timestamp <= deadline, "burn: deadline exceeded");
uint256 n = tokens.length;
require(lpAmount > 0, "burn: zero lp");
@@ -119,7 +120,7 @@ contract PartyPoolMintImpl is PartyPoolBase {
}
// Compute proportional withdrawal amounts for the requested LP amount (rounded down)
uint256[] memory withdrawAmounts = _burnAmounts(lpAmount);
withdrawAmounts = burnAmounts(lpAmount, lmsr.nAssets, totalSupply(), cachedUintBalances);
// Transfer underlying tokens out to receiver according to computed proportions
for (uint i = 0; i < n; ) {
@@ -165,11 +166,8 @@ contract PartyPoolMintImpl is PartyPoolBase {
emit Burn(payer, receiver, withdrawAmounts, lpAmount);
}
function mintAmounts(uint256 lpTokenAmount, uint256 numAssets, uint256 totalSupply) public view returns (uint256[] memory depositAmounts) {
return _mintAmounts(lpTokenAmount, numAssets, totalSupply);
}
function _mintAmounts(uint256 lpTokenAmount, uint256 numAssets, uint256 totalSupply) internal view returns (uint256[] memory depositAmounts) {
function mintAmounts(uint256 lpTokenAmount, uint256 numAssets, uint256 totalSupply) public view
returns (uint256[] memory depositAmounts) {
depositAmounts = new uint256[](numAssets);
// If this is the first mint or pool is empty, return zeros
@@ -190,23 +188,20 @@ contract PartyPoolMintImpl is PartyPoolBase {
return depositAmounts;
}
/// @notice Internal helper to calculate withdrawal amounts for burning LP tokens
function _burnAmounts(uint256 lpTokenAmount) internal view returns (uint256[] memory withdrawAmounts) {
uint256 n = tokens.length;
withdrawAmounts = new uint256[](n);
function burnAmounts(uint256 lpTokenAmount,
uint256 numAssets, uint256 totalSupply, uint256[] memory cachedUintBalances) public view
returns (uint256[] memory withdrawAmounts) {
withdrawAmounts = new uint256[](numAssets);
// If supply is zero or pool uninitialized, return zeros
if (totalSupply() == 0 || lmsr.nAssets == 0) {
if (totalSupply == 0 || numAssets == 0) {
return withdrawAmounts; // Return zeros, nothing to withdraw
}
// Calculate withdrawal amounts based on current proportions
uint256 totalLpSupply = totalSupply();
// withdrawAmount = floor(lpTokenAmount * currentBalance / totalLpSupply)
for (uint i = 0; i < n; i++) {
for (uint i = 0; i < numAssets; i++) {
uint256 currentBalance = cachedUintBalances[i];
withdrawAmounts[i] = (lpTokenAmount * currentBalance) / totalLpSupply;
withdrawAmounts[i] = (lpTokenAmount * currentBalance) / totalSupply;
}
return withdrawAmounts;