Hands-on tutorial to start using WardenSwap SDK for developers.
Installation
1.Install the package
npminstall@wardenswap/bestrate-sdk
2. Install additional dependencies. If already installed, ensure the version of ethers ≥ 5.4.0
npminstallethers
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.
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.
To use SDK with Polygon, we need to input the different parameters from BSC, as shown in this example.
// This params map is showing the difference params needed for each chain. // The token address is also different, despite the same symbol. // See token list for addresses and decimal placesconstparams= {// ethereum mainnet chain id: 11: { CHAIN_NAME:'ethereum', JSON_RPC_URL:'YOUR_RPC_URL', ROUTER_ADDRESS:'0x39f97198c5DbC193EB962c4B3B7e447091A18eAB', FROM_AMOUNT:ethers.utils.parseUnits('10',6),// 10 USDC in BigNumber FROM_ADDR:'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',// USDC TO_ADDR:'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',// ETH },// bsc chain id: 5656: { CHAIN_NAME:'bsc', JSON_RPC_URL:'https://bsc-dataseed.binance.org/'ROUTER_ADDRESS: '0x451ef8D6B645a60115EB8b8bEa76B39C0C761004', FROM_AMOUNT:ethers.utils.parseUnits('20',18),// 20 BUSD in BigNumber FROM_ADDR:'0xe9e7cea3dedca5984780bafc599bd69add087d56',// BUSD TO_ADDR:'0x0feadcc3824e7f3c12f40e324a60c23ca51627fc',// WAD },// polygon chain id: 137137: { CHAIN_NAME:'polygon', JSON_RPC_URL:'https://rpc-mainnet.matic.quiknode.pro', ROUTER_ADDRESS:'0x030B8b7bF245E762736e65c0903295447B898c30', FROM_AMOUNT:ethers.utils.parseUnits('20',6),// 20 USDC in BigNumber FROM_ADDR:'0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',// USDC TO_ADDR:'0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619',// ETH }}// given the params are using for PolygonconstcurrentParams= params[137]// get quoteconstprovider=newethers.providers.JsonRpcProvider(currentParams.JSON_RPC_URL)constwardenClient=newWardenBestRate(provider,currentParams.CHAIN_NAME)constquote=awaitwardenClient.getQuote(...)...// approvalconstallowance=awaiterc20Contract.allowance(account,currentParams.ROUTER_ADDRESS)...// swapconstwardenContract=newethers.Contract(wardenRouterAddress, wardenRouter2Abi, signer)constswap=awaitwardenClient.swap(...)
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.
constquote=awaitwardenClient.getQuote( srcAddr, destAddr, amountIn, gasPrice,// 👈 this should be L1 gas price (Ethereum gas price) { gasPriceL2:ethers.utils.parseUnits("0.001","gwei") })
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:
presets: [ ['@babel/preset-env', {... exclude: [...'transform-exponentiation-operator',// << this line here ], }, ], ],