Skip to main content

Trade

@pegasys-fi/v3-sdk / Exports / Trade

Class: Trade<TInput, TOutput, TTradeType>

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

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20
TOutputextends CurrencyThe output token, either Ether or an ERC-20
TTradeTypeextends TradeTypeThe trade type, either exact input or exact output

Table of contents

Constructors

Properties

Accessors

Methods

Constructors

constructor

Private new Trade<TInput, TOutput, TTradeType>(__namedParameters)

Construct a trade by passing in the pre-computed property values

Type parameters

NameType
TInputextends Currency
TOutputextends Currency
TTradeTypeextends TradeType

Parameters

NameType
__namedParametersObject
__namedParameters.routes{ inputAmount: CurrencyAmount<TInput> ; outputAmount: CurrencyAmount<TOutput> ; route: Route<TInput, TOutput> }[]
__namedParameters.tradeTypeTTradeType

Defined in

entities/trade.ts:397

Properties

_executionPrice

Private _executionPrice: undefined | Price<TInput, TOutput>

The cached result of the computed execution price

Defined in

entities/trade.ts:143


_inputAmount

Private _inputAmount: undefined | CurrencyAmount<TInput>

The cached result of the input amount computation

Defined in

entities/trade.ts:97


_outputAmount

Private _outputAmount: undefined | CurrencyAmount<TOutput>

The cached result of the output amount computation

Defined in

entities/trade.ts:120


_priceImpact

Private _priceImpact: undefined | Percent

The cached result of the price impact computation

Defined in

entities/trade.ts:164


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

entities/trade.ts:82


tradeType

Readonly tradeType: TTradeType

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

Defined in

entities/trade.ts:91

Accessors

executionPrice

get executionPrice(): Price<TInput, TOutput>

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

Returns

Price<TInput, TOutput>

Defined in

entities/trade.ts:148


inputAmount

get inputAmount(): CurrencyAmount<TInput>

The input amount for the trade assuming no slippage.

Returns

CurrencyAmount<TInput>

Defined in

entities/trade.ts:102


outputAmount

get outputAmount(): CurrencyAmount<TOutput>

The output amount for the trade assuming no slippage.

Returns

CurrencyAmount<TOutput>

Defined in

entities/trade.ts:125


priceImpact

get priceImpact(): Percent

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

Returns

Percent

Defined in

entities/trade.ts:169


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

entities/trade.ts:73

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

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

Returns

CurrencyAmount<TInput>

The amount in

Defined in

entities/trade.ts:456


minimumAmountOut

minimumAmountOut(slippageTolerance, amountOut?): CurrencyAmount<TOutput>

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

Parameters

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

Returns

CurrencyAmount<TOutput>

The amount out

Defined in

entities/trade.ts:438


worstExecutionPrice

worstExecutionPrice(slippageTolerance): Price<TInput, TOutput>

Return the execution price after accounting for slippage tolerance

Parameters

NameTypeDescription
slippageTolerancePercentthe allowed tolerated slippage

Returns

Price<TInput, TOutput>

The execution price

Defined in

entities/trade.ts:471


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

NameType
TInputextends Currency
TOutputextends Currency

Parameters

NameTypeDefault 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

Defined in

entities/trade.ts:495


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

NameType
TInputextends Currency
TOutputextends Currency

Parameters

NameTypeDefault 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

Defined in

entities/trade.ts:576


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

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20
TOutputextends CurrencyThe output token, either Ether or an ERC-20
TTradeTypeextends TradeTypeThe type of the trade, either exact in or exact out

Parameters

NameTypeDescription
constructorArgumentsObjectThe 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

Defined in

entities/trade.ts:346


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

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20
TOutputextends CurrencyThe output token, either Ether or an ERC-20
TTradeTypeextends TradeTypeThe type of the trade, either exact in or exact out

Parameters

NameTypeDescription
constructorArgumentsObjectThe arguments passed to the trade constructor
constructorArguments.routes{ inputAmount: CurrencyAmount<TInput> ; outputAmount: CurrencyAmount<TOutput> ; route: Route<TInput, TOutput> }[]-
constructorArguments.tradeTypeTTradeType-

Returns

Trade<TInput, TOutput, TTradeType>

The unchecked trade

Defined in

entities/trade.ts:377


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

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20
TOutputextends CurrencyThe output token, either Ether or an ERC-20

Parameters

NameTypeDescription
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

Defined in

entities/trade.ts:194


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

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20
TOutputextends CurrencyThe output token, either Ether or an ERC-20

Parameters

NameTypeDescription
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

Defined in

entities/trade.ts:209


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

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20.
TOutputextends CurrencyThe output token, either Ether or an ERC-20.
TTradeTypeextends TradeTypeThe type of the trade, either exact in or exact out.

Parameters

NameTypeDescription
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

Defined in

entities/trade.ts:226


fromRoutes

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

Constructs a trade from routes by simulating swaps

Type parameters

NameTypeDescription
TInputextends CurrencyThe input token, either Ether or an ERC-20.
TOutputextends CurrencyThe output token, either Ether or an ERC-20.
TTradeTypeextends TradeTypeThe type of the trade, either exact in or exact out.

Parameters

NameTypeDescription
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
tradeTypeTTradeTypewhether the trade is an exact input or exact output swap

Returns

Promise<Trade<TInput, TOutput, TTradeType>>

The trade

Defined in

entities/trade.ts:276