When both tokens are on the same chain, a swap is one transaction the user sends. Tern finds the best route across DEX aggregators and builds the transaction. The user receives at least the quoted minimum, or the transaction reverts and nothing moves. There’s no secret, no deposit address, and no waiting on a solver. This guide swaps 0.01 ETH for USDC on Base. The Quickstart runs the same flow as a script.

Before you start

  • The user’s address, which sends the swap and pays the gas.
  • The user’s wallet, to sign the transaction.
  • An API key, for production. You can build and test without one.
1

Get a price

Send the two tokens and the amount. Use native for the chain’s native token.
  • route: "swap" means it happens on one chain.
  • to.amount is what the user should receive after Tern’s fee, and to.minAmount the least they’ll receive.
  • fees breaks down the cost: see Fees and pricing.
  • sourceGas is the gas the user will pay. Include sender in the request and it only counts the approval that address actually needs.
  • etaSecs is how long the chain takes to confirm.
Wallets set aside extra gas while a transaction is pending. If the user is selling their entire balance of the native token, leave about three times sourceGas unsold.
2

Get the transaction

When the user confirms, ask again with sender (who sends and pays) and recipient (who receives). You can also set slippageBps: how far below the expected amount the swap may land before it reverts. It’s 50 (0.5%) by default and at most 300 (3%).
3

Send it right away

From sender’s wallet:
  • EVM chains: send execute.transactions in order. An approval comes first when it’s needed, and USDT on Ethereum may get an extra approval of zero before it. When selling the native token, the amount travels as the transaction’s value.
  • Starknet: send execute.calls together, as a single transaction.
Before the user signs, check that the transactions only approve the token being sold to Tern’s swap contract and then call it. Hard-code its address from Contract addresses.The quote lasts about 60 seconds, because prices move quickly. After expiresAt the contract refuses the swap, so get a new quote rather than sending late.
4

Track it

GET /v1/quote/{id} shows awaiting_deposit until the transaction confirms, then completed. A swap that isn’t sent in time becomes expired.Both source.txHash and destination.txHash are the swap’s one transaction. source.amount is what it spent and destination.amount is what the user received.

If something goes wrong

How it differs from a cross-chain swap

  • No secret, no reveal step, and no deposit address: only sender can send the swap.
  • If the minimum can’t be met, the transaction reverts and nothing moves. There’s never a refund to wait for.
  • The quote lasts about a minute, not 30 minutes.

Next steps

Private swaps

The same swap, from a shielded balance on Starknet.

TernSwap

How the swap contract guarantees the minimum.