price() and poolPrice() fixes

This commit is contained in:
tim
2025-11-06 15:04:01 -04:00
parent 2b941e0969
commit de108cc1e4
4 changed files with 67 additions and 31 deletions

View File

@@ -176,7 +176,7 @@ contract PartyPoolTest is Test {
token2.transfer(address(pool), INIT_BAL);
// Perform initial mint (initial deposit); receiver is this contract
pool.initialMint(address(this), 0);
pool.initialMint(address(this), INIT_BAL * tokens.length * 10**18);
// Set up pool10 with 10 _tokens
IERC20[] memory tokens10 = new IERC20[](10);
@@ -1124,5 +1124,52 @@ contract PartyPoolTest is Test {
vm.stopPrank();
}
/// @notice Verify that the initial relative price between token0 and token1 is 1.0000000
function testInitialPriceIsOne() public view {
// Query the viewer for the relative price between token index 0 and 1
int128 price = viewer.price(pool, 0, 1);
// Expected price is 1.0 in ABDK 64.64 fixed point
int128 expected = ABDKMath64x64.fromInt(1);
// Cast int128 to uint128 then to uint256 for assertEq (values are non-negative)
assertEq(uint256(uint128(price)), uint256(uint128(expected)), "Initial relative price must be 1.0000000");
}
/// @notice Verify that the initial LP price in terms of token0 is 1.0000000
function testInitialPoolPriceIsOne() public {
// Query the viewer for the pool price for token0
int128 price = viewer.poolPrice(pool, 0);
// Expected price is 1.0 in ABDK 64.64 fixed point
int128 expected = ABDKMath64x64.fromInt(1);
// Cast int128 to uint128 then to uint256 for assertEq (values are non-negative)
assertEq(uint256(uint128(price)), uint256(uint128(expected)), "Initial pool price must be 1.0000000");
// Mint a small amount of LP into the pool from alice and verify price remains 1.0
vm.startPrank(alice);
// Approve tokens for pool to pull
token0.approve(address(pool), type(uint256).max);
token1.approve(address(pool), type(uint256).max);
token2.approve(address(pool), type(uint256).max);
// Choose a small LP request (1% of supply or at least 1)
uint256 lpRequest = pool.totalSupply() / 100;
if (lpRequest == 0) lpRequest = 1;
// Compute required deposits and perform mint if not trivial
uint256[] memory deposits = viewer.mintAmounts(pool, lpRequest);
bool allZero = true;
for (uint i = 0; i < deposits.length; i++) { if (deposits[i] != 0) { allZero = false; break; } }
if (!allZero) {
pool.mint(alice, alice, lpRequest, 0);
}
vm.stopPrank();
// Re-query the pool price and ensure it remains 1.0 (within exact fixed-point equality)
int128 priceAfter = viewer.poolPrice(pool, 0);
assertEq(uint256(uint128(priceAfter)), uint256(uint128(expected)), "Pool price should remain 1.0000000 after mint");
}
}
/* solhint-enable */