prefunding

This commit is contained in:
tim
2025-11-11 18:33:14 -04:00
parent ddfe7651ea
commit 70960630ce
8 changed files with 722 additions and 69 deletions

View File

@@ -12,10 +12,10 @@ import {IERC3156FlashBorrower} from "../lib/openzeppelin-contracts/contracts/int
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {IERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import {Funding} from "../src/Funding.sol";
import {IPartyPool} from "../src/IPartyPool.sol";
import {LMSRStabilized} from "../src/LMSRStabilized.sol";
import {PartyPlanner} from "../src/PartyPlanner.sol";
import {PartyPool} from "../src/PartyPool.sol";
import {Deploy} from "./Deploy.sol";
import {TestERC20, FlashBorrower} from "./GasTest.sol";
@@ -238,7 +238,7 @@ contract GasTest is Test {
/// @notice Helper function: perform 10 swaps back-and-forth between the first two _tokens.
function _performSwapGasTest(IPartyPool testPool) internal {
_performSwapGasTest(testPool, false);
_performSwapGasTest(testPool, Funding.USE_APPROVALS);
}
function sendTokensCallback(IERC20 token, uint256 amount) external {
@@ -247,44 +247,52 @@ contract GasTest is Test {
token.transferFrom( alice, msg.sender, amount);
}
function _performSwapGasTest(IPartyPool testPool, bool useCallback) internal {
function _performSwapGasTest(IPartyPool testPool, bytes4 fundingSelector) internal {
IERC20[] memory tokens = testPool.allTokens();
require(tokens.length >= 2, "Pool must have at least 2 tokens");
address payer;
address spender;
bytes4 selector;
if (useCallback) {
if (fundingSelector == Funding.USE_PREFUNDING) {
payer = address(this);
spender = address(this);
selector = this.sendTokensCallback.selector;
}
else {
else if (fundingSelector == Funding.USE_APPROVALS) {
payer = alice;
spender = address(testPool);
selector = bytes4(0);
}
else {
payer = address(this);
spender = address(this);
}
TestERC20 token0 = TestERC20(address(tokens[0]));
TestERC20 token1 = TestERC20(address(tokens[1]));
vm.prank(alice);
TestERC20(address(tokens[0])).approve(spender, type(uint256).max);
token0.approve(spender, type(uint256).max);
vm.prank(alice);
TestERC20(address(tokens[1])).approve(spender, type(uint256).max);
token1.approve(spender, type(uint256).max);
uint256 maxIn = 10_000;
// Perform swaps alternating directions to avoid large imbalance
vm.startPrank(alice);
for (uint256 i = 0; i < 20; i++) {
vm.prank(alice);
if (i % 2 == 0) {
if (fundingSelector == Funding.USE_PREFUNDING)
token0.transfer(address(testPool), maxIn);
// swap token0 -> token1
testPool.swap(payer, selector, alice, 0, 1, maxIn, 0, 0, false);
testPool.swap(payer, fundingSelector, alice, 0, 1, maxIn, 0, 0, false);
} else {
// swap token1 -> token0
testPool.swap(payer, selector, alice, 1, 0, maxIn, 0, 0, false);
if (fundingSelector == Funding.USE_PREFUNDING)
token1.transfer(address(testPool), maxIn);
testPool.swap(payer, fundingSelector, alice, 1, 0, maxIn, 0, 0, false);
}
// shake up the bits
maxIn *= 787;
maxIn /= 1000;
}
vm.stopPrank();
}
/// @notice Gas measurement: perform 10 swaps back-and-forth between first two _tokens in the 2-token pool.
@@ -299,9 +307,13 @@ contract GasTest is Test {
/// @notice Gas measurement: perform 10 swaps back-and-forth between first two _tokens in the 10-token pool using the callback funding method.
function testSwapGasCallback() public {
_performSwapGasTest(pool10, true);
_performSwapGasTest(pool10, this.sendTokensCallback.selector);
}
/// @notice Gas measurement: perform 10 swaps back-and-forth between first two _tokens in the 10-token pool using the callback funding method.
function testSwapGasPrefunding() public {
_performSwapGasTest(pool10, Funding.USE_PREFUNDING);
}
/// @notice Gas measurement: perform 10 swaps back-and-forth between first two _tokens in the 20-token pool.
function testSwapGasTwenty() public {