Skip to main content

Test your integration

On mainnet the builder decides when to post a route, so you cannot produce a fill on demand. In a test, post the route yourself. Anyone can post a route to the settler, so a test needs no special access.

Run the sample tests​

  • JitIntegration.sol is the sample contract from Integrate, with both integrations and their callback variants.
  • JitIntegration.t.sol tests each one with no route, a better route, a worse route, a route below amountOutMin, and routes from an untrusted poster (better and worse). It tests the two safe integrations with a route that uses all its gas, from the trusted poster and from another one. Most tests also check the JITDefaultV1 event of the report.

The tests deploy the settler from source and use mock tokens, so they need no fork and no RPC:

git clone --recursive https://github.com/flashbots/JITSettler
cd JITSettler
mkdir test/sample # put the two files here
forge test --match-path 'test/sample/*' -vv

Then replace the Integrator contract in the test with your contract, and its mock venue with your route.

Post a route​

A route is an executor address and the calldata for it. The settler transfers tokenIn to the executor, calls it with the calldata, and then checks what recipient received.

// Stands in for the builder's executor: pays `out` of tokenOut to the recipient.
contract Executor {
function fill(Token tokenOut, address recipient, uint256 out) external {
tokenOut.mint(recipient, out);
}
}

vm.prank(poster); // the address your contract trusts
jit.setRoute(address(executor), abi.encodeCall(Executor.fill, (usdc, recipient, out)));
// then, in the same block, call your contract with the request

Choose out to test each case: above what your route pays, below it, and below amountOutMin. Post from another address to test that your integration refuses the route. Post a worse route from the trusted address to test that a safe integration runs your route.

A route is live only at the block.timestamp it was posted at, and one successful swap uses it up.

On a mainnet fork​

To test against real tokens and your real route, fork mainnet and use the deployed settler in place of new JITSettler(). Post with vm.prank from the builder's poster address (reference), and fund your executor with tokenOut using deal. The mock executor above calls mint, which real tokens do not have. For the fork test, use an executor that transfers out from its own balance to recipient.

Troubleshooting​

SymptomCause and fix
peek returns 0 and swap reverts with NoRouteNo route is live. The post and the swap are at different timestamps, or an earlier swap already used the route.
peek returns an address and your contract still runs your routeThe poster is not in your contract's trusted list, or the route failed. In a safe integration, the route may also not have beaten yours, or it ran into the gas cap.
RouteDeclined()You passed callback = true and your contract declined: routeAvailable reverted, is not implemented, returned less than 32 bytes, or posted a route itself.
InsufficientOutput(a, b)The route paid recipient the amount a. The floor b is the larger of amountOutMin and the value your callback returned.
TransferFailed()Your contract lacks the tokenIn balance or the allowance to the settler.
Revert with no dataThe executor or tokenOut has no code, the executor reverted without data, or the call ran out of gas. A try does not catch a call to an address without code, so also check the settler address.
Any other revert dataIt comes from the route's executor or from tokenOut. The settler passes on at most 260 bytes of it.
On mainnet, no route is ever posted for your swapsA condition for routing is not met.