
Uniswap is one of the most discussed and important projects in the DeFi space. It’s a pretty popular project for many reasons - in this guide, we will learn how to interact with the Uniswap smart contracts using a JavaScript library called
Build more with QuickNode - New pricing plans and a free tier! Read the press release
You can register for a free trial, as well as see pricing here
coding the bot
mkdir PancakeSwapBot cd PancakeSwapBot
mkdir PancakeSwapBot cd PancakeSwapBot
coding the bot
npm i ethers
npm i ethers
coding the bot
const ethers = require('ethers'); const addresses = { WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", router: "0x10ED43C718714eb63d5aA57B78B54704E256024E", factory: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73", me: "YOUR_WALLET_ADDRESS" } const mnemonic = "orange banana apple bravo charlie delta gamma ........." const provider = new ethers.providers.WebSocketProvider("QUICKNODE_WSS_PROVIDER_HERE") const wallet = ethers.Wallet.fromMnemonic(mnemonic); const account = wallet.connect(provider)
const ethers = require('ethers'); const addresses = { WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", router: "0x10ED43C718714eb63d5aA57B78B54704E256024E", factory: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73", me: "YOUR_WALLET_ADDRESS" } const mnemonic = "orange banana apple bravo charlie delta gamma ........." const provider = new ethers.providers.WebSocketProvider("QUICKNODE_WSS_PROVIDER_HERE") const wallet = ethers.Wallet.fromMnemonic(mnemonic); const account = wallet.connect(provider)
coding the bot
const factory = new ethers.Contract( addresses.factory, ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'], account ); const router = new ethers.Contract( addresses.router, [ 'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)', 'function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)', ], account );
const factory = new ethers.Contract( addresses.factory, ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'], account ); const router = new ethers.Contract( addresses.router, [ 'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)', 'function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)', ], account );
coding the bot
factory.on("PairCreated", async (token0, token1, addressPair) => { console.log(` ~~~~~~~~~~~~~~~~~~ New pair detected ~~~~~~~~~~~~~~~~~~ token0: ${token0} token1: ${token1} addressPair: ${addressPair} `); // This block ensures we pay with WBNB let buyToken, sellToken; if(token0 === addresses.WBNB) { buyToken = token0; sellToken = token1; } if (token1 === addresses.WBNB) { buyToken = token1; sellToken = token0; } // Neither token is WBNB and we cannot purchase if(typeof buyToken === "undefined") { return } const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]); const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS console.log(` ~~~~~~~~~~~~~~~~~~~~ Buying new token ~~~~~~~~~~~~~~~~~~~~ buyToken: ${amountIn.toString()} ${buyToken} (WBNB) sellToken: ${amountOutMin.toString()} ${sellToken} `); const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [buyToken, sellToken], addresses.me, Date.now() + 1000 * 60 * 5 //5 minutes ); const receipt = await tx.wait(); console.log('Transaction receipt'); console.log(receipt); } )
factory.on("PairCreated", async (token0, token1, addressPair) => { console.log(` ~~~~~~~~~~~~~~~~~~ New pair detected ~~~~~~~~~~~~~~~~~~ token0: ${token0} token1: ${token1} addressPair: ${addressPair} `); // This block ensures we pay with WBNB let buyToken, sellToken; if(token0 === addresses.WBNB) { buyToken = token0; sellToken = token1; } if (token1 === addresses.WBNB) { buyToken = token1; sellToken = token0; } // Neither token is WBNB and we cannot purchase if(typeof buyToken === "undefined") { return } const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]); const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS console.log(` ~~~~~~~~~~~~~~~~~~~~ Buying new token ~~~~~~~~~~~~~~~~~~~~ buyToken: ${amountIn.toString()} ${buyToken} (WBNB) sellToken: ${amountOutMin.toString()} ${sellToken} `); const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [buyToken, sellToken], addresses.me, Date.now() + 1000 * 60 * 5 //5 minutes ); const receipt = await tx.wait(); console.log('Transaction receipt'); console.log(receipt); } )
coding the bot
factory.on("PairCreated", async (token0, token1, addressPair) => { console.log(` ~~~~~~~~~~~~~~~~~~ New pair detected ~~~~~~~~~~~~~~~~~~ token0: ${token0} token1: ${token1} addressPair: ${addressPair} `);
factory.on("PairCreated", async (token0, token1, addressPair) => { console.log(` ~~~~~~~~~~~~~~~~~~ New pair detected ~~~~~~~~~~~~~~~~~~ token0: ${token0} token1: ${token1} addressPair: ${addressPair} `);
coding the bot
// This block ensures we pay with WBNB let buyToken, sellToken; if(token0 === addresses.WBNB) { buyToken = token0; sellToken = token1; } if (token1 === addresses.WBNB) { buyToken = token1; sellToken = token0; } // Neither token is WBNB and we cannot purchase if(typeof buyToken === "undefined") { return } const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]); const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS console.log(` ~~~~~~~~~~~~~~~~~~~~ Buying new token ~~~~~~~~~~~~~~~~~~~~ buyToken: ${amountIn.toString()} ${buyToken} (WBNB) sellToken: ${amountOutMin.toString()} ${sellToken} `);
// This block ensures we pay with WBNB let buyToken, sellToken; if(token0 === addresses.WBNB) { buyToken = token0; sellToken = token1; } if (token1 === addresses.WBNB) { buyToken = token1; sellToken = token0; } // Neither token is WBNB and we cannot purchase if(typeof buyToken === "undefined") { return } const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]); const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS console.log(` ~~~~~~~~~~~~~~~~~~~~ Buying new token ~~~~~~~~~~~~~~~~~~~~ buyToken: ${amountIn.toString()} ${buyToken} (WBNB) sellToken: ${amountOutMin.toString()} ${sellToken} `);
coding the bot
const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [buyToken, sellToken], addresses.me, Date.now() + 1000 * 60 * 5 //5 minutes ); const receipt = await tx.wait(); console.log('Transaction receipt'); console.log(receipt); } )
const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [buyToken, sellToken], addresses.me, Date.now() + 1000 * 60 * 5 //5 minutes ); const receipt = await tx.wait(); console.log('Transaction receipt'); console.log(receipt); } )
Note: This will only work if you have both WBNB to purchase the token and BNB to pay the gas fees!
coding the bot
const ethers = require('ethers'); const addresses = { WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", router: "0x10ED43C718714eb63d5aA57B78B54704E256024E", factory: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73", me: "YOUR_WALLET_GOES_HERE" } const mnemonic = "your mnemonic goes here just like this .. .. .. .." const provider = new ethers.providers.WebSocketProvider("__WEB_SOCKET_PROVIDER_FROM_QUICKNODE__") const wallet = ethers.Wallet.fromMnemonic(mnemonic); const account = wallet.connect(provider) const factory = new ethers.Contract( addresses.factory, ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'], account ); const router = new ethers.Contract( addresses.router, [ 'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)', 'function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)', ], account ); factory.on("PairCreated", async (token0, token1, addressPair) => { console.log(` ~~~~~~~~~~~~~~~~~~ New pair detected ~~~~~~~~~~~~~~~~~~ token0: ${token0} token1: ${token1} addressPair: ${addressPair} `); // This block ensures we pay with WBNB let buyToken, sellToken; if(token0 === addresses.WBNB) { buyToken = token0; sellToken = token1; } if (token1 === addresses.WBNB) { buyToken = token1; sellToken = token0; } // Neither token is WBNB and we cannot purchase if(typeof buyToken === "undefined") { return } const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]); const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS console.log(` ~~~~~~~~~~~~~~~~~~~~ Buying new token ~~~~~~~~~~~~~~~~~~~~ buyToken: ${amountIn.toString()} ${buyToken} (WBNB) sellToken: ${amountOutMin.toString()} ${sellToken} `); const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [buyToken, sellToken], addresses.me, Date.now() + 1000 * 60 * 5 //5 minutes ); const receipt = await tx.wait(); console.log('Transaction receipt'); console.log(receipt); } )
const ethers = require('ethers'); const addresses = { WBNB: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", router: "0x10ED43C718714eb63d5aA57B78B54704E256024E", factory: "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73", me: "YOUR_WALLET_GOES_HERE" } const mnemonic = "your mnemonic goes here just like this .. .. .. .." const provider = new ethers.providers.WebSocketProvider("__WEB_SOCKET_PROVIDER_FROM_QUICKNODE__") const wallet = ethers.Wallet.fromMnemonic(mnemonic); const account = wallet.connect(provider) const factory = new ethers.Contract( addresses.factory, ['event PairCreated(address indexed token0, address indexed token1, address pair, uint)'], account ); const router = new ethers.Contract( addresses.router, [ 'function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)', 'function swapExactTokensForTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)', ], account ); factory.on("PairCreated", async (token0, token1, addressPair) => { console.log(` ~~~~~~~~~~~~~~~~~~ New pair detected ~~~~~~~~~~~~~~~~~~ token0: ${token0} token1: ${token1} addressPair: ${addressPair} `); // This block ensures we pay with WBNB let buyToken, sellToken; if(token0 === addresses.WBNB) { buyToken = token0; sellToken = token1; } if (token1 === addresses.WBNB) { buyToken = token1; sellToken = token0; } // Neither token is WBNB and we cannot purchase if(typeof buyToken === "undefined") { return } const amountIn = ethers.utils.parseUnits('0.1', 'ether'); //ether is the measurement, not the coin const amounts = await router.getAmountsOut(amountIn, [buyToken, sellToken]); const amountOutMin = amounts[1].sub(amounts[1].div(10)); // math for Big numbers in JS console.log(` ~~~~~~~~~~~~~~~~~~~~ Buying new token ~~~~~~~~~~~~~~~~~~~~ buyToken: ${amountIn.toString()} ${buyToken} (WBNB) sellToken: ${amountOutMin.toString()} ${sellToken} `); const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [buyToken, sellToken], addresses.me, Date.now() + 1000 * 60 * 5 //5 minutes ); const receipt = await tx.wait(); console.log('Transaction receipt'); console.log(receipt); } )
coding the bot
node bot.js
node bot.js
Noah Hein
Demystifying Blockchain and Web3, one article at a time! -- Technical Content Editor @ QuickNode
We'll send you the latest tech and tutorials via our weekly Web3 Vibes newsletter.
Uniswap is one of the most discussed and important projects in the DeFi space. It’s a pretty popular project for many reasons - in this guide, we will learn how to interact with the Uniswap smart contracts using a JavaScript library called
Smart-contracts are the heart and soul of all the development happening on the Ethereum blockchain, and as more and more people develop on Ethereum, smart contracts are becoming more complex.Sometimes a smart contract wants information about the real world, like...
Here is a video representation of this guide if you prefer to watch instead of read[yt:YjQj6uk9M98]On ethereum, before being included in a block, transactions remain in what is called a pending transaction queue, tx pool, or mempool -...
Bitcoin is the father of blockchain technology. With Bitcoin started a new era of blockchain and decentralization. Bitcoin enabled everyone to make the peer-to-peer transactions they enjoy today; this guide will teach you how to get these transactions from the Bitcoin...
Aave, previously known as ETHLender, has catapulted to the forefront of the DeFi space. Aave was the first in the space to come up with the idea of a Flash Loan. Before flash loans, you would have to stake an over-collateralized...
Aave, anteriormente conocido como ETHLender, se ha catapultado hacia la delantera en el espacio DeFi. Aave fue el primero de todos en aparecer con la idea de los Prestamos Flash. Antes de los Préstamos Flash, tenías que tener...
Compound finance are early pioneers in the decentralized finance space, as one of the first defi lenders. Compound offers a way to earn interest on several tokens: ETH, BAT, DAI, REP, WBTC, USDC & a few others. Compound makes this possible by locking your assets in a...
At a high level, keeper auctions allow speculators to automatically buy assets at a discount, like the $4m+ of ETH that was bought for near-zero DAI on Black Thursday. You can see the results...
We have seen tremendous growth in trade volume in DEXs. With many of these coming to the market, it is tough to decide which DEX to choose when you want to swap your token for another. That's where 0x and its APIs come into the picture. It helps us to fetch the DEX with...
Bots are often made to automate manual workflows; one such type is trading bots. Trade Butler is a very secure and popular trading bot so let’s see how we can run Trade Butler Bot backed by robust
On Ethereum, when a transaction is sent, before being added to a block, it resides in what is called a Mempool. To receive information about this transaction, the Mempool must be queried. This guide will demonstrate how to query a node’s mempool using QuickNode Ethereum...
Not all users interact via the front-end UI when trading tokens on a decentralized exchange. Some users (or entities) trade programmatically via a smart contract or server-side scripts. This guide will demonstrate how to swap tokens on Uniswap using Javascript and the...