poolPrice() bugfix; burn() and mint() precision bugfixes
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
/* solhint-disable */
|
||||
pragma solidity ^0.8.30;
|
||||
|
||||
import "forge-std/console2.sol";
|
||||
import {ABDKMath64x64} from "../lib/abdk-libraries-solidity/ABDKMath64x64.sol";
|
||||
import {CommonBase} from "../lib/forge-std/src/Base.sol";
|
||||
import {StdAssertions} from "../lib/forge-std/src/StdAssertions.sol";
|
||||
@@ -254,14 +253,15 @@ contract PartyPoolTest is Test {
|
||||
/// does not undercharge (no value extraction). This test verifies the request succeeds
|
||||
/// and that computed deposits are at least the proportional floor (ceil >= floor).
|
||||
function testProportionalMintOneWeiSucceedsAndProtectsPool() public {
|
||||
// Request a tiny LP amount (1 wei). Approve pool to transfer _tokens on alice's behalf.
|
||||
// Request a tiny LP amount. Approve pool to transfer _tokens on alice's behalf.
|
||||
vm.startPrank(alice);
|
||||
token0.approve(address(pool), type(uint256).max);
|
||||
token1.approve(address(pool), type(uint256).max);
|
||||
token2.approve(address(pool), type(uint256).max);
|
||||
|
||||
// Inspect the deposit amounts that the pool will require (these are rounded up)
|
||||
uint256[] memory deposits = info.mintAmounts(pool, 1);
|
||||
uint256 lpAmount = pool.totalSupply() / 2**64 + 1; // smallest mintable amount
|
||||
uint256[] memory deposits = info.mintAmounts(pool, lpAmount);
|
||||
|
||||
// Basic sanity: deposits array length must match token count and not all zero necessarily
|
||||
assertEq(deposits.length, 3);
|
||||
@@ -270,16 +270,16 @@ contract PartyPoolTest is Test {
|
||||
uint256 totalLp = pool.totalSupply();
|
||||
for (uint i = 0; i < deposits.length; i++) {
|
||||
uint256 bal = IERC20(pool.allTokens()[i]).balanceOf(address(pool));
|
||||
uint256 floorProportional = (1 * bal) / totalLp; // floor
|
||||
uint256 floorProportional = (lpAmount * bal) / totalLp; // floor
|
||||
// Ceil (deposit) must be >= floor (pool protected)
|
||||
assertTrue(deposits[i] >= floorProportional, "deposit must not be less than floor proportion");
|
||||
}
|
||||
|
||||
// Perform the mint — it should succeed for a 1 wei request (pool uses ceil to protect itself)
|
||||
pool.mint(alice, alice, 1, 0);
|
||||
pool.mint(alice, alice, lpAmount, 0);
|
||||
|
||||
// After mint, alice should have received at least 1 wei of LP
|
||||
assertTrue(pool.balanceOf(alice) >= 1, "Alice should receive at least 1 wei LP");
|
||||
assertTrue(pool.balanceOf(alice) >= lpAmount, "Alice should receive more LP token");
|
||||
|
||||
vm.stopPrank();
|
||||
}
|
||||
@@ -301,9 +301,10 @@ contract PartyPoolTest is Test {
|
||||
poolValueBefore += IERC20(toks[i]).balanceOf(address(pool));
|
||||
}
|
||||
uint256 totalLpBefore = pool.totalSupply();
|
||||
uint256 lpAmount = totalLpBefore/10**18; // tiny amount
|
||||
|
||||
// Compute required deposits and perform mint for 1 wei
|
||||
uint256[] memory deposits = info.mintAmounts(pool, 1);
|
||||
uint256[] memory deposits = info.mintAmounts(pool, lpAmount);
|
||||
|
||||
// Sum deposits as deposited_value
|
||||
uint256 depositedValue = 0;
|
||||
@@ -311,8 +312,8 @@ contract PartyPoolTest is Test {
|
||||
depositedValue += deposits[i];
|
||||
}
|
||||
|
||||
// Execute mint; it may revert if actualLpToMint == 0 but for 1 wei we expect it to succeed per design.
|
||||
pool.mint(alice, alice, 1, 0);
|
||||
// Execute mint; it may revert if actualLpToMint == 0 but for small nonzero values we expect it to succeed per design.
|
||||
pool.mint(alice, alice, lpAmount, 0);
|
||||
|
||||
// Observe minted LP
|
||||
uint256 totalLpAfter = pool.totalSupply();
|
||||
@@ -321,6 +322,7 @@ contract PartyPoolTest is Test {
|
||||
require(minted > 0, "sanity: minted should be > 0 for this test");
|
||||
|
||||
// Economic invariant check:
|
||||
// The depositor should pay at least as much value per LP token as the pool's rate before the mint:
|
||||
// depositedValue / minted >= poolValueBefore / totalLpBefore
|
||||
// Rearranged (to avoid fractional math): depositedValue * totalLpBefore >= poolValueBefore * minted
|
||||
// Use >= to allow the pool to charge equal-or-more value per LP (protects against extraction).
|
||||
@@ -1079,8 +1081,12 @@ contract PartyPoolTest is Test {
|
||||
// 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");
|
||||
// Allow a small tolerance for fixed-point rounding (~1e-9)
|
||||
int128 ratio = ABDKMath64x64.div(price, expected);
|
||||
int128 expectedRatio = ABDKMath64x64.fromUInt(1);
|
||||
int128 tol = ABDKMath64x64.divu(1, 1_000_000_000);
|
||||
int128 diff = ratio.sub(expectedRatio).abs();
|
||||
assertLe(diff, tol, "poolPrice(token0) should be ~ 1.000000000");
|
||||
|
||||
// Mint a small amount of LP into the pool from alice and verify price remains 1.0
|
||||
vm.startPrank(alice);
|
||||
@@ -1105,7 +1111,57 @@ contract PartyPoolTest is Test {
|
||||
|
||||
// Re-query the pool price and ensure it remains 1.0 (within exact fixed-point equality)
|
||||
int128 priceAfter = info.poolPrice(pool, 0);
|
||||
assertEq(uint256(uint128(priceAfter)), uint256(uint128(expected)), "Pool price should remain 1.0000000 after mint");
|
||||
// Allow a small tolerance for fixed-point rounding (~1e-9)
|
||||
ratio = ABDKMath64x64.div(price, priceAfter);
|
||||
expectedRatio = ABDKMath64x64.fromUInt(1);
|
||||
tol = ABDKMath64x64.divu(1, 1_000_000_000);
|
||||
diff = ratio.sub(expectedRatio).abs();
|
||||
assertLe(diff, tol, "Pool price should remain 1.0000000 after mint");
|
||||
}
|
||||
|
||||
/// @notice For the same 3x-imbalanced pool, verify that the LP pool price in terms of
|
||||
/// token0 is 1/3 of the pool price in terms of token1 (up to rounding).
|
||||
function testPoolPriceWhenToken0HasThreeTimesToken1() public {
|
||||
// Build tokens array (reuse test tokens)
|
||||
IERC20[] memory tokens = new IERC20[](3);
|
||||
tokens[0] = IERC20(address(token0));
|
||||
tokens[1] = IERC20(address(token1));
|
||||
tokens[2] = IERC20(address(token2));
|
||||
|
||||
uint256 feePpm = 1000;
|
||||
int128 kappa = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
|
||||
|
||||
// Same 3x imbalance as in testPriceWhenToken0HasThreeTimesToken1
|
||||
uint256[] memory deposits = new uint256[](3);
|
||||
deposits[0] = INIT_BAL * 3; // token0 = 3 * INIT_BAL
|
||||
deposits[1] = INIT_BAL; // token1 = INIT_BAL
|
||||
deposits[2] = INIT_BAL * 2; // token2 = 2 * INIT_BAL
|
||||
|
||||
(IPartyPool poolCustom, ) = Deploy.newPartyPoolWithDeposits(
|
||||
"LP3X_POOLPRICE",
|
||||
"LP3X_POOLPRICE",
|
||||
tokens,
|
||||
kappa,
|
||||
feePpm,
|
||||
feePpm,
|
||||
false,
|
||||
deposits,
|
||||
INIT_BAL * 6 * 10**18
|
||||
);
|
||||
|
||||
// Get LP price in terms of token0 and token1 (Q64.64, quote units per LP)
|
||||
int128 p0 = info.poolPrice(poolCustom, 0); // token0 as quote
|
||||
int128 p1 = info.poolPrice(poolCustom, 1); // token1 as quote
|
||||
|
||||
// ratio = p0 / p1 should be close to 3
|
||||
int128 ratio = ABDKMath64x64.div(p0, p1);
|
||||
int128 expectedRatio = ABDKMath64x64.fromUInt(3);
|
||||
|
||||
// Allow a small tolerance for fixed-point rounding (~1e-6)
|
||||
int128 tol = ABDKMath64x64.divu(1, 1_000_000);
|
||||
int128 diff = ratio.sub(expectedRatio).abs();
|
||||
|
||||
assertLe(diff, tol, "poolPrice(token0) should be ~ 1/3 of poolPrice(token1)");
|
||||
}
|
||||
|
||||
/// @notice Create a 3-token pool where token0 has 3x the balance of token1 and verify
|
||||
@@ -1183,12 +1239,9 @@ contract PartyPoolTest is Test {
|
||||
|
||||
// Compute swap-implied price as Q64.64 (quote per base) = amountOut / netIn
|
||||
int128 swapPrice = ABDKMath64x64.divu(amountOut, netIn);
|
||||
console2.log('info price', infoPrice);
|
||||
console2.log('swap price', swapPrice);
|
||||
|
||||
// Absolute difference between info.price and swap-implied price
|
||||
int128 slippage = ABDKMath64x64.fromUInt(1) - swapPrice.div(infoPrice);
|
||||
console2.log('slippage', slippage);
|
||||
|
||||
// Tolerance ~ 4e-5 in Q64.64
|
||||
int128 tol = ABDKMath64x64.divu(4, 100_000);
|
||||
|
||||
Reference in New Issue
Block a user