// SPDX-License-Identifier: MIT pragma solidity ^0.8.28; // Sample JIT routing integration for the BuilderNet docs. Four ways to call the settler: // // with peek without peek (callback) // efficient tradeEfficient tradeEfficientCallback // safe tradeSafe tradeSafeCallback // // Not audited. Add access control and your own route before you use any of it. interface IERC20 { function balanceOf(address) external view returns (uint256); } interface IJITSettler { struct Request { address tokenIn; address tokenOut; uint256 amountIn; uint256 amountOutMin; address recipient; } function peek(Request calldata req) external view returns (address poster); function swap(Request calldata req, bool callback) external returns (uint256 amountOut); function reportDefault(Request calldata req, uint256 amountOut) external; } abstract contract JitIntegration { // Keep both settable: a new settler is a new address, and a new builder is a new poster. IJITSettler public jit; mapping(address => bool) public trustedPoster; // The safe integrations cap the gas that a route can use. uint256 public routeGas = 1_500_000; error Measured(uint256 out); error YourRouteFailed(); // The request of the trade in progress. Set only while a trade runs. bool private transient _inTrade; bool private transient _safe; address private transient _tokenIn; address private transient _tokenOut; uint256 private transient _amountIn; uint256 private transient _amountOutMin; address private transient _recipient; // A route runs code you did not choose, in the middle of your transaction. modifier nonReentrant() { require(!_inTrade, "reentered"); _inTrade = true; _; _inTrade = false; } // For the helpers that a trade calls on this contract. Your route must never be able to // call this contract with calldata that someone else chose. modifier onlySelf() { require(msg.sender == address(this) && _inTrade, "only self"); _; } /// Your own route. Swaps `req.amountIn` and pays `req.tokenOut` to `req.recipient`. function _yourRoute(IJITSettler.Request memory req) internal virtual; // ---------------------------------------------------------------- efficient, with peek /// Fills through the builder's route when one is posted. Trusts the builder to post only /// routes that beat your route. function tradeEfficient(IJITSettler.Request calldata req) external nonReentrant returns (uint256) { // Always make this call. With no route it returns 0, and the builder finds your swap // from it. if (trustedPoster[jit.peek(req)]) { try jit.swap(req, false) returns (uint256 got) { return got; } catch { // The route failed, and no tokens were transferred. } } uint256 out = _runYourRoute(req); jit.reportDefault(req, out); // tells the builder what a route has to beat return out; } // ---------------------------------------------------------------- safe, with peek /// Runs a route only from a trusted poster, and only if the fill pays more than your route /// pays on the same state. Your route runs twice when a trusted poster's route loses. function tradeSafe(IJITSettler.Request calldata req) external nonReentrant returns (uint256) { if (trustedPoster[jit.peek(req)]) { (bool ok, uint256 yours) = _measureYourRoute(req); if (ok) { // The gas cap keeps enough gas for your route if this route uses all of its gas. try this.swapChecked{ gas: routeGas }(req, yours) returns (uint256 got) { return got; } catch { // The route failed or did not beat your route. The fill is undone. } } } uint256 out = _runYourRoute(req); jit.reportDefault(req, out); return out; } /// A revert here undoes the fill and leaves the rest of the transaction intact. function swapChecked(IJITSettler.Request calldata req, uint256 yours) external onlySelf returns (uint256 got) { got = jit.swap(req, false); require(got > yours, "not better"); } // ---------------------------------------------------------------- without peek (callback) /// As `tradeEfficient`, without `peek`: the settler calls `routeAvailable` when a route is /// posted. function tradeEfficientCallback(IJITSettler.Request calldata req) external nonReentrant returns (uint256) { return _tradeCallback(req, false); } /// As `tradeSafe`, without `peek`: `routeAvailable` measures your route and returns it as /// the floor. function tradeSafeCallback(IJITSettler.Request calldata req) external nonReentrant returns (uint256) { return _tradeCallback(req, true); } function _tradeCallback(IJITSettler.Request calldata req, bool safe) private returns (uint256) { _safe = safe; (_tokenIn, _tokenOut, _amountIn) = (req.tokenIn, req.tokenOut, req.amountIn); (_amountOutMin, _recipient) = (req.amountOutMin, req.recipient); // The safe integration caps the gas of the call. The cap also covers the trial of your // route in `routeAvailable`. uint256 gasCap = safe ? routeGas : gasleft(); // Always make this call. With no route it reverts with NoRoute, and the builder finds // your swap from it. try jit.swap{ gas: gasCap }(req, true) returns (uint256 got) { return got; } catch { // No route was posted, your contract declined it, or it failed. No tokens were transferred. } uint256 out = _runYourRoute(req); jit.reportDefault(req, out); return out; } /// Called by the settler during `swap(req, true)` when a route is posted. Return the least /// the recipient must receive, or revert to decline the route. function routeAvailable(address poster) external returns (uint256 floor) { require(msg.sender == address(jit) && _inTrade, "not in a trade"); require(trustedPoster[poster], "untrusted poster"); // a revert declines the route if (!_safe) return 0; // the floor stays at req.amountOutMin // Safe: the fill must also beat your route. (bool ok, uint256 yours) = _measureYourRoute( IJITSettler.Request(_tokenIn, _tokenOut, _amountIn, _amountOutMin, _recipient) ); require(ok, "your route failed"); return yours + 1; } // ---------------------------------------------------------------- your route // Runs your route and returns what it paid. The caller reports it with `reportDefault`. function _runYourRoute(IJITSettler.Request memory req) private returns (uint256 out) { uint256 before = IERC20(req.tokenOut).balanceOf(req.recipient); _yourRoute(req); out = IERC20(req.tokenOut).balanceOf(req.recipient) - before; require(out >= req.amountOutMin, "below minimum"); } // What your route would pay on the current state. Changes no state. function _measureYourRoute(IJITSettler.Request memory req) private returns (bool ok, uint256 yours) { try this.trialYourRoute(req) { } catch (bytes memory err) { if (err.length == 36 && bytes4(err) == Measured.selector) { ok = true; assembly ("memory-safe") { yours := mload(add(err, 36)) } } } } /// Always reverts: with `Measured(out)` if your route ran, with `YourRouteFailed` if not. /// The inner call keeps a contract in your route from forging `Measured`. function trialYourRoute(IJITSettler.Request calldata req) external onlySelf { try this.yourRouteOnce(req) returns (uint256 out) { revert Measured(out); } catch { revert YourRouteFailed(); } } function yourRouteOnce(IJITSettler.Request calldata req) external onlySelf returns (uint256 out) { uint256 before = IERC20(req.tokenOut).balanceOf(req.recipient); _yourRoute(req); out = IERC20(req.tokenOut).balanceOf(req.recipient) - before; } }