API3 🛠Pay royalties onchain with API3's toolset
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join us for an API3 workshop! From November 17-19 2023, ETHGlobal welcomed developers from around the globe to take part in shaping the future of Ethereum at ETHGlobal Istanbul. Over $600,000 USD in prizes were given away at the prestige Istanbul Congress Center... thanks for an amazing weekend hackers! ----------------- Follow us: https://x.com/ETHGlobal?s=20 Check us out: https://ethglobal.com/ Email us at: hello@ethglobal.com
Transcript
[Music] hello everybody Welcome to the API 3 Workshop real world data for web 3 Straight From the Source what does that even mean means we run first-party articles meaning we don't me we aren't the middleman okay so let's get into this today we're going to be doing a workshop about using our aode product and dais which is price feeds uh we're going to do a workshop on paying onchain royalties with any API of you're choosing who is this guy standing in front of you and the guy next to you I am Billy Jitsu everybody knows me as Billy I'm the developer advocate for API 3 go ahead uh I AAR uh I'm a protocol developer for API 3 so double the quality okay so what does API 3 provide well we have chain API which lets you bring your own API data on chain uh qrng that allows us to do random numbers on chain and the API price feeds basically uh price fees that are constantly fed on chain on your local chain I am in a 20- minute time so I'm going to go a little bit fast uh so today's workshops is going to be used two uh uh two services that we have the chain API plus a nodee and the dapi price feeds and so what is an air node uh in short the are smart contracts that we have deployed on every chain that you want to use that have the ability to do requests offchain and then communicate back to your smart contract so if I were to like just break it down uh let's say you have an API and you want to call it onchain uh an air node is a very lightweight application that you Deploy on a cloud provider like gcp or AWS uh which will basically act as an Adaptive layer between the API and the blockchain so again how does it work uh we have a contract that you have is your requestor contract on the very very left of you and then we have our air node that is employ that acts as a communication between the offchain and your smart smart contract the a node then does requests to our cloud provider and then talks to the API receives the data answers back and then throws it back to your smart contract in a short easy way Manner and so our other product is dapi which is price feeds um so we have onchain data feeds that we aggregate data with uh from multiple sources and we grabed all that data that is provable and bring it into a single price feed for you that is averaged out uh you can prove every price feed is coming straight from the source based on their data feed ID so you can prove that on the contract level so if you know uh our air nodes we took the concept of the air node which anyone can deploy and we went to very reputable uh price providers and told them hey can you run this air node for us and they run the air node we get the data from them and then we aggregate it on chain uh basically providing you with data that is straight from the source and that anyone can verify where the data came from because you can actually see the air node uh is hosted by Notre or dxfeed or you know whichever data provider exactly so what we're going to use today is again we're going to mix that chain API the off data information bringing it onchain and the price feeds from dapi and we're going to mix it to to make an onchain royalty payment system so we're going to go ahead and break down the API uh usually this is a little bit more in detail but we're going to show you how to use the chain API with a slideshow because we aren't Limited in time but let's go ahead and break down the API so for this example I'm going to be using YouTube I'm music producer I want this YouTuber to get a million views I have an agreement with this person to say hey if you get you know 10,000 views I will pay you a certain amount of royalties over time so what we're going to do is we go to the Google Docs and you can see that we have a getter which is the Google API YouTube which is our main main uh link and it has videos If you dig into the docs a little bit more you look exactly for what you want to see I highlighted statistics it's a little bit hard to see here and it gives me all the information that what I get in statistics and that's all in the Google Doc and that's going to be uh specific for your API that you want to deploy off chain if we look at that into Postman it basically breaks down what we want to see exactly what want you see here can you guys see that okay I know it's a little bright on here uh but it breaks it down into the pieces that we need we have the base we have the endpoint and all the parts that are required for it so you'll see that we have a part and then it goes to statistics and then we have an ID which is the unique YouTube ID and then it requires an API key that I need to get permission to read that dat and then you see on the bottom the response that I get and so what I'm looking for based on that ID I want to see the view count so the view count is what the data that I want to get on chain so in our chain API interface I'm going to go ahead and do my base URL which is is the Google API YouTube V3 and then for security measures I want to put in my API 3 my API key so I'm going to go ahead and put that API key as a query on the next part is a simple wizard as soon as it goes I'm going to have to do my endpoint I want videos from my endpoint and so in my path again if you look back you'll see that I have it on the part with Statistics and the ID so I don't want to hardcode it to always get that same video every time so I'm going to let the user Define what they want from that YouTube API so I'm going to let them so for the the part I'll say it could be statistics or any other part that they want from the Google Docs and then the ID any YouTube ID that they want to choose so it doesn't have to be my artist it can be any artist and then here specifically we have preserve types aara can you go a little bit deep that those preserves sure uh it's a little bit complicated but essentially what it is is that you want a specific response within the API you do not want the entire uh entire response that the API returns so you have the user Define the path within the API that you want so in case of the YouTube API if you if you'd go like uh two slids back we want the view count which is uh within this Json response it's within the items statistics uh view count field so we have the user defined the Json path and that's essentially what the path is within chain API and then there's uh two other parameters like type and times so the type of this uh response is going to be uh integer and the times is if let's say we want to multiply it because you know ethereum is commonly 18 decimal places and you have to multiply with 18 but in this case we don't really want to do that so we'll just leave it empty yeah for this example we're keeping it very simple if you were doing just a basic integer request online you can use that as the true number as it is so for this example we're keeping it simple uh for those that are using oath uh we have a pre-processing and post-processing so sometimes you have an API key that only lasts for an hour and you need to use your your authorization to do so you can do a pre-processing before the request to get an API key as well but for this one we're keeping it really really simple and just having the API key directly in there so we go ahead and we create that wizard Wiz it gives us a small file and we're going to go ahead and deploy that API using either AWS or Google cloud or any kind of service of your choice but the wizard helps you with AWS so we're going to do AWS and so when I deploy I just give it any name I want so I'm just calling it YouTube and then uh it's going to be on my version one because it's be my first deployment and this wizard takes you to the whole thing and so I'm choosing AWS and then it's going to ask me what kind of cloud providers I want and for this one I'm using uh Us East but you have multiple options to choose from and then it's going to ask you what chains you want to deploy on so we have multiple chains that we support whether a main net or test net for this one specific one I'm just using sepolia but you can run this on multiple test Nets if you want if you wanted to support multiple chains for our for our bounties we have a uh Sports API that's going to be available across multiple uh test Mets yeah so uh this is particularly useful for the bounties that are available in this hackathon uh because we support all the major l2s as well as uh some of the major l1s so if you're if you're planning on hacking on the l2s or the l1s uh you can mix and match the bounties by deploying here and we've kind of already done it for you so you can almost skip this part um and so authorizers so depending on how you want to deploy this to production for testnet uh you can have it restricted to only have an allow list of certain contracts or addresses that can do the request for this example I'm making it public to anybody so anybody can you know make the request so if you're doing it for testnet or MVP keep it public and then uh it just checks to make sure that we have all our settings are correctly you know our versions where we want to deploy it to and the networks we want to deploy with and then we go ahead and gives us a package that we download and so on the very very left you'll see the package contents it'll have a AWS EnV config Json readme and a secrets in a sense all you have to do is fill in the aw AWS where you have to do your access key ID and your secret access key which is on your AWS account and then on the secrets. EMV file it's going to ask for a wallet pneumonic uh there doesn't need to be any funds uh the way this system works is is somebody has to pay the gas when you send a request and get a response you pay for the request on your first transaction but then things have to happen a few blocks later and so somebody has to pay for the gas to get that response so the sponsor wallet will do that and this pneumonic generates a sponsor code for then you to fund a little bit later so it doesn't have to be a funded wallet it just has to be a random pneumonic you can make this with a generator or open up metam mask and just make a brand new wallet and then use those keys there's no funds required on that and then uh depending on the test that you supplied you have to make sure that you have a RPC address I just went to chain list.org and then just grabbed a ROM one if you have one that you want to be a little bit more solid you can get one from like Alchemy and fury or any of these other RPC for providers and then I'm also inputting my API key for YouTube uh this is where you keep it secret to deploy it so I just have my API key I already burned it guys so don't bother using it but just for the example I wanted to show that that's where you're going to input your API key so that way it stays on the air node and then you don't have to give that out publicly it's not front facing okay and so then once we deploy it it's going to go ahead the deployment uses Docker deploys it you'll get a success and then you'll get more files in your folder and this is the stuff you really really need in order to use at the Smart contract level you'll see that you have two additional files uh you'll have a receipt and log files and so in the receipt you're going to get your air note address that's specific to your chain that's what you're going to use to communicate with our air node smart contract uh it's also going to give you an air node extended public address that you'll be to generate your sponsor wallet and then in the configuration you'll also have your endpoint ID this is the endpoint ID that you'll use to communicate with the uh the API scheme or sorry the uh the link scheme and this is what you're going to need to to actually deploy with your smart contract anything to add on that yeah I'll just say that uh this all seems a little more complicated than it actually is uh once you run through the wizard The the only thing you actually need to do is just deploy the air node and uh and you're mostly done and then uh you have an onchain address for your API now uh anyone can call that onchain address and in effect call your API so uh you know we are just going through this pretty fast but uh I think overall the process is much simpler than it it looks for those hacking with the F1 Sports feeds uh basically you could skip this entire part because we've already did it for you and you have the air note address and the n.8 to work with already so this would would be the only need we need the smart contract addresses okay so I am not a big fan of PowerPoints I like breaking down code and talking about code that was the offchain data stuff so let's go ahead and build out what how we would actually build something here on the space so let me go ahead and open up my uh thing so I know the code looks a little bit scary because it's a lot to to uh to swallow but let me go ahead and Control Plus that but let's go ahead I'm sorry oh more more more more more boom co okay better hopefully okay and let me go ahead and minimize this okay so the proxy address uh for reading our a dapi price feeds is really really simple literally you can see like it's 23 lines of code including my spaces all it requires is One Import for I proxy doou library and I just have one public variable to set our proxy address which is basically the price feed we're going to choose uh our Constructor doesn't require anything uh and what we're doing here in this function is literally setting a proxy address we're telling our smart contract what price feed are we looking to look at so let's say we want to do ethusd or R BTC USD we get to choose whatever proxy address we want to do and then we literally just read the data feed and so it's a very very small lines of code uh what we get in return is we get the value which is the price of the asset at that specific time and the time time stamp of when it was last updated so we have a certain threshold of 24 hours or a change in deviation of price if we go over to our Marketplace you can see that we have uh this one specific one on mayet we have a managed dapi which is a option of 0.25% deviation 0.
5% deviation and a 1% deviation meaning if eth is ,000 and it changed price to 990 that would be a 1% change of deviation on chain and then it would automatically update and so for smaller deviations and change in price it would update more frequently or within 24 hours depending on the Range and you can see that we have current sources you can actually look at yourself if you look at the details of what we're getting on chain we're getting multiple sources from that specific price feed on that specific chain and we're aggregating all that data down to get you your specific price and so that is how you would get the market feed on there and then the second part of this is a request response let me zoom in a little bit more here this one looks a little bit more complicated but let me break it down very simple uh our request response is basically if you get a request transaction it's going to call out to the air node and then it's going to response so we have this make request and we have this fulfillment what do we do when this number comes back uh our request parameters that we have here is the three functions is basically telling us what chain are you using so what air note should we port to what endpoint are you using so where do we point to and then who's going to pay for the gas and that's pretty much our setup and so the make request is a pretty standard a um boilerplate code you don't really have to change too much around it unless you have something very very specific out of the box that you're trying to do uh and all the magic happens on the Fulfillment uh so when we get that number in return or that that answer of what we were requesting in return uh once we get that answer what do we do with it what is the Smart contract logic that we're going to do and I'm literally saying do what you want in this middle of the array here so then you can go ahead and see that so let's look at this royalty code and we'll break it down piece by piece so this is the music label uh royalties onchain system so let me just break it down really quick as like we said before it's in multiple section so we're combining the two libraries request response and the dpis so what I have here is my set proxy address right here hidden I want to say what am I going to what asset am I looking to pay with so I'm going to do ethusd um and my set request parameters is basically saying what chain I'm going to do this on what endpoint I'm going to do this on which is my Google endpoint API and then who's going to pay for the gas which we're going to generate in just a moment and then we're going to make the request the only thing that I've changed here is who am I requesting to so I am doing a request for this one specific artist that I have so I'm going to register that artist with a specific wallet so when I do that ID and make sure that they hit enough hits I'm going to go ahead and make sure that it's that artist if I have multiple artists I'll have multiple IDs so I just have an input to check for that ID and then the Fulfillment array is once we get that view count from YouTube I'm going to say well now that we have the YouTube what are we going to do with it right so the return response is going to give me a number of how many views it hits and then I'm going to call a function which is this function right here which is called called payout I'm going to try to highlight it just a bit oh sure of course better little more little more more okay perfect so this payout is just a function that I made to have the logic to be able to check to see if it hits the requirements if we hit any agreements if they had 10 views 100 views a th000 views make sure it's going to check those balances to say all right you've hit the requirements let's go ahead and pay you out and then now we're going to go into the data feed right so we go bring in this dapi and you'll see that we have our price feed right so we're just basically reading our our proxy data which is going to be eusd we're going to see the value we're going to check the prime stamp and we're going to say okay this value is legit let's go ahead and make sure that this value pays out on that value and then here is my payout function which is more logic than anything is basically saying okay has this person hit the Milestones if the Milestones have been met let's go ahead and make sure the views haven't been paid out already I want to pay you out for 10 views 100 views 1,000 views I want to make sure I'm not paying you twice so just some checks and balances here I'm actually checking the price of the of the data feed so I'm going to check to make sure eth is this price so I can make sure that I pay you $100 $1,000 in eth eth changes dynamically so we want to know at that moment what the price of eth is and then I'm doing some conversion I'm changing it to dollar value and then I'm making sure that my smart contract has enough funds to pay the the royalty out and then I will then pay that specific artist their Bounty very very simple uh breakdown on here and so the way we get this all set up is you know I I have all my variables on here which should look familiar we have our airn note sponsor wallet and npoint ID that we need to set up parameters our proxy address and then I have a global variable for the return response so you can see simply what the return response that I got and then I've created another artist ID variable to make sure that I can have multiple artists on here so if I s of Wars their ID 1 2 3 4 to whatever they want and then I just have a simple mapping to make sure that I have fulfillment requests because if we do more than one request we have to keep track of how many requests are being are being sent and how many requests are being returned to make sure that we're not getting any faulty data and then I'm also keeping track of how many Milestones have been paid per artist on there and then this is a quick simple of what the Milestones are you know I'm picking up any random on so I want to make sure like some YouTube video got 10 views 100 views or a th000 views it could be a million views whatever is you know discussed beforehand and so I went ahead you know the internet is a little funky sometimes at hackathons it wouldn't be a real hackathon if the internet worked right out of the box so I went ahead and deployed it okay and so you can see that when I have the deployment I have to deploy my air node so we can go to our providers and we want to make sure that for this case I'm doing sapoia so I am grabbing excuse me let me go ahead and zoom in I am grabbing our a note that we have specifically set for our for our air node at seoan Network and you can see all our test Nets have all Air nodes deployed on every chain that are available and so I've already gonee and deploy that and you can see that I have my music label already set and so I am setting my proxy address because I want to make sure that my smart contract reads the ethusd price feed so I'm going to go ahead to the ap3 market I am going to the girly test net and I'm sorry spolia test net I think one thing to keep in mind is that the reason we are using the eusd feed in this uh contract is that we want the user to be able to pay an eth and uh but we charge them a set amount in US Dollars and that's why the data feed is necessary but uh you could do this with any of the assets that are listed on the market so let's say you want the user to pay in a specific currency maybe not eat maybe some other currency uh you could also do something of similar um the one like I've seen a similar concept with someone building here in the hackathon that they are building like a Fiverr onchain uh they wanted to charge people in in a in USD and what I recommended to them is do something similar as what we are doing here is that you just use the data seed to uh calculate how much e you would need to pay for an equivalent amount of USD right so don't confuse the data feied ID with the proxy address the data feed ID is up on the top that is a unique ID for that price feed the proxy contract address for your smart contract to read is on the very bottom right here which is the proxy contract address so you would copy that out and then go in and then set your proxy address so once we have our parameters once we have our sponsor wallet everything set up now we can request to say okay we want to get those IDs if you follow the repo we have this encoding structure you can encode it on the smart contract level if you wanted but that would required to be hardcoded so we have this JavaScript function on the repo where you can then set your parameters and say all right I want to get this specific YouTube ID which is this ID that we have for this one YouTube video and then I want statistics and then basically breaking it down on that original Json response I want to get the items statistics and I want to go specifically to the view count so you can modify this API to whatever way you want is converts it into the bite code and so you'll see this big long script of bite code here we would grab that information pop it out into our code and then we would make our request and then within our request we would say okay let's go ahead and make a request for here so we want to see what the statistics counts are for that specific YouTube ID I've already registered the artist because we have limited time and that artist is 12 I would make the transaction it would resp do the transaction request and then based on the sponsor wallet depending on the time we have it would then fulfill our request a few blocks later because we have a request received so we would send out our transaction it would grab the API a few blocks later depending on the Chain you use on it was send a fulfillment request and then we would get it back can we do it in real time the request we can't we got like one minute left yeah so that's why I redeployed it so if we look right now I'm going to look at my request my return response is I'm going to try to zoom in as much as we [Music] can 792 353 and on that YouTube ID that I used for this one random one right here it's 792k views is it average it down so we get the exact view count that we get from the API and then it'll automat a Ally play in on the smart contract level and then once it goes it'll distribute all the eth available to it so I I put this thing with like point one eth on the smart contract and it paid it out to the the artists that are available and paid out directly I know that was a lot to swallow I had 20 minutes to speak that in so we are in the booth we can help you deploy your AER node we can help you set up your request response your dpis and if you're doing random numbers uh we can definitely help you get that set up as well just to really quick before we go talk about our bounties let me go ahead and slideshow this really really quick it's going to take me to the very beginning there we go and so we have the best use of dapi price feeds which is going to be $4,000 the best use of qng which is our random number generators uh we've seen plenty of unop forks we've seen plenty of lotteries so spice it up a little bit you saw the creative usage that I was using for the price feeds to just check the price to make sure we pay out an e in dollar value and then we have three API Runners up $1,000 each to see the best creative idea using any of their technology you have any questions I'll be at the booth all weekend long uh it's going to be a grind guys thank you guys thank [Applause] you
Automatic transcript — names and jargon may be misspelled.