A coin sniping bot is an automated application that monitors decentralized exchanges like Uniswap for newly created liquidity pools and automatically purchases the new tokens as soon as they become available. The bot works by listening to the 'PairCreated' event emitted by the Uniswap Factory smart contract, which triggers whenever a new liquidity pool is added. When detected, the bot calculates the swap path, estimates gas, and executes a token swap using the swapExactTokensForTokens function to buy the new cryptocurrency. This strategy allows developers to speculate on new tokens by purchasing small amounts whenever they hit the market, leveraging the permissionless nature of blockchain technology.
Build a Crypto Coin Sniping Bot with Node.js and Uniswap
Added:one of the coolest things about blockchain is that you have open access to a brand new financial system it's completely permissionless and in many cases this presents new opportunities for people with coding skills to actually take advantage of this and create applications or bots to earn money and in this video i'm going to show you how to do that i'm going to create an application or a bot that buys brand new cryptocurrencies as soon as they hit the market this is called a coin sniping bot and this is something i've seen a lot of people talking about so i want to make this video for you all today so if that's something you're interested in then make sure you watch this whole tutorial subscribe to this channel turn on notifications smash that like button and if you're new around here hey i'm gregory and on this channel i turn you into a blockchain master and i first created this video uh for my members inside of our dap diversity community but i decided to make this one public for you all much later because it's such a hot topic and so if you like what you see and you want to see more videos like this then make sure you sign up today over at university dot com forward slash boot camp alright with that being said let's get started all right so let's look at what we're going to create today in this project so we are going to look at a cryptocurrency exchange like uni swap and buy new coins that are listed as soon as they are listed okay so basically you know uni swap is a decentralized cryptocurrency exchange how it works is um you know uh basically you can tell it what cryptocurrency you have and you tell a cryptocurrency you want to buy and you just click swap okay it's powered by smart contracts it's on a blockchain okay there's no centralized middlemen controlling what coins can be listed on uni swap the really cool thing is that you know anybody in the community can basically add a cryptocurrency to uni swap and you as a developer have full access to all that code on the blockchain with smart contracts and you can actually create a bot that watches for new cryptocurrencies anytime they are added uni swap by the community and just buys them automatically okay that's one potential strategy that you can create for accumulating cryptocurrencies just buy anything and maybe a little bit of anything it's like a speculative bet uh anytime it hits uni swap i'm gonna show you how to do that in this tutorial so let's get a quick overview of how uni swap works all right so you can understand how that you're going to be able to create uh this bot that can you know look for new coins and buy them whenever they hit the hit the market okay so if you look at a website like we saw a second ago with uni swap that website talks directly to the blockchain where the smart contracts are so you have a central you know smart contract here all right that uni swap uh kind of controls everything where this is where the trader whenever they uh talk to the website it talks to this sort of central smart contract but then on the back end for every single cryptocurrency that uni swap supports those things called liquidity pools okay so like there's gonna be uh trading pairs for each liquidity pool that powers the automated market makers so you might have a pool that has two cryptocurrencies in it like weath and usdc for example or with usdc um you might have one that has like link and weath inside of it uh um so basically you have two cryptocurrencies um that are trading pairs and then all the trades get routed through these liquidity pools so that you know the end user has uh liquidity it has has tokens uh whenever it says like you go to the website you say i have ethan i want to buy usdc well how does that happen well it finds usdc it's actually parked into the app so we have liquidity providers on the backend who are the users who actually supply liquidity to these pools okay and they are incentivized to do this because they get a share of the trading fees it's the whole decentralized uh incentives at play that power this app so likewise anybody can add a brand new liquidity pool to uni swap with whatever token they want to as long as they add a second uh cryptocurrency with that pair and then unisop can now have it so you know you could have some fictitious pair like you know a dap token all right and then as long as you put dab token and weas inside that pool then now you have an entirely new market that can be traded and you swap and you as a developer can create a bot that finds out anytime somebody adds one of these uh pools and just buys a little bit of that cryptocurrency as a speculative bet and so that's exactly show you how to do right now all right so let's go ahead and start coding this project okay so um if you're watching this video this is the blockchain mastery program there should be a zip file down below okay if you're watching this video uh maybe in a different location later we might open up this repository for other people to uh you know view it so if you have the public repository link if this repository is not private you can just you know go to your terminal and type in git clone and then paste in the repository okay and it'll download your computer so i've already done that uh you of course want to enter into this directory and uh you know go to the project directory to start running the bot okay so you definitely need some dependencies on your computer installed in order to do this i'm not going to go through every dependency if you're watching this on the blockchain mastery program we list all the dependencies here inside the readme and uh how to install the initial requirements like node.js truffle and ganache cli okay definitely go check out the videos in the video vault that explain exactly how to do that okay so um let's just go ahead and install the project specific dependencies on our computer like this we'll do npm install okay and that will uh put everything in our package.json file onto our system here so that we can use all the project dependencies and let's go ahead and open up the project here okay so you can see we've got some smart contracts in here so the truffle project we have a bot script okay that's gonna actually run the bot we'll talk about that we have a package.json file for all the packages a truffle dash config file that connects our project to the blockchain and then finally some scripts that uh you know mimic the scenario of new liquidity pools uh being created on uni swap okay so um let me explain how we're actually gonna do this all right as a coding tutorial so if you go back to this diagram that i showed you uh you know with uni swap on it uh you can see all this complex logic here this uni swap contract all these liquidity pools all these active traders okay so how do we actually you know simulate that on our development environment like how do we build a bot where we can kind of you know watch this happen in real time okay because there's a real problem here all the uni swap smart contracts and liquidity pools are on the ethereum blockchain or some other evm compatible chain or layer two environment okay they don't exist on our computer to play around with so you got a couple options here one is you could just fork uni-swap and set it up on your uh you know development environment here but this is a lot of code okay that'd be pretty complex and you'd have to rebuild all the state of this all right you could try to just write a bot that you know interacts with the ethereum blockchain and actually you know spends money in real time to create a sniping bot but that's probably not what we want either okay so the final solution is actually to create a copy of all this stuff on our blockchain um our our development blockchain so that we can basically get a copy of the entire blockchain on our computer to develop against and that's exactly what we're going to do with ganache cli so ganache is a development blockchain that lets us you know run a blockchain on a computer so we have to you know spend any real money we can put smart contracts on our blockchain for free we can get free cryptocurrency to play around with all right um but there's a limitation with ganache out of the box which is it doesn't know about uh you know uni swap it doesn't know about all these other cryptocurrencies so how can we do that well basically ganache has a command line interface version so ganache cli and that actually has a fork option okay so you can actually fork ganache as long uh and point it to the main ethereum blockchain to get all its information its state all its cryptocurrencies all its applications everything access to that on your computer so you can play around that stuff without you know spending any real money so we're gonna have something that looks like this okay so out here on the actual ethereum blockchain you have uni swap okay but how do we get access to uni swap right here we have you know our bot that we're building all right we're going to connect it to ganache our development block chain but it doesn't know about you know uni swap up here so how does it do that well it connects you can connect ganache to the blockchain to learn about uni swap and everything else in the blockchain so how do you do that uh well to talk to the blockchain you talk to a node all right this is just a computer that acts as a gateway to the blockchain again a blockchain's a peer-to-peer network of nodes i'll talk to one another and have a redundant copy of the state etc etc so you need access to a node all right that node is going to give you access to everything on the blockchain you know including uni swap other account balances all the cryptocurrencies and so how do you get access to node well you could download one and install it yourself or you could just access a remote node on somebody else's server which is fine for development purposes in this case so we're going to use alchemy for this all right so head over to alchemy.com you can log in and you can create an app for free and that's going to give you access to uh you know an ethereum node all right it's going to have a websocket url just go to documentation and find out how to create an alchemy key all right it's pretty straightforward all right whenever you're done actually view the key copy the url for your mainnet address and we'll start setting up our project all right so let's go ahead and set up our um blockchain to point to the real blockchain as in ganache our development blockchain let's point it to the real one okay so make sure you've got that alchemy api key from the previous step and we're going to look at the documentation here and um you know run this in your terminal so we're going to do this ganache cli command we're going to pass it the dash f command which tells us to fork the blockchain we're going to put in our api url for alchemy make sure you use the websocket version because that's what's going to allow us to listen to new pairs created on the blockchain okay and then you know use this string and then paste into your url sorry into your terminal but in this place right here where it says you know your app key you actually want to put your real mainnet key from uh alchemy here so don't don't paste in your app key put your you know url key here you start your alchemy key here then um you want to put this dash m all right for your mnemonic i'll show you how you can do that in a second put in your mnemonic phrase and then we're going to unlock this specific account all right and then dash p 4 7 5 45 so i'll just copy this which i'm talking about here the easiest thing to do is do ganache cli first okay sorry let me do that again ganache cli and then uh you can run ganache once without all these options and you can find this mnemonic seed phrase okay so what this is gonna do is let you act as the same wallet over and over again whenever you start ganache so that way every time you restart it it doesn't just change wallets you can save the same all over and over again of course you don't want to use this mnemonic seed phrase on screen okay because that's going to uh basically you don't want put any real cryptocurrency in this wallet because anybody's watched this video or seen your mnemonic seed phrase and plain text could steal your money so trust me i'm not storing any crypto in this mnemonic uh neither should you and so you could save that you could put it in a text editor okay but then you want to take all these values and put it in here and then start ganache cli this way all right actually fill in the values and have your blockchain running all right so um i have i started ganache here um and you can see my accounts here so you do the same thing i recommend having it in a different terminal tab you could use whatever app you want to or you could open a new tab in terminal okay i realize that part of my mnemonic is showing it's not a big deal i don't use this for real i know somebody commented on my videos hey you're mnemonic showing but that's that's okay um so it means ideal but i don't put any real crypto in this wallet so don't worry uh anyways open that in a new uh terminal tab keep it running in the background and then we're to go ahead and follow the other steps in this in this readme here okay uh we can go ahead and open their project again see what else is in here so we got the contracts okay this is a token we're just going to put a token on the blockchain and create a new liquidity pool okay you can see that inside the migrations directory here to play contracts we're gonna put a new took on the blockchain okay then we have a script inside here that lets us sort of set the scenario up and a uh a script also lets us create a pool so we can watch the bot take place in real time okay so we're going to do is actually run this bot file here we'll explain the code here in a second but we're going to start the bot okay so that it um so that it is just watching for new pools getting created all right so um we can run the bot in a separate tab like this all right we can basically just uh open this over here maybe open a new tab here and we can do uh let's just copy the command for the documentation here make sure i'm doing it right so it's just me nodebot.js all right we're going to demo it and then we'll explain the code after all right so it's listening for new pairs this is boss just going to sit there and listen for new uh pairs add it to uni swap now we can do is go ahead and create a pair so what are the steps to do that well we want to create a new fictional cryptocurrency all right we're going to take this token here uh this is just an off-the-shelf token from the oakland zeppelin library okay we don't need to code one ourselves we can just get it from a library for free all right then we can put on the co on the blockchain with our migration contract all right our migration script so i'll say truffle migrate dash dash reset wait for this to finish all right nothing's changing about our bot uh because we haven't actually added any pairs on uni swap yet all right it's just um it's listening for that but we haven't done anything yet but um next what we're going to do is actually uh you know set up a scenario we're going on this script that's called fund.js all right so what it does is it pulls in our uh it pull it's gonna it's gonna take in our um our cryptocurrency we created here this erc20 token just the interface for it it's going to get weak which is the pair that we want to use for our uni swap pool it's going to take the unlocked account all right which is the account we found them on the main net that's going to have uh have some weak so that we can actually obtain weak the whole point is whenever we fork the blockchain we have access to all the accounts and so you can just go check this account on etherscan right now okay so if you ever have any trouble with this tutorial um you know in the wild you could potentially change this address for some reason it doesn't have weak we can see that this address has quite a lot of money in it all right um so you can see it has you know 34 million dollars in a wrapped ether or weak the time score in this video so we can pretend to be this account uh inside ganache that's exactly what we did whenever we forked the blockchain we said you know start the blockchain uh point it to the main net you know use my demonic seed phrase but then unlock this account right here so now we have pretended to be this account we have access to it we can pretend like you know that's who we are and so we unlock it here um we get access to our token we get access to uh you know the weak contract and then basically um you know we set up a scenario here we have a deployer this is one of our accounts from ganache and then the sniper this is going to run the sniping bot and then basically we take an amount and we transfer a bunch of wease from um uh oh uh from the deployer okay and then transfer a bunch of weave from the sniper okay so this is going to give both uh sorry sorry uh we transfer a weak from the unwanted account to the deployer and also some weak uh from the sniper so this is gonna allow us to create a new liquidity pool on any swap and also have somebody who's going to run the bot so one person is going to create a new uni swap pool and the other person is going to buy the tokens so we're buying tokens with weath we're also creating a new liquidity pool for weath with weath plus our token and we need accounts funded to do that that's the script does it funds the deployer account that's going to create a new pool and it funds the sniper bot who's going to buy some cryptocurrency okay with weath so now we're just going to log the balance of the deployer to make sure they have some weak all right after the script is run and then we are also going to um you know fund the uh sniper account and check their balance after this script is run so this is just a truffle script it's inside the truffle project you can just run truffle exec you know fund so we can say truffle exec scripts one fun js we've numbered them so they're in order you know order on them so now uh the deployer has 10 wastes so they can create a pool and the sniper has 10 weak so that they can um you know actually purchase some cryptocurrency and run the bot so you can see the bot's not doing anything it's just listening for new pairs now if we were running this on the blockchain it might detect real pairs right now but for now you know we have a test copy the blockchain and the only activity that's going to occur on it is the activity that we um you know do so basically whenever you fork a nosh it just freezes the blockchain at the current state it doesn't get any new state you just freeze it at that uh point in time and then you're adding all the new activity all the new blocks after that right there right so now we can actually observe the bot in action so we're gonna run a script that creates a uni swap pool okay you you just want pair up say using those terms interchangeably whenever you create a new pool it creates a new pair and our bot is actually going to detect this okay so um let's go ahead and and do that all right you'll see the script in here it's going to create a pool so what it does is it pulls in our token that we that we coded in the project all right it gets the uni swap router interface and then it's going to create a new pool so let's look at some key concepts in here to understand how this works really quickly before we continue so you want to make sure you understand how the unitround router works okay so the uni swap router you can see the documentation here uh this is the uh contract okay that is the main interface for uni swap right so when we're like creating pools and stuff um we need to know about this right here you can see the add liquidity function this is how we add at least liquidity so we remove liquidity all right all that type of stuff and so we create a new pool we're going to need to add uh liquidity and things like that which we'll see exactly in the uh in the thing uh starting the script and this is this is the documentation where you can uh you know read more on exactly how the uni swap router works if you want to learn to manipulate this uh code for your own purposes so we're born in the uni swap router version two uh we have the erc20 interface um so basically we create a new version of the uniswap router with the actual mainnet address here this is the javascript version of this contract okay we um basically um just log out to the console that we are preparing to create a pool the deployer we set that up and then a variable here okay and we get the we get the uh uh we contract here all right this is the actual we smart contract on the mainnet all right we get our token that we deployed in the project here we are going to give a equal equal ratio of tokens we're going to say that the rate is now 250 weak uh and then well 250 dap tokens all right and one uh weak you can see this is denominated ether here but that really just means tokens all right we just treat uh this token like having 18 decimal places just like whis does so he's basically 250 tokens that's worth one week that sets the price of the token and so we have to prove the weave if we're going to uh send it to the uh cuny swap so we do that and then we must approve the adapt tokens before sending them to uni swap that's what we do there anytime that uh you know a different app is going to spend tokens on your behalf you must approve it first okay so now we create um you know the uni swap pool that's what the add liquidity method is from the router here so you can see um let's see here this add liquidity function it takes the first token the second token the amount desired for the first token the amount desired for the second token the minimum amount of the first token the minimum second token the two address and then the deadline so we add those here all right here's the dapu token address the weak address the amount of each token all right and then basically the deployer address okay and then uh the deadline all right so um then we want to add the liquidity here okay actually sorry this is this is the estimate gas step my fault we estimate the gas first so that we can calculate the proper amount for the transaction my fault we estimate the gas first and then we do the actual liquidity because we want to do the appropriate gas amount before we stick it on the network okay so let's go ahead and run the script well actually before that we're gonna run that script like this okay let's just see in our documentation here we can do truffle exec scripts to create pool all right and i don't want to run it just yet because when i do the bot is going to actually purchase the tokens but i want to explain how the bot works before we see it in action all right all right so let's look at the script for the bot so you can see it inside of here so what this is is just a node.js script okay that's going to run continuously on our computer that's what we started uh right here okay it's actually listening for pairs so we haven't just set up here which i'll explain in a second but uh here's the main function that it sits there and listens for is this main function okay and so uh what does it do well we see the you know the accounts configured here at the top where he gets the deployer account the sniper account these are you know important addresses to use throughout this script that's the first thing but here's the real key to understand is effectively what this bot does is it listens for new uh pools created on uni swap anytime a new pool is created it runs this script by the token so we'll step the logic of that in a minute but this is important to understand is it it listens for events and uh you know acts on those events whenever they take place so you do that with web3js with this events uh right here so you can see web3e contract this is the contract interface with web3.js anytime you have a javascript version of this contract which we've created right here okay uh you have access to all the events anytime they occur so smart contracts can emit events so anytime a smart contract function logs an event right inside the inside the uh solidity code in javascript you can listen to the events okay so you can just see the event happen here and you can act on it it has all this data inside of it and then you can take the information out of that event and then act on it because you know something happened on the blockchain so these events are coming from the uni swap factory so earlier we talked about the uni swap router which is uh what you need to access anytime you create liquidity pools it's the add liquidity function but underneath the hood there's a separate smart contract the uni swap factory that actually creates the pools on the back end so the router ends up talking to the factory to create a new uh pool all right so uh you know we have this uh pair created event all right you can see this pair created in the code pair created event it is emitted each time a pair is created via the create pair function which it ends up getting called whenever you do add liquidity on uh the factory so anytime this event is emitted we can we can find the uh information about the token all right the first token of the second token so it's going to be our dap token and weath and the pair address okay so uh we need access to this uni swap contract which we get the abi for the smart contract from the uni swap router or sorry the uni swaps sdk so we get the factory uh the unit's not factory it's just in our package.json file we pull in the factory abi we get the router api we get the pair abi the erc20 api okay we have our web 3 connection at the top we get the rafter factory address the router address okay we create javascript versions of them here for the factory the router weave all right we have some other settings we'll talk about those in a second um but then we basically take we have our javascript version of the new swap factory we look at the events and we uh look for the pair created event and anytime the pair created event um gets triggered we are going to run this script inside of here so what does it do well basically it's um it gets it gets the information from the event each token all right and the peer address so it tells you here's this first token here's the second token here's the pair address okay so it um calculates the swap path because you know we want to listen for this and then we want to go back to the uni swap router and actually purchase tokens okay so we need to tell it how to do that so we need we need the pair and the token all right and then we check the reserves to get the price and then we actually do this swap here so we get the settings for the swap exact tokens for tokens function you can see that's at this usef documentation swap exact tokens for tokens this is what actually does the trade so we tell the amount in the amount out the address for the call data and then uh two and then the deadline okay you can see all that in the documentation here okay and so we estimate the gas for that function we calculate all the arguments for that function just like this all right amount amount out past sniper deadline okay that's the trader and then uh we just swap it and it tells us that the swap is successful and um we check the balance when it's done and we say you know you've successfully swapped tokens you've purchased them okay and if there's an error we log the air to the console so the last thing i want to talk about are two important settings um the uh the amount okay and then the slippage all right so um the amount is we want 0.25 okay weak and then we want uh 0.25 weak is how much you're willing to spend on new tokens all right so if you're going to make a speculative bet on this this is how much you'd be willing to spend and then the slippage is just five percent so we're gonna up this actually for this tutorial we're going to 10 because the bot if if you see the bot blow up on you you might adjust this value slippage is something you always have to worry about on dexes okay uh we're going to go to 10 percent but now let's just uh keep that that's how the bot works it's listening now let's actually create the pair so we're going to go to the script here for create pool and then we're going to you know add liquidity which on the back end is going to create the pool and the bot's going to hear that and it's going to fire so let's do it all right and boom there we go so our script uh prepared to create the uni swap pool it approved the week it approved the deputy tokens it created the new swap pool and pull credit successfully and so you can see here the bot it's listing for new pairs the pair is detected um here's the first token here's the second token here's the pair address okay it checked the liquidity swap the weave swap successful and then we are swat for 0.25 weak for 49 uh deputy tokens awesome all right so that is how you create a coin sniping bot all right they basically just buys a little bit of new cryptocurrency anytime that you uh you know a new one is listed on a new swap so of course um you know this is a uh be careful about running this on the main net of course um there's no guarantee that this is gonna be a profitable strategy it's not what i'm telling in this video but this is a cool idea that you can use as a launch pad uh to create your own bots all right so you can play with this slippage amount here you can put the amount that you want to buy of course you don't necessarily want to buy 25 week or 0.25 weak worth of a new coin every time maybe on a low cost chain you could you know make this a few dollars or something like that and see what happens um uh but yeah definitely uh run this at your own risk on the main net but this is how you do it without having to risk any real money in development all right so that's all i got for today as always you know smash that like button down below subscribe to this channel if you haven't already that really helps these videos out so the more people can learn about blockchain and if you like what you saw in this video and you want more videos like this uh then i can show you to master blockchain step by step start to finish over at dap university dot com forward slash boot camp you do have to be an expert to get started today again i've helped people with zero coding experience become real world blockchain developers create their own projects in a matter of months so that's all i've got until next time thanks for watching dap university
Up Next

How to Connect Smart Contracts to Metamask: 6 Frontend Methods
@PatrickAlphaC
82.8K views•2022-02-11

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Solidity Programming: Complete Blockchain Developer Course
@DappUniversity
156.2K views•2020-05-22

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto















































![🚀ТОП ТРЕЙДИНГ РОБОТЫ 2025 VELES FINANCE [Не Наблюдатель]](https://i.ytimg.com/vi_webp/nfAS8KpeenI/maxresdefault.webp)

