How to Get Price Data from Quickswap with Javascript | 10-Minute Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up everyone steven here from techmaker studio in this episode of 10 minute tutorials i'm going to show you how to interact with the quick swap smart contracts using javascript i'm currently thinking about how to do a longer series on how to build out your own trading application in a nutshell we would build out a user interface that's our own design on top of the quick swap smart contracts which would be pretty cool um maybe come up with some interesting ideas in there so be sure to subscribe if you're interested in that i think we may do that one pretty soon but with that said let's go ahead and jump into this quick episode so before we can actually write any code the first thing that we're going to need is a way to interact with the polygon mainnet and the easiest way is with alchemy and i'm going to go ahead and sign up for a new account here so we'll just do steven techmaker demo is my last name and then we'll do steven plus demo at techmaker.tv so i'm setting up a demo account here just so that you know essentially you guys can follow along and see my info without having to hide too much stuff on the screen and it's just a throwaway account um so i'm gonna go confirm this email really fast um and then we'll get back into it so i just verified my email and i'm back on this screen um now it's prompting me to pick an ecosystem so i'm gonna pick uh the ethereum ecosystem i'm gonna just type demo here the demo then the app name will be demo and then what we're going to do is select the polygon main net down here now alchemy is one of if not the only i haven't looked at every single thing but it's one of the only things one of the only platforms out there that will let you access the polygon mainnet for free in my case i'm just going to pick free forever and continue you can skip this credit card info and then we go with capped capacity because that's the only thing you can get when you're using free and then let's just type tutorial here and say let's go there's a link to alchemy down in the description if you want to click that it is an affiliate link so that helps us out um but now that we're in here we have access to two things so we have this uh polygon mainnet http and then the same thing web sockets we're gonna make use of the http just leave this screen open and we'll come back to it in just a minute and by the way the reason i mentioned this is a throwaway account is because i have this uh polygonmainnet.com or whatever alchemy.com and then eventually you'll see the key in here and this will be essentially my api key for this account uh don't use that because if everybody who watches this uses it it'll run out and it won't work so you can just set up your free account and uh i just kind of made this so that i wouldn't have to worry about like blurring this out in editing so over in our uh in our terminal i'm just going to make a new directory and we'll call this demo again i guess i'm just going with easy naming in this video and we need to install two libraries we need to install the quick swap sdk so we'll just say npm install quick swap sdk and uh this shouldn't take too long and we need to install one other library called ethers now if you know much about the crypto space you'll know that quick swap is a fork of uniswap v2 and so this quick swap sdk is also the same as the uniswap sdk so if you're ever confused quick swap doesn't really have a lot of documentation it's pretty much the same though as the uniswap sdk um so you can check if you ever have any issues in the unit swap and it should pretty much be relevant so with those two things installed uh let's clear the screen and then we'll say uh node and then i'm gonna run the experimental if i can spell repel awaits so this will allow us to use a weight in the in the command line here and what we want to do first is just const q s equals require and then quick swap sdk and then i always like to kind of poke around and see what all is in here obviously we're not going to read all of this but we can look and see like we have qs.chain id which you can see like if you read the uniswap docs like i was saying it's a bit different it's not exactly the same but we can actually pull back the matic chain id here which is 137. we'll always just type 137 because it's easier for the most part um [Music] so yeah what we want to do is actually look at a couple of tokens and look at how we can get a price coming back from the actual contracts for those tokens so we're going to kind of build up to it before we go too much further though we actually do need to go ahead and hook up our provider in this case that is alchemy and provider is sort of like the sort of standard terminology just for your uh blockchain connection essentially um so what we want to do is just say alchemy equals and then we'll paste in this url and again paste your own and then what we want to do and i actually forgot i need to say const ethers equals require ethers in here and then we'll just say provider equals ethers dot providers dot json rpc provider if i'm remembering that correctly and then we can just type in alchemy and i need to add new at the beginning that's actually a very good error message actually which is unusual um but anyway so we missed new so we go ahead and create a new provider and now what we can do is actually start talking to the contracts a little bit the first logical thing to do is to actually go to quick swap and find a token that we want to look up some info about and so i'm going to open up this charts in a new tab and this takes a second to load sometimes but this has all your stats about liquidity and so on on quick swap if we click on pairs over here i'm just gonna pull up pairs for abbe and what you'll see why we're looking up pairs in a second but for now there's an ave tell pair and if you don't know anything about these tokens it really doesn't matter but what we need to do is search for the ave address on polygon and normally that will pop up on polygon scan which you can click on and it'll tell you this is the ave address and then we can copy the address here okay so what i want to do is say ave address equals and then just paste that in and then we'll make use of that first to look up some token info in our qs object that we have there's another object called fetcher and you can see inside of here what's a function with a couple of functions in it so we can either fetch token data or fetch pair data and so what we want to do here is say qs.fetcher dot fetch token data and what we're going to do first thing it takes is the network id which as i said we can just hard code if you wrote this out and you were testing it on different networks you might actually not use it hard coded because you could pull it from the actual system and do it dynamically but anyway we're hard coding it i'm gonna pass in the ave address and then we need to pass in our provider okay and so we run that and actually we need to go ahead and await this and set it to a variable so what we're going to do is say ave equals await and then you can see that now we have ave returning a token object which tells us how many decimals it has and then two things we already knew which were the chain id and the address and let's do the same thing for tell okay so it's we can actually probably just search here so we can get telcoin on polygon scan and copy that address and let's do the same deal tell address equals paste that in and then we can actually await the tail address um tell and uh sorry with all of my running back like that we'll just say tell equals and so now we have ave and tell and you can see that tail actually only has two decimals where ave has 18 decimals so that's interesting now what we want to do is actually get the pair info and to do that what we need to do is say a pair equals a weight and we're going to pull back the qs fetcher and we're going to use the other method which is fetch pair data and we can pass in ave tell and then our provider i don't believe the order on the tokens really matters and so now we have our pair info but to actually get the price we need to define a route so which direction are we going so which token are we trying to go to from the other and what we can do is say route equals new qs dot route and pass in the pair and it's important to put that in an array because you can actually pass in multiple pairs if if you have i'll explain that one later but you can pass in multiple pairs and let's start with tell and so now we have a route and once we have a route i believe that the route gets us the price and all that stuff actually probably acquiring the pair does initially but like if we look at this really quick we can say mid price dot two significant and then give it like six uh decimal points basically or six uh precision so whatever um what we wanna look at is basically we're saying that one tell is 0.0006 to so on and so forth ave so let's go check that really fast so let's come over to the quick swap interface let's pull up tell and let's say one tell to ave and so you can see here point zero zero zero zero six two three nine eight hours is a little different and it's because actually when we pull this back the price is changing all the time and it took us a couple seconds to actually type this out so if we were to sit here for very long this would actually change again most likely now if we flip this and we put a 1 up here we should see okay we have 16 000.5 now again um let's query our pair here and the pair is the only thing we're awaiting so i think that's where the price info probably comes back from but now let's switch this to ave and then route mid price to significant and we get 16.056.2 i don't know why that's not the same to be honest with you i feel like that should be the same maybe i'm getting out of date data on the screen potentially um so anyway we are getting back something that's approximately accurate this mid price function is essentially supposed to return the market price but i just kind of started looking at this uh recently so there may be more details to it and obviously if we build out a real application we'll dig way deeper into it but anyway i think that's pretty much it for this episode i just wanted to kind of walk through how you can start talking to these contracts and it seems like everything is working for the most part minus a few details here that we need to sort out but hopefully this was helpful and again if you want to follow the longer series when we do that hopefully pretty soon be sure to subscribe to the channel and if you like this video give it a thumbs up and i'll talk to you in the next episode
Info
Channel: Techmaker Studio
Views: 379
Rating: undefined out of 5
Keywords: trading bot, learn to code, defi on polygon, defi on matic, trading bots crypto, trading bots for beginners, trading bot tutorial, quickswap tutorial, quickswap crypto, quickswap matic, Quickswap
Id: 9QclpA_4txI
Channel Id: undefined
Length: 12min 45sec (765 seconds)
Published: Fri Oct 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.