Getting Started

Hands-on tutorial to start using WardenSwap SDK for developers.

Installation

1.Install the package

npm install @wardenswap/bestrate-sdk

2. Install additional dependencies. If already installed, ensure the version of ethers ≥ 5.4.0

npm install ethers

Implementation

This tutorial focuses on the implementation for BNB Chain. However, it can be applied to the other network in the same way, by changing provider, contract addresses and token list.

Depending on the use cases, you might not need to implement all the steps provided here. However, to integrate the swap page into your own, you will need 1. Getting Quote 2. Approval and 3. Swap.

For Ethereum and Polygon, it has the same implementation as BNB Chain except the parameters mentioned in here.

1. Getting Quote

You can use SDK to easily get quote.

import {
  initProvider,
  WardenBestRate,
} from "@wardenswap/bestrate-sdk";
import { ethers } from "ethers";

async function getQuote() {
  const provider = initProvider("https://bsc-dataseed.binance.org");
  const warden = new WardenBestRate(provider, "bsc");

  const src = "0xe9e7cea3dedca5984780bafc599bd69add087d56"; // source Token Address: BUSD
  const srcDecimals = 18; // source token's decimals
  const dest = "0x0feadcc3824e7f3c12f40e324a60c23ca51627fc"; // destination Token Address: WAD
  const amountIn = ethers.utils.parseUnits("100", srcDecimals); // amount of source token
  const gasPrice = await provider.getGasPrice()

  // Get BEST RATE!
  const quote = await warden.getQuote(src, dest, amountIn, gasPrice, {
    enableSplit: true,
  });

  console.log(JSON.stringify(quote, null, 2))

  return {
    quote,
    warden,
  }
}

JSON-RPC URL can be found in the document: BSC JSON-RPC

The quote response will looks like this. You can use amountOut to preview to the user, including building the swap params.

2. Approving Transaction to swap (Metamask with web3-react)

Before the token can be swapped, the user has to approve it first. This part shows the example integrated with the web3-react. After integrating web3-react to our app, we can do this. And we need to connect to user wallet first to let the user confirm approving.

Read about using React to connect wallets like Metamask here: https://medium.com/coinmonks/web3-react-connect-users-to-metamask-or-any-wallet-from-your-frontend-241fd538ed39

ERC20 ABI JSON Can be downloaded here

3.1. Sending Basic Swap Transaction (Metamask with web3-react)

After getting quote, we can send the transaction to the contract to swap with the strategies or split type method depending on the quote result.

The WardenSwap ABI JSON v2.0 can be retrieved from BSCScan or downloaded here.

3.2. Sending Swap with Native currency (BNB, Ether, Matic)

As the general convention, the native currency or gas will be represented with the address 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE .

Ref:

Full Example for Swap Page (React TypeScript)

Code Example:

https://gist.github.com/wardenluna/a46bd21540060958fa3003aa349ebd58

Usage for Ethereum or Polygon

To use SDK with Polygon, we need to input the different parameters from BSC, as shown in this example.

Note: you may detect the chain ID to switch the parameters accordingly.

Usage for Optimism & Arbitrum

To use SDK with Optimism and Arbitrum (coming soon), we need to Input both gasPrice and gasPriceL2 as shown below.

Troubleshooting

TypeError: Cannot convert a BigInt value to a number

Sometimes certain tools will output Math.pow(BigInt(2), BigInt(128)) - BigInt(1) because they have @babel/plugin-transform-exponentiation-operator included.

If you are using @babel/preset-env, you can exclude it like this to fix:

Ref: https://github.com/hirosystems/stacks.js/issues/1096#issuecomment-929362393

TypeError: Argument of type 'JsonRpcProvider' is not assignable to parameter of type 'Provider'

This error can be caused by the version of Ethers.js not matching the library. We suggest to update the Ethers.js to version newer than 5.4.0

In case you are comfortable to use the latest version of Ethers.js, run this command to upgrade.

Last updated