Skip to main content

Trade

@uniswap/v4-sdk / Trade

Defined in: entities/trade.ts:66

Represents a trade executed against a set of routes where some percentage of the input is split across each route.

Each route has its own set of pools. Pools can not be re-used across routes.

Does not account for slippage, i.e., changes in price environment that can occur between the time the trade is submitted and when it is executed.

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20
TOutput extends CurrencyThe output currency, either Ether or an ERC-20
TTradeType extends TradeTypeThe trade type, either exact input or exact output

Properties

swaps

readonly swaps: object[]

Defined in: entities/trade.ts:83

The swaps of the trade, i.e. which routes and how much is swapped in each that make up the trade.

inputAmount

inputAmount: CurrencyAmount<TInput>

outputAmount

outputAmount: CurrencyAmount<TOutput>

route

route: Route<TInput, TOutput>


tradeType

readonly tradeType: TTradeType

Defined in: entities/trade.ts:92

The type of the trade, either exact in or exact out.

Accessors

executionPrice

Get Signature

get executionPrice(): Price<TInput, TOutput>

Defined in: entities/trade.ts:149

The price expressed in terms of output amount/input amount.

Returns

Price<TInput, TOutput>


inputAmount

Get Signature

get inputAmount(): CurrencyAmount<TInput>

Defined in: entities/trade.ts:103

The input amount for the trade assuming no slippage.

Returns

CurrencyAmount<TInput>


outputAmount

Get Signature

get outputAmount(): CurrencyAmount<TOutput>

Defined in: entities/trade.ts:126

The output amount for the trade assuming no slippage.

Returns

CurrencyAmount<TOutput>


priceImpact

Get Signature

get priceImpact(): Percent

Defined in: entities/trade.ts:170

Returns the percent difference between the route's mid price and the price impact

Returns

Percent


route

Get Signature

get route(): Route<TInput, TOutput>

Defined in: entities/trade.ts:74

Deprecated

Deprecated in favor of 'swaps' property. If the trade consists of multiple routes this will return an error.

When the trade consists of just a single route, this returns the route of the trade, i.e. which pools the trade goes through.

Returns

Route<TInput, TOutput>

Methods

bestTradeExactIn()

static bestTradeExactIn<TInput, TOutput>(pools, currencyAmountIn, currencyOut, __namedParameters, currentPools, nextAmountIn, bestTrades): Promise<Trade<TInput, TOutput, EXACT_INPUT>[]>

Defined in: entities/trade.ts:454

Given a list of pools, and a fixed amount in, returns the top maxNumResults trades that go from an input currency amount to an output currency, making at most maxHops hops. Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting the amount in among multiple routes.

Type Parameters

Type Parameter
TInput extends Currency
TOutput extends Currency

Parameters

ParameterTypeDefault valueDescription
poolsPool[]undefinedthe pools to consider in finding the best trade
currencyAmountInCurrencyAmount<TInput>undefinedused in recursion; the original value of the currencyAmountIn parameter
currencyOutTOutputundefinedthe desired currency out
__namedParametersBestTradeOptions{}-
currentPoolsPool[][]used in recursion; the current list of pools
nextAmountInCurrencyAmount<Currency>currencyAmountInexact amount of input currency to spend
bestTradesTrade<TInput, TOutput, EXACT_INPUT>[][]used in recursion; the current list of best trades

Returns

Promise<Trade<TInput, TOutput, EXACT_INPUT>[]>

The exact in trade


bestTradeExactOut()

static bestTradeExactOut<TInput, TOutput>(pools, currencyIn, currencyAmountOut, __namedParameters, currentPools, nextAmountOut, bestTrades): Promise<Trade<TInput, TOutput, EXACT_OUTPUT>[]>

Defined in: entities/trade.ts:533

similar to the above method but instead targets a fixed output amount given a list of pools, and a fixed amount out, returns the top maxNumResults trades that go from an input currency to an output currency amount, making at most maxHops hops note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting the amount in among multiple routes.

Type Parameters

Type Parameter
TInput extends Currency
TOutput extends Currency

Parameters

ParameterTypeDefault valueDescription
poolsPool[]undefinedthe pools to consider in finding the best trade
currencyInTInputundefinedthe currency to spend
currencyAmountOutCurrencyAmount<TOutput>undefinedthe desired currency amount out
__namedParametersBestTradeOptions{}-
currentPoolsPool[][]used in recursion; the current list of pools
nextAmountOutCurrencyAmount<Currency>currencyAmountOutthe exact amount of currency out
bestTradesTrade<TInput, TOutput, EXACT_OUTPUT>[][]used in recursion; the current list of best trades

Returns

Promise<Trade<TInput, TOutput, EXACT_OUTPUT>[]>

The exact out trade


createUncheckedTrade()

static createUncheckedTrade<TInput, TOutput, TTradeType>(constructorArguments): Trade<TInput, TOutput, TTradeType>

Defined in: entities/trade.ts:305

Creates a trade without computing the result of swapping through the route. Useful when you have simulated the trade elsewhere and do not have any tick data

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20
TOutput extends CurrencyThe output currency, either Ether or an ERC-20
TTradeType extends TradeTypeThe type of the trade, either exact in or exact out

Parameters

ParameterTypeDescription
constructorArguments{ inputAmount: CurrencyAmount<TInput>; outputAmount: CurrencyAmount<TOutput>; route: Route<TInput, TOutput>; tradeType: TTradeType; }The arguments passed to the trade constructor
constructorArguments.inputAmountCurrencyAmount<TInput>-
constructorArguments.outputAmountCurrencyAmount<TOutput>-
constructorArguments.routeRoute<TInput, TOutput>-
constructorArguments.tradeTypeTTradeType-

Returns

Trade<TInput, TOutput, TTradeType>

The unchecked trade


createUncheckedTradeWithMultipleRoutes()

static createUncheckedTradeWithMultipleRoutes<TInput, TOutput, TTradeType>(constructorArguments): Trade<TInput, TOutput, TTradeType>

Defined in: entities/trade.ts:336

Creates a trade without computing the result of swapping through the routes. Useful when you have simulated the trade elsewhere and do not have any tick data

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20
TOutput extends CurrencyThe output currency, either Ether or an ERC-20
TTradeType extends TradeTypeThe type of the trade, either exact in or exact out

Parameters

ParameterTypeDescription
constructorArguments{ routes: object[]; tradeType: TTradeType; }The arguments passed to the trade constructor
constructorArguments.routesobject[]-
constructorArguments.tradeTypeTTradeType-

Returns

Trade<TInput, TOutput, TTradeType>

The unchecked trade


exactIn()

static exactIn<TInput, TOutput>(route, amountIn): Promise<Trade<TInput, TOutput, EXACT_INPUT>>

Defined in: entities/trade.ts:195

Constructs an exact in trade with the given amount in and route

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20
TOutput extends CurrencyThe output currency, either Ether or an ERC-20

Parameters

ParameterTypeDescription
routeRoute<TInput, TOutput>The route of the exact in trade
amountInCurrencyAmount<TInput>The amount being passed in

Returns

Promise<Trade<TInput, TOutput, EXACT_INPUT>>

The exact in trade


exactOut()

static exactOut<TInput, TOutput>(route, amountOut): Promise<Trade<TInput, TOutput, EXACT_OUTPUT>>

Defined in: entities/trade.ts:210

Constructs an exact out trade with the given amount out and route

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20
TOutput extends CurrencyThe output currency, either Ether or an ERC-20

Parameters

ParameterTypeDescription
routeRoute<TInput, TOutput>The route of the exact out trade
amountOutCurrencyAmount<TOutput>The amount returned by the trade

Returns

Promise<Trade<TInput, TOutput, EXACT_OUTPUT>>

The exact out trade


fromRoute()

static fromRoute<TInput, TOutput, TTradeType>(route, amount, tradeType): Promise<Trade<TInput, TOutput, TTradeType>>

Defined in: entities/trade.ts:227

Constructs a trade by simulating swaps through the given route

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20.
TOutput extends CurrencyThe output currency, either Ether or an ERC-20.
TTradeType extends TradeTypeThe type of the trade, either exact in or exact out.

Parameters

ParameterTypeDescription
routeRoute<TInput, TOutput>route to swap through
amountTTradeType extends EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>the amount specified, either input or output, depending on tradeType
tradeTypeTTradeTypewhether the trade is an exact input or exact output swap

Returns

Promise<Trade<TInput, TOutput, TTradeType>>

The route


fromRoutes()

static fromRoutes<TInput, TOutput, TTradeType>(routes, tradeType): Promise<Trade<TInput, TOutput, TTradeType>>

Defined in: entities/trade.ts:272

Constructs a trade from routes by simulating swaps

Type Parameters

Type ParameterDescription
TInput extends CurrencyThe input currency, either Ether or an ERC-20.
TOutput extends CurrencyThe output currency, either Ether or an ERC-20.
TTradeType extends TradeTypeThe type of the trade, either exact in or exact out.

Parameters

ParameterTypeDescription
routesobject[]the routes to swap through and how much of the amount should be routed through each
tradeTypeTTradeTypewhether the trade is an exact input or exact output swap

Returns

Promise<Trade<TInput, TOutput, TTradeType>>

The trade


maximumAmountIn()

maximumAmountIn(slippageTolerance, amountIn): CurrencyAmount<TInput>

Defined in: entities/trade.ts:415

Get the maximum amount in that can be spent via this trade for the given slippage tolerance

Parameters

ParameterTypeDescription
slippageTolerancePercentThe tolerance of unfavorable slippage from the execution price of this trade
amountInCurrencyAmount<TInput>-

Returns

CurrencyAmount<TInput>

The amount in


minimumAmountOut()

minimumAmountOut(slippageTolerance, amountOut): CurrencyAmount<TOutput>

Defined in: entities/trade.ts:397

Get the minimum amount that must be received from this trade for the given slippage tolerance

Parameters

ParameterTypeDescription
slippageTolerancePercentThe tolerance of unfavorable slippage from the execution price of this trade
amountOutCurrencyAmount<TOutput>-

Returns

CurrencyAmount<TOutput>

The amount out


worstExecutionPrice()

worstExecutionPrice(slippageTolerance): Price<TInput, TOutput>

Defined in: entities/trade.ts:430

Return the execution price after accounting for slippage tolerance

Parameters

ParameterTypeDescription
slippageTolerancePercentthe allowed tolerated slippage

Returns

Price<TInput, TOutput>

The execution price