@wardenswap/bestrate-sdk
v2.10.0
Published
- Update src/config.ts to add more tables
Downloads
201
Readme
WardenSwap Best Rate Query SDK
An JavaScript/TypeScript SDK library for querying trading quotes from WardenSwap platform. In order to begin trading on WardenSwap, we need the trading path that provide the best rate. The SDK help finding the path with ease!
For more information, check out our documentation here: https://docs.wardenswap.finance/warden/wardenswap-sdk/overview
Installation
# For NPM
npm install @wardenswap/bestrate-sdk
# For Yarn
yarn add @wardenswap/bestrate-sdk
Example
Here is the example if JavaScript code for querying best rate and perform a simple swap via WardenSwap SDK.
// npm install @wardenswap/bestrate-sdk ethers
const { WardenBestRate } = require('@wardenswap/bestrate-sdk')
const { ethers } = require('ethers')
// WardenRouter_2_0.abi.json can be found here: https://gist.github.com/AncientWarden/4aeae54509dd21020ab29a13c804cb57
// or BSCScan: https://bscscan.com/address/0x451ef8D6B645a60115EB8b8bEa76B39C0C761004#code
const wardenRouterAbi = require('./WardenRouter_2_0.abi.json')
const WARDEN_ROUTER_ADDRESS = '0x451ef8D6B645a60115EB8b8bEa76B39C0C761004'
const src = '0xe9e7cea3dedca5984780bafc599bd69add087d56' // source Token Address: BUSD
const dest = '0x0feadcc3824e7f3c12f40e324a60c23ca51627fc' // destination Token Address: WAD
const amountIn = ethers.utils.parseUnits('10', 18) // 10 BUSD in BigNumber
const gasFee = ethers.BigNumber.from('5000000000') // default BSC gas fee for rate calculation
const options = { enableSplit: false } // calculate without using split trades
// initialize provider, requires version of ether.js >= 5.4.0
const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org')
async function getQuote() {
// initialize the warden client
const wardenClient = new WardenBestRate(provider, 'bsc')
// get the quote detail with warden client
const quote = await wardenClient.getQuote(src, dest, amountIn, gasFee, options)
console.log('quote:', quote)
return quote
}
async function swap(quote) {
// add the acceptable slippage of 1% to amountOut
// since the actual amount can be lower or higher.
const minAmountOut = ethers.BigNumber.from(quote.amountOut).mul(99).div(100)
// Get the signer with private key
// For using with Metamask, see: https://docs.ethers.io/v5/getting-started/#getting-started--connecting
const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider)
const wardenContract = new ethers.Contract(WARDEN_ROUTER_ADDRESS, wardenRouterAbi, signer)
// send a swap transaction to warden contract
const swapTx = await wardenContract.swap(
quote.swapAddress,
quote.data,
quote.depositAddress,
src,
amountIn,
dest,
minAmountOut,
signer.address, // dest token receiver
0, // Partner ID for profit sharing, default to 0
0) // metadata, reserved for future use
console.log(`swap tx successfully submitted, tx hash: ${swapTx.hash}`)
}
getQuote().then(quote => swap(quote))