How a consumer turns input amounts into a Trade with the params needed to execute on-chain.
sequenceDiagram
accTitle: Trade execution sequence
accDescr { Consumer builds Token instances, fetches the Pair via the Pair Fetcher, constructs a Route, instantiates a Trade (exactIn or exactOut), reads slippage bounds, and submits router calldata. }
autonumber
actor C as Consumer
participant F as Pair Fetcher
participant R as Route
participant T as Trade
C->>C: build Token instances (in, out)
C->>F: fetchPairData(tokenA, tokenB, provider)
F->>F: derive pair address (factory + init code hash)
F-->>C: Pair (with reserves)
C->>R: new Route([pair, ...], input, output)
R-->>C: Route
alt EXACT_INPUT
C->>T: Trade.exactIn(route, inputAmount)
else EXACT_OUTPUT
C->>T: Trade.exactOut(route, outputAmount)
end
T-->>C: Trade (inputAmount, outputAmount, priceImpact)
C->>T: minimumAmountOut(slippage) / maximumAmountIn(slippage)
T-->>C: bound CurrencyAmount
Note over C: Format router calldata, sign, and submit
- Consumer builds
Tokeninstances for the input and output currencies. EachTokencarries its chain id, address, and decimals. - Consumer asks the Fetcher for the
Pairbetween the two tokens. The fetcher derives the pair address (factory + init code hash) and reads reserves from chain via the ethers provider. - Consumer constructs a Route from the input currency through one or more pairs to the output currency. Single-hop routes use one pair; multi-hop routes chain pairs together.
- Consumer instantiates a Trade:
Trade.exactIn(route, inputAmount)— fix the input, derive the output.Trade.exactOut(route, outputAmount)— fix the output, derive the input.
- Consumer picks a slippage
Percentand reads the bounds:trade.minimumAmountOut(slippage)forEXACT_INPUT.trade.maximumAmountIn(slippage)forEXACT_OUTPUT.
- Consumer uses the
Routerhelpers to format calldata for the swap, then signs and sends it through the ethers signer.
Tokeninstances on the same chain.- An ethers v5 provider.
- A user-chosen slippage tolerance.
- A
TradewithinputAmount,outputAmount,route,priceImpact, andexecutionPrice. - Router calldata for on-chain execution.
SDK consumers building swap UIs, aggregators, or back-office tools.