Initializes a new instance of the Router with configurations for connecting to a specified backend and retrieving pair exchange information.
Example:
const router = new Router({
pairsCacheInSeconds: 20,
protocols: [Protocol.SOROSWAP],
network: Networks.TESTNET,
})
Private
_maxPrivate
_networkPrivate
_pairPrivate
_protocolsPrivate
_quotePrivate
_buildConstructs a trade object from the provided route and amount details. This method finalizes the trade details, including calculating the minimum amount out for exact input trades, or maximum amount in for exact output trades.
The currency of the input token.
The currency of the output token.
The type of trade (EXACT_INPUT or EXACT_OUTPUT).
The selected route and amount details.
A trade object containing detailed information about the trade, or null if the routeAmount is null.
Private
_computeRecursively computes all routes from tokenIn to tokenOut using a depth-first search algorithm, considering a maximum number of hops.
The starting token for route computation.
The destination token for route computation.
An array of all available pairs to be considered for routing.
The ID of the blockchain network.
The current path being explored (used for recursion).
Accumulator for all valid paths found.
The original input token (used for recursion).
The maximum number of hops to consider.
An array of V2Route objects representing all computed routes.
Generic function to find the best distribution based on a strategy (minimization or maximization).
The total amount to distribute.
A function to compare values (e.g., Math.max or Math.min).
The initial value (positive or negative infinity).
Private
_findFinds the best route for a given exact input amount by comparing all possible routes from tokenIn to tokenOut. It selects the route with the best quote.
The exact amount of the input token.
The output token for the trade.
An array of potential routes to evaluate.
A promise that resolves to the route with the best quote, or null if no suitable route is found.
Private
_findFinds the best route for a given exact output amount by comparing all possible routes from tokenIn to tokenOut. It selects the route with the best quote suitable for the required output amount.
The desired output amount.
The input token for the trade.
An array of potential routes to evaluate.
A promise that resolves to the route with the best quote, or null if no suitable route is found.
Private
_getRetrieves all possible routes between a given input and output token pair by exploring all combinations of pairs provided by the PairProvider.
The input token for generating routes.
The output token for generating routes.
Optional
factoryAddress: stringOptional
sorobanContext: SorobanContextTypeA promise that resolves to an array of V2Route objects representing all possible routes.
Private
_getRetrieves all routes for swapping tokens based on the specified protocol.
The input token for the swap.
The output token for the swap.
The protocol to use for the swap.
Optional
factoryAddress: stringThe address of the factory contract.
Optional
sorobanContext: SorobanContextTypeThe Soroban context type.
A promise that resolves to the array of routes for swapping tokens.
Private
_getEvaluates all provided routes with their corresponding quotes to determine the best quote based on the specified trade type (exact input or output).
An array of potential routes to be evaluated.
Raw quote data for each route.
The token for which the quote is provided.
The type of trade, determining how to compare quotes.
The best quote found for the given trade type, or null if no suitable quote is found.
This is the main method and calculates the optimal route for a trade given the amount, quote currency, and trade type. Returns the trade route and details if successful; otherwise returns null.
Example:
const route = await router.route(amountCurrency, quoteCurrency, tradeType);
The amount for the trade.
The currency to quote the trade in.
The type of trade, either EXACT_INPUT or EXACT_OUTPUT.
Optional
factoryAddress: stringOptional
sorobanContext: SorobanContextTypeThe trade details including the route, or null if no route is found.
Finds the best route for an exact input amount given the input and output currencies and the input amount. Returns the trade details if successful.
Example:
if (tradeType === TradeType.EXACT_INPUT) {
const route = await router.routeExactIn(amountCurrency, quoteCurrency, amount);
}
The input currency.
The output currency.
The exact input amount.
The trade details for the best route, or null if no route is found.
Finds the best route for an exact output amount given the input and output currencies and the output amount. Returns the trade details if successful.
Example:
if (tradeType === TradeType.EXACT_OUTPUT) {
const route = await router.routeExactOut(amountCurrency, quoteCurrency, amount);
}
The input currency.
The output currency.
The exact output amount.
The trade details for the best route, or null if no route is found.
This method splits a given trade amount into multiple parts and determines the optimal distribution among different protocols.
The total trade amount to be split.
The currency to quote the trade in.
The type of trade, either EXACT_INPUT
or EXACT_OUTPUT
.
Optional
factoryAddress: stringOptional
sorobanContext: SorobanContextTypeA promise that resolves to an object containing the total value of the split amounts and the distribution details.
const result = await router.routeSplit(amountCurrency, quoteCurrency, TradeType.EXACT_INPUT);
console.log(result.totalAmount); // Output: 150
console.log(result.distribution);
// Output:
// [
// { protocol: Protocol.SOROSWAP, amount: 100, path: ['0x...', '0x...', '0x...'] },
// { protocol: Protocol.PHOENIX, amount: 50, path: ['0x...', '0x...', '0x...'] }
// ]
The Router class is the core of the soroswap-router-sdk, facilitating the discovery of optimal trade routes and quotes for token exchanges on a specified blockchain network. It leverages quote and pair providers to find the best exchange route based on the specified trade type, either exact input or exact output.