@hikaru-fi/swap-router

0.2.0 • Public • Published

swap-router

Router for swaps

Backend usage

Import getSwaps function from package:

const { getSwap } = require('hikaru-swap-router');

function calculateSwaps() {
  ...
  const result = getSwap(tokenIn, tokenOut, swapAmount, swapType, pools, options);
  ...
}

Input

To get required path you need to provide four arguments:

  • tokenIn: token that user will pay
  • tokenOut: token that user will receive
  • swapType: what kind of swap user wants (Sell/Buy)
  • swapAmount: amount of tokens that user wants to sell/buy

Swap types:

const SwapTypes = {
    // main swap types
    Sell: 'SellTokens',
    Buy: 'BuyTokens',

    VirtualSwap: 'VirtualSwap',
    None: 'None',
}

Output

If everything is ok, you will receive object with payload that can be used to call smart contracts:

/**
 * @typedef RouteInfo
 * @property {String} swapType
 * @property {String} expectedResult
 * @property {SellTokens | BuyTokens | VirtualSwap} swapRoute
 */

Where:

  • swapType - type of swap that determines what contract functions you need to call
  • expectedResult - expected result of swap (expected receive/pay amount of tokens)
  • swapRoute - Object containing required information for calculating/performing swaps

There are three swapTypes:

  • SellTokens:
    • function for swap result calculation: calculateSellTokens
    • function for performing swap: sellTokens
    • Object with parameters for router smart contract - SellTokens type:
/**
 * @typedef SellTokens
 * @property {String} vault
 * @property {String} pool
 * @property {String} tokenIn
 * @property {String} tokenOut
 * @property {String} amountIn
 * @property {String} minAmountOut
 * @property {String} deadline
 */
  • BuyTokens:
    • function for swap result calculation: calculateBuyTokens
    • function for performing swap: buyTokens
    • Object with parameters for router smart contract - BuyTokens type:
/**
 * @typedef BuyTokens
 * @property {String} vault
 * @property {String} pool
 * @property {String} tokenIn
 * @property {String} tokenOut
 * @property {String} amountToBuy
 * @property {String} maxAmountIn
 * @property {String} deadline
 */
  • VirtualSwap:
    • function for swap result calculation: calculateVirtualSwap
    • function for performing swap: virtualSwap
    • Object with parameters for router smart contract VirtualSwap type:
/**
 * @typedef VirtualSwapInfo
 * @property {String} pool
 * @property {String} tokenIn
 * @property {String} tokenOut
 */

/**
 * @typedef VirtualSwap
 * @property {String} vault
 * @property {VirtualSwapInfo[]} swapRoute
 * @property {String} amountIn
 * @property {String} minAmountOut
 * @property {String} deadline
 */

If something goes wrong you will receive object:

/**
 * @typedef FailReason
 * @property {String} swapType
 * @property {String} reason
 */

Where:

  • swapType == 'None', alerts that there is no available path for swap and in cannot be performed
  • reason - string containing reason why swap cannot be performed

Readme

Keywords

Package Sidebar

Install

npm i @hikaru-fi/swap-router

Weekly Downloads

1

Version

0.2.0

License

GPL-3.0-or-later

Unpacked Size

112 kB

Total Files

19

Last publish

Collaborators

  • 4erpakoff
  • pafaul