refactor PartyPoolSwapMintImpl

This commit is contained in:
tim
2025-09-25 21:46:59 -04:00
parent 6edad6e510
commit 9cac58013b
12 changed files with 481 additions and 291 deletions

View File

@@ -6,9 +6,9 @@ import "@abdk/ABDKMath64x64.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../src/LMSRStabilized.sol";
import "../src/PartyPool.sol";
// Import the flash callback interface
import "../src/PartyPlanner.sol";
import "../src/IPartyFlashCallback.sol";
import {Deploy} from "../src/Deploy.sol";
/// @notice Test contract that implements the flash callback for testing flash loans
contract FlashBorrower is IPartyFlashCallback {
@@ -130,6 +130,7 @@ contract TestERC20 is ERC20 {
contract GasTest is Test {
using ABDKMath64x64 for int128;
PartyPlanner planner;
PartyPool pool2;
PartyPool pool10;
PartyPool pool20;
@@ -172,7 +173,7 @@ contract GasTest is Test {
}
// Compute kappa from slippage params and number of tokens, then construct pool with kappa
int128 computedKappa = LMSRStabilized.computeKappaFromSlippage(ierc20Tokens.length, tradeFrac, targetSlippage);
PartyPool newPool = new PartyPool(poolName, poolName, ierc20Tokens, bases, computedKappa, feePpm, feePpm, false);
PartyPool newPool = Deploy.newPartyPool(poolName, poolName, ierc20Tokens, bases, computedKappa, feePpm, feePpm, false);
// Transfer initial deposit amounts into pool before initial mint
for (uint256 i = 0; i < numTokens; i++) {
@@ -212,7 +213,7 @@ contract GasTest is Test {
ierc20Tokens[i] = IERC20(tokens[i]);
}
int128 computedKappa = LMSRStabilized.computeKappaFromSlippage(ierc20Tokens.length, tradeFrac, targetSlippage);
PartyPool newPool = new PartyPool(poolName, poolName, ierc20Tokens, bases, computedKappa, feePpm, feePpm, true);
PartyPool newPool = Deploy.newPartyPool(poolName, poolName, ierc20Tokens, bases, computedKappa, feePpm, feePpm, true);
// Transfer initial deposit amounts into pool before initial mint
for (uint256 i = 0; i < numTokens; i++) {
@@ -229,6 +230,8 @@ contract GasTest is Test {
alice = address(0xA11ce);
bob = address(0xB0b);
planner = Deploy.newPartyPlanner();
// Configure LMSR parameters similar to other tests: trade size 1% of asset -> 0.01, slippage 0.001
tradeFrac = ABDKMath64x64.divu(100, 10_000); // 0.01
targetSlippage = ABDKMath64x64.divu(10, 10_000); // 0.001

View File

@@ -5,6 +5,7 @@ import "forge-std/Test.sol";
import "../src/LMSRStabilized.sol";
import "../src/PartyPlanner.sol";
import "../src/PartyPool.sol";
import {Deploy} from "../src/Deploy.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
// Mock ERC20 token for testing
@@ -38,7 +39,7 @@ contract PartyPlannerTest is Test {
function setUp() public {
// Deploy PartyPlanner
planner = new PartyPlanner();
planner = Deploy.newPartyPlanner();
// Deploy mock tokens
tokenA = new MockERC20("Token A", "TKNA", 18);
@@ -87,7 +88,7 @@ contract PartyPlannerTest is Test {
// Compute kappa then create pool via kappa overload
int128 computedKappa = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
(PartyPool pool, uint256 lpAmount) = planner.createPool(
(PartyPool pool, uint256 lpAmount) = planner.newPool(
name,
symbol,
tokens,
@@ -166,7 +167,7 @@ contract PartyPlannerTest is Test {
deposits1[1] = INITIAL_DEPOSIT_AMOUNT;
int128 kappa1 = LMSRStabilized.computeKappaFromSlippage(tokens1.length, int128((1 << 64) - 1), int128(1 << 62));
(PartyPool pool1,) = planner.createPool(
(PartyPool pool1,) = planner.newPool(
"Pool 1", "LP1", tokens1, bases1,
kappa1, 3000, 5000, false,
payer, receiver, deposits1, 1000e18, 0
@@ -186,7 +187,7 @@ contract PartyPlannerTest is Test {
deposits2[1] = INITIAL_DEPOSIT_AMOUNT / 1e12; // Adjust for 6 decimals
int128 kappa2 = LMSRStabilized.computeKappaFromSlippage(tokens2.length, int128((1 << 64) - 1), int128(1 << 62));
(PartyPool pool2,) = planner.createPool(
(PartyPool pool2,) = planner.newPool(
"Pool 2", "LP2", tokens2, bases2,
kappa2, 3000, 5000, false,
payer, receiver, deposits2, 1000e18, 0
@@ -230,7 +231,7 @@ contract PartyPlannerTest is Test {
// Test token/deposit length mismatch
vm.expectRevert("Planner: tokens and deposits length mismatch");
// call old-signature convenience (it will still exist) for the mismatched-length revert check
planner.createPool(
planner.newPool(
"Test Pool", "TESTLP", tokens, bases,
int128((1 << 64) - 1), int128(1 << 62), 3000, 5000, false,
payer, receiver, deposits, 1000e18, 0
@@ -244,7 +245,7 @@ contract PartyPlannerTest is Test {
int128 kappaErr = LMSRStabilized.computeKappaFromSlippage(tokens.length, int128((1 << 64) - 1), int128(1 << 62));
vm.expectRevert("Planner: payer cannot be zero address");
planner.createPool(
planner.newPool(
"Test Pool", "TESTLP", tokens, bases,
kappaErr, 3000, 5000, false,
address(0), receiver, validDeposits, 1000e18, 0
@@ -252,7 +253,7 @@ contract PartyPlannerTest is Test {
// Test zero receiver address
vm.expectRevert("Planner: receiver cannot be zero address");
planner.createPool(
planner.newPool(
"Test Pool", "TESTLP", tokens, bases,
kappaErr, 3000, 5000, false,
payer, address(0), validDeposits, 1000e18, 0
@@ -263,7 +264,7 @@ contract PartyPlannerTest is Test {
int128 kappaDeadline = LMSRStabilized.computeKappaFromSlippage(tokens.length, int128((1 << 64) - 1), int128(1 << 62));
vm.warp(1000);
vm.expectRevert("Planner: deadline exceeded");
planner.createPool(
planner.newPool(
"Test Pool", "TESTLP", tokens, bases,
kappaDeadline, 3000, 5000, false,
payer, receiver, validDeposits, 1000e18, block.timestamp - 1
@@ -289,7 +290,7 @@ contract PartyPlannerTest is Test {
deposits[1] = INITIAL_DEPOSIT_AMOUNT;
int128 kappaLoop = LMSRStabilized.computeKappaFromSlippage(tokens.length, int128((1 << 64) - 1), int128(1 << 62));
(PartyPool pool,) = planner.createPool(
(PartyPool pool,) = planner.newPool(
string(abi.encodePacked("Pool ", vm.toString(i))),
string(abi.encodePacked("LP", vm.toString(i))),
tokens, bases,

View File

@@ -9,6 +9,8 @@ import "../src/PartyPool.sol";
// Import the flash callback interface
import "../src/IPartyFlashCallback.sol";
import {PartyPlanner} from "../src/PartyPlanner.sol";
import {Deploy} from "../src/Deploy.sol";
/// @notice Test contract that implements the flash callback for testing flash loans
contract FlashBorrower is IPartyFlashCallback {
@@ -137,6 +139,7 @@ contract PartyPoolTest is Test {
TestERC20 token7;
TestERC20 token8;
TestERC20 token9;
PartyPlanner planner;
PartyPool pool;
PartyPool pool10;
@@ -151,6 +154,7 @@ contract PartyPoolTest is Test {
uint256 constant BASE = 1; // use base=1 so internal amounts correspond to raw integers (Q64.64 units)
function setUp() public {
planner = Deploy.newPartyPlanner();
alice = address(0xA11ce);
bob = address(0xB0b);
@@ -197,7 +201,7 @@ contract PartyPoolTest is Test {
uint256 feePpm = 1000;
int128 kappa3 = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
pool = new PartyPool("LP", "LP", tokens, bases, kappa3, feePpm, feePpm, false);
pool = Deploy.newPartyPool("LP", "LP", tokens, bases, kappa3, feePpm, feePpm, false);
// Transfer initial deposit amounts into pool before initial mint (pool expects tokens already in contract)
// We deposit equal amounts INIT_BAL for each token
@@ -227,7 +231,7 @@ contract PartyPoolTest is Test {
}
int128 kappa10 = LMSRStabilized.computeKappaFromSlippage(tokens10.length, tradeFrac, targetSlippage);
pool10 = new PartyPool("LP10", "LP10", tokens10, bases10, kappa10, feePpm, feePpm, false);
pool10 = Deploy.newPartyPool("LP10", "LP10", tokens10, bases10, kappa10, feePpm, feePpm, false);
// Mint additional tokens for pool10 initial deposit
token0.mint(address(this), INIT_BAL);
@@ -1231,11 +1235,11 @@ contract PartyPoolTest is Test {
// Pool with default initialization (lpTokens = 0)
int128 kappaDefault = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
PartyPool poolDefault = new PartyPool("LP_DEFAULT", "LP_DEFAULT", tokens, bases, kappaDefault, feePpm, feePpm, false);
PartyPool poolDefault = Deploy.newPartyPool("LP_DEFAULT", "LP_DEFAULT", tokens, bases, kappaDefault, feePpm, feePpm, false);
// Pool with custom initialization (lpTokens = custom amount)
int128 kappaCustom = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
PartyPool poolCustom = new PartyPool("LP_CUSTOM", "LP_CUSTOM", tokens, bases, kappaCustom, feePpm, feePpm, false);
PartyPool poolCustom = Deploy.newPartyPool("LP_CUSTOM", "LP_CUSTOM", tokens, bases, kappaCustom, feePpm, feePpm, false);
// Mint additional tokens for both pools
token0.mint(address(this), INIT_BAL * 2);
@@ -1307,9 +1311,9 @@ contract PartyPoolTest is Test {
uint256 feePpm = 1000;
int128 kappaDefault2 = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
PartyPool poolDefault = new PartyPool("LP_DEFAULT", "LP_DEFAULT", tokens, bases, kappaDefault2, feePpm, feePpm, false);
PartyPool poolDefault = Deploy.newPartyPool("LP_DEFAULT", "LP_DEFAULT", tokens, bases, kappaDefault2, feePpm, feePpm, false);
int128 kappaCustom2 = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
PartyPool poolCustom = new PartyPool("LP_CUSTOM", "LP_CUSTOM", tokens, bases, kappaCustom2, feePpm, feePpm, false);
PartyPool poolCustom = Deploy.newPartyPool("LP_CUSTOM", "LP_CUSTOM", tokens, bases, kappaCustom2, feePpm, feePpm, false);
// Mint additional tokens
token0.mint(address(this), INIT_BAL * 4);
@@ -1329,7 +1333,7 @@ contract PartyPoolTest is Test {
uint256 lpDefault = poolDefault.initialMint(address(this), 0);
uint256 scaleFactor = 3;
uint256 customLpAmount = lpDefault * scaleFactor;
uint256 lpCustom = poolCustom.initialMint(address(this), customLpAmount);
poolCustom.initialMint(address(this), customLpAmount);
// Verify initial LP supplies
assertEq(poolDefault.totalSupply(), lpDefault, "Default pool should have default LP supply");