Smart Router

A tool that provides better trading strategy

Background

To help users boost returns, SUN.io has rolled out the Smart Router feature, which plays an essential role in the following aspects:

  1. Executing trades at the best price: The Smart Router finds the best price for traders by choosing the optimal path to execute trades based on the price difference across markets. This means users get more favorable trade prices at execution to maximize returns.

  2. Enhancing liquidity: The Smart Router utilizes the liquidity of different markets to provide better depth and liquidity for traders. By executing trades across multiple liquidity pools, it reduces slippage and market shocks, thus offering an enhanced trading experience.

  3. Improving liquidity utilization: The Smart Router helps improve the utilization of liquidity. By channeling liquidity from highly liquid markets to less liquid ones, it helps improve the overall utilization of liquidity and enables the completion of more trades.

Mechanism

SUN.io's Smart Router algorithm service retrieves data from SunSwap V1, SunSwap V2, SunSwap V3, PSM, and SunCurve by utilizing the contract image built on the server. It sorts trade paths by the amount of the token users can obtain and displays the top three as options for users. The contract of the Smart Router is a nested contract through which the liquidity pools on SunSwap V1, SunSwap V2, SunSwap V3, PSM, and SunCurve can be called to complete trades.

Smart Router Contract

Contract address on Mainnet: TFVisXFaijZfeyeSjCEVkHfex7HGdTxzF9

Contract address on Nile testnet: TB6xBCixqRPUSKiXb45ky1GhChFJ7qrfFj

Smart Router Algorithm Service

Smart Router algorithm service URL: https://rot.endjgfsv.link/swap/router

Contract Interaction

We utilize TronWeb to facilitate interaction with on-chain contracts. First, you need to initialize the TronWeb instance.

Initialize the TronWeb
const TronWeb = require('tronweb')
const privateKey = process.env.PRIVATE_KEY
const apiKey = process.env.API_KEY

var tronWeb = new TronWeb({
	fullHost: "https://api.trongrid.io",
	headers: { "TRON-PRO-API-KEY": apiKey },
	privateKey: privateKey,
      })

Get Smart Router Info

Smart Router algorithm service endpoint

NameDescription

fromToken

Address of fromToken

toToken

Address of toToken

amountIn

Amount of the token to be swapped

typeList

Types available for the swap

(PSM,CURVE,CURVE_COMBINATION,WTRX,SUNSWAP_V1,SUN

_V2,SUNSWAP_V3)

  • Return

    NameDescription

    amountIn

    Amount of the token entered (divided by precision)

    amountOut

    Amount of the token that can be swapped for, calculated by the Smart Router (divided by precision)

    InUsd

    USD price of the entered token

    OutUsd

    USD price of the token to be swapped for

    impact

    Price impact

    fee

    Transaction fee

    tokens

    Addresses of the tokens that the path from fromToken to toToken involves

    symbols

    Symbols of the tokens that the path from fromToken to toToken involves

    poolFees

    Transaction fees of the liquidity pools that the path from fromToken to toToken involves

    (0 is displayed for non-SunSwap V3 pools)

    poolVersions

    Versions of the liquidity pools that the path from fromToken to toToken involves

    stepAmountsOut

    Amounts of the tokens obtained from each pool along the path from fromToken to toToken

>>> curl 'https://rot.endjgfsv.link/swap/router?fromToken=TWrZRHY9aKQZcyjpovdH6qeCEyYZrRQDZt&toToken=TG9XJ75ZWcUw69W8xViEJZQ365fRupGkFP&amountIn=100000000000000000&typeList=PSM,CURVE,CURVE_COMBINATION,WTRX,SUNSWAP_V1,SUNSWAP_V2,SUNSWAP_V3'
{"code":0,"message":"SUCCESS","data":[{"amountIn":"0.100000000000000000","amountOut":"0.00340809","inUsd":"0.000552211279796638800000000000000000","outUsd":"102.28490008408047256262147451","impact":"-0.004658","fee":"0.001019799010000000","tokens":["TWrZRHY9aKQZcyjpovdH6qeCEyYZrRQDZt","TGfVzt44kg6ZJ4fUqpHzJy3Jb37YMf8pMH","TGjgvdTWWrybVLaVeFqSyVqJQWjxqRYbaK","TG9XJ75ZWcUw69W8xViEJZQ365fRupGkFP"],"symbols":["SUN","HT","USDD","BTC"],"poolFees":["100","10000","100","0"],"poolVersions":["v3","v3","v3"],"stepAmountsOut":["0.204301041106368782","0.306491628388168604","0.00340809"]},{"amountIn":"0.100000000000000000","amountOut":"0.00315241","inUsd":"0.000552211279796638800000000000000000","outUsd":"94.61133417076900038177793499","impact":"-0.000471","fee":"0.001593109000000000","tokens":["TWrZRHY9aKQZcyjpovdH6qeCEyYZrRQDZt","TYsbWxNnyTgsZaTFaue9hqpxkU3Fkco94a","T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb","TF17BgPaZYbz8oxbjhriubPDsA7ArKoLX3","TG9XJ75ZWcUw69W8xViEJZQ365fRupGkFP"],"symbols":["SUN","WTRX","TRX","JST","BTC"],"poolFees":["0","0","0","10000","0"],"poolVersions":["v2","v2","v1","v3"],"stepAmountsOut":["9.406036","9.406036","5797.537373084257622564","0.00315241"]},{"amountIn":"0.100000000000000000","amountOut":"0.00311306","inUsd":"0.000552211279796638800000000000000000","outUsd":"93.43034692621015170250621534","impact":"-0.000305","fee":"0.001019799010000000","tokens":["TWrZRHY9aKQZcyjpovdH6qeCEyYZrRQDZt","TRz7J6dD2QWxBoumfYt4b3FaiRG23pXfop","TGjgvdTWWrybVLaVeFqSyVqJQWjxqRYbaK","TG9XJ75ZWcUw69W8xViEJZQ365fRupGkFP"],"symbols":["SUN","TUSD","USDD","BTC"],"poolFees":["10000","100","100","0"],"poolVersions":["v3","v3","v3"],"stepAmountsOut":["0.279484570175183752","0.279951568868017126","0.00311306"]}]}

Trade Execution

  • Trade

    • Name: swapExactInput( address[], string[], uint256[], uint24[], SwapData)

    • Parameters: array of paths, array of pool versions, array of lengths of adjacent pool versions, array of fee rates, [amount of the token to be swapped, minimum acceptable amount of the token obtained from the swap, address to receive the token obtained from the swap, deadline].

    >>> let contract = await tronWeb.getContract('TQAvWQpT9H916GckwWDJNhYZvQMkuRL7PN')
    >>> await router.swapExactInput(
                        ['TWrZRHY9aKQZcyjpovdH6qeCEyYZrRQDZt','TGfVzt44kg6ZJ4fUqpHzJy3Jb37YMf8pMH','TGjgvdTWWrybVLaVeFqSyVqJQWjxqRYbaK','TG9XJ75ZWcUw69W8xViEJZQ365fRupGkFP'],
                        ['v3'],
                        [4],
                        [100,10000,100,0],
                        ['100000000000000000','1', 'TF5MekHgFz6neU7zTpX4h2tha5miPDUj3z',1662825600]
                    ).send({feeLimit: 10000 * 1e6,shouldPollResponse: true});

Last updated