LMSRStabilized pure refactor; swapMintAmounts

This commit is contained in:
tim
2025-10-01 17:08:02 -04:00
parent a6f6fd034c
commit 5a2e7039d1
4 changed files with 417 additions and 87 deletions

View File

@@ -128,37 +128,14 @@ contract PartyPool is PartyPoolBase, IPartyPool {
/// @inheritdoc IPartyPool
function initialMint(address receiver, uint256 lpTokens) external nonReentrant
returns (uint256 lpMinted) {
uint256 n = tokens.length;
// Check if this is initial deposit - revert if not
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]);
depositAmounts[i] = bal;
unchecked { i++; }
}
// Initialize the stabilized LMSR state with provided kappa
lmsr.init(newQInternal, KAPPA);
// Compute actual LP tokens to mint based on size metric (scaled)
if( lpTokens != 0 )
lpMinted = lpTokens;
else {
int128 newTotal = _computeSizeMetric(newQInternal);
lpMinted = ABDKMath64x64.mulu(newTotal, LP_SCALE);
}
require(lpMinted > 0, "initialMint: zero LP amount");
_mint(receiver, lpMinted);
emit Mint(address(0), receiver, depositAmounts, lpMinted);
bytes memory data = abi.encodeWithSignature(
"initialMint(address,uint256,int128)",
receiver,
lpTokens,
KAPPA
);
bytes memory result = Address.functionDelegateCall(address(MINT_IMPL), data);
return abi.decode(result, (uint256));
}
/// @inheritdoc IPartyPool
@@ -460,11 +437,29 @@ contract PartyPool is PartyPoolBase, IPartyPool {
return netUint + fee;
}
// --- New events for single-token mint/burn flows ---
// Note: events intentionally avoid exposing internal ΔS and avoid duplicating LP mint/burn data
// which is already present in the standard Mint/Burn events.
function swapMintAmounts(uint256 inputTokenIndex, uint256 maxAmountIn) external view
returns (uint256 amountInUsed, uint256 fee, uint256 lpMinted) {
return SWAP_MINT_IMPL.swapMintAmounts(
inputTokenIndex,
maxAmountIn,
SWAP_FEE_PPM,
lmsr,
bases,
totalSupply()
);
}
// todo swapMintAmounts and burnSwapAmounts
function burnSwapAmounts(uint256 lpAmount, uint256 inputTokenIndex) external view
returns (uint256 amountOut) {
return SWAP_MINT_IMPL.burnSwapAmounts(
lpAmount,
inputTokenIndex,
SWAP_FEE_PPM,
lmsr,
bases,
totalSupply()
);
}
/// @notice Single-token mint: deposit a single token, charge swap-LMSR cost, and mint LP.
/// @dev This function forwards the call to the swapMint implementation via delegatecall