Trade
The Trade entity represents a fully specified trade along a route. This entity supplies all the information necessary to craft a router transaction.
#
Example#
Properties#
routeThe path property of the route should be passed as the path parameter to router functions.
#
tradeTypeTradeType.EXACT_INPUT
corresponds to swapExact*For*
router functions. TradeType.EXACT_OUTPUT
corresponds to swap*ForExact*
router functions.
#
inputAmountFor exact input trades, this value should be passed as amountIn to router functions. For exact output trades, this value should be multiplied by a factor >1, representing slippage tolerance, and passed as amountInMax to router functions.
#
outputAmountFor exact output trades, this value should be passed as amountOut to router functions. For exact input trades, this value should be multiplied by a factor <1, representing slippage tolerance, and passed as amountOutMin to router functions.
#
executionPriceThe average price that the trade would execute at.
#
nextMidPriceWhat the new mid price would be if the trade were to execute.
#
slippageThe slippage incurred by the trade.
- Strictly > .30%.
#
MethodsIn the context of the following two methods, slippage refers to the percent difference between the actual price and the trade executionPrice
.
#
minimumAmountOutReturns the minimum amount of the output token that should be received from a trade, given the slippage tolerance.
Useful when constructing a transaction for a trade of type EXACT_IN
.
#
maximumAmountInReturns the maximum amount of the input token that should be spent on the trade, given the slippage tolerance.
Useful when constructing a transaction for a trade of type EXACT_OUT
.
#
Static methodsThese static methods provide ways to construct ideal trades from lists of pairs. Note these methods do not perform any aggregation across routes, as routes are linear. It's possible that a better price can be had by combining multiple trades across different routes.
#
bestTradeExactInGiven a list of pairs, a fixed amount in, and token amount out,
this method returns the best maxNumResults
trades that swap
an input token amount to an output token, making at most maxHops
hops.
The returned trades are sorted by output amount, in decreasing order, and
all share the given input amount.
#
bestTradeExactOutSimilar to the above method, but targets a fixed output token amount. The returned trades are sorted by input amount, in increasing order, and all share the given output amount.