I created a meme coin trading bot in 2 hours

Brief Summary

This video provides a walkthrough of building a tweet tracker that automatically buys meme coins based on influencer tweets. It covers the architecture, coding, and challenges involved in creating such a system. Key points include:

  • Identifying and scraping relevant tweets.
  • Using an LLM to determine if a tweet is a buy signal.
  • Interfacing with decentralized exchanges (DEXs) to execute trades.
  • Addressing the challenges of transaction speed and reliability on the Solana blockchain.

What are we making?

The video aims to create a tweet tracker that monitors Twitter accounts for endorsements of tokens. The goal is to automatically purchase these tokens from decentralised exchanges (DEXs) immediately after a tweet, capitalising on the price surge that often follows. The strategy exploits market manipulation, with the intention of selling the tokens within a short timeframe (2-3 minutes) to secure a profit. The creator issues disclaimers, advising viewers against trading with money they cannot afford to lose and emphasising that the video is for educational purposes only. The project requires knowledge of Node.js, Solana, web3.js, DEXs, wallets, private keys, and the Twitter API. By the end of the video, viewers will learn how to interface with DEXs and automate buying and selling processes.

How $TRUMP coin and other Meme Coin work

The video discusses the recent trend of celebrities and influencers releasing or endorsing meme coins, using Donald Trump's meme coin as a prime example. Trump's tweet about his official meme coin led to significant market activity, with early investors making substantial profits. The video highlights how crypto influencers' tweets about other coins also cause immediate price spikes, presenting opportunities for quick gains. The common pattern involves a large green candle immediately after the tweet, followed by consolidation back to original levels. The goal is to follow these Twitter accounts, identify tokens they mention, and execute immediate buys with stop-loss orders. The creator reiterates the risks involved, advising against trading with unaffordable funds and suggesting building such products for others instead.

Architecture (Dumb v/s Smart way)

The video outlines two architectural approaches for building the tweet tracker: a "dumb" way and a "smart" way, mirroring how a high-frequency trading (HFT) system would operate. The "dumb" approach involves using a polling server to scrape Twitter for tweets from influencers. Due to Twitter API limitations, this requires either paying for expensive API access or using unofficial methods. Once a tweet is detected, it's sent to a large language model (LLM) to determine if it's a bullish endorsement and to extract the token address. The system then forwards a buy request to a Solana Remote Procedure Call (RPC) to execute the trade. The "smart" approach involves running a personal RPC node close to Solana validators, reducing latency and improving transaction priority.

Building the Twitter API scraper

The video addresses the challenges of using the Twitter API due to rate limits. The official API is heavily restricted, making it difficult to get real-time tweet data. The creator explores alternative methods, including unofficial APIs and creating multiple Twitter accounts to bypass rate limits. They demonstrate using RapidAPI's Twitter API to fetch user tweets, acknowledging that the response format is convoluted but manageable. The chosen API provides a decent solution for polling Twitter without immediate rate limiting, allowing the project to proceed with coding the tweet scraper.

Code - Scraper

The video transitions into the coding phase, starting with setting up a new Node.js project with TypeScript. The core logic involves creating a main function that polls Twitter every few seconds, retrieves new tweets, and forwards them to an LLM. The getTweets function is created to fetch tweets from a specified user, using the RapidAPI endpoint. The function parses the API response to extract relevant tweet information, such as content and timestamp, filtering out tweets older than a specified threshold. Environment variables are used to store API keys and other sensitive information.

Code - Sending Message to the LLM

The video focuses on implementing the getTokenFromLLM function, which sends tweet content to an LLM to determine if it's a bullish endorsement of a Solana token. The OpenAI API is used, with a system prompt to guide the LLM's response. The system prompt instructs the LLM to identify Solana token addresses within the tweet and only return the address if the tweet is a buy signal. The function is tested to ensure it correctly identifies bullish tweets and extracts token addresses, while ignoring bearish tweets.

Implementing the Swap Function

The video explains how to swap tokens on Solana using decentralized exchanges (DEXs) like Radium and Jupiter. It details the process of creating a local wallet, exporting its private key, and using the Radium API to perform on-chain swaps. The importance of slippage tolerance is discussed, along with the need to get a quote from Radium before executing a swap. The video walks through the code required to get a quote, create a swap transaction, sign it with the private key, and send it to the Solana blockchain. The challenges of transaction congestion and rate limiting are addressed, with the creator demonstrating how to use different RPC providers to improve transaction success rates.

Conclusion (Recording Stopped :/)

The video concludes with a summary of the project's learnings and potential improvements. Key takeaways include the unreliability of LLM responses, the need for manual parsing of tweets, and the importance of retry logic for swap transactions. The creator suggests spamming transactions, using different RPCs, and adding logic to sell tokens after a set period. The video ends with a demonstration of the tweet tracker in action, successfully buying a meme coin based on a tweet.

Bye Bye

The video ends.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the Apple Store
Get it on Google Play

Transcript

Full transcript of “I created a meme coin trading bot in 2 hours”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.

0:00 all right let me set the context for the video in this video we're trying to build a a tweet tracker what is happening very often is K some celebrity or influencer is releasing a token or endorsing a token and as soon as they tweet in the next one to two minutes the chart goes up by a lot this is what's called Market manipulation um the people are doing it um which leads to an opportunity um to you know try to make money so so what we're going to do is build a platform that is going to track a bunch of these Twitter accounts and whenever someone bull posts or posts about a tweet we'll try to immediately buy that from a decentralized exchange um and maybe sell it in like 2 to 3 minutes because that's usually how long the week lasts so the goal is it's very clear you know Arbitrage and Market

0:45 manipulation that is happening but thankfully it's happening in a decentralized way so everyone including us can you know participate in it um the video comes with a bunch of disclaimers most probably you're going to lose money so do not try any of this U maybe build it as a project for someone else um any token that you see in this video is not an endorsement for a tokens please stay away from all influencers I'm going to show in this video and all coins that I'm showing do not trade with any money uh that you don't afford to lose or otherwise just stay away from this U this is just a very clear you know obvious product to build which a lot of people are building SL need right now um if you saw my last video the person got like a 2.5 lakh rupe uh project for someone to build something very similar meme coin telegram bot so that's why we're building it do not buy any of these tokens U prerequisites it might be good for you to know a little bit of

1:31 node Jess Solana web3.js is good optional but you know good if you have it a little bit of knowledge about decentralized exchanges is great uh wallets private Keys uh and Twitter API even if you don't know that's fine we'll understand and build it as we go through the video um what you're going to learn by the end of it is going to be how do you interface with you know decentralized exchanges how do you buy sell do things like these in an automated fashion um I wouldn't consider this close to what uh an hft does hfts usually trade between centralized exchanges the challenges there are significantly different from when you're doing it on chain um think like chain transactions are slower and harder and you have to retry them versus you know much easier not much easier like much more comparative and much more easier programmatically to do this on a centralized exchange but when you're doing it on a DEX U there are some

2:16 challenges you have to keep at the back of your head those are the things that you're going to learn so if you want to build any different kind of a I don't know Market making B that's Market making between a centralized exchange and decentralized exchange this would give you all the parts needed for the Dex side and of course you know centralized exchange is a whole different Beast uh which probably is a title for another video with that let's get right into it all right hi everyone um and welcome to a new video so a lot of crazy things have been happening in Krypto recently um I'm sure some of you saw what Trump did he released his own um meme coin and by that I mean he literally released his own meme coin this is not a scam or whatever you can call it a scam but Donald J Trump the president of the United States uh tweeted this out which

3:04 a lot of people felt initially was a scam the Tweet says my new official Trump meme is here it's trying to celebrate everything we stand for winning uh join my very special Trump community and get your Trump now something you would expect from a scammy u influencer but something that came from him um if you go to the website um it gives you an to buy with the debit card buy with crypto but more importantly it gives you the contract address for the Trump token um given this contract address you can paste it in Dex screener or a different website U and look at the Trump ticker it's been a few days now so now it's Consolidated

3:51 to $36 but it started off of course at zero did some price Discovery went up to $90 and now is you know consolidating around the $40 $50 Mark um so that's a brief of what he did um if were you were early on this um by which I mean you know if you were following Donald Trump um and bought immediately even if you bought after an hour or two you would have made significant money um as in a 4 5 10 100x um the biggest bet here was someone put aund put like 1 mil right after the token was released um and cashed out 300 mil U there are speculations that's an Insider who who knew a few things um but still you know at least a week ago when this was released on Jan 18th um you could have

4:38 made U significant money if you were tracking this Twitter account um and had Bots running to buy the token even if you do not have Bots and you manually did it um you would have made some money um since then what has been happening is a lot of these crypto influencers who asked people to buy Trump um have been tweeting about a bunch of other coins so here is an example U there is this Twitter account called Crypt Beast that just tweeted when was this like 12 18 so like 2 hours ago okay I am bullish on this specific token the founder has this many followers and Instagram so on and so forth um and if you look at this specific token as soon as he tweeted um there's a very big green candle over

5:23 here um and I hope you you understand where I'm going with this um if you were following this account and immediately bought he would have you know made some some sort of a multiplier U and that's almost a given um of course this happens in less than a minute like after he Tweets in less than a minute he need to buy and then hopefully sell out quick because the common pattern that's happening here is whenever the influencer tweets there's a very big green green candle and then it consolidates back to its original levels here is another example uh there's this guy called Gordon he said I want to see Mia break this resistance Mia is another token um and this is when he tweeted like I don't know 20 minutes ago and as you can see there's a green candle over here though it's not guaranteed but

6:10 almost a given okay if you are buying within a minute of these people tweeting and then maybe selling after it 2 X's U for example here it started from0 2 and went up to 44 U so there some probability um that it'll 2x let's see what happened here it started from 0.08 and went up to 0.13 so did not really uh 14 um 0.08 0.14 did not really double up U so we'll have to figure out what the what the math is and where we you know take profit where we put the stop loss U but the goal of the video is what the goal of the video is to follow a bunch of these Twitter accounts U and whenever they tweet figure out is there a token they're talking about and if there is

6:56 one just buy the token immediately U and sell it at some profit or or put some sort of a stop- loss that's what we're building today um a lot of disclaimers this comes with um do not trade at all or with money you can't afford to lose because there's a very high probability all of this will go down to zero and especially meme coins like buy Blue Chips if you want but do not speculate at all with meme coins if anything try to build uh like a lot of these products for companies if you've seen my recent podcast onexis the thing that the user had built for which he got like $4,000 U was a meme coin trading bot only so so there's a lot of opportunity right now to build for uh dumb people rather than

7:41 being the dumb person yourself so even though the strategy will work or whatever you know for some time um do not recommend anyone do this and you know keep your head down and build um but it'll teach you a lot through this video you'll understand what are token accounts uh what are dexes what is a popular Dex on Solana we'll have to use a little bit of AI to forward this tweet and get context of what is the token address what is the user talking about things like these uh so it'll be sort of a I think a 2hour video that should be enough to be able to build something like this with that uh let me take you through the architecture of what we're building all righty U let's get into the architecture of building this there are

8:27 two ways to build this one is the slightly more dumb way um which will still work because of how the market is right now um and one is the the way an hft would do it um ideally you want to do this but this involves you having a validator I'll talk through a bit of it basically the the learning curve slash U the amount of resources you need to do this is a little bit higher compared to the dumb way the dumb way is fairly simple um this is the Solana blockchain um which means a bunch of validators that are constantly doing consensus whenever you are swapping whenever you're trying to buy an asset you hit the Solana blockchain um through what's called an RPC um server um an RPC is very close to a

9:16 validator only you don't need a lot of resources to run an RPC server um you need a lot more resources to run a validator to definitely won't be able to do this we might be able to do this U and if you want to understand how rpcs are run or how you can run a validator you should go through my recent video on 100ex steps where I've talked to someone called Dro who runs a validator on Solana um the the dumb approach says K U of course we'll have like before we get into the dumb and the hft approach we should probably first understand the web two parts of this okay this is twitter.com or a bunch of Twitter servers and this is going to be your call it a polling server or you know a scraping server um this needs to

10:03 constantly either PLL Twitter and get the latest feed of one of these influencers or you can also subscribe to it via a websocket stream um the only problem is Twitter apis are highly gated if you want to subscribe to a real-time stream you have to pay them $5,000 a month um which is a lot of money um even if you want to poll them um you need to pay them around $200 U yeah Elon has really bummed up the API request uh prices um so so basically either way you have to spend some money to be able to PLL Twitter there are unofficial ways to do it which is probably what we'll go with today um because even if you pay them $1200 a month um you can only request 15 requests per 15 minutes which

10:48 is very slow um basically we won't these trades need to happen you know a second after the person is tweeted so ideally you need real time connection to Twitter servers which is worth $5,000 a month something not we're doing today u a slightly more unoptimal approach is to subscribe to their $200 a month plan but then also you can only send 15 requests in 15 minutes that's one request per minute and a minute is enough time for you to lose out on the trade so the long story short we either have to go with the unofficial Twitter API or we have to find a different way around to you know um get around this one request a minute limit if this is the Twitter server we need the we need a intimation from them as quickly as possible as soon as

11:35 someone tweets okay buy the XYZ coin um almost immediately we need this to hit our systems um so that we can you know build the trade from there and execute it um that's step one what is step one um Step One is just getting the tweet from Twitter that's the not harder bit conventionally um it's just Twitter apis are so heavily rate limited okay it's not very easy to get it um you know in less than a minute or less than a second it'll take you some time to be able to scrape so we'll have to figure this out uh we were trying to build this yesterday in in our office so that's where we realized you know there's no easy way to do this um last thing we

12:20 left off at was there was an unofficial API that we can use up or what we'll do is we'll create 30 Twitter accounts so that way you know even if the request limit is 15 requests per second um or 15 requests per minute if we have 30 Twitter accounts then the request limit becomes whatever one request per 2 seconds U if my math is right so to be able to get around their API limits we'll have to create a bunch of accounts um on Twitter and only then can we get back an intimation from Twitter every 2 seconds whether or not a tweet has been made up but let's say this is our back end and we receive an intimation okay someone has tweeted from there comes the first part of sending this to an llm um the llm needs to tell us is

13:08 this a bull tweet um and what is the token address hopefully the person has you know put the token address directly over here in the Tweet that's easy if I send this to an llm the llm will simply pass this out and return this to me so I know what token to buy but if the person person has tweeted something like this um oh where did the the other one go yeah if the user has tweeted something like this then from Twitter we need to hit index tools and get the token address from here U they have an API that we can hit so we'll have to give this token name as an input um and

13:54 that will return us the token address so that's you know two steps that we have to go through so let's assume the person who's tweeting um has written the token address in the Tweet itself so the llm will quickly be able to tell us this is the token that we need to buy um once we have that token address then comes okay how do we buy do we buy using the dumb approach or we buy using the better approach the dumb approach May um we will forward a buy request to an RPC on Solana um how do you get an RPC will get to but basically uh we will spam the sonana network with a bunch of buy requests because a lot of people will be buying at the same time so we'll have to figure out priority fee and you know tip the validators a little bit to make sure our transaction lines um the reason this

14:41 is dumb is because U you're very far away from the action um if as I said there are there are two ways to do this the dumb way and in the dumb way what will happen is U this is the Solana blockchain there's an RPC in front this is our our back end and our back end will hit the RPC which will then forward it to a validator node which will then follow forward it to the leader up so there are a bunch of hops we're going through um the RPC may or may not prioritize you um and if your transaction doesn't land basically you've lost on the trade so this is even though it's easy to implement and in this case it'll work because there's no high frequency trading happening on these coins right now um not a lot of hfts are trading on chain all of them are you know trading on centralized

15:27 exchanges so since we are are trading on the Solana blockchain right now um this approach might work um but the better approach is that you have your own RPC so this is the Solana validators um you have your own RPC which means you run this node and on this node itself you have your backend server running that is receiving the request from Twitter forwarding it to an llm getting the token address and immediately forwarding it to a a node that is able to submit the transaction so if you're close to to the action um that gives you a bunch of benefits um some of the benefits being you can get the current price you can get U what is the most optimal pool to buy this from um we'll come to what all of this means um but ideally if what biggest Farmers or Traders are doing

16:14 right now is running their own validators and you know front running everyone um since this is a very big green candle it wouldn't really matter if you go with the dumb approach as well you still make money but the real way to make money over here is running your own validator uh or at the very least running your own on RPC so that you you know you can forward the request much faster than other people like it's one thing for you to know okay something like this has happened but it's a different thing to be able to land the transaction as quickly as possible um to like you can do this manually as well right like there's no nothing stopping me from opening this website as soon as I see the Tweet connecting my wallet over here and quickly swapping Solana for Mia but the problem there is okay it if you've ever tried this it'll take you like 10 clicks to be able to land the transaction um So to avoid that and also

17:01 I might not be on my machine right I might be somewhere else or sleeping uh the script needs to keep on running even when I'm sleeping and do the buy and the sell which is why um even though there are manual ways of doing it we are going to automate it U but there are two ways of automating it the dumb approach and the better approach um let's start with the dumb approach and then eventually we'll we'll see if we get the timer I'll talk through the better approach as well so to recap the architecture um of what we're building um um number one this is the Twitter server we need to make sure the Twitter server is somehow telling our backend server okay someone has tweeted one of these influencers that we Mark has tweeted we need to take their tweet and forward it to an llm the llm

17:48 needs to tell us is this a bull tweet is this person asking us to buy a token and filter out the token address for us given the token address we need to forward a request to the Solana a Solana RPC which will forward it to the Solana blockchain um to buy the token um how do you create a request to buy a token how do you find um what is the current price U that is a whole different ball game basically creating this transaction will involve a bunch of other challenges um figuring out a market where we can buy this currently because this is this might be a token that has just released so there might not be too many markets U that are listing it there might not be too many liquidity pools if you've heard of what those are U to figuring out there'll be another service here something like a Jupiter or a radium

18:34 where there is a pool up where you can buy these tokens U so we'll have to figure that out here as well create the transaction and then finally forward it to the RPC that is going to be the architecture Twitter will tell us hey there is a person who has tweeted we'll forward it to the llm LM will tell us this is the token address we will go to Jupiter or radium find if there is a pool find the best price for buying this right now and then forward this request to the RPC this is where we'll be spamming the RPC with a bunch of requests because there's a high probability a lot of our transactions don't land um so we'll start spamming by which I mean you know some 100 transactions 20 transactions we'll send at the same time and hope okay one of the transactions um eventually land in that is our B order usually the activity only happens here when you're buying when you're selling you should be able to sell it without spamming the the

19:21 network but when you're buying when someone is just tweeted there are a lot of bots trying to do what we're doing um and hence you know you have to spam the network you have to to get closer to the validator if you're using the dumb approach it's a good idea for you to run it you know somewhere where a validator is running let's say in Japan um so that your transaction lands really quickly that's a high level of the architecture um in terms of the prerequisites um there are no prerequisites but it might be good for you to know basic coding SL scripting um it is also good for you to know a bit about the Solana blockchain if you know what are liquidity pools um if you've heard of oh Oopsy Daisy radium or Meteora or Jupiter U might be good U

20:06 what else um you need to understand what are rpcs or you're going to learn what are rpcs um Twitter API which is similar to any other scraping so this is the web two bit here uh and and that's it there's nothing too complicated here as we try to build these systems you'll see you know it'll probably be less than 100 lines of code I don't know that's my guess um so without any further Ado I think it's time for us to get right into it and and try to code this thing um and as there's a lot of activity happening there's a high probability by the time this video ends even though the releases today at 600 p.m. in 3 hours um let's see if someone tweets and we able to catch the trade all right with that uh let's get into the coding

20:53 bit all righty folks uh getting into the coding bit now um so Step One is Twitter API U as I said Twitter has some apis I can show you one that you know gives you all the tweets of a user the only problem is it's heavily rate limited um so the way I found it yesterday was went to where was that uh try a live request NOP that's not the one API reference yeah this is their API reference page if you look at posts you can find post look up

21:38 um by user ID is what we want um so it's somewhere here users timelines that's probably it yeah there you go get user post timeline by user ID um so this is the API that you have to hit um slash two that's probably the API version SL users SL ID of the user SL tweets um and you have to pass in an authorization header um the way to get the authorization header is just go to uh Twitter a like just go to developer. x.com um and even without paying you

22:24 should be able to create an API uh uh a betterer token so let's see where that is uh AP reference fundamentals tools subscribe try request all right there we go um so developer. x.com n/ portal projects and apps um when you go over here it'll probably have a default project for you feel free to create a different one um and somewhere here there's this option to reach keys and tokens um which is what you want to create so you want to create a PA a token for yourself um I'm not going to regenerate once but but once you create one over here copy it over um and you can paste it here as the Au token

23:11 uh where did the API go I have to search it again oh go oh there we go so they have to paste it over here U so I copied the token from here when I generated it uh the better token pasted it over here and when I send this request um it will return me everything all the posts of that user so for example the ID parameter is not correct uh yeah I need to pass in the user ID over here um so yeah over here that means we need to find the user ID of that specific person so let's say you know golden is the person that we want to follow U how do you find their user ID is I don't know there probably a better way to do it but the way I did it was I clicked on message over here and when you try to

23:57 message someone um it gives you you know both of these IDs I think one of this is me and one of this is him this is their ID I'm sure there's an API that Returns the same thing um but let me just assume the user ID that I got there was correct and try to send it now um now if I send it as you can see I'm already rate limited because I can only send one request every 15 minutes that's a problem um this this this is great I was trying this yesterday as well and um this is the same request over here I have my o header here and in the body not in in the body there's no body that I need to send um this is the user ID of that person 13544 when I click on send um it returns me all the tweets of that

24:44 specific user um at cobrate just sent this to my wife oh that's the first tweet SL reply that this person has U and if you look at Gordon over here and his tweets he probably this is probably a reply um but this tweet if you see homie had three Soul left that's the same tweet he made over here looks good to me Dollar Regent and that tweet should be somewhere here everything else that you see here are replies I'm sure there's a way to filter out replies even though it might be a good idea for you to look at replies as well or quotes as well for example you know here he's telling everyone to buy the Regent token um to God these guys are weird um so so that's how you get the tweets of a user

25:32 um this is there are a bunch of problems here first problem is you know you can only send one request every X seconds if I try to send the same request again um I'll get rate limited so Twitter is heavily rate limited even if I buy the $200 plan um it'll still be one request per minute which isn't great though so what's the way around this way around the first way is can you know have 40 Keys 50 Keys ready so you can keep cycling between the keys and send requests that way you don't get rate limited um number two buy the $200 subscription number three um is use the Twitter I mean there's another way to you know look at like keep find the network request as you scroll I'm sure there's a request there you go user tweets um there's a this is a graph ql request where I can

26:18 send some payload and you know in response I get all the tweets of the user u in this timeline V2 object um content item content social content tweet result result uh views and Source somewhere here should be the Tweet as well yeah there you go full text I gave you th000 text on the first day of the year so so another thing you can do is you know copy or like bring over this request and figure out the graphql inputs that are going um and try to change them um the problem there is again one this isn't legal like you can't use this as a as a scraper um but

27:05 when you know and you also also might your Twitter account might get banned um so that's the problem with using this approach you try to reverse engineer their apis and try to figure out what happens as you scroll through a post and try to hit that API again and again that's the second approach um the third approach is The Unofficial API which I think starts a browser I could be wrong so Twitter unofficial API uh this is one Twitter API or twttr API um which is on rapid API which is great let's see get tweet use a tweet that's what we want um and let's try to copy this call request ah that that's looks easy enough

27:53 um user so tw Twitter api. p. rpid ai.com user tweet Mark username equal to Elon Musk um let me replace with them with this guy send and it says you not subscribed to this API so this is a paid API um API overview basic $ a month will give you 50 notification 50 a month let's start the free plan um and go for from there uh log in with Google I'm not sure how rapid if does this like is it constantly indexing um

28:40 Twitter I don't know we'll see her oh God uh learning submit all right let's start things with the free plan um which still expects me to give a credit card that's not great um yeah let me put this off screen here um and quickly try to pay all right um I've made the payment and now I think this x rapid API key has been added to my curl request now if I

29:26 try to import this in post man I hope um the request goes out I don't want to send DM I want to get the users tweets so this is the API I'm going to select I see the rapid API um key over here to import replace Elon Musk with our guy and there we go um oh the response is oh seems like they are using some level of graphql um looking at this um yeah God it be sort of hard to pass this

30:11 out but there we go that's the first twet of this person and then you know as we keep going we can pass out the other tweets um it'll just be a lot of text that we don't need um but that's fine results timeline responses timeline instructions um timeline pin entry that's probably the Pinn tweet timeline add entries um entries and then this is the first one this is the second one um content tweet results uh there you go that's the second tweet that this person made third tweet so I'm pretty sure uh these guys are using the graphql Twitter graphql API under the hood um doesn't really matter for us as long as they're

30:56 not rate limiting us which seems like they're not um if I send the request again yep um I'm able to get back the response so great I think this is a decent API for us to use uh while we're polling what is the problem with this API the only problem with this API is that this uh the response is fairly convoluted so we'll have to filter it out but that shouldn't be too difficult though the most difficult bit is done um honestly it's pretty simple uh I thought we'd have to spend a lot of time here but in the worst case we we can subscribe to that plan and you know um increase our limits but for now this is good I think we can get into writing the code um and get the tweet for the person forward to the llm yeah pretty simple stuff from here let's get into the coding bit

31:43 next all right folks let's get into coding it um as I said this shouldn't be too many lines of code um so let's quickly write some pseudo code of what we have to build then we'll build all the functions one after the other uh uh bot app Soul CD bot app soul and open it in Visual Studio code uh npm init Dy to initialize an mty package.json npx DSC D- init to initialize an Mt TS config.js

32:30 um install typescript before that maybe use pnpm so pnpm install typescript um followed by npx TSC D- init um then go to TS config and change the root D to be/ source and the out D to be/ list um create a source / index.ts file where we'll be writing all of our code um effectively we need to have a main function um that needs to do a few things number one like first of all M needs to be get like needs to keep on pulling every 5 Seconds let's say um there are better ways to do this but let me just put an interval let's not put an interval let's

33:16 just call it once for now um and then we'll see how we can spawn the process every 10 seconds um here we have to do a few things we have to um const new tweets equal to await get to tweets and the user ID um of the person that we want to scrape so let's say there's a user ID here um or even better let's create an array of user ID maybe I don't know let's just start with one person that we want to scrape or one tweet that we want to stay uh scrape um once we get the new tweets of tweets of this user um we need to forward them to the llm so const llm response equal to await um or I rarely will it happen get the person tweeted twice and we got two new tweets at the same time but still um

34:04 for tweet in of new tweets um await check cons token equal to or token address equal to await get token from llm um which will take the tweet do description or something um tweet array let's make sure let's make a tweet um look something like this it'll have a description string and an ID string I think that's good enough description is you know whatever is written or the Ty contents is probably a better name for it contents of the tweet and the ID of the Tweet um so this will be tweet.

34:53 contents um once we if the token address that is returned from the llm um which means there is a token in there is when we have to await um create swap instruction and await or maybe you know broadcast the transaction really bad so const transaction equal to this thing and for zero I let than um threshold or I don't know spam count um send

35:42 transaction the transaction that we've created um that is good enough to buy and then we have to write a lot of Ric to sell as well um but right now what we have done is we've created an async function called main um which will first call the this get tweets function so let me create a bunch of files here uh export function get tweets this will take a user ID as input and its job is to get all the new TW tweets that have happened in the last 5 minutes um I think that's fair any tweet that is happen in the last 5 minutes or maybe 2 minutes probably a minute considering we'll be running it every minute um is what this

36:28 guy needs to get so here is where we have to write what we wrote in Postman before um so I can just export this request as an axio request from Postman I can add axos as a dependency um and I can import axios at the top of the file cool um now that we get the response we need to parse out the response which looks fairly ugly um this is what the response looks like um and we need to get all the tweets that have happened in the last let's say two minutes um const

37:16 like there's no point reading a tweet that's more than a minute late or 2 minutes late tweet underscore maxcore time equal to let's say you know 60 milliseconds so if it's more than that then just ignore it um so we'll first have to get all the tweets and then filter them out only the tweets that have come in the last one minute is what we interested in um so I'm sure I can change the no well let's see let's see if the Twitter API docs whatever we were using rapid API um does it let us specify as inputs uh pars yeah it does not um so that's fine also this can just be usern name and you

38:03 not user ID um name and here we'll be replacing it with username um and in get tweets Also let's take username as an input and not the user ID uh cool this guy needs to return all the tweets that are relevant um so it'll return a tweet array this is the request that we're sending I should probably get this from an environment variable

38:53 um let's create a top level. n file where I export this environment variable um let's also create a top level do n do example file so other people can look at what kind of Secrets they have to put um cool uh the get tweets function returns a tweet array so here we can just throw an error we can't I mean or we can just return an empty array I guess um but here is where we have to um pass out the tweets and get the latest tweets so here I would have loved if AI was here or I'm just probably just going to use Claud to filter out this response because this is a very big response I'll have to spend a lot of time figuring out getting the

39:38 right Keys um I can still do it manually you know it's for sure data. user results. results um do timeline response so I I can also do this uh const tweets equal to response do data dot let's put this on the right put this on the right and then maybe vs code on the left and try to figure out how do you get the tweets so oh well all right we can do that so let's make this full screen make post man full screen data. user results. result. timeline response so data

40:24 dot user result Dot result dot timeline uncore response dot um timeline do instructions dot oh that's an array um which has the Tweet so timeline do instructions pin entry um we don't need this we don't need this we need this timeline add

41:10 entries um so instructions dot timeline response equal to filter X doore type name equal to um timeline add entries um so that gives us this array um this do entries is the array of tweets so const have a meeting in 4 minutes tweets equal to um Tim response dot entries dot Map X to um

41:58 um content will be X do content. so x. content do um more content x. content. content. tweet result do result. core dot user result. result do description user result. result. description um that should hopefully be it um this should give us the tweets which we

42:44 return to the end user um we should give this u i mean feel I don't know this like Twitter API will change a lot so you know there's some Merit to giving it a better type here but even this is fine um what I'm doing is I'm sending the same request and trying to pass out this very big response object into some more digestible things that we can work around um one more thing we probably need here is the time stamp because we're only um interested in the last 1 minute so let's see if we can get that um there you go created at um in the same line I think not result so so content is this

43:30 thing ID and created at these are the three things we need um let's see where ID ising do created add is right there so it can just be created undor at here but for the ID uh I might have to spend some time here idore St Str which level is this at entries yeah same level I think um yeah

44:17 so ID would be idore S there you go so that's how you should get the tweets I have a meeting in 1 minute let's see if this it works um let me go to to index.ts let me comment out everything below it and let's see that's called main with that alt coin Garden ID um let's try to log this and then we go from there console.log new tweets I'm sure I've missed something I can't get this WR the first time but we'll see uh let's add a build script and the Run script really quickly um build script will do TSC

45:08 DB start script would do node dis index.js and Dev script would do npm run build followed by npm Run start let's try to run the dev script and of course it gives an error let's quickly try to see where the error is promise of tweet array export a sync function and access. request should be awaited so cons response equal to await this thing um and everything else should look like this there we go um all the type errors

45:54 have gone away let's try to build it one more time um X has an explicitly any type so let's make it implicitly any sorry implicitly any type let's make it explicitly any let's try that one more time and it says can't find d/ index.js even though I do see it here um what folder am I running this on looks like the right folder H oh index.ts is named incorrectly there's a space in front of it let me remove that let me try that one more time and now this time invalid API key is what it says um oh I haven't added n I'm one minute late to my meeting so pnpm add.

46:40 EnV uh require n do config let's try that one more time and this time yeah I've not pared the response correctly um which is easily fixable I will take a small break attend my meeting and back in a while all right uh where we left off was that the response isn't being passed correctly um entries. map is not a function so timeline response let's get there uh get tweets. TS this is what timeline

47:27 response looks like um do entries is not so let's log timeline response let's try it one more time and timeline response looks fine oh of zero do entries here so response uh of zero. entries let's try it now x. content. content. tweet results I think it'll be tweet uncore results let's check it in Postman as well

48:12 uh oh it is tweet results content dot tweet result result oh cont I think it's a content um oh entries is an array and which has one content of type name this is where AI would have been super helpful um timeline and entry so the first first entry has content that looks like this

48:58 the second entry has content that looks like this so yeah we only need uh One content here let's try it now tweet result. result uh content dot h okay this is complicated now reading through its schema one second hello all right um oh where were we um tweet

49:46 result instructions has three items only have we don't worry about that entries dot each entry is what it has a content and then that has another content inside so every entry does have two contents one second um entries every entry has one content which has another content which has the thing right um so I guess what we had was right why were why was this failing or where was this failing cont content. tweet result is what's failing so content.

50:33 content do tweet result that looks right um maybe it's failing on one one item maybe it's not the schema isn't the same for everything even though it looks the same so let's do this let's just log this here console.log jon. string if I um entries of zero Dot content and then let's also log. content it looks fine content cont looks fine um then tweet result is giving us a problem so let's do one more thing let's log. tweet

51:21 result it is true for few rows not true for a few rows I'm assuming some of these post are I don't know a retweet where tweet result isn't a thing so what we can do is put this inside a try catch or something so um yeah let's do this um con twet equal to Mt and let's over here put this inside a try catch

52:16 uh tweets. push this thing content doesn't exist on tweet uh is it content or contents it is contents so let's do that and let's also add created at here created at which is a string let's see if that fixes it um oh one thing I no I didn't miss anything all right everything's undefined uh all right [Music] um content. content. tweet results. result content. content. twet result.

53:05 result do core do user result do result do description uh that looks right God damn it um console. log let's see what that gives us that looks fine uh

53:51 dot what did we log do result so we logged this which has a bunch of things um it has Legacy do description that makes sense um all right that looks good um we've been able to log or get all the tweets of this person only problem is some of

54:36 these tweets might be very old so let's filter those out as well what is our tweet Max threshold it's 60 seconds so what do we have to do tweets. filter xx. created at new date. get time is less than um date do now is greater than date. now minus threshold um or what is it called tweet Max time Ms that looks right um let me increase this to 1 hour for now so that's 1 minute that's 1 hour um yeah so let's

55:24 see if can I get back to the tweets only for the last 1 hour when I do this uh I got an empty tweet so let's do 24 hours that will be one day nothing I think I'm missing another thousand here um because micr still very empty then the logic is incorrect um new date ex get time J twe is greater than um the current date minus this thing now that looks right there's nothing wrong there filter this thing is greater than this thing so let's log this

56:12 um the zero tweet do created at. get time um and let's see what that returns that returns this number um and what is the current time console.log date do now that is very far uh why is that like 1611 what was created at console.log

56:57 it would just be easier if there was a Epoch timestamp somewhere here um Wednesday 27th Jan today's what the 2021 uh huh that looks wrong this was a tweet from a few days ago uh this is something else this is I don't know the user created time or something this is not the Tweet create time so so let's keep on going and's see if we can find a different Tim stamp uh I can't this tweet was definitely created today and today isn't 27th Jan 2021 so the time stamp here looks incorrect unless I'm missing something

57:42 obvious edit control edit read this looks like a time stamp okay until when is this thing editable um this looks right legacy. creatore uh okay so let's do that so it will be result do Legacy do creatore at yeah that looks right is result. Legacy dot

58:28 that's exactly what we have oh no what result do Legacy hold on result dot core result do legacy. created at let's try that and there we go all right let's decrease the time stamp to be just 1 hour p let's see if we can get just their tweet for the last 1 hour um created at 23 and7 UTC I don't know if that's 1 hour oh that's too many

59:14 thousands here I think all right let's try that one more time 24 11:57 12 12:30 that looks right um yeah that's 1 hour uh is it though 12:30 UTC is plus 5:30 that's 6 very good um this was tweeted at yeah 5:30 and this was tweeted at five or 7 and six yeah makes sense um I think it's right I could be wrong um but this looks right to me um so we have the tweets for the last 1 hour we can decrease this eventually to you know a minute because that's what we really care about so let's do do that or for now let's keep it an hour so we have something to try or maybe 30 minutes

1:00:00 whatever gets us one tweet 30 minute also gets us three tweets or no only two tweets that's great um cool take we get all the tweets and for every tweet we need to now so that part is done right you get how you can get the tweets for the last one hour 1 minute from a user and put them in this new tweets variable if you're confused just go through the impation of get tweets it's very ugly because of the schema of what the API returns us but given we have that now we need to write the core of the logic to hit the llm figure out if there's a token here and do the swap so that's the more interesting tricky bit here let's FOC focus on that next um let's comment this let's just try to implement the get

1:00:46 token from llm call back so let me create another file here get token from llm dots export a sync function get token from LM which will take some contents as an input and needs to hit the llm get back if this is a uh a tweet that is about a token or not and if it is about a token it should return a token um address to us so now we go through open a docks um let let me do that in the next section of the video so next up we're going to go and implement this get token from LM function very simple implementation based on what um API you're using let's get into that

1:01:36 next all right uh let's see what llm we are using I have CLA and open credits I think uh api. open open dashboard I think I have some credits either in CLA on or open AI um oh that was simple uh let me just copy this first where did it go it was a very nice um code yeah there we go um let me just first bring this to the code base rooll user right hio about I think I can give a system prompt here but yeah I should give a system prompt here because the Tweet can just you know inject inside the LM response

1:02:21 so how do you give a system prompt oh pnpm add open aai and meanwhile let me create an API DOC for myself sorry an API key for myself uh dashboard API Keys generate one I'm going to call it U sobot create secret key I will of course hide this or delete this by the time the video comes uh let me copy it let me put it in my n file open AI key equal to this thing and let me do the same thing inn.

1:03:08 example uh next up let's go back to this file and let's see can I add a system prompt how do you add a system prompt here create body um messages model audio frequency function function called system I guess I have to give it in messages only so rooll system there we go content you are an eii agent that needs to um tell me if this tweet is about a

1:03:55 Solana token return me either the address of the Solana token or return me null if you can't find a Solana token address in this tweet um that looks good and here I can give the contents of the Tweet as such um that should be it I don't think I need to do anything else this prevents prompt injection like no one can tweet okay something random that goes to my open aai U whatever they tweet goes as the user prompt and the system prompt is okay you're supposed to pass this out and return me either the token ID or null um so let's test this function out

1:04:40 really quickly um return completion dot um uh I also don't know what it returns so let's go back to the original page which had the API docs really quickly um yeah it doesn't know it doesn't return the response so let's quickly log it um completion and I'm assuming I need to pass in some Secret in here um API [Music] key process. n dot open I key let's try that let's just independently test this function and

1:05:25 make sure it works as expected so I'll in index.ts let [Music] me comment a few things and simply call what is the function called it was called get token from llm so get token did I not export the function I did get token from llm I should call it get oh yeah token is fine it's just a little confusing and I'm going to pass in the contents of the Tweet which let's copy one of his tweet where he was Bull posting um with the address of the you know there you go just open a 250k shot

1:06:11 on wi Co SP for a huge profit wake up and start selling let's see all right um when did you tweet sir 613 618 H small break guys sorry folks I wasn't recording for the last 10 minutes um I'll take you through what I did in the last 10 minutes um let me comment this I pretty much fixed the implementation of get token from llm so this get token from llm function me I change the system prompt a bit because there could be short signals as well so I added this line can only return the token address if it is a bull post if it is a bare post then we don't want to buy the token um and this is returning us the right things it returns us either null um in this content or we fall back to null or

1:06:56 it returns us the token address let me quickly show you uh that in action uh what the function called get token from llms and if I pass it uh a bull post like this and try to log it const response equal to await this thing then you will notice uh that the token address gets logged over here um but if I make it a bearish post if I say um I am

1:07:43 bearish on freak uh short it or something like that then it returns null um that way we know okay we're not buying tokens that people are asking us to shot all right um cool so the get token from llm function is done um so right now we have a function that gives us the tweets for the last one minute of a user um then it asks the LM is it a relevant tweet is this a token Buy Signal that this user is telling us and then if that is indeed true this is a tweet about that is when we validate tokens so yeah I wrote I haven't written these functions I wrote the function signatures for them you don't really need this effectively you have to build the swap function now um the swap function which will take a token address as an input um what token

1:08:29 you're trying to buy and the amount that you want to buy it with uh how much Soul you want to spend for example I've said this to One Soul right now um Lamport per soul for those of you who know is you know it's the smallest unit in Solana just like you have PES as the unit in Indian rupees like you can give someone 20 PES or 50 PES I don't know if you've done that um similarly in salana you can give someone 0.1 Soul which is equalent to like 100 lamp ports so lamp Port is the smallest unit um whenever you want to send send Solana to someone what you're actually selling them you're not sending them one Soul you're sending them 109 lamp orts so that is what this amount over here is and we've written the function signature of a swap function that we need to now Implement here that takes a token address as an input and amount as an input and does the swap um this is where I introduce a bunch of things like radium Meteora uh

1:09:15 what's it Jupiter and you know we understand what these are what are dexes how can you do an onchain swap for these meme coins because these meme coins aren't really listed on a on binance or VX whatever sorry coin dcx U so you can't really go there and swap uh by or shot to do all of this we do this on chain um and hence we need to implement the swap um function over here cool so let's get into implementing the swap function which will actually try to do the swap SP spam the blockchain with a bunch of transactions and let's see U if you're able to land one of these all right let's get into that in the next section all right um implementing the swap function let me give you a quick one1 of how you can swap tokens on salana especially mean coins um if you

1:10:01 have a wallet like this one right for example I have backpack over here metam mask is not the best example uh I don't have Phantom open none of my wallets are working I don't know what happened yesterday I me see if works here yeah if you have a wallet like this for example which has $24 for me um what you can do is um you can swap this token for a different token this is Solan and there are something called liquidity pools um which is the easiest way to not easiest probably the only way to like there are other ways but this is the most efficient way to swap one token for another um what do I want to do I want to swap how do I close this um seems like I can't oh there we go I

1:10:48 want to swap sold for you know whatever random token address that this person tweeted about right so what was the token address that this person tweeted about it was this one right here so let's copy the token address paste it over here and as you can see I can see the freak token and I can whatever uh connect my wallet Phantom and buy let's say 0.05 Soul worth of token by clicking on Swap here so if I would have done the same thing over here right when he tweeted I would have made some money if I exited at some point of course uh whenever these candles happen they also go down

1:11:34 eventually um though all I had to do was this but I don't really have a UI so thankfully these guys have apis that'll build the transaction for you then all you have to do is forward the transaction to the blockchain so there are a few things I have to do number one I have to create a wallet like this but locally so what I can do is I can export the private key of this wallet U and import it in my nodejs codebase that's the first thing I can do and once that is done uh I can hit the radium apis or the Jupiter apis or the Meteora apis and try to swap from my Solana to this specific token so I'm hoping this is straightforward like it's very similar to a web2 system just like we're scraping Twitter through apis we're scraping radium for through apis the only thing is you can't hit radium and it can't build it can't send

1:12:19 your transaction um when you hit radium what happens is you ask it to build a transaction for you um okay hey this is me this is my wallet address and I want to swap 0.1 Solana for this random meme coin and it'll build a transaction object for you which is basically a bunch of bytes and then again your nodejs process that's what's on the left here needs to forward this um to the Solana blockchain to because you have to sign this transaction first so I will have my private key over here um this private key is you know what governs my funds if I I will show you my private key in this video to whoever is the first person can pull out $10 from it you're welcome uh given my noj back end I will

1:13:05 hit radium um ask it to build a transaction for me it'll build a transaction I will sign the transaction using my private key and then forward the signed transaction over to the salana blockchain the most difficult bit or the most you know um time consuming bit is this one because sonana blockchain will be very clogged especially for these accounts where everyone is swapping to get in their transaction um so we'll be spamming this transaction again and again um so that hoping one of them lands um and that's the system we have to build U we can replace radium with a different um decks or Dex aggregator like Jupiter uh but most of these meme coins are getting listed on radium first so you know if a tweet comes immediately after it's listed U radium might be the best choice or Jupiter both of them have apis either is fine okay so let's see if

1:13:51 radium has apis I know they do because we taught this in the C radium apis um let me open it and I just want a simple swap API uh uh trading trading balances all typ trading Fe archive support all right there we go um create a pool burning just search for swap there you go swapping at the top so which one of these do you want Oh God to

1:14:37 listing trade apis get quot so we don't really worry about quot in case of meme coin quotes will anyways not mean much because quot but you know that's just God knows what that will change very quickly as a lot of people are buying so there's no no point hitting this end point um it uh actually might have to hit it because we'll set slippage based on the code um okay so what happens in case of these token any token right if you look at any Dex the one that I was showing you U there's this thing called the slipage so if you click on this settings over here it says something called swap swap slippage tolerance um whenever you are sending a transaction to swap um you have to give two things you have to give okay I want

1:15:23 to swap 0.1 Solana but you also have to give I want to swap it for at least 10,000 of you know F tokens um you have to give this second argument okay because the thing is uh yeah very long thing but you might have heard of me or you know U sandwich attacks what happens in case of a blockchain is if you just tell it 0.1 Soul if you tell it these are 0.1 Soul give me as much as I can get um you might get squeeze and get zero um you might have heard of horror stories where people had very high slipage and put in a million dollars only got back $5 worth of ethereum um so for that reason um we first have to get the current price um the current quote from radium um so we have to send two requests not one before that um there are three things we have

1:16:09 to send I want to convert 0.1 soul I want to get back at least 10,000 uh or 100,000 FAA and I have a 5% slipage tolerance U this means even if I get 10% less FAA I'm fine with that because this is the current price U when you look look at this UI this number that you see here isn't necessarily what you will get you might get less than this you might also get more than this but the price of freck might change by the time you click on the swap button and sign the transaction so in blockchains um the first thing you do whenever you want to convert let's say I want to convert 0.09 Soul you get back a quote price 1741 it's changing but at this that specific second this was the price based on which I could get back 1741 tokens so when you select swap um or when you click on Swap

1:16:55 you're telling it I want to receive at least this much fck token this is the current price I have 5% or 0.1% or 0.2% slipage tolerance at least this is the current price come if the price moves by a lot or if I'm getting sandwich by a bad actor then I don't want this come fine if it's less than this I don't want it so whenever you are on the blockchain on any token let's open one of these tokens on Dex screener um this was our token right uh F FR I token even if I go here and I click on buy um you will notice okay it has how much do you want to swap and what is your slipage tolerance how much uh slipage can you accept given the current price so here also if I it says you will

1:17:42 receive minimum of these many F tokens you might receive more but your slippage tolerance is clearly stating okay current price I might get 1,00 but I want 1900 say 5% come I should get back at least 18853 so that is why the first request that's going out is what is the current price and then the second request that you can send is okay based on the current price I'm okay with 5% slippage or 10% slippage and then put my transaction in so in Jupiter I will first have to hit this get quote parameter or get quote endpoint um and once I get back the quote is when I will you know um send the transaction to actually uh swap so if you look at it uh they have blocks over here thankfully and they have an SDK that we can use

1:18:28 which is even better so let me add their SDK to my codebase uh let me import all the things they expect us to import here which are you know the transaction object that comes from s/. JS U native mint account or you know sorry uh argument that comes from pnpm add at Solana SL SPL token if you've gone through the web three Cod hopefully you know all of this is fairly straightforward U Connection owner and fetch account data from do/ config so we have to create these um const connection equal to new connection this is um this connection

1:19:16 object is you know you telling where is the RPC server where does where should I send the transaction if you remember I said after you're done with all of this you forward this to an RPC server which forwards it to the Solana blockchain or the leader of this current you know block up to this connection object represents you your connection with your RPC um how do you get an RPC what is an RPC URL we'll come to um but that is what this connection object over here means um and it expects an argument which is your RPC URL so there are many ways to get RP PCS and this is the thing you optimize for if your RPC is very fast U like you can go to something like Alchemy or helas get an RPC URL and if

1:20:02 you're RPC is extremely fast if you've paid them a premium for faster transactions um your transaction will land faster so here is where you decide you know or pay a lot of money either bring up your own RPC um or pay someone money to get you a dedicated RPC that's really fast so that your transaction lands as soon as possible so one way to get an RPC URL a free one is by going to Alchemy um creating a new app I already have a bunch of apps um so let me just open one old app that I had creating an app is nothing it's just creating you know for every script that you might create every wallet that you might create every deex that you might create you will create a separate app and in the network section over here you can copy the Solana RPC URL and paste it over here um so that is what this

1:20:47 connection object represents um so I'm not importing it from do/ config I'm creating it over here owner is most probably uh the owner private key so const owner equal to new uh key pair and I can paste the private key of my wallet so this is the wallet that I have which has $24 in it let me create a new one called Soul class which is currently empty let me export its private key so I can go to settings search for private key and Export it now if you guys can see this private key you guys can pretty much you know have access to my funds I don't have anything in there so you know feel free to and the problem is you can't really

1:21:34 paste the private key like this over here so we have to um convert the string into a bunch of bytes to Solana key pair object from string or base 58 string there's a gist um for it like fairly common that everyone use is um yeah that's it I think there's a simpler implementation there you go yeah since this is a base 58 encoded string we have to add the BS 58 Library um which we can import import BS 58 from

1:22:20 BS 58 and now I can create a function that that given this secret key um creates the keypad object so is owner object here um can be built like this process. do private

…

Share

More video summaries