Class: Trade
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
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20 |
TOutput | extends Currency | The output token, either Ether or an ERC-20 |
TTradeType | extends TradeType | The trade type, either exact input or exact output |
Table of contents
Constructors
Properties
Accessors
Methods
- maximumAmountIn
- minimumAmountOut
- worstExecutionPrice
- bestTradeExactIn
- bestTradeExactOut
- createUncheckedTrade
- createUncheckedTradeWithMultipleRoutes
- exactIn
- exactOut
- fromRoute
- fromRoutes
Constructors
constructor
Private
new Trade TInput
, TOutput
, TTradeType
(__namedParameters
)
Construct a trade by passing in the pre-computed property values
Type parameters
Name | Type |
---|---|
TInput | extends Currency |
TOutput | extends Currency |
TTradeType | extends TradeType |
Parameters
Name | Type |
---|---|
__namedParameters | Object |
__namedParameters.routes | (inputAmount : CurrencyAmount TInput ; outputAmount : CurrencyAmount TOutput ; route : Route TInput , TOutput )[] |
__namedParameters.tradeType | TTradeType |
Defined in
Properties
_executionPrice
Private
_executionPrice: undefined
| Price
TInput
, TOutput
The cached result of the computed execution price
Defined in
_inputAmount
Private
_inputAmount: undefined
| CurrencyAmount
TInput
The cached result of the input amount computation
Defined in
_outputAmount
Private
_outputAmount: undefined
| CurrencyAmount
TOutput
The cached result of the output amount computation
Defined in
_priceImpact
Private
_priceImpact: undefined
| Percent
The cached result of the price impact computation
Defined in
swaps
Readonly
swaps: ( inputAmount
: CurrencyAmount
TInput
; outputAmount
: CurrencyAmount
TOutput
; route
: Route
TInput
, TOutput
)[]
The swaps of the trade, i.e. which routes and how much is swapped in each that make up the trade.
Defined in
tradeType
Readonly
tradeType: TTradeType
The type of the trade, either exact in or exact out.
Defined in
Accessors
executionPrice
get
executionPrice(): Price
TInput
, TOutput
The price expressed in terms of output amount/input amount.
Returns
Price
TInput
, TOutput
Defined in
inputAmount
get
inputAmount(): CurrencyAmount
TInput
The input amount for the trade assuming no slippage.
Returns
CurrencyAmount
TInput
Defined in
outputAmount
get
outputAmount(): CurrencyAmount
TOutput
The output amount for the trade assuming no slippage.
Returns
CurrencyAmount
TOutput
Defined in
priceImpact
get
priceImpact(): Percent
Returns the percent difference between the route's mid price and the price impact
Returns
Percent
Defined in
route
get
route(): Route
TInput
, TOutput
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
Defined in
Methods
maximumAmountIn
maximumAmountIn(slippageTolerance
, amountIn?
): CurrencyAmount
TInput
Get the maximum amount in that can be spent via this trade for the given slippage tolerance
Parameters
Name | Type | Description |
---|---|---|
slippageTolerance | Percent | The tolerance of unfavorable slippage from the execution price of this trade |
amountIn | CurrencyAmount TInput | - |
Returns
CurrencyAmount
TInput
The amount in
Defined in
minimumAmountOut
minimumAmountOut(slippageTolerance
, amountOut?
): CurrencyAmount
TOutput
Get the minimum amount that must be received from this trade for the given slippage tolerance
Parameters
Name | Type | Description |
---|---|---|
slippageTolerance | Percent | The tolerance of unfavorable slippage from the execution price of this trade |
amountOut | CurrencyAmount TOutput | - |
Returns
CurrencyAmount
TOutput
The amount out
Defined in
worstExecutionPrice
worstExecutionPrice(slippageTolerance
): Price
TInput
, TOutput
Return the execution price after accounting for slippage tolerance
Parameters
Name | Type | Description |
---|---|---|
slippageTolerance | Percent | the allowed tolerated slippage |
Returns
Price
TInput
, TOutput
The execution price
Defined in
bestTradeExactIn
Static
bestTradeExactIn TInput
, TOutput
(pools
, currencyAmountIn
, currencyOut
, __namedParameters?
, currentPools?
, nextAmountIn?
, bestTrades?
): Promise
Trade
TInput
, TOutput
, EXACT_INPUT
[]
Given a list of pools, and a fixed amount in, returns the top maxNumResults
trades that go from an input token
amount to an output token, 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
Name | Type |
---|---|
TInput | extends Currency |
TOutput | extends Currency |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
pools | Pool [] | undefined | the pools to consider in finding the best trade |
currencyAmountIn | CurrencyAmount TInput | undefined | used in recursion; the original value of the currencyAmountIn parameter |
currencyOut | TOutput | undefined | the desired currency out |
__namedParameters | BestTradeOptions | {} | - |
currentPools | Pool [] | [] | used in recursion; the current list of pools |
nextAmountIn | CurrencyAmount Currency | currencyAmountIn | exact amount of input currency to spend |
bestTrades | Trade TInput , TOutput , EXACT_INPUT [] | [] | used in recursion; the current list of best trades |
Returns
Promise
Trade
TInput
, TOutput
, EXACT_INPUT
[]
The exact in trade
Defined in
bestTradeExactOut
Static
bestTradeExactOut TInput
, TOutput
(pools
, currencyIn
, currencyAmountOut
, __namedParameters?
, currentPools?
, nextAmountOut?
, bestTrades?
): Promise
Trade
TInput
, TOutput
, EXACT_OUTPUT
[]
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 token
to an output token 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
Name | Type |
---|---|
TInput | extends Currency |
TOutput | extends Currency |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
pools | Pool [] | undefined | the pools to consider in finding the best trade |
currencyIn | TInput | undefined | the currency to spend |
currencyAmountOut | CurrencyAmount TOutput | undefined | the desired currency amount out |
__namedParameters | BestTradeOptions | {} | - |
currentPools | Pool [] | [] | used in recursion; the current list of pools |
nextAmountOut | CurrencyAmount Currency | currencyAmountOut | the exact amount of currency out |
bestTrades | Trade TInput , TOutput , EXACT_OUTPUT [] | [] | used in recursion; the current list of best trades |
Returns
Promise
Trade
TInput
, TOutput
, EXACT_OUTPUT
[]
The exact out trade
Defined in
createUncheckedTrade
Static
createUncheckedTrade TInput
, TOutput
, TTradeType
(constructorArguments
): Trade
TInput
, TOutput
, TTradeType
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
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20 |
TOutput | extends Currency | The output token, either Ether or an ERC-20 |
TTradeType | extends TradeType | The type of the trade, either exact in or exact out |
Parameters
Name | Type | Description |
---|---|---|
constructorArguments | Object | The arguments passed to the trade constructor |
constructorArguments.inputAmount | CurrencyAmount TInput | - |
constructorArguments.outputAmount | CurrencyAmount TOutput | - |
constructorArguments.route | Route TInput , TOutput | - |
constructorArguments.tradeType | TTradeType | - |
Returns
Trade
TInput
, TOutput
, TTradeType
The unchecked trade
Defined in
createUncheckedTradeWithMultipleRoutes
Static
createUncheckedTradeWithMultipleRoutes TInput
, TOutput
, TTradeType
(constructorArguments
): Trade
TInput
, TOutput
, TTradeType
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
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20 |
TOutput | extends Currency | The output token, either Ether or an ERC-20 |
TTradeType | extends TradeType | The type of the trade, either exact in or exact out |
Parameters
Name | Type | Description |
---|---|---|
constructorArguments | Object | The arguments passed to the trade constructor |
constructorArguments.routes | ( inputAmount : CurrencyAmount TInput ; outputAmount : CurrencyAmount TOutput ; route : Route TInput , TOutput )[] | - |
constructorArguments.tradeType | TTradeType | - |
Returns
Trade
TInput
, TOutput
, TTradeType
The unchecked trade
Defined in
exactIn
Static
exactIn TInput
, TOutput
(route
, amountIn
): Promise
Trade
TInput
, TOutput
, EXACT_INPUT
Constructs an exact in trade with the given amount in and route
Type parameters
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20 |
TOutput | extends Currency | The output token, either Ether or an ERC-20 |
Parameters
Name | Type | Description |
---|---|---|
route | Route TInput , TOutput | The route of the exact in trade |
amountIn | CurrencyAmount TInput | The amount being passed in |
Returns
Promise
Trade
TInput
, TOutput
, EXACT_INPUT
The exact in trade
Defined in
exactOut
Static
exactOut TInput
, TOutput
(route
, amountOut
): Promise
Trade
TInput
, TOutput
, EXACT_OUTPUT
Constructs an exact out trade with the given amount out and route
Type parameters
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20 |
TOutput | extends Currency | The output token, either Ether or an ERC-20 |
Parameters
Name | Type | Description |
---|---|---|
route | Route TInput , TOutput | The route of the exact out trade |
amountOut | CurrencyAmount TOutput | The amount returned by the trade |
Returns
Promise
Trade
TInput
, TOutput
, EXACT_OUTPUT
The exact out trade
Defined in
fromRoute
Static
fromRoute TInput
, TOutput
, TTradeType
(route
, amount
, tradeType
): Promise
Trade
TInput
, TOutput
, TTradeType
Constructs a trade by simulating swaps through the given route
Type parameters
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20. |
TOutput | extends Currency | The output token, either Ether or an ERC-20. |
TTradeType | extends TradeType | The type of the trade, either exact in or exact out. |
Parameters
Name | Type | Description |
---|---|---|
route | Route TInput , TOutput | route to swap through |
amount | TTradeType extends EXACT_INPUT ? CurrencyAmount TInput : CurrencyAmount TOutput | the amount specified, either input or output, depending on tradeType |
tradeType | TTradeType | whether the trade is an exact input or exact output swap |
Returns
Promise
Trade
TInput
, TOutput
, TTradeType
The route
Defined in
fromRoutes
Static
fromRoutes TInput
, TOutput
, TTradeType
(routes
, tradeType
): Promise
Trade
TInput
, TOutput
, TTradeType
Constructs a trade from routes by simulating swaps
Type parameters
Name | Type | Description |
---|---|---|
TInput | extends Currency | The input token, either Ether or an ERC-20. |
TOutput | extends Currency | The output token, either Ether or an ERC-20. |
TTradeType | extends TradeType | The type of the trade, either exact in or exact out. |
Parameters
Name | Type | Description |
---|---|---|
routes | ( amount : TTradeType extends EXACT_INPUT ? CurrencyAmount TInput : CurrencyAmount TOutput ; route : Route TInput , TOutput )[] | the routes to swap through and how much of the amount should be routed through each |
tradeType | TTradeType | whether the trade is an exact input or exact output swap |
Returns
Promise
Trade
TInput
, TOutput
, TTradeType
The trade