New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

How to Use Any Web API From a Smart Contract - Ashar Shahid

Ethereum DenverSun, Oct 9, 2022, 12:00 AM

Transcript

okay hello everyone can you guys hear me okay so since we are a bit short on time i'll head straight into my presentation which is how to use any web api from a smart contract so as we all know uh apis are the foundation of web 2 infrastructure they help us build complex applications blockchains would like to utilize these apis in themselves but unfortunately that's not really possible since natively black blockchains don't support apis so we need an oracle that helps facilitate bringing off-chain data and that's where our protocol air node comes in so air node and the oracle integration specification are tools we've designed to help bring off chain data on chain nodes our first party oracle solution designed to work with the oracle integration specification i think all of us are familiar with the open api specification we believe there's a need for a standardized way for apis to integrate with blockchain and that's what ois hopes to accomplish the ois describes how apis interface with air node in a nutshell smart smart contracts query endnote requesting specific data the air anode in turn queries the api based on the ois and retails the result of course there are more nuances to this of how android works on the backend but you can read more about that in our docs so uh the basis or the backbone of uh air node is the request response protocol the request response protocol is basically how air nodes communicate with on-chain smart contracts you can think of the uh air node rrp contract as the central hub where all smart contracts request request their data and then the air nodes check the rrp contract for any pending request and then fulfills the data for those smart contracts so how are request defines are defined on chain using air node well each air node has its own on-chain identity in the form of an ethereum address and each air node serves a single api so you can think of api.example.com as a single api and within the api you have different endpoints so like slash latest price slash latest uh historical price etc so each endpoint gets its own id that we refer to it as the endpoint id and the query parameters you can also define them while querying within your smart contract one thing to keep in mind is that each smart contract needs a sponsor the sponsor is used to derive the sponsor wallet which is the wallet that android uses to make the fulfillment transaction since this is a request response protocol you will be making the request but the response has to be fulfilled and the air node won't do it for free you will also have to pay for the response transaction so for today's workshop we'll be calling the coin gecko air node the specific endpoint we'll be calling is the slash coins endpoint so the complete url is api.congeco.com slash api v3 coins and you'll be querying at the price of ethereum so uh i'll just no i'll just complete this so let's if you want you can follow along the contract isn't that huge uh we'll be writing our own requested contract uh what we'll start off with is by defining the rrp requester.

sol any uh contract that wishes to communicate with air nodes needs to import rpe requested or so once you do that you need to inherit it after you have inherited it you just need to define an incoming fulfillments uh mapping this basically says that the request has been fulfilled or not fulfilled and in this case we the data we want is the price of ethereum so we'll just define a variable to store it in it after that we'll define a constructor and call the parent constructor of rrp requester uh as the address we'll be specifying the air node rrp contract once that's done we just need to define two more functions the first function is the make request function this is the function that actually makes the api request we define the air node address which is the unique address of the coin gecko air node we define the endpoint id which is the slash coins endpoint we define the sponsor which is our own metamask wallet we'll be sponsoring this request and the sponsor wallet which is derived from our metamask wallet this is the wallet that the endnote will use to fulfill this request uh the parameter is ethereum in bytes 32 so we'll convert theory uh the word ethereum into bytes32 once that's done we just make the request uh by calling the node rp address and that will make the request then we'll have a fulfill function where the request will be fulfilled so once you make the request airnode will try to fulfill it and it will fulfill it within the fulfill function so all we are doing here is decoding the result and then assigning it to the price of it it's not really that complicated so why don't we try doing this in real time i have remix opened up here inside remix you can see that i have requester and rp requester set and i'll attempt to fulfill i'll attempt to make a request to do that first i'll have to deploy this contract so i will pass in the address of anode rrp to this contract so basically we are deploying this requester right now and once it's been deployed we can make the request and you can do this for any uh api it's not necessary that we you have to use coin gecko i deployed the coin gecko endnote prior to this talk but you can go ahead and deploy any api deploying an air node is very simple all you need is an aws account a free a free tier works so uh yeah you can have an uh a node running in a matter of minutes and you can query any api in a matter of minutes okay so our requester has been deployed uh now we just need to copy the requestor address head over to node rrp and sponsor it so we will be sponsoring it with our metamask the address of our metamask so we'll just paste in the address of our requester set the sponsorship status to true and confirm the transaction so now we have sponsored this requester contract just to make sure the price of it is zero right now so as soon as we make the request the price of it will be will get updated according to the coin gecko api uh okay so we have successfully sponsored our requester and now all we need to do is make the request so what we need to do first what we'll do is we'll put in the address of our air node so this is the air node that uh is running the coin gecko api uh i have deployed this prior to this talk but you guys can deploy your own endnote anytime you want and then i specify the endpoint id this is the slash coins endpoint so this is the endpoint id and the sponsor the sponsor is us so we can just put in our metamask address the sponsor wallet is the address derived from the sponsor address i have derived it prior to this talk but it's just a one-line script to derive the sponsor wallet you also need to fund the sponsor wallet so you have to keep that in mind and finally we specify the parameters so the parameters is nothing but the word ethereum converted into bytes32 and that's it and i paste that in i do a transaction now we have made an api request to the net to an error node to update the price of ethereum within our smart contract this might take a while two or three minutes so if anyone has any questions before that go ahead uh how would you go about automating like api calls like regularly what do you mean like make calls every five minutes or one hour um like do you want to get the price every five minutes anything like that okay so that's uh we have a product called beacons which does exactly that uh where it updates and fetches the price every five minutes and you can read from the beacon uh whenever you want right so yeah uh one thing to note is we are launching our first data feeds which are called beacons and beacons are data feeds that get updated every minute and uh yeah we've launched dev on this test net on test nets for e-denver anyway i think the price should have been updated by now but i'll just check yeah so as you can see the price of ethereum right now is two thousand seven hundred seventy nine uh the last i checked this was three thousand so not a good day but yeah that's it for my workshop uh if you guys want to learn more you can head over to our docs all of the steps i've documented in my uh repo that i've done right now but we also have bounties running for api three where you can build on top of beacons and the products that you build we can we'll pay out boundaries for it um thank you do you have any questions from the audience all right if not another warm round of applause for ashar please

Automatic transcript — names and jargon may be misspelled.