Swapping from Contracts
A contract cannot call the HyperFlow API itself, so swaps from a contract always have two parts:
- Off-chain, your keeper or backend calls
GET /swapwithreceiverset to your contract’s address, and gets the router and the transaction data. - On-chain, it passes those to your contract, which approves the input token, calls the router with that data, and checks how much it received.
The contract never builds router calldata itself — the data from /swap is the only supported way to call the routers.
What your contract must do
How long the data stays valid
- Each
/swapresponse encodes a deadline: by default 20 minutes after the call. Executing after it reverts. Passdeadline(Unix seconds) to shorten it — for a keeper that executes immediately, 1–2 minutes is enough. A deadline in the past is ignored and the default applies. - Each response also encodes a minimum output. Pass your own
minAmountOut(in wei) to set it;/swapreturns400if it is above the current quote less slippage, so you find out before spending gas. - Prices move. Request
/swapright before executing, not ahead of time.
Example contract
A minimal swapper, run against both routers on an Elysium fork in both directions (HYPE to USDC with an integrator fee, and USDC to HYPE). Adapt the access control and withdrawals to your vault.
Deploy it with the routers of your chain from Smart Contracts: the standard router, plus the integrator-fee router if you charge a fee.
Keeper side
Check the HTTP status and body before sending — see Errors.
Estimate gas for every transaction instead of setting a fixed limit. The quote’s totalGas covers the route only, not your contract’s own approvals and balance checks, and actual usage varies by route.
Integrator fee from a contract
To charge a fee, add feeBps and feeReceiver to the /swap call. The response then names the integrator-fee router, so that router must be on your allowlist. The fee is paid to feeReceiver inside the same transaction; your contract receives amountOut, which is already net of the fee. See Integrator fee.