Last active
January 6, 2022 06:59
-
-
Save AncientWarden/9c39a6510b7cd6fd5a58574ebdf36a24 to your computer and use it in GitHub Desktop.
Quick Sample Usage for Warden Swap SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment