burnAmounts
This commit is contained in:
@@ -126,40 +126,6 @@ contract PartyPool is PartyPoolBase, IPartyPool {
|
||||
---------------------- */
|
||||
|
||||
/// @inheritdoc IPartyPool
|
||||
function mintAmounts(uint256 lpTokenAmount) public view returns (uint256[] memory depositAmounts) {
|
||||
return _mintAmounts(lpTokenAmount);
|
||||
}
|
||||
|
||||
function _mintAmounts(uint256 lpTokenAmount) internal view returns (uint256[] memory depositAmounts) {
|
||||
uint256 n = tokens.length;
|
||||
depositAmounts = new uint256[](n);
|
||||
|
||||
// If this is the first mint or pool is empty, return zeros
|
||||
// For first mint, tokens should already be transferred to the pool
|
||||
if (totalSupply() == 0 || lmsr.nAssets == 0) {
|
||||
return depositAmounts; // Return zeros, initial deposit handled differently
|
||||
}
|
||||
|
||||
// Calculate deposit based on current proportions
|
||||
uint256 totalLpSupply = totalSupply();
|
||||
|
||||
// lpTokenAmount / totalLpSupply = depositAmount / currentBalance
|
||||
// Therefore: depositAmount = (lpTokenAmount * currentBalance) / totalLpSupply
|
||||
// We round up to protect the pool
|
||||
for (uint i = 0; i < n; i++) {
|
||||
uint256 currentBalance = cachedUintBalances[i];
|
||||
// Calculate with rounding up: (a * b + c - 1) / c
|
||||
depositAmounts[i] = (lpTokenAmount * currentBalance + totalLpSupply - 1) / totalLpSupply;
|
||||
}
|
||||
|
||||
return depositAmounts;
|
||||
}
|
||||
|
||||
/// @notice Initial mint to set up pool for the first time.
|
||||
/// @dev Assumes tokens have already been transferred to the pool prior to calling.
|
||||
/// Can only be called when the pool is uninitialized (totalSupply() == 0 or lmsr.nAssets == 0).
|
||||
/// @param receiver address that receives the LP tokens
|
||||
/// @param lpTokens The number of LP tokens to issue for this mint. If 0, then the number of tokens returned will equal the LMSR internal q total
|
||||
function initialMint(address receiver, uint256 lpTokens) external nonReentrant
|
||||
returns (uint256 lpMinted) {
|
||||
uint256 n = tokens.length;
|
||||
@@ -195,6 +161,11 @@ contract PartyPool is PartyPoolBase, IPartyPool {
|
||||
emit Mint(address(0), receiver, depositAmounts, lpMinted);
|
||||
}
|
||||
|
||||
/// @inheritdoc IPartyPool
|
||||
function mintAmounts(uint256 lpTokenAmount) public view returns (uint256[] memory depositAmounts) {
|
||||
return MINT_IMPL.mintAmounts(lpTokenAmount, lmsr.nAssets, totalSupply());
|
||||
}
|
||||
|
||||
/// @notice Proportional mint for existing pool.
|
||||
/// @dev This function forwards the call to the mint implementation via delegatecall
|
||||
/// @param payer address that provides the input tokens
|
||||
@@ -210,35 +181,13 @@ contract PartyPool is PartyPoolBase, IPartyPool {
|
||||
lpTokenAmount,
|
||||
deadline
|
||||
);
|
||||
|
||||
bytes memory result = Address.functionDelegateCall(address(MINT_IMPL), data);
|
||||
return abi.decode(result, (uint256));
|
||||
}
|
||||
|
||||
/// @inheritdoc IPartyPool
|
||||
function burnAmounts(uint256 lpTokenAmount) external view returns (uint256[] memory withdrawAmounts) {
|
||||
return _burnAmounts(lpTokenAmount);
|
||||
}
|
||||
|
||||
function _burnAmounts(uint256 lpTokenAmount) internal view returns (uint256[] memory withdrawAmounts) {
|
||||
uint256 n = tokens.length;
|
||||
withdrawAmounts = new uint256[](n);
|
||||
|
||||
// If supply is zero or pool uninitialized, return zeros
|
||||
if (totalSupply() == 0 || lmsr.nAssets == 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++) {
|
||||
uint256 currentBalance = cachedUintBalances[i];
|
||||
withdrawAmounts[i] = (lpTokenAmount * currentBalance) / totalLpSupply;
|
||||
}
|
||||
|
||||
return withdrawAmounts;
|
||||
return MINT_IMPL.burnAmounts(lpTokenAmount, lmsr.nAssets, totalSupply(), cachedUintBalances);
|
||||
}
|
||||
|
||||
/// @notice Burn LP tokens and withdraw the proportional basket to receiver.
|
||||
@@ -247,7 +196,8 @@ contract PartyPool is PartyPoolBase, IPartyPool {
|
||||
/// @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 nonReentrant {
|
||||
function burn(address payer, address receiver, uint256 lpAmount, uint256 deadline) external nonReentrant
|
||||
returns (uint256[] memory withdrawAmounts) {
|
||||
bytes memory data = abi.encodeWithSignature(
|
||||
"burn(address,address,uint256,uint256)",
|
||||
payer,
|
||||
@@ -255,8 +205,8 @@ contract PartyPool is PartyPoolBase, IPartyPool {
|
||||
lpAmount,
|
||||
deadline
|
||||
);
|
||||
|
||||
Address.functionDelegateCall(address(MINT_IMPL), data);
|
||||
bytes memory result = Address.functionDelegateCall(address(MINT_IMPL), data);
|
||||
return abi.decode(result, (uint256[]));
|
||||
}
|
||||
|
||||
/* ----------------------
|
||||
|
||||
Reference in New Issue
Block a user