Advance Usage

For even more use cases, let's see how to use it here.

Payment Use Case

Assuming you want the buyer to pay with any tokens on BSC, ex. BNB, CAKE. Then your system will swap any token to be the stablecoin (ex. BUSD) for your merchants.

You can request user to swap their token on Warden Best Rate Swap and then send the stablecoin to the merchant, or yourself as a payment gateway by utilizing the last parameter of the swap method. No need for new solidity contract deployment.

const swap = await wardenClient.swap(
      srcAddr,
      destAddr,
      amountIn,
      minAmountOut,
      quote,   
      undefined,
      undefined,
      merchantAccount) // 👈 specify receiver account to receive token after swapped

Understanding Different Swap Type

Warden Best Rate API have 2 types of response for trading depending on the many factors including gas price. getQuote will determine which type is better by including gas fee factor into the calculation.

1. Strategies Type.

Basic type. It selects the best path for the best price.

Sample Response
{
  "type": "strategies",
  "path": {
    "routes": [
      {
        "address": "0x0dEa19CAb88f45301a83C6b5eAa18E8387546afE",
        "id": "0",
        "name": "WARDEN"
      }
    ],
    "tokens": []
  },
  "amountOut": "565042446063757949355",
  "gasFees": {
    "dest": "1913868530378129232",
    "unit": "166401",
    "usd": "0.338591545617514122"
  },
  "blockNumber": 16875978,
  "routerAddress": "0x451ef8d6b645a60115eb8b8bea76b39c0c761004",
  "swapAddress": "0x9cbd1322c3d029d32357808665dcae5c286c7081",
  "depositAddress": "0xc95B1750043FCE5dfCc8539835Ea3830Ec002A89"
}

2. Split Type.

The split type not only selects the best path for the trade, but it also determines other paths that are most appropriate for your overall trading volume. Example for split trades available: 40:60 and 20:80. It generally gives the better rate, but will use more gas. So it is not suitable for a small swap.

Sample response
{
  "type": "split",
  "paths": [
    {
      "routes": [
        {
          "address": "0x5aD333EAf52E48AB19095805F552A0734Bb57252",
          "id": "25",
          "name": "SMOOTHY"
        },
        {
          "address": "0x0e7DBD5E56fFdb53cd098732598c4082eB8fE872",
          "id": "19",
          "name": "DOP:UST"
        },
        {
          "address": "0x0dEa19CAb88f45301a83C6b5eAa18E8387546afE",
          "id": "0",
          "name": "WARDEN"
        }
      ],
      "tokens": [
        "0x23396cF899Ca06c4472205fC903bDB4de249D6fC",
        "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56"
      ]
    },
    {
      "routes": [
        {
          "address": "0x0cD12512ac262fFF5441D34B72F4E9e921a39D92",
          "id": "13",
          "name": "ACRYPTOS:ACS4"
        },
        {
          "address": "0x1A48f4827D96bFd03e1d5a4B3c34f6D2313CBA08",
          "id": "23",
          "name": "BISWAP"
        },
        {
          "address": "0x0dEa19CAb88f45301a83C6b5eAa18E8387546afE",
          "id": "0",
          "name": "WARDEN"
        }
      ],
      "tokens": [
        "0x55d398326f99059fF775485246999027B3197955",
        "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
      ]
    }
  ],
  "volumns": [
    59,
    41
  ],
  "amountOut": "11203636215691549028954",
  "gasFees": {
    "dest": "9984416517876489624",
    "unit": "870792",
    "usd": "1.775312577024362376"
  },
  "blockNumber": 16877199,
  "routerAddress": "0x451ef8d6b645a60115eb8b8bea76b39c0c761004",
  "swapAddress": "0x9cbd1322c3d029d32357808665dcae5c286c7081",
  "depositAddresses": [
    "0x5aD333EAf52E48AB19095805F552A0734Bb57252",
    "0x0cD12512ac262fFF5441D34B72F4E9e921a39D92"
  ]
}

Note that, even it is not recommended, you can disable the split type by setting enableSplit to false in the options. It could speed up the rate calculation but giving worse rate overall.

const quote = await wardenClient.getQuote(
    srcAddr, 
    destAddr, 
    amountIn, 
    gasPrice,
    { enableSplit: false }
)

Last updated