price() view bugfix

This commit is contained in:
tim
2025-11-25 13:26:24 -04:00
parent 0422031c5c
commit 64e73e5570
4 changed files with 108 additions and 3 deletions

View File

@@ -87,6 +87,73 @@ library Deploy {
return newPartyPool2(NPPArgs(name_, symbol_, tokens_, _kappa, _swapFeePpm, _flashFeePpm, wrapper, _stable, _initialBalance, _lpTokens));
}
/// @notice Deploy a pool using explicit per-token initial deposits (useful for non-uniform initial balances)
function newPartyPoolWithDeposits(
string memory name_,
string memory symbol_,
IERC20[] memory tokens_,
int128 _kappa,
uint256 _swapFeePpm,
uint256 _flashFeePpm,
bool _stable,
uint256[] memory initialDeposits,
uint256 _lpTokens
) internal returns (IPartyPool pool, uint256 lpTokens) {
require(initialDeposits.length == tokens_.length, "mismatched deposits length");
NativeWrapper wrapper = new WETH9();
// Prepare planner and arrays
NPPVars memory v = NPPVars(
address(newPartyPlanner(address(this), wrapper)),
new uint256[](tokens_.length),
new uint256[](tokens_.length)
);
address self = address(this);
// Build per-asset fee vector from scalar for tests
for (uint256 i = 0; i < tokens_.length; i++) { v.feesArr[i] = _swapFeePpm; }
// Mint/prepare the specified deposits for each token and approve the planner
for (uint256 i = 0; i < tokens_.length; i++) {
v.deposits[i] = initialDeposits[i];
if (address(tokens_[i]) == address(wrapper)) {
// Wrap native value and approve planner
if (initialDeposits[i] > 0) {
argsWrapperDeposit(wrapper, v.planner, initialDeposits[i]);
}
} else {
MockERC20 t = MockERC20(address(tokens_[i]));
if (initialDeposits[i] > 0) {
t.mint(self, initialDeposits[i]);
t.approve(v.planner, initialDeposits[i]);
}
}
}
// Create pool with provided deposits
(pool, lpTokens) = IPartyPlanner(v.planner).newPool(
name_,
symbol_,
tokens_,
_kappa,
_swapFeePpm,
_flashFeePpm,
_stable,
self,
self,
v.deposits,
_lpTokens,
0
);
}
// Helper to deposit native wrapper value and approve planner (kept separate for clarity)
function argsWrapperDeposit(NativeWrapper wrapper, address planner, uint256 amount) internal {
if (amount == 0) return;
wrapper.deposit{value: amount}();
wrapper.approve(planner, amount);
}
struct NPPVars {
address planner;
uint256[] feesArr;

View File

@@ -1107,5 +1107,43 @@ contract PartyPoolTest is Test {
assertEq(uint256(uint128(priceAfter)), uint256(uint128(expected)), "Pool price should remain 1.0000000 after mint");
}
/// @notice Create a 3-token pool where token0 has 3x the balance of token1 and verify
/// that the relative price token1/token0 equals 3 (in ABDK 64.64 fixed point).
function testPriceWhenToken0HasThreeTimesToken1() 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;
// Compute kappa using existing tradeFrac/targetSlippage setup
int128 kappa = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
// Prepare explicit per-token initial deposits so token0 starts at 3x token1
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; // token2 = INIT_BAL
// Deploy a fresh pool with the specified deposits (no subsequent minting required)
(IPartyPool poolCustom, ) = Deploy.newPartyPoolWithDeposits("LP3X", "LP3X", tokens, kappa, feePpm, feePpm, false, deposits, 0);
// Sanity-check balances
uint256 b0 = token0.balanceOf(address(poolCustom));
uint256 b1 = token1.balanceOf(address(poolCustom));
assertEq(b0, INIT_BAL * 3, "token0 balance should be 3x INIT_BAL");
assertEq(b1, INIT_BAL, "token1 balance should be INIT_BAL");
// Query the price of token1 in terms of token0 (token1/token0)
int128 price = info.price(poolCustom, 1, 0);
// Expected price is 3.0 in ABDK 64.64 fixed point
int128 expected = ABDKMath64x64.fromInt(3);
// Compare as uint representation (values are non-negative)
assertEq(uint256(uint128(price)), uint256(uint128(expected)), "Price token1/token0 should be 3.0000000");
}
}
/* solhint-enable */