CREATE2 callback validation; init code storage contracts

This commit is contained in:
tim
2025-11-13 16:41:52 -04:00
parent c2ac0e3624
commit 9273430f2a
28 changed files with 779 additions and 588 deletions

View File

@@ -4,14 +4,23 @@
pragma solidity ^0.8.30;
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";
import {StdChains} from "../lib/forge-std/src/StdChains.sol";
import {StdCheats, StdCheatsSafe} from "../lib/forge-std/src/StdCheats.sol";
import {StdUtils} from "../lib/forge-std/src/StdUtils.sol";
import {Test} from "../lib/forge-std/src/Test.sol";
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {Funding} from "../src/Funding.sol";
import {IPartyInfo} from "../src/IPartyInfo.sol";
import {IPartyPlanner} from "../src/IPartyPlanner.sol";
import {IPartyPool} from "../src/IPartyPool.sol";
import {LMSRStabilized} from "../src/LMSRStabilized.sol";
import {PartyInfo} from "../src/PartyInfo.sol";
import {PartyPool} from "../src/PartyPool.sol";
import {PartyPoolDeployer} from "../src/PartyPoolDeployer.sol";
import {PartySwapCallbackVerifier} from "../src/PartySwapCallbackVerifier.sol";
import {Deploy} from "./Deploy.sol";
import {TestERC20, SwapCallbackContract} from "./FundingSwapTest.sol";
/// @notice Minimal ERC20 token for tests with an external mint function.
contract TestERC20 is ERC20 {
@@ -37,9 +46,11 @@ contract SwapCallbackContract {
address public pool;
address public tokenSource;
bool public shouldFail;
IPartyPlanner public planner;
constructor(address _pool) {
constructor(address _pool, IPartyPlanner _planner) {
pool = _pool;
planner = _planner;
}
function setTokenSource(address _tokenSource) external {
@@ -51,16 +62,17 @@ contract SwapCallbackContract {
}
/// @notice Called by PartyPool.swap on the payer. Signature must be:
/// provideFunding(address token, uint256 amount)
/// provideFunding(bytes32 nonce, IERC20 inputToken, uint256 amount, bytes memory data)
/// @dev The pool will call this function to request the input token; this function
/// pulls funds from tokenSource (via ERC20.transferFrom) into the pool.
function provideFunding(address token, uint256 amount) external {
function provideFunding(bytes32 nonce, IERC20 token, uint256 amount, bytes memory) external {
PartySwapCallbackVerifier.verifyCallback(planner, nonce);
require(msg.sender == pool, "Callback not called by pool");
if (shouldFail) revert("callback failed");
require(tokenSource != address(0), "no token source");
// Pull the required tokens from tokenSource into the pool
TestERC20(token).transferFrom(tokenSource, pool, amount);
token.transferFrom(tokenSource, pool, amount);
}
}
@@ -73,9 +85,10 @@ contract FundingTest is Test {
TestERC20 token0;
TestERC20 token1;
TestERC20 token2;
PartyPool pool;
PartyPool poolZeroFee;
PartyInfo info;
IPartyPlanner planner;
IPartyPool pool;
IPartyPool poolZeroFee;
IPartyInfo info;
SwapCallbackContract callbackContract;
address alice;
@@ -117,31 +130,26 @@ contract FundingTest is Test {
// Deploy pool with a small fee (0.1%)
uint256 feePpm = 1000;
int128 kappa = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
pool = Deploy.newPartyPool(address(this), "LP", "LP", tokens, kappa, feePpm, feePpm, false);
// Transfer initial deposit amounts into pool
token0.transfer(address(pool), INIT_BAL);
token1.transfer(address(pool), INIT_BAL);
token2.transfer(address(pool), INIT_BAL);
planner = Deploy.newPartyPlanner();
uint256[] memory deposits = new uint256[](tokens.length);
for(uint256 i=0; i<deposits.length; i++)
deposits[i] = INIT_BAL;
// Perform initial mint
pool.initialMint(address(this), INIT_BAL * tokens.length * 10**18);
token0.mint(address(this), INIT_BAL*2);
token1.mint(address(this), INIT_BAL*2);
token2.mint(address(this), INIT_BAL*2);
token0.approve(address(planner), INIT_BAL*2);
token1.approve(address(planner), INIT_BAL*2);
token2.approve(address(planner), INIT_BAL*2);
vm.prank(planner.owner());
(pool,) = planner.newPool("LP", "LP", tokens, kappa, feePpm, feePpm, false,
address(this), address(this), deposits, 0, 0);
// Deploy pool with zero fees for exact balance matching
poolZeroFee = Deploy.newPartyPool(address(this), "LP_ZERO", "LP_ZERO", tokens, kappa, 0, 0, false);
// Mint additional tokens for zero-fee pool
token0.mint(address(this), INIT_BAL);
token1.mint(address(this), INIT_BAL);
token2.mint(address(this), INIT_BAL);
// Transfer to zero-fee pool
token0.transfer(address(poolZeroFee), INIT_BAL);
token1.transfer(address(poolZeroFee), INIT_BAL);
token2.transfer(address(poolZeroFee), INIT_BAL);
// Initialize zero-fee pool
poolZeroFee.initialMint(address(this), INIT_BAL * tokens.length * 10**18);
vm.prank(planner.owner());
(poolZeroFee,) = planner.newPool("LP_ZERO", "LP_ZERO", tokens, kappa, 0, 0, false,
address(this), address(this), deposits, 0, 0);
// Mint tokens to alice and bob for testing
token0.mint(alice, INIT_BAL);
@@ -153,7 +161,7 @@ contract FundingTest is Test {
token2.mint(bob, INIT_BAL);
// Deploy callback contract
callbackContract = new SwapCallbackContract(address(pool));
callbackContract = new SwapCallbackContract(address(pool), planner);
info = Deploy.newInfo();
}
@@ -184,7 +192,8 @@ contract FundingTest is Test {
maxIn, // maxAmountIn
0, // limitPrice
0, // deadline
false // unwrap
false, // unwrap
''
);
// Verify amounts
@@ -225,7 +234,7 @@ contract FundingTest is Test {
uint256 poolToken1Before = token1.balanceOf(address(poolZeroFee));
// Execute swap
(uint256 amountIn, uint256 amountOut, uint256 fee) = poolZeroFee.swap(
(, uint256 amountOut, uint256 fee) = poolZeroFee.swap(
alice,
Funding.PREFUNDING,
bob,
@@ -234,7 +243,8 @@ contract FundingTest is Test {
maxIn,
0,
0,
false
false,
''
);
// With zero fees, fee should be 0
@@ -270,7 +280,8 @@ contract FundingTest is Test {
maxIn,
0,
0,
false
false,
''
);
vm.stopPrank();
@@ -310,7 +321,8 @@ contract FundingTest is Test {
maxIn, // maxAmountIn
0, // limitPrice
0, // deadline
false // unwrap
false, // unwrap
''
);
// Verify amounts
@@ -335,7 +347,7 @@ contract FundingTest is Test {
uint256 maxIn = 10_000;
// Setup callback for zero-fee pool
SwapCallbackContract zeroFeeCallback = new SwapCallbackContract(address(poolZeroFee));
SwapCallbackContract zeroFeeCallback = new SwapCallbackContract(address(poolZeroFee), planner);
zeroFeeCallback.setTokenSource(alice);
zeroFeeCallback.setShouldFail(false);
@@ -356,7 +368,8 @@ contract FundingTest is Test {
maxIn,
0,
0,
false
false,
''
);
// With zero fees, fee should be 0
@@ -387,7 +400,8 @@ contract FundingTest is Test {
maxIn,
0,
0,
false
false,
''
);
}
@@ -395,52 +409,95 @@ contract FundingTest is Test {
Validation Against swapAmounts()
---------------------- */
function createTestPools2() public returns (IPartyPool testPool1, IPartyPool testPool2) {
// Create two identical test pools
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);
uint256[] memory deposits = new uint256[](tokens.length);
for(uint256 i=0; i<deposits.length; i++)
deposits[i] = INIT_BAL;
token0.mint(address(this), INIT_BAL*2);
token1.mint(address(this), INIT_BAL*2);
token2.mint(address(this), INIT_BAL*2);
token0.approve(address(planner), INIT_BAL*2);
token1.approve(address(planner), INIT_BAL*2);
token2.approve(address(planner), INIT_BAL*2);
vm.prank(planner.owner());
(testPool1,) = planner.newPool("LP_TEST_1", "LP_TEST_1", tokens, kappa, feePpm, feePpm, false,
address(this), address(this), deposits, INIT_BAL * tokens.length * 10**18, 0);
vm.prank(planner.owner());
(testPool2,) = planner.newPool("LP_TEST_2", "LP_TEST_2", tokens, kappa, feePpm, feePpm, false,
address(this), address(this), deposits, INIT_BAL * tokens.length * 10**18, 0);
}
function createTestPools3() public returns (IPartyPool testPool1, IPartyPool testPool2, IPartyPool testPool3) {
// Create two identical test pools
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);
uint256[] memory deposits = new uint256[](tokens.length);
for(uint256 i=0; i<deposits.length; i++)
deposits[i] = INIT_BAL;
token0.mint(address(this), INIT_BAL*3);
token1.mint(address(this), INIT_BAL*3);
token2.mint(address(this), INIT_BAL*3);
token0.approve(address(planner), INIT_BAL*3);
token1.approve(address(planner), INIT_BAL*3);
token2.approve(address(planner), INIT_BAL*3);
vm.prank(planner.owner());
(testPool1,) = planner.newPool("LP_TEST_1", "LP_TEST_1", tokens, kappa, feePpm, feePpm, false,
address(this), address(this), deposits, INIT_BAL * tokens.length * 10**18, 0);
vm.prank(planner.owner());
(testPool2,) = planner.newPool("LP_TEST_2", "LP_TEST_2", tokens, kappa, feePpm, feePpm, false,
address(this), address(this), deposits, INIT_BAL * tokens.length * 10**18, 0);
vm.prank(planner.owner());
(testPool3,) = planner.newPool("LP_TEST_3", "LP_TEST_3", tokens, kappa, feePpm, feePpm, false,
address(this), address(this), deposits, INIT_BAL * tokens.length * 10**18, 0);
}
/// @notice Verify that pre-funded swap amounts match swapAmounts() view predictions
function testPreFundingMatchesSwapAmountsView() public {
uint256 maxIn = 10_000;
(IPartyPool testPool1, IPartyPool testPool2) = createTestPools2();
// Perform a reference swap with USE_APPROVALS to get expected amounts
vm.startPrank(bob);
token0.approve(address(pool), type(uint256).max);
(uint256 refAmountIn, uint256 refAmountOut, uint256 refFee) = pool.swap(
token0.approve(address(testPool1), type(uint256).max);
(uint256 refAmountIn, uint256 refAmountOut, uint256 refFee) = testPool1.swap(
bob,
Funding.APPROVALS,
Funding.APPROVAL,
bob,
0,
1,
maxIn,
0,
0,
false
false,
''
);
vm.stopPrank();
// Reset pool state by creating a fresh pool with identical parameters
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);
PartyPool testPool = Deploy.newPartyPool(address(this), "LP_TEST", "LP_TEST", tokens, kappa, feePpm, feePpm, false);
token0.mint(address(this), INIT_BAL);
token1.mint(address(this), INIT_BAL);
token2.mint(address(this), INIT_BAL);
token0.transfer(address(testPool), INIT_BAL);
token1.transfer(address(testPool), INIT_BAL);
token2.transfer(address(testPool), INIT_BAL);
testPool.initialMint(address(this), INIT_BAL * tokens.length * 10**18);
// Now test pre-funding with same initial state
// Now perform a swap using the prefunding method on the second pool
vm.startPrank(alice);
token0.transfer(address(testPool), maxIn);
token0.transfer(address(testPool2), maxIn);
(uint256 preAmountIn, uint256 preAmountOut, uint256 preFee) = testPool.swap(
(uint256 preAmountIn, uint256 preAmountOut, uint256 preFee) = testPool2.swap(
alice,
Funding.PREFUNDING,
alice,
@@ -449,7 +506,8 @@ contract FundingTest is Test {
maxIn,
0,
0,
false
false,
''
);
vm.stopPrank();
@@ -463,45 +521,28 @@ contract FundingTest is Test {
function testCallbackMatchesSwapAmountsView() public {
uint256 maxIn = 10_000;
(IPartyPool testPool1, IPartyPool testPool2) = createTestPools2();
// Perform a reference swap
vm.startPrank(bob);
token0.approve(address(pool), type(uint256).max);
token0.approve(address(testPool1), type(uint256).max);
(uint256 refAmountIn, uint256 refAmountOut, uint256 refFee) = pool.swap(
(uint256 refAmountIn, uint256 refAmountOut, uint256 refFee) = testPool1.swap(
bob,
Funding.APPROVALS,
Funding.APPROVAL,
bob,
0,
1,
maxIn,
0,
0,
false
false,
''
);
vm.stopPrank();
// Create fresh pool for callback test
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);
PartyPool testPool = Deploy.newPartyPool(address(this), "LP_TEST2", "LP_TEST2", tokens, kappa, feePpm, feePpm, false);
token0.mint(address(this), INIT_BAL);
token1.mint(address(this), INIT_BAL);
token2.mint(address(this), INIT_BAL);
token0.transfer(address(testPool), INIT_BAL);
token1.transfer(address(testPool), INIT_BAL);
token2.transfer(address(testPool), INIT_BAL);
testPool.initialMint(address(this), INIT_BAL * tokens.length * 10**18);
// Setup callback for test pool
SwapCallbackContract testCallback = new SwapCallbackContract(address(testPool));
SwapCallbackContract testCallback = new SwapCallbackContract(address(testPool2), planner);
testCallback.setTokenSource(alice);
testCallback.setShouldFail(false);
@@ -510,7 +551,7 @@ contract FundingTest is Test {
vm.stopPrank();
// Test callback with same initial state
(uint256 cbAmountIn, uint256 cbAmountOut, uint256 cbFee) = testPool.swap(
(uint256 cbAmountIn, uint256 cbAmountOut, uint256 cbFee) = testPool2.swap(
address(testCallback),
CALLBACK,
alice,
@@ -519,7 +560,8 @@ contract FundingTest is Test {
maxIn,
0,
0,
false
false,
''
);
// Callback amounts should match reference swap amounts
@@ -538,56 +580,26 @@ contract FundingTest is Test {
for (uint i = 0; i < swapAmounts.length; i++) {
uint256 swapAmount = swapAmounts[i];
// Create three identical pools
IERC20[] memory tokens = new IERC20[](3);
tokens[0] = IERC20(address(token0));
tokens[1] = IERC20(address(token1));
tokens[2] = IERC20(address(token2));
int128 kappa = LMSRStabilized.computeKappaFromSlippage(tokens.length, tradeFrac, targetSlippage);
PartyPool poolApproval = Deploy.newPartyPool(address(this), "LP_A", "LP_A", tokens, kappa, 0, 0, false);
PartyPool poolPreFund = Deploy.newPartyPool(address(this), "LP_P", "LP_P", tokens, kappa, 0, 0, false);
PartyPool poolCallback = Deploy.newPartyPool(address(this), "LP_C", "LP_C", tokens, kappa, 0, 0, false);
(IPartyPool poolApproval, IPartyPool poolPreFund, IPartyPool poolCallback) = createTestPools3();
// Initialize all three pools identically
token0.mint(address(this), INIT_BAL * 3);
token1.mint(address(this), INIT_BAL * 3);
token2.mint(address(this), INIT_BAL * 3);
token0.transfer(address(poolApproval), INIT_BAL);
token1.transfer(address(poolApproval), INIT_BAL);
token2.transfer(address(poolApproval), INIT_BAL);
poolApproval.initialMint(address(this), 0);
token0.transfer(address(poolPreFund), INIT_BAL);
token1.transfer(address(poolPreFund), INIT_BAL);
token2.transfer(address(poolPreFund), INIT_BAL);
poolPreFund.initialMint(address(this), 0);
token0.transfer(address(poolCallback), INIT_BAL);
token1.transfer(address(poolCallback), INIT_BAL);
token2.transfer(address(poolCallback), INIT_BAL);
poolCallback.initialMint(address(this), 0);
// Test with USE_APPROVALS (bytes4(0) with approvals)
// Test with APPROVALS
vm.startPrank(alice);
token0.approve(address(poolApproval), type(uint256).max);
(uint256 apprIn, uint256 apprOut, ) = poolApproval.swap(
alice, Funding.APPROVALS, alice, 0, 1, swapAmount, 0, 0, false
alice, Funding.APPROVAL, alice, 0, 1, swapAmount, 0, 0, false, ''
);
vm.stopPrank();
// Test with PRE_FUNDED (bytes4(0) with pre-funding)
// Test with PREFUNDING
vm.startPrank(alice);
token0.transfer(address(poolPreFund), swapAmount);
(uint256 preIn, uint256 preOut, ) = poolPreFund.swap(
alice, Funding.PREFUNDING, alice, 0, 1, swapAmount, 0, 0, false
alice, Funding.PREFUNDING, alice, 0, 1, swapAmount, 0, 0, false, ''
);
vm.stopPrank();
// Test with CALLBACK
SwapCallbackContract cb = new SwapCallbackContract(address(poolCallback));
SwapCallbackContract cb = new SwapCallbackContract(address(poolCallback), planner);
cb.setTokenSource(alice);
cb.setShouldFail(false);
@@ -596,7 +608,7 @@ contract FundingTest is Test {
vm.stopPrank();
(uint256 cbIn, uint256 cbOut, ) = poolCallback.swap(
address(cb), CALLBACK, alice, 0, 1, swapAmount, 0, 0, false
address(cb), CALLBACK, alice, 0, 1, swapAmount, 0, 0, false, ''
);
// All three methods should produce identical results