Overview
The WardenSwap SDK is a Javascript/Typescript library that provides developers the seamless way of integrating the most sophisticated best rate AI swap into your application or DeFi with ease.
Last updated
import {
initProvider,
WardenBestRate,
} from "@wardenswap/bestrate-sdk";
import { ethers } from "ethers";
async function swapWithWarden() {
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 = ethers.utils.parseUnits("5", "gwei"); // current gas price
// Get BEST RATE!
const quote = await warden.getQuote(src, dest, amountIn, gasPrice, {
enableSplit: true,
});
// Get the signer with private key
// For using with Metamask, see: https://docs.ethers.io/v5/getting-started/#getting-started--connecting
// For using other wallet, see: https://docs.ethers.io/v5/single-page/#/v5/api/signer/-%23-Wallet
const YOUR_PRIVATE_KEY = process.env.PRIVATE_KEY;
const signer = new ethers.Wallet(YOUR_PRIVATE_KEY, provider);
// Specify slippage, which is 1%
const minDestAmount = ethers.BigNumber.from(quote.amountOut).mul(99).div(100);
// Allow WardenSwap contract to use token in your wallet to perform swapping
console.log("Approving token...");
let tx = await warden.approve(signer, src, amountIn);
console.log("Tx Hash:", tx.hash);
await tx.wait();
// Perform a swap!
console.log("Swapping...");
tx = await warden.swap(signer, src, dest, amountIn, minDestAmount, quote);
console.log("Tx Hash:", tx.hash);
await tx.wait();
console.log("Swapped!");
}
swapWithWarden();