hey everyone I'm a head of developer experience at chaintech the leading web3 infrastructure provider with seven plus years in blockchain I'm in constant Hands-On build mode in this fastpaced web3 environment today I'm not just working you through building the pump. funbot on salana I'm sharing my research approach for tackling unfamiliar web three projects I've been doing this for many years if you'd like to dive straight into the Bots code and set up check the links in the video description but if you want to learn how to rush through research in web stre development and come back with a result sck around I'll show you how to do just that effectively and build in this fast-paced industry let's go okay let's talk the approach to tackling web three projects we'll call it the speedrun method which is how Gamers rush through their video games in their backlog the speed run concept imagine you're playing game you want to complete quickly you're not concerned taking in on every detail you're clicking through all the dialogues you kind of go skip skip skip um all the cut scenes your goal is to reach the final credits as soon as possible why this approach Works in web three it mirrors the fast-paced nature of the web3 industry it keeps you engaged with consistent challenges and it keeps giving you kind of doent hits and completing every Mission it allows you to complete mission after mission efficiently so the pro say rush through through the project focusing on the core functionality don't worry about distinguishing between main missions and side quests you'll figure it out later aim for working product first optimize later the benefits of the speedrun approach you'll quickly achieve a fully functional project you'll be able to maintain the momentum in your interest throughout the development you'll get the results fast and this is how web3 works after the speed run completion you can do everything else if you feel like it for example you can learn the foundations you might have missed during the run you can optimize your code you know think of it as new game plus you can use your working project as base for further development like building a full product PS I'll make occasional references to ethereum but don't worry if you're unfamiliar with ethereum you will still be able to complete this project fine you'll find this process is a lot easier than you might expect in final note creating this uh video tutorial took about three times longer than doing the actual project so that's how e the project is let's go okay so let's dive into what we are actually building here we're talking about pump. fun and we are going to create a trading and sniping butt for it now if you're not familiar with pump.
fun it's this project salana the basic IDE is you can create your own uh coin could be MIM coin could be whatever when you do that it sets up this bonding curve contract or a program they call it on salana this is where you can trade your new tokens once your token hits a certain level of liquidity on the bonding curve contract it moves over to an exchange like radium pound. f is actually pretty big we're talking millions in Daily volume everyone's been talking about it and of course like everything successful in three there are already clones popping up for it also the p. fine UI let's just say it's you know it's not for everyone it's not for me for sure I barely used it myself I maybe opened the 10 10 time tops while building this spot it's never going to be my preferred method of interacting with a set of p.
fun contracts secondly and this is key for us in web 3 we always want to get as close to the model as possible Right we don't want to rely on someone else's uh interface we want direct access uh to the onchain programs now founded fun doesn't have any apis out there some have built their own microsof platform software is service platforms with apis to interact with the p. fun programs so those could be used we however are going to build a bot that bypasses all the third party stuff no p. fun UI no external apis we are talking directly to to the salana programs with our own python code and through a chain stack node and yes we are doing this in Python and if you can build a bot for p.
fun you're basically set up to do the same for any similar project or clone out there so let's do it all right let's talk about setting up our own interaction with p. fun think of this like playing a video game with different difficulty levels we have four levels of inter ction here so let's go let's walk through them level one easy mode this is your basic entry level stuff you're just using the p. fun front end to interact with their programs simple slow not for us level two normal mode this is a bit more fun uh you look around and you find these third party apis like pump portal these guys pop up when you're searching for p.
fun apis and here is how it works they have their own set of apis that interact with p. funds onchain programs they construct the transactions for you you sign the transaction then you send it back on chain through them now they do take a fee for the service about 0. 5% per trade not too bad but here's the thing is it true web stre builds or do you really want to rely on someone else's apis today it's 0.
5% but what if tomorrow the jacket UPS to 50% I mean the the P portal guys they they seem like really great folks they build a solid service I don't think they're going to do this but you get my point right you want to rely on your own infrastructure uh level three normal plus mode normal plus mode this is where you're still using the third party API but with the your own RPC node you get the coin info from the pump portal API you subscribe to their web socket for all the new limited coin they construct the transaction for the coin you want to buy an add their fee and here is where the normal plus mode kicks in instead of sending the transaction through the pump portal guys through their AP API is signed and submitted through your own Salan Noe this where you can start using some Ser serious Firepower like chain Sal RPC nodes but chain Sal a Trader nodes these propagate transactions to the validators as fast as possible with priority which is going to be a lot better than the easy mode and the normal mode but here is level four expert mode this is the real deal here is what you do you bypass the interface the pump. fun interface completely you ditch all those third party apis you build your own python boat or any B trailer that interacts directly with the p. fun programs use an RPC node or Salon Trader node again chain provides all of those and this is the level we are going for we are going to speed speedrun this game on expert mode and the payoff is going to be big by going this route we are not just building boat we are gaining the Knowledge and Skills to potentially build our own microservices like P portal and we'll be relying on our own infrastructure so let's get technical okay we are rushing through this game so the first step is to find the program address here is what we need to do go to pound.
fun and pick a new random coin copy the coin's address paste it into an Explorer like sales scan look for the first mean transaction and grab that hash from that transaction we can find the p. fun program idea that's the program that creates all coins on p. fun so now we have our program idea but there is a cage how do we interact with this now we have this program idea but it's just a bunch of useless bite code to us right now we need a key to read this it's kind of like needing an AI for a smart contract on a through time for an educated guest because we are speed running through through this we are going to make an assumption most salana program prrams are built using the anchor framework so let's go with this we need an IDL in anchor on salana instead of an ABI on ethereum we have something called an IDL interface description language this is our magic key to the program it describes how to interact with the program and all the data structures in there so let's find the ideal now in perfect world with find this ideal verified on the Explorer like you might find uh verified smart contract on on either scan for example on etherum but pal.
fun of course does not have that there is no verified ideal and there a quick side note if you are curious what a purified program with an idea looks like here is an example but that's not our p. fun program unfortunately so here we are needing this idea our major keit the pound. fun program but it's not readily available we need to find another way to get our hands on so let's find the CED IDL all right our mission is to get our hands on that IDL for p.
fund so let's walk through this step by step here is how we deal with idls depending on how p. fun has set up their app this IDL could be hiding in one of two places it's either on the server side if it's it's on the server we are out of lck can't touch it or it's on the client side this is what we are hoping for and it's the easier route for developers and this is how most web applications have it so let's see what the p. fun web uploads on our machine here is what we do head over to p.
fun right click and select inspect to open the Chrome developer tools this lets us to see all the calls being made now we can just manually explore the source to find our IDL but here is a quick trick that works for most web apps on salana buil using anchor idea go to the source tab in dep tools and here the short card search for these Json parts and if we are lucky this should lead us straight to the ideal and a side quest while we are here keep your eyes peel to what you see you might stumble upon some other stuff like unprotected RPC and point you know free for all to use apparently which is what we have here in the front end API for pump. fun coins that allows you to retrieve the coin information if we wanted to use this but we're not going to so now now we have our previous guesses confirmed p. fund is built with anchor and they they have the IDL right there on the client side and this is going pretty easy so far and pretty good the bigger picture however is don't think we just got super lucky here this is actually pretty common in the salana world most applications follow this pattern so these tricks can yield quite a bit what's next we've got our IDL which means we have the key to interact with the p fun and chain program so let's architect our bot okay so now we have the ideal for p.
fun it's like getting the English version of the Japanese RPG you been that into play now you can actually uh make some progress let's talk architecture I know we're doing this preate in the game but remember we are speed running this so here is what uh we have in our toolkit so far third party endpoints P portal web socket that they provide for free theoretically we could use that as well uh check the description tutorial in the GitHub repository in the video description if you want we have that front end coin API we found while snooping around p. Fone with chrome developer tools and of course we're not going to use any of this our architecture is going is going to be simple the pound. fun program does three main things it creates coins it lets you buy coins it lets you sell coins so we are going to mirror that in our bot here is the plan first listener script we'll create a script that subscribes our web socket to the program ID and this will get us all the coin information all the new coins that are being printed then we'll have the buyer script the buyer script jumps into action when our listener script spots a new coin and buys it and then we have a seller script a script that sells the coins that we hold we're going to start by building these three scripts separately it's like building the individual Parts uh before we put it all together and we are going to start with the listener script that listens to all the create events is going to be our foundation the first piece of our p.
fun TR Bo so let's do the create listener script okay so let's build our first create listener what we need to do first is set up a way to listen to the salana program we are specifically looking for the create event which happens when new coin is minted let's start by using the log subscribe method why well when you look into our IDL we can see there is a create event structure in there this tells us the events are being emitted by the program now remember we trying to speedrun this so we are going for the easiest route the log subscribed method let's just connect our web socket to the program I Dimensions so we run this method and we start seeing some output in thew logs we can spot program log instruction create that's our signal that new coin is minted now we need to filter the noise we are getting all sorts events here create sell by probably others too but we're coin hun Hunters so we need to filter out everything except those program log instruction create messages that's our Target and here are the tricky part that raw output it really doesn't tell us much about the coins uh themselves it doesn't provide us with the name or U URI or none of the good stuff of of the co so to get that info we need to decode the raw output using our IDL if you poke around in Anchor docs you will see that just like ethereum uses 4 byte signatures for function identifier sidity anchor uses 8 byte identifiers for each instruction this is crucial for our next step armed with our IDL and this 8 byte offset knowledge we're going to create a script that does two key things it listens to the pump. fund program prints out the decoded output for every new coin and this script is going to use the IDL to understand the raw data and it's going to use that 8 by offset properly to decode everything and there and there we have it we've gone from a raw message data to clean de Gold Stream of new new coin information looks like we're doing good progress at this point we are already not relying on any third party apis and getting data directly from Source now let's proceed further and try a buy transaction okay so we have our create event listener up and running decoding and printing all the coin info now we are Switching gears and getting into the right transaction territory until now all we've been doing is doing read requests not changing the state of the chain so how do we construct a buy transaction on salana with p. fun here's our plan first we try to figure out how a buy transaction actually works let's use the salana get transaction method on a buy transaction you'll grab the signature or transaction hash get the data and decoded using our IDL in that 8 by anchor offset that we learned about previously now that we have decoded transaction output let's have a look we can look at it side by side with the IDL and okay looking at this you kind of feel I do uh feel this is a wow moment looks like there is just a lot of accounts involved in a single buy transaction there's Global fee recipient meant Bond and curve Associated Bond and curve Associated user user system program program token program rent event Authority program and look at at those this seems to be a mix of three categories there is pump.
fun programs like bondon curve there is Anchor framework related accounts like Global and event Authority and there is default Salon system programs so at first look this might feel overwhelming and seem to be not as easy as expected but we are rushing through this so the obvious idea is to try and figure out the relationship between all these accounts as side node it's kind of funny that we're trying to figure out the architecture of p. fun this late into the game but that's our Speed Run approach might seem counterintuitive but it works okay so how do we figure out the relationship we're going to run the salana get account info method on each of these addresses and in the output we're going to look for two things one is is it executable true means it's a program and two what account owns it Pro tip here if you're feeling overwhelmed you could dump the entire output of all the get account info method calls the cla a or anything similar ask it to draw the relationships between the accounts but be careful however and verify is certainly error prone now we do quick fast forward and here is the architecture of pound. fund that we've been uh able to figure out based on the buy transaction in all the relationships the green ones are the pum program owned by the p.
fund accounts and programs and the only one that is executable here is the pump program itself the program idea that we've been using so far the pump program owns pump Global and P Global again if you look at the anchor dos they briefly mention Global account and the global accounts since be an account that holds different variables like who receives the fee what the event Authority is and and again event Authority itself is also mentioned in the anchor dogs briefly because it's a part of uh the anchor framework that's responsible for emitting the events and verifiably doing so but this blue here because it's not owned by the pump program and neither is he recipient so all the blue ones that start with system here on the scheme are not owned by the pump program the global account is owned by the P program and has the fee recipient and the event Authority set in it and then the pum bonding curve is owned by the pump program and there is a separate Associated bonding curve which is actually owned by the system the token program and the Min address is also which is the address of the coin is owned by the token program and there is also the associated system token account and then there is the user account also owned by Salan So based on this we are able to figure out that when a mint is created a new coin is minted there is a bonding curve account created and to hold this new coin the user needs to create an Associated token account which is how Salon Works and to hold this account this token bonding curve program also needs an Associated bonding curve which again is how salana Works mean in Associated token account and then there is bonding curve in Associated bonding curve account same mechanism okay so this is what we've been able to figure out so far based on all the information that we have the decoded buy transaction and the ownership of the accounts let's actually build the flow of a buy transaction which is again much more complex than expected so it turns out you actually need to interact with the bonding curve right each coin has its own bonding curve contract and the bonding curve contract in the associated bonding curve is holding the tokens that you buy that you sell so to be able to buy a token you actually need first to fetch the price from the bonding curve of the token of the coin that you are going to buy you need to fetch the price and then once you have this price you need to construct your transaction and we've already figure out all the accounts involved based based on the IDL and the decoded by transaction you need the p. fund program ID which is the main executable program you need the pump Global because it sets the he recipient and the event Authority so you need the bonding curve and you need the associated bonding C because it holds the tokens that you buy you need the token address itself you need the associated account which you create for the token that you're going to buy and hold which is how salana works and you need all the system programs like event like R System talken Program and all of that stuff so it's much more complex than we imagined and also the key thing here that we are learning by looking into all of this is that we when we run our listener it's not printing one of the key items one of the key addresses that we need which is associated Bond and curve we have the hash of the created event of the token we have the name we have the symbol we have the address we have the bonding curve we have the user who created the token but we do not have the associated Bond and curve address which we need to properly construct our buy transaction which means we are going back to the drawing board and we need to kind of rethink how we retrieve our create events from the chain from the pump. fund program we need the assciated bonding curve and we don't have it so back to the drawing board so how do we get the associated bundon curve let's first check check the create event in IG looking at the create event igl we can see that the associated abundant curve is not part of the emitted logs when the token is minted and the bonding curve is created when we look at the create event IDL we notice that the associated button curve isn't part of the immedate logs but the button curve and token are minted so how do we get the associated button curve address our first thought might be to extract the associated b curve from the transaction itself let's have a look at that process and what what it might look like we'll need to run a get transaction decode the transaction extract the associated bondon curve from the decoded create transaction sounds simple right well yes and no let's break it down we need the transaction hch or signature which we can get from our log subscribe method you would then go back to the chain and run the get transaction method with this hash after gaing the full transaction we would extract the associated bonding curve and finally we'll construct the buy transaction this approach while feasible doesn't look too good given the rate at which tokens are minted on p.
fun and Salan speed in general it involves a lot of requests it's a complex construction that might not be sustainable remember we are rushing through this game on expert mode and we and because we are experts we have intuition that there is a better solution out there kind of like you sense a secret in a spot in a game so what if we could get the full transaction data over web socket as transactions come in without any additional calls we browse for a bit and figure out there is actually a way to do this enter the blog subscribe method this method often overlooked because the salana docks say it's unstable well it's pretty stable on chain stex thanks to our amazing engineering team so this is not an issue for us here is how block subscribe works it gets us all the blocks or web socket which come in very fast on salana it has two crucial parameters program mentions filters for account of a specific program and transaction details full which gives us the complete transaction data that's exactly what we need that's all what we we need instead of sending multiple G transactions decoding them and shuffling data back and forth with simplify our process subscribe to all incoming blocks that mention our program ID get full transaction data from these blocks decode the relevant transactions using our IDL extract all the information we need including the associated Bond and curve this approach is Lan efficient and gives us exactly what we need now that we have a neat way of getting our data we are almost there next we need to figure out how to Fitch the token price from the Bon curve to be able able to buy all right now that we have our Associated BN and curve let's tackle our next challenge which is fetching the token price I have a little side note on that while I I was working on constructing the buy transaction and checking the account relationships in general fing around get curious about how the uh p. fun web application actually features the prices and here's how you can do it so it's also on the client side as per usual go to the p. web and click on any coin open Chrome developer tools by doing a right click and then inspect and check out the network tab you'll notice it's making a few different types of calls but one call stands out it's making constant calls to with a get account info method to specific contract that contract is always a bondon curve account of the token that we have open there was yet another kind of interface additional proof that the price is fed from the bondon curve now that we can fetch this data with the get account info right which is a simple call we need to to deserialize it this is where we go back to our discriminator again and check our IDL as well but first let's talk the discriminator it's actually High time we started doing the discriminator calculation we'll need it for the buy and sell transactions later as well calculating the discriminator is pretty straightforward just check the anchor docks for the description on how it's calculated and create a basic python script to calculate the 8 by unique entry point and as always Pro tip you can probably uh use a claa to generate the script for you just dump that anchor chapter in it and ask to create a simple python script for the discriminator calculation so now the idal again first look at the ideal of the Buton curve specifically the entry for the bond and curve account you'll see a structure with feels like virtual token reserves virtual solar reserves real token reserves real solar reserves to token total Supply and complete we need to fish this data properly deserialize it using the discriminator in our IDL and a quick note on decimals it's something that tripped me up initially while Salan programs usually have nine decimals the p.
fun tokens actually have six decimals so you can verify this by looking at your the tokens in your wallet and I have a lot of them by doing the testing with the script and keep in mind keep this in mind the six decimal thing is crucial for accurate calculations so once we have all the data decoded we can calculate the token price and the formula is pretty simple token price in s is virtual Solan reserves divided by virtual token reserves you can also double check your calculation against the uh actual price on the pal. fun web interface it never hurts to double check and this will generally let you know if you're fing in the right direction and that your calculations are correct and there you have it now we are armed with everything we need we can fish the associated bondon curve we can get the token PR price data we can distalize data we can calculate the actual token price so now we should be able to construct the buy transactions and we are almost at the Finish Line all right now we have all the pieces time to put them together and construct our buy transaction let's break down what we already have we have three types of addresses that we operate with we have pump program constants pump Global which holds the fear recipient and the event Authority we have pump fear recipient we have pump program ID we have pump event Authority and then we have salana system accounts System Program account system token program in system rent and then we have token specific data that we get from listening over block subscribe and this these are mint the address of the token the bondon curve that we use to fetch the token price Associated Buon curve which holds the mean token Associated token account which we create to hold the token and then the user address now let's walk through how we construct and sign the transaction we connect our web socket to the salana network we use the block subscribe uh method and remember it gives us full transaction details and we extract all the necessary token data for the new limitted tokens then we call decod this data using our discriminator which we know how to calculate for the buy transaction once we get a printed coin we fish the bonding curve data and calculate the price then we use all this information to construct our buy transaction I also add the the sage in there so that it can be useful especially considering the fast token movements on p. fun and a quick note on timing because salana is lighting fast I read the cool down period in the script and here is why we get the coin me and all its details but to buy this token we need to create an Associated token account to hold this token sometimes you might get errors because the information from the mint event hasn't fully propagated yet I've said the 15 second cool down cool down period but feel free to experiment with this it's also the perfect opportunity to use the salana trader notes on chain stack which significantly speed up your token by process and there we have it now let's do a cell script all right we've M mastered The Bu script and now it's time to flip the coin and do the cell script it's basically the same but the other way around here is what stays the same we are still working with the p.
fun program on salana we are using the same Global constants and system accounts we are still interacting with the bonding cves and Associated token accounts and here is what we are not no event listening unlike the B script we are not listening for new token creation events we already know which token we want to sell here is what we need the address of the token we're selling the Bon curve and Associated Bon curve for this token we still fetch the price from the Bon curve just like in the biscript we need to set a slipage tolerance for our sell order this accounts for potential price changes as always between when we submit our transaction and between when it's processed we're ising the pre-calculated discriminator for the sell transaction remember how we calculate this for the B instruction same concept different instruction just like in the B script uh we are working with the six decimal places for the pump. fund tokens which is always crucial for accurate calculations with all these pieces in place we construct the sell transaction and it's very straightforward we take all our information the token address the B our details price LePage in our pre-calculated discriminator and put them together in a Sal transaction now let's put everything together in and create the actual trade bot all right now let's put it all together and create our very own trading and snapping bot our trading bot which we call trade. piy is essentially combination of all the scripts we have created so far listening to the create EV events extracting the necessary token information EX exu and buy transactions executing sell transactions let's also add some login to keep track of our Bots activities which is always a good idea so we'll have the token database we'll create a separate file in the trades directory for each token we encounter the file name will be the tokens address creating a simple database of all tokens we have interacted with then we'll have a trade log we'll maintain independable trades.
log file for each trade we will the time step action buy or sell token address and token price and the transaction hash now the global configuration to keep our bot organized and easily configurable we'll create a config pile P file this file will contain p. fun addresses salana system addresses and accounts the tring parameters slipage tolerance and the buy amount and also the network information the RPC endpoint the webset endpoint and our private key for execution the trades in the simplest form our our bot will follow this workflow connect to the salano network via web socket listen for the new limited tokens retrieve the token details execute a buy transaction for the new token wait for 20 seconds and then execute a sell transaction for the same token and that's our trading board very easy web 3 is certainly wild and fun so let's make our trading B fun as well we going to add a few Flags to it the yellow flag it puts the b in continuous trading mode mode uh the bot keeps buying and selling tokens without stopping then the match flag next let's add the match flag for the trend Watchers with this flag the B can token Nam stickers and descriptions for partial matches to the keywords that you specify the bot only buys tokens that match your criteria these are meme tokens and there is always a new trend so you can just round about partially matching the word and it will trade only those tokens that match the trend the bra flag the bra flag takes in a user account it follows a specific user address and buys the tokens created by that user for example you know that the tokens created by this user account always do well and the dev basically never leaves the chat and you don't want to Muse them the mer flag last but not least the flag that makes the trade board just buy the tokens and never sell you can also combine the the flags for example Mary and bro will buy the tokens from the dev that never leaves the chat and never sell them or YOLO and match will always continuously trade the Trent tokens have fun bonus section take your button Next Level congratulations you will you've made it to the end of our tutorial and your trading bot is up and running now let's have a little peek into what else you can do with your new found skills now that you've got your trade Bo working and you've even added Flags to it why stop there you're at the perfect launching point to take this project to the next level like turn your Bot into micras which is software service platform you can now create some similar to pum portal but with your own unique twist and this doesn't uh just apply to pump.