This commit is contained in:
tim
2025-09-18 19:23:31 -04:00
parent 19f14c6a95
commit bd210b56ff
5 changed files with 491 additions and 220 deletions

View File

@@ -505,136 +505,6 @@ contract PartyPoolTest is Test {
assertEq(token1.balanceOf(bob) >= amountOut, true);
}
/// @notice Gas measurement: perform 100 swaps back-and-forth between token0 and token1.
function testSwapGas3() public {
// Ensure alice approves pool for both tokens
vm.prank(alice);
token0.approve(address(pool), type(uint256).max);
vm.prank(alice);
token1.approve(address(pool), type(uint256).max);
uint256 maxIn = 1_000;
// Perform 100 swaps alternating directions to avoid large imbalance
for (uint256 i = 0; i < 100; i++) {
vm.prank(alice);
if (i % 2 == 0) {
// swap token0 -> token1
pool.swap(alice, alice, 0, 1, maxIn, 0, 0);
} else {
// swap token1 -> token0
pool.swap(alice, alice, 1, 0, maxIn, 0, 0);
}
}
}
/// @notice Gas measurement: perform 100 swaps back-and-forth between token0 and token1 in the 10-token pool.
function testSwapGas10() public {
// Ensure alice approves pool10 for both tokens
vm.prank(alice);
token0.approve(address(pool10), type(uint256).max);
vm.prank(alice);
token1.approve(address(pool10), type(uint256).max);
uint256 maxIn = 1_000;
// Perform 100 swaps alternating directions to avoid large imbalance
for (uint256 i = 0; i < 100; i++) {
vm.prank(alice);
if (i % 2 == 0) {
// swap token0 -> token1
pool10.swap(alice, alice, 0, 1, maxIn, 0, 0);
} else {
// swap token1 -> token0
pool10.swap(alice, alice, 1, 0, maxIn, 0, 0);
}
}
}
/// @notice Gas-style test: alternate swapMint then burnSwap on the 3-token pool to keep pool size roughly stable.
function testSwapMintBurnSwapGas3() public {
uint256 iterations = 100;
uint256 input = 1_000;
// Top up alice so repeated operations won't fail
token0.mint(alice, iterations * input * 2);
vm.startPrank(alice);
token0.approve(address(pool), type(uint256).max);
for (uint256 k = 0; k < iterations; k++) {
// Mint LP by providing single-token input; receive LP minted
uint256 minted = pool.swapMint(alice, alice, 0, input, 0);
// If nothing minted (numerical edge), skip burn step
if (minted == 0) continue;
// Immediately burn the minted LP back to tokens, targeting the same token index
pool.burnSwap(alice, alice, minted, 0, 0);
}
vm.stopPrank();
}
/// @notice Gas-style test: alternate swapMint then burnSwap on the 10-token pool to keep pool size roughly stable.
function testSwapMintBurnSwapGas10() public {
uint256 iterations = 100;
uint256 input = 1_000;
// Top up alice so repeated operations won't fail
token0.mint(alice, iterations * input * 2);
vm.startPrank(alice);
token0.approve(address(pool10), type(uint256).max);
for (uint256 k = 0; k < iterations; k++) {
uint256 minted = pool10.swapMint(alice, alice, 0, input, 0);
if (minted == 0) continue;
pool10.burnSwap(alice, alice, minted, 0, 0);
}
vm.stopPrank();
}
/// @notice Combined gas test (mint then burn) on 3-token pool using mint() and burn().
/// Alternates minting a tiny LP amount and immediately burning the actual minted LP back to avoid net pool depletion.
function testMintBurnGas3() public {
uint256 iterations = 50;
uint256 input = 1_000;
// Ensure alice has enough tokens for all mints
token0.mint(alice, iterations * input * 2);
token1.mint(alice, iterations * input * 2);
token2.mint(alice, iterations * input * 2);
vm.startPrank(alice);
// Approve pool to transfer tokens for proportional mint
token0.approve(address(pool), type(uint256).max);
token1.approve(address(pool), type(uint256).max);
token2.approve(address(pool), type(uint256).max);
for (uint256 k = 0; k < iterations; k++) {
// Request a tiny LP mint (1 wei) - pool will compute deposits and transfer from alice
uint256 lpRequest = 1;
// Snapshot alice LP before to compute actual minted
uint256 lpBefore = pool.balanceOf(alice);
// Perform mint; this will transfer underlying from alice into pool
pool.mint(alice, alice, lpRequest, 0);
uint256 lpAfter = pool.balanceOf(alice);
uint256 actualMinted = lpAfter - lpBefore;
// If nothing minted due to rounding edge, skip burn
if (actualMinted == 0) {
continue;
}
// Burn via plain burn() which will transfer underlying back to alice and burn LP
pool.burn(alice, alice, actualMinted, 0);
}
vm.stopPrank();
}
/// @notice Verify mintDepositAmounts matches the actual token transfers performed by mint()
function testMintDepositAmountsMatchesMint_3TokenPool() public {
@@ -869,55 +739,6 @@ contract PartyPoolTest is Test {
}
}
/// @notice Combined gas test (mint then burn) on 10-token pool using mint() and burn().
/// Alternates small mints and burns to keep the pool size roughly stable.
function testMintBurnGas10() public {
uint256 iterations = 50;
uint256 input = 1_000;
// Ensure alice has enough tokens for all mints across 10 tokens
for (uint i = 0; i < 10; i++) {
// mint to alice corresponding token; use token0..token9 mapping in setUp ordering
if (i == 0) token0.mint(alice, iterations * input * 2);
else if (i == 1) token1.mint(alice, iterations * input * 2);
else if (i == 2) token2.mint(alice, iterations * input * 2);
else if (i == 3) token3.mint(alice, iterations * input * 2);
else if (i == 4) token4.mint(alice, iterations * input * 2);
else if (i == 5) token5.mint(alice, iterations * input * 2);
else if (i == 6) token6.mint(alice, iterations * input * 2);
else if (i == 7) token7.mint(alice, iterations * input * 2);
else if (i == 8) token8.mint(alice, iterations * input * 2);
else if (i == 9) token9.mint(alice, iterations * input * 2);
}
vm.startPrank(alice);
// Approve pool10 to transfer tokens for proportional mint
token0.approve(address(pool10), type(uint256).max);
token1.approve(address(pool10), type(uint256).max);
token2.approve(address(pool10), type(uint256).max);
token3.approve(address(pool10), type(uint256).max);
token4.approve(address(pool10), type(uint256).max);
token5.approve(address(pool10), type(uint256).max);
token6.approve(address(pool10), type(uint256).max);
token7.approve(address(pool10), type(uint256).max);
token8.approve(address(pool10), type(uint256).max);
token9.approve(address(pool10), type(uint256).max);
for (uint256 k = 0; k < iterations; k++) {
uint256 lpRequest = 1;
uint256 lpBefore = pool10.balanceOf(alice);
pool10.mint(alice, alice, lpRequest, 0);
uint256 lpAfter = pool10.balanceOf(alice);
uint256 actualMinted = lpAfter - lpBefore;
if (actualMinted == 0) continue;
pool10.burn(alice, alice, actualMinted, 0);
}
vm.stopPrank();
}
/// @notice Basic test for swapMint: single-token deposit -> LP minted
function testSwapMintBasic() public {
@@ -1389,39 +1210,4 @@ contract PartyPoolTest is Test {
pool.flash(alice, wrongLengthAmounts, "");
}
/// @notice Gas measurement: flash with single token
function testFlashGasSingleToken() public {
FlashBorrower borrower = setupFlashBorrower();
// Configure borrower
borrower.setAction(FlashBorrower.Action.NORMAL, alice);
// Create loan request for single token
uint256[] memory amounts = new uint256[](3);
amounts[0] = 1000;
// Execute flash loan 10 times to measure gas
for (uint256 i = 0; i < 10; i++) {
borrower.flash(amounts);
}
}
/// @notice Gas measurement: flash with multiple tokens
function testFlashGasMultipleTokens() public {
FlashBorrower borrower = setupFlashBorrower();
// Configure borrower
borrower.setAction(FlashBorrower.Action.NORMAL, alice);
// Create loan request for multiple tokens
uint256[] memory amounts = new uint256[](3);
amounts[0] = 1000;
amounts[1] = 2000;
amounts[2] = 3000;
// Execute flash loan 10 times to measure gas
for (uint256 i = 0; i < 10; i++) {
borrower.flash(amounts);
}
}
}