// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; // Tests for the sample integration. They deploy the settler from source and use mock tokens, so // they need no fork. Clone https://github.com/flashbots/JITSettler, put this file and // JitIntegration.sol in test/sample/, and run `forge test --match-path 'test/sample/*'`. // // Anyone can post a route, so a test posts its own: `vm.prank(poster); jit.setRoute(executor, // data)`. The executor here pays a fixed amount. A real route swaps on venues. import { Test } from "forge-std/Test.sol"; import { JITSettler, IJITEvents } from "../../src/JITSettler.sol"; import { JitIntegration, IJITSettler } from "./JitIntegration.sol"; contract Token { mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; function mint(address to, uint256 a) external { balanceOf[to] += a; } function approve(address s, uint256 a) external returns (bool) { allowance[msg.sender][s] = a; return true; } function transfer(address to, uint256 a) external returns (bool) { balanceOf[msg.sender] -= a; balanceOf[to] += a; return true; } function transferFrom(address f, address to, uint256 a) external returns (bool) { allowance[f][msg.sender] -= a; balanceOf[f] -= a; balanceOf[to] += a; return true; } } /// Stands in for a venue: takes tokenIn and pays `rate` tokenOut per tokenIn. contract Venue { function swap(Token tIn, Token tOut, uint256 amt, uint256 rate, address to) external { tIn.transferFrom(msg.sender, address(this), amt); tOut.mint(to, amt * rate); } } /// Stands in for the builder's executor: pays `out` of tokenOut to the recipient. contract Executor { function fill(Token tOut, address recipient, uint256 out) external { tOut.mint(recipient, out); } /// A hostile route: uses all the gas it gets. function burn() external pure { while (true) { } } } contract Integrator is JitIntegration { Venue public immutable venue; uint256 public rate = 2000; // what your route pays per tokenIn constructor(IJITSettler jit_, address poster, Venue venue_) { jit = jit_; trustedPoster[poster] = true; venue = venue_; } function approve(Token token, address spender) external { token.approve(spender, type(uint256).max); } function _yourRoute(IJITSettler.Request memory req) internal override { venue.swap(Token(req.tokenIn), Token(req.tokenOut), req.amountIn, rate, req.recipient); } } contract JitIntegrationTest is Test, IJITEvents { JITSettler jit; Token weth = new Token(); Token usdc = new Token(); Venue venue = new Venue(); Executor executor = new Executor(); Integrator integ; address builder = makeAddr("builder poster"); address stranger = makeAddr("stranger"); address recipient = makeAddr("recipient"); uint256 constant AMT = 1e18; uint256 constant YOURS = AMT * 2000; // what the integrator's route pays uint256 constant MIN = AMT * 1900; function setUp() public { jit = new JITSettler(); integ = new Integrator(IJITSettler(address(jit)), builder, venue); weth.mint(address(integ), 10 * AMT); integ.approve(weth, address(jit)); integ.approve(weth, address(venue)); } function _req() internal view returns (IJITSettler.Request memory) { return IJITSettler.Request(address(weth), address(usdc), AMT, MIN, recipient); } function _post(address poster, uint256 out) internal { vm.prank(poster); jit.setRoute(address(executor), abi.encodeCall(Executor.fill, (usdc, recipient, out))); } function _trade(uint256 flow) internal returns (uint256) { if (flow == 0) return integ.tradeEfficient(_req()); if (flow == 1) return integ.tradeSafe(_req()); if (flow == 2) return integ.tradeEfficientCallback(_req()); return integ.tradeSafeCallback(_req()); } function _expectDefault() internal { vm.expectEmit(address(jit)); emit JITDefaultV1( address(integ), address(weth), address(usdc), recipient, AMT, MIN, YOURS ); } // No route posted: your route runs and is reported. function test_noRoute_allFlows() public { for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); _expectDefault(); assertEq(_trade(f), YOURS); vm.revertToState(snap); } } // A better route from the trusted poster fills in every flow. function test_betterRoute_fills_allFlows() public { for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); _post(builder, YOURS + 5); assertEq(_trade(f), YOURS + 5); assertEq(usdc.balanceOf(recipient), YOURS + 5); assertEq(weth.balanceOf(address(integ)), 9 * AMT); vm.revertToState(snap); } } // A better route from another poster: no flow runs it. function test_untrustedPoster_betterRoute_fallsBack_allFlows() public { for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); _post(stranger, YOURS + 5); _expectDefault(); assertEq(_trade(f), YOURS); assertEq(usdc.balanceOf(recipient), YOURS); vm.revertToState(snap); } } // A worse route from another poster never fills. function test_untrustedPoster_worseRoute_fallsBack_allFlows() public { for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); _post(stranger, YOURS - 5); _expectDefault(); assertEq(_trade(f), YOURS); assertEq(usdc.balanceOf(recipient), YOURS); vm.revertToState(snap); } } // A route that uses all its gas: the safe flows cap it, and your route still runs within the // transaction's gas. function test_gasBurn_safeFallsBack() public { for (uint256 f = 1; f < 4; f += 2) { uint256 snap = vm.snapshotState(); vm.prank(builder); jit.setRoute(address(executor), abi.encodeCall(Executor.burn, ())); _expectDefault(); uint256 got = f == 1 ? integ.tradeSafe{ gas: 2_000_000 }(_req()) : integ.tradeSafeCallback{ gas: 2_000_000 }(_req()); assertEq(got, YOURS); vm.revertToState(snap); } } // A route below amountOutMin reverts inside swap, and your route runs. function test_belowMinimum_fallsBack_allFlows() public { for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); _post(builder, MIN - 1); assertEq(_trade(f), YOURS); assertEq(usdc.balanceOf(recipient), YOURS); vm.revertToState(snap); } } // A route that clears amountOutMin but pays less than your route: the efficient flows // fill (they trust the builder not to post it), the safe flows fall back. function test_worseRoute_efficientFills_safeFallsBack() public { for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); _post(builder, YOURS - 5); bool safe = f == 1 || f == 3; assertEq(_trade(f), safe ? YOURS : YOURS - 5); assertEq(usdc.balanceOf(recipient), safe ? YOURS : YOURS - 5); assertEq(weth.balanceOf(address(integ)), 9 * AMT); vm.revertToState(snap); } } // A route that pays exactly what your route pays does not pass the safe flows. function test_equalRoute_safeFallsBack() public { for (uint256 f = 1; f < 4; f += 2) { uint256 snap = vm.snapshotState(); _post(builder, YOURS); _expectDefault(); assertEq(_trade(f), YOURS); vm.revertToState(snap); } } // A route from another poster that uses all its gas: the safe flows do not run it or the // trial, so your route fits in a small gas budget. function test_untrustedPoster_gasBurn_safeSkipsIt() public { for (uint256 f = 1; f < 4; f += 2) { uint256 snap = vm.snapshotState(); vm.prank(stranger); jit.setRoute(address(executor), abi.encodeCall(Executor.burn, ())); _expectDefault(); uint256 got = f == 1 ? integ.tradeSafe{ gas: 400_000 }(_req()) : integ.tradeSafeCallback{ gas: 400_000 }(_req()); assertEq(got, YOURS); vm.revertToState(snap); } } function test_selfCalls_areClosed() public { IJITSettler.Request memory q = _req(); vm.expectRevert("only self"); integ.swapChecked(q, 0); vm.expectRevert("only self"); integ.trialYourRoute(q); vm.expectRevert("only self"); integ.yourRouteOnce(q); vm.expectRevert("not in a trade"); integ.routeAvailable(builder); } // Gas of each flow, for the docs. function test_gas() public { string[4] memory names = ["efficient, peek", "safe, peek", "efficient, callback", "safe, callback"]; for (uint256 f; f < 4; f++) { uint256 snap = vm.snapshotState(); uint256 g = gasleft(); _trade(f); emit log_named_uint(string.concat("no route | ", names[f]), g - gasleft()); vm.revertToState(snap); _post(builder, YOURS + 5); g = gasleft(); _trade(f); emit log_named_uint(string.concat("fill | ", names[f]), g - gasleft()); vm.revertToState(snap); } } }