Chainlink: Oracles, Decentralized Data, and External Computation with Harry Papacharissiou
ETH Warsaw·Thu, Oct 7, 2021, 12:00 AM
Chainlink: https://gitcoin.co/issue/smartcontractkit/chainlink/4375/100025683
Transcript
hey guys we're back um i just received a message from youtube that they did remove our youtube channel uh so we're currently looking for a new hosting and we're gonna give you some updates on our discord and twitter once we set that up but right now i'm with uh harry potter charissio developer advocate for training clubs hey hi i'm tech how's it going i'm good how are you very good thank you very good awesome so khara will give you an intro to chain link and yeah the stage is yours awesome thanks thanks zantec so yeah very excited to be part of the xerox hackathon really can't wait to to see what everyone builds my name is hari papa karissie and i'm a developer advocate for chaining labs and today we're going to be talking about oracle's decentralized data and external computation so i'm just going to share my screen if you just bear with me okay hopefully you guys can see that so basically today blockchain and smart contract adoption and innovation has really skyrocketed the past year and we're seeing some really great things being built by all these people and we're seeing so many amazing opportunities to ahead of us as we continue to push for this technology to kind of take us into a new age of what we like to say uh economic fairness transparency and efficiency right and and these hackathons are really the the front lines of the battlefield is basically where where the real innovation happens and some of these great projects are being born so with that being said let's jump in so today we're going to talk about smart contracts we'll just give a brief recap of what smart contracts are we'll talk about the oracle problem then we'll talk about chain link price feeds and also d5 we'll talk about chain link vrf and then we'll go through some use cases for your hackathon submissions and then we'll talk about where you can go as a developer to learn about chain link and then we'll go through a couple example uh projects as well so first of all smart contracts right so what is a smart contract a smart contract is essentially a highly secure tamper-proof digital agreement that runs on decentralized infrastructure such as a blockchain right so compared to a traditional digital agreement right in a traditional digital agreement one of the parties in the digital agreement is also the party that actually executes and enforces the digital agreement right so it's almost a conflict of interest because you've got one of these parties that's kind of incentivized to act in bad faith uh executing and enforcing the agreement and then you've got these other one or more parties in the agreement as well that kind of have to trust that the other guy is going to act in good faith right so that's kind of uh one of the main differences between the two so a smart contract runs on this decentralized infrastructure such as a blockchain that's separate to the parties that are within these digital agreements right and in in in terms of transparency in a traditional digital agreement uh the party that's executing and enforcing the contract can choose how transparent or um they wish to be basically they can be very transparent or not transparent at all and sometimes this can create issues such as we've seen in the past so you might say well okay so why smart smart contracts better than these traditional digital agreements then and it basically boils down to five things and the first thing is security or what i like to call tamper proofness right so because these smart contracts are running on this highly secure decentralized infrastructure they can't be hacked they can't be gained no one can influence the execution of the agreement because it's running on this decentralized infrastructure right whether it's dozens or hundreds or thousands of nodes one party in a digital agreement can't say i'm going to change the outcome of this contract right they can't do it right it's it's highly secure two reliable guaranteed execution right because once again these smart contracts are running on this decentralized blockchain infrastructure you've got guaranteed execution right the smart contract is executed on all of the nodes and they're all in perfect sync with each other and they're all going to the guaranteed they're going to execute the agreement as per the way it was written right there's no oh this web server was down or this payment system was down we couldn't actually pay you out your insurance agreement for some reason you know there's there's none of that blockchain 24 7 365 it's constantly running processing transactions and executing agreements right so you've got guaranteed execution three transparency right so in traditional digital agreements like i said the party executing and enforcing the agreement can choose how transparent they want to be and sometimes they're not very transparent at all but in a smart contract scenario transparency of the agreement right is actually unavoidably built in to the contract and infrastructure itself right so there's much higher level of transparency between parties within a digital agreement four trust minimization right so we've got reduced counterparty risk because all the parties that can't kind of take part in a digital agreement like i said they don't have control over the execution of the agreement so they don't necessarily need to trust each other anymore right because they're trusting in the smart contracts ability to execute and do what it's going to do which always will because nothing short of maths or physics not working like it does today is going to stop that from happening in this scenario five efficiency so all of these traits that i've talked about uh already security guaranteed execution transparency trust minimization you know they lead to basically provide an opportunity where we can kind of migrate some of these existing manual and cumbersome processes to more automated highly efficient ones that basically smart contracts can help facilitate and this leads to better efficiency and reduce costs as well in many cases so just to reiterate in in a traditional digital agreement example it's always going to favor the the side of whoever is executing and enforcing the agreement so in in the case of such as a crop insurance agreement between a farmer and an insurer the the insurer will say you pay me a thousand dollars a month and i'll insure your farm and if you have drought conditions you know you'll get paid and the pharmacy is okay and they pay the thousand dollars every month then there's some big drought and a whole bunch of farmers putting the claim and the insurer says well actually i should pay you out but i'm not going to because we have to pay all these people out and we've got these uh vague terms and conditions in in kind of legal jargon in our agreements and i can actually get out of it and then the farmer has to you know take them to court if they want to basically get paid so they're they're trusting in the brand uh in in a traditional digital agreement you're trusting in in what's called paper guarantees you're saying trust my insurance company trust my bank the trust meyer you know whatever right it's it's just a brand based guarantee where you've got high counterparty risk and usually you've got low transparency right whereas with a smart contract scenario we move to what's called a math-based guarantee right where we've got low counterparty risk and transparency is unavoidably built in right so that's kind of the the monumental shift from these brand-based digital agreements to math-based digital agreements right where you know that they're going to execute you know that the other entity or entities within the agreements don't have control and they can't do the scenario that i just described where they're like oh actually i don't want to pay you out like it's not possible so now that we know why smart contracts are so awesome compared to these traditional digital agreements right let's talk about their biggest limitation now smart contracts are not able to connect to external data or any off chain resource on their own so you might ask well why can't a smart contract just make an api call to a web api or somewhere and the simple answer is that the blockchain is deterministic and everything needs to be able to be validated by all the nodes on the chain right so if making an api call is part of that validation then nodes can get out of sync and you know maybe the api gets hacked or something and you kind of lose your determinism each node needs to get the same result back so that i can continue executing a smart contract in perfect sync with each other otherwise you don't have determinism so this is defined as the smart contract connectivity problem right not being able to connect to external data systems and events and this actually limits the use cases for smart contracts purely to things that can only be done on chains such as moving tokens around or doing payments in the native asset of the blockchain you know based on smart contract logic right which is great but actually the majority of digital agreements need some kind of external data or input or external trigger basically so the next question you might ask is well how do we solve this problem so to interact with the outside world we need what's called an oracle right and an oracle is any device that interacts with the off-chain world to provide data or connectivity to smart contracts right so they they report the result of that data or off-chain computation on chain so that the blockchain can stay deterministic so just to give an example a smart contract might say i need to get the price of oil uh and an oracle will say okay i'll get you the price of oil and then it returns the result and that result is then the input back to the smart contract which goes to all the nodes in the blockchain and all the nodes go yep this is the input now let's all continue and they're in perfect sync with each other and we kind of keep that determinism now here's things we get here's where things get really interesting right using a single centralized oracle actually creates a single point of failure for smart contracts and compromises the overall security of the digital agreement in this scenario right because if you've got this highly secured decentralized blockchain running on you know this highly decentralized infrastructure but then you're triggering your smart contract from this single oracle right then your end-to-end security is only as good as the the weakest part of that system and in this case it's your your oracle right so all of those traits that i talked about before such as trust me organization tamper-proofness etc it all goes out the window once in this scenario because you've got a single oracle triggering your smart contract right so we've gone from we've got this awesome highly secure system that no one can hack and it's awesome but we can't do much with it because it's limited to unchained you know functions to we've got this awesome highly secure system that can do all these cool things now because we can connect it to external data and inputs and computation but it's actually not that secure because we're triggering it in an insecure way by using a single article right so the next question one might ask is you know how do we solve this problem now the answer to all of this is a decentralized oracle network right so chain link is designed to be a modular decentralized oracle network for getting securable and reliable data on chain and for triggering external computation so we want to bring the data or trigger external computation in a decentralized way right and by doing this we solve this this oracle problem by extending the decentralization and trust minimization that we have in the smart contract layer we take it we extend it out to the data delivery and data source layer as well right so we've got a better overall decentralization across the whole system and you can do this with as many nodes oracles or as few as you want it's entirely flexible now this flexibility and heterogeneous design is one of chaining's strongest features right being able to take any input from the real world adder on chain as well as any output from the blockchain and sending it back to the real world but to do it in a decentralized way right and this end-to-end connectivity reliability and security is actually the real value proposition of smart contracts right being able to perform digital agreements that we do today but to do them in a better more secure more tamper-proof automated and highly efficient way so to try wrap your head around this visually here's a simple example of how a chain link decentralized oracle network can provide price data to a smart contract in a in a secure tamper-proof and decentralized manner so as you can see here we've got a number of chain link oracles here fetching price data from various price points and they're aggregating the data and coming to a consensus on a final result there and then that result is fed on chain and just just to reiterate the none of the nodes in this example here and none of the data providers either right but none of them can influence the final result that's fed on chain because we're kind of doing a decentralized way here as you can see so that final figure cannot be influenced by a single uh entity uh in in this kind of data delivery or data source layer here now d5 is currently one of the biggest and most popular use cases for using a decentralized oracle network such as chain ink right so if you're thinking of doing something in the d5 space to get you started we've gone ahead and found some of the best blockchain note operators in the business and we've combined their skills and experience to call to build what we call on chain reference contracts right so these are decentralized on-chain reference points that these operator node operators kind of contribute to with the goal of providing these single aggregated final price results that take metrics into account such as you know volume and things like that so and these reference contracts can be considered the source of truth for various currency pairs you know like the price of eth in usd for example so we've got an example here where we've got you know dozens of exchanges a few data providers there a number of chaining nodes which is sourcing price data from the data providers and the exchanges and then they're kind of coming into a consensus on an aggregated result and then that's fed into the reference contract and that reference contract says yes this is the price of you know eth usd at this very moment across the whole market and then the on-chain user contracts can then access the reference contracts and use that price data at will now you may ask why is having such a highly secure and tamper-proof data delivery layer so important right and great question one of the the answers basically is defy attacks right so we've already seen attacks that have kind of lost projects literally millions of dollars right due to using centralized oracles or not having sufficient market coverage in their oracles and you know people end up losing money so as a community we really need to make a conscious effort to try and avoid this from happening in the future because you know in the end people lose money uh when this happens right and if if crypto's taught us anything it's that if you've got a protocol that's open to being exploited and and someone can profit from exploiting it then the protocol is going to be exploited eventually right now the best place to go to kind of check out the price feeds or data feeds is to go to data.chain.link so as you can see here we've got a whole bunch of currency pairs here i'll open up the efusd one and you can kind of see each node in the network independently gets data from some of the highest quality data apis and reports the answer on chain and you can see that the answers are all there everything's transparent as well and you know we've got uh most of the most popular crypto or fiat currency pairs are supported and we're constantly adding new ones as well right and the best part about it is if you don't want to use these reference contracts if you want to use your own nodes your own data providers etc you don't have you can create your own oracle network as well it's completely flexible so going back to d fire like i said before it's one of the most popular use cases of blockchain technology at the moment and it's always innovating and it's got a ton of momentum and knowledgeable people working in the field right and chaining currently supports some of the top projects in d5 already and i think at the moment when i checked this morning there was something like 75 billion dollars in in assets locked up in decentralized finance right now which is absolutely mind-blowing right and i think as time goes on d5 is going to continue to get more and more popular as well so here's just a couple examples of some top d5 projects currently using chaining price feeds so we've got ave which does uh lending and borrowing with a you know casual 11 billion dollars locked up there synthetics couple billion they do synthetic assets and then we've got sushi swap and bangkor which are dexas and they've kind of got quite a bit locked up as well so i think just between the four of those it's something like 20 billion dollars in in tbr locked up right so that's 20 billion dollars of crypto locked up in these protocols and secured by chaining price feeds so that users can access d file and use d5 on those platforms so it just goes to show how how widely accepted and used these price feeds are in d5 today so we'll go through an example in a couple of minutes but if you head over to the docs.chain.link site which is at docs you'll get to see how to implement how to use these price feeds in your hackathon submission and it's really easy guys it's literally a few lines of code if you want to use these reference contracts we'll show you in a couple of minutes but chain link is more than just price feeds right so chaining provides the ability to do off-chain computation as well now one of the more pressing needs for off chain computation was to implement a verifiably random number generator right so random numbers have actually been used for thousands of years whether it's flipping a coin or rolling a dice the goal is always to leave the end result up to random chance right and random numbers random number generators in a computer are similar right they're an attempt to achieve an unpredictable random result but until now getting random numbers on chain has been an issue right because the the previous conventional way was to rely on on-chain things such as a on-chain hash or using a centralized api that can kind of be hacked and or something like that like i explained earlier and we've we see exploits happen in in this as well right i think literally last week there was a a random number generator nfp exploit where someone was able to min uh an ultra rare nft right because the source of the randomness was being done on chain in a predictable way so chaining vrf brings a solution to generate provably random numbers this means that the on chain contracts that request a random number can actually verify that the random number was actually generated uh in a in a verifiable way using the seed that was given right so the seed is basically a source of input for a pseudo random number generator so in in this example flow here you might have a smart contract that says hey i need a i need some randomness and a changing oracle goes okay i'll get you a random number the chaining oracle sends the result back and uh when it goes back to the smart contract smart contract knows it was verifiably random created with the seed that the smart contract gave and that it was created by that exact uh chaining oracle right and not some you know a hacker or other system or anything now there's a number of uh interesting use cases already using this in production today one popular use case that i like is minting nfts with provably random traits i think that's pretty cool and as the nft space grows bigger and bigger i expect to see a big update uptake in vrf especially for things like random allocation of you know packs or sets because uh anything that deals with collectibles whether it's baseball card packs or digital nft drops or you know anything like that they'll deal with some kind of random allocation for these collectibles right you users want randomness when they get these things [Music] so just to just wanted to reiterate quickly that chain link can be used to integrate to any api right whether it's a web api an enterprise back-end database an iot sensor api enabled physical device such as a car literally anything you name it right and in addition to that it's actually it's blockchain agnostic so um we're live on ethereum polygon finance smart chain extire and there's more to come as well so um if you want to access off chain data or computation um via non-kind of traditional methods such as you know an iot sensor or a database or something you can use what's what we call external adapters and external initiators right so these allow you to connect to basically any api no matter how unusual it may be and basically use that source of data or computation in your smart contract right and we'll get to those in a little bit so let's go through some use cases now so if you're looking for some cool ideas to to implement for your hackathon submission i highly recommend you check out this awesome blog post done by our content content team it's called 77 smart contract use cases enabled by chain link and you can find it on our blog at blog.
chain.link and there's some awesome ideas here for things to implement if you're kind of stuck for ideas and they span various categories such as d fire gaming and fts insurance supply chain etc there's plenty of stuff there if you if you want some inspiration i definitely recommend checking that one out now next where is developer can you go to learn where can you learn how to use chain link to come up with your awesome hackathon submission so the first place you should definitely go is to add docs this is at docs.chain.link it's it really is the go-to place for info on how to use chain link and we've got code examples we've got setup help we've got a really great hackathon resources page as well i think here in the developer reference there we go so there's some some cool links there that you should check out so definitely definitely have a look there we'll get back to the docs in a minute next highly recommend you check out our blog at blog.chain.
link we've got a special developers tab with some really great content specifically for developers and it's all kind of geared towards specific use cases or specific tools or features and the best thing about it is it's all kind of done in a step by step follow the bouncing ball kind of way where you can kind of follow along really easily so for example how to get a random number on polygon if you go in here you can see it's got all the setup steps you need it's got code you can kind of go through step by step deployment and you can kind of achieve what you're trying to achieve really easily so definitely recommend checking the the blog out there's some cool stuff on there changing youtube definitely check this out as well so we've got um an official youtube channel we've got a whole bunch of playlists we've got a specific engineering tutorial playlist too which i highly recommend you check out if you search for chain link engineering tutorials and there's a whole bunch of really great stuff that can help you in your hackathon journey here as well such as you know building and using an external adapter or dynamic nft creation right so there's plenty of stuff here i highly recommend you you check it out as well now the solidity and smart contract starter kits these things are awesome so if you go to our repository on github you'll see these three starter kits pinned to the top of the repository so what these are are basically working repositories based on evm development environments such as truffle brownie or hard hat and they all contain complete and working smart contract examples that basically allow you to use all the main chain link tools and features such as vrf any api or price feeds right and in addition to the contracts there's proper deployment scripts and proper tests and and proper configs to kind of package everything together in a really cool and easy to use way so that when you want to start building smart contracts that use chain link you can just kind of use the starter kits and you've got everything you need to kind of get going right and then you can just take the contracts modify them do whatever you want and it's all ready to go and it's easy so highly recommend you check those out if you're a javascript person recommend checking out the truffle or the hard hat starter kit if python's more of your thing then definitely check out the brownie one uh it's really cool if if you're not sure if you're not experienced with with any of them then i probably recommend to start off with the hard hat starter kit and see how you go so just wanted to also say chain link is an open source project and we're constantly improving and we'd love to get your input or feedback on anything right so the changing community is one of the friendliest and most helpful communities out there and just if you have any questions jump into the chaining discord or the xerox discord right we've got a wealth of resources and and we're happy to help and if this is your first hackathon and you're just getting started then awesome so glad you're here hackathons are a great place to learn great place to grow great place to kind of get your name out there as well so just contribute and definitely submit what you create even if you think it's not polished and uh we just want to see you basically contribute right and uh if you're a veteran then you know you know what to do you've been here before and you know how much fun you're going to have right so i think if we go to the bound here on our github we've got five prizes of one thousand dollars in link here and you can see there's some judging criteria so if you're not sure you can kind of check out check out the the prize information over here so i think now we'll go through a couple example demos to kind of give you guys an idea of kind of how to use some of these chaining tools and features so what i'm going to do is i'm going to go to the docs like i said and if you're completely new to doing this highly recommend you check out the uh tutorials here we've got a beginner one an intermediate one and an advanced one so you can kind of go through them in in that order if you want but to start off with i think uh what we'll do is we'll start talking about price feeds actually um so we've got a section on price feeds right here as you can see there's some info and what we're going to do is we're going to go through an example of how to get the the latest price from a reference contract and to kind of get into your smart contract so if you literally go to the get the latest price section here we've got a code example and it's deploy this contract using remix button that you can just press right and it opens up the the remix ide might just make it a bit bigger hopefully you guys can see that okay so let's go through the the price consumer contract now so we're importing uh the aggregator v3 interface library here and we're kind of creating this new price feed object based off of it and when you create your your smart contract um all we're literally doing is we're instantiating this and passing in uh we're passing in the reference contract address right so if you go back to the docs over here uh there's a contract addresses section that you can click on if you go there depending on what chain you want to use price feeds on you can kind of you know pick whichever one you want in our case we're going to do an ethereum on the coven network so let's say you want to access the eqsd price feed then you just kind of copy this reference contract address and you kind of throw it into here right so we're saying we want to access the ethersd price feed reference contract once that's done literally all you need to do is kind of call the latest round data function on your object it's it's that easy right and in our case we're just gonna um see what the current price of ethersd is right so here's the the main net version here but what we're going to do is we're going to compile it then we're going to deploy it to coven just quickly if you if you haven't got any test eth or link which which you need for for doing this kind of thing uh we're not linked but you'll need e for the price feeds one there's some good instructions in the developer reference section here so you can kind of check that out if you need um yeah acquire test net link and install configure and fund meta mask so you can check that out if you're not sure what to do but once you get to a point where you have a deployed price feed contract here you literally just need to call your get latest price function and there you go we've got the price of beef and you can use it in your hackathon submission use it in your default application whatever you want to use it for so really easy to do literally a few lines of code you get all the benefits that the chaining price feeds offer they're already used by all the top d5 projects so it's really a no-brainer if you need price data to just check out these price feeds so if we go back to the docs now maybe let's take a look at the next one which is vrf right so let's say you want to build a submission where you kind of need randomness whether it's for an nft or for something and you know you want to use vrf so you come to the docs the get a random number page here has a really great simple code example that we're going to follow along with so if i go you could just click on the link there but i've already got one open here so i'll just use that one so if we go back to here so this is the random number consumer contract right in this case where we're importing another chaining library called the vrf consumer base contract and we're setting some variables here which i'll go through in a minute and in in the constructor where we're basically just um instantiating the vrf consumer base object here and it really just takes two parameters right it takes the the link token for the network that you're in and the vrf coordinator contract address for the environment that you're in right so the link token is just the address of the link token for the network um the vrf coordinator is the there's a smart contract that sits between other contracts and chaining oracles that kind of acts as a intermediary um for all random number requests basically and if you're not sure what to put if you go to the contract addresses section of using randomness it's got all the information you need there so if you're working on polygon you've got the information here if you're working on binance or if you're working on ethereum it's kind of all here now one other thing you need to put in is the the fee in link as well right so when you whenever you're doing computation in ethereum uh you where you're kind of changing the state of the chain you kind of need to pay gas in the form of eth right so it's similar when working retaining whenever you have a request for external data or computation you also pay gas right but in the form of link to the node operators right so um in this example a vrf request costs 0.1 link so we need to specify that here and once that's done then we're kind of ready to request our random number right so we have a get random number function here and it takes a seed number as an input right so we need to kind of provide that to the node to use to create our verifiable random number and it's literally just one line you call request randomness and you pass in the the key hash the fee and the user provided seed right and uh sorry i forgot to go over the key hash actually so the key hash is taken from the uh contract addresses page as well here and what the key hash is is that's the public key of the chain link node that's going to fulfill the request for randomness right so the node's saying this is my key hash whenever you want to send me a request to get your random number use this send it to me i'll send you proof back that kind of proves that the random number was generated by me and not by someone else right so it's that's very important as well so once you kind of pass those things in uh the the smart contract will basically reach out to the vrf coordinator contract up here and that vrf coordinator will then pass the request to the changing oracle right so um and and when the chaining oracle fulfills the random number request the vrf coordinator will verify that it was created uh in a verifiably random way with the given seed that was kind of uh given by the smart contract and then the random number result will come back and this fulfill randomness function will get called and we'll store the random number result in this variable here right so that's kind of the the end of the line for this example so i'm just going to compile this and deploy it to coven we'll give that a minute okay so we've got a deployed contract here so we've got a get random number um function here which we're going to call so let's say i put in a seed and i call it you'll see that we actually get an error right this error like i said before it's because we the contract doesn't have any link in it and we need 0.1 link as a minimum to send as payment for the oracle to basically fulfill our vrf request so what we need to do is we need to copy the contract address and we we need to go to meta mask and we need to actually send some link to the contract so in my case i'll send one just because we've got um so that we can do 10 requests if we want so what we're going to do is we're going to wait for that to go through and once that transaction goes through we're going to make the request again and hopefully this time we'll get a successful result okay so now we can successfully make a request for randomness now that the contract has been funded for link so that transaction has now been sent so the vrf coordinator has basically gone okay you want to request a random number with this seed buy this oracle here with this key hash and then the coordinator will send it to the oracle the chaining oracle will then give the result back along uh to the vrf coordinator contract and the coordinator contract will verify that it was created uh in a verifiably random way using the seed that it was given and it will also verify that the random number was created by that oracle right with this public key hash and if everything looks good then we should get a random result back here and and there you can see we've got a random number here so we have a verifiably random number uh in our smart contract and you can do whatever you want with it you can chop it up into smaller numbers you can you know use it to do whatever you want basically so that is chain link vrf uh in terms of just a basic example so the next one that we'll go through is uh an api request right so let's say you need to reach out to find some kind of um you know market data or you know anything so in this example we've got a public api here where we can get kind of crypto price data but in the in this case we're interested in you know volume data actually not not the price data so you've got an api that you can reach here and we want to create a smart contract that kind of consumes this data and uses it on chain in some kind of way right so we're going to open the make a get request code example here so i've got it over here and we're going to kind of see see how we can kind of do this now so once again we import a chaining library in this case it's the chaining client library and in our constructor here we don't need to specify the uh link address the the address of the link token because with this library you can you can kind of use this function it will kind of do it for you which is cool um we need to specify a fee again in link so once again it's 0.1 link now in in this case we've got these two things called oracle and and job id right and an oracle is basically an oracle contract it's a smart contract that um that it basically sits between uh smart contracts requesting data and then nodes that fulfill these requests right so um we need to specify one here and we also need to specify the the job id so each node has a number of jobs uh set up that it can basically do do tasks and fulfill these requests with so uh we kind of need to specify a job id as well so um in terms of what to put here you can go to for example the market.link website here and you can kind of explore all types of jobs so i think we're on the coven yep so if you want to do a in this case it's a http get and and now our result is going to be a number right then we want a get and uint256 type job right so we go here and we can see okay this one does a http get it passes a result to something that we specify um we can multiply the result and then it converts it to a number and and sends it on chain right which is kind of what we want so you can then copy this node job id and if you go to the job spec in the address here you can get the oracle contract address as well and you can then plug those two numbers into the job id and the oracle field here right and then you can kind of use this job here as part of your request now just take note that if you don't get a response you might pay to reach out to the node operator [Music] via their website or discord or whatever because some of them do have a white list for for certain jobs uh having said that i'm not sure if the white list is in place in in test nets all the time but definitely just keep that in mind if you don't get a response right so in our case we're going to do a similar job basically where we're going to do a http get and we're going to have a number returned and when it comes to building up the requests right all you need to do is it's literally once again a few lines of code so you create a new request here you say when the request comes back to our smart contract we want to call this fulfill function which is down here and the request that we want to do is a get on this url here which is the one that we just saw here and as part of our request we want to traverse the returned json path and go down to this volume 24 hour element here right as you can see so we specify that and because solidity can't kind of do decimals and and things like that we want to multiply the the result um so that we can kind of avoid any issues if we need to do any arithmetic or anything like that and once that's done we we call the send changing request to function passing in the request the oracle and the fee right and that will send the request to the oracle contract that we specified up here the and the specified chaining node that contains that job id will basically listen out and go hey there's a job that's for me and it will kind of take it it will fulfill the request and it will send the result back and eventually it will come back to our fulfill function and the final result will get stored in the volume parameter here right so let's give this one a go now so i've compiled that one i'm going to now deploy it and once it's deployed remember because we're doing a an external data request we need to fund the contract with link once again so i'm going to send one link to the contract and right now we can see that the volume um this volume variable here is zero because uh we haven't done anything with our request yet but once that transaction has gone through i'm going to call the request volume data function which is this one here and like i said before we're going to build up a new request we're going to access this api here do a http get on it then once we get a result we want the node to traverse down and to get this value here and then we want it to multiply the result and basically send it back to us right so um give that a second to to kind of complete first so once we kind of get a result um then then we kind of have it stored in our smart contract and we can kind of use it wherever we want right and there we go so there's the volume so as you can see to get external data from you know some kind of external api it's it's really easy especially if you're just doing a http get or a um or a http post you can kind of use the the standard adapters if you need to use other um if you need to do some kind of other api connection such as connecting to like a database or something that's you know a bit different check out the nodes section of our docs here where we talk about jobs and more specifically external adapters and external initiators as well we've got some great uh documentation on the blog as well as on youtube for external adapters as well if you kind of need to go down that path or check out the adapters section on market.
link as well because there's some adapters um there's some adapters for all kinds of things such as ipfs and all these kind of different systems um and apis that you can kind of use as well in your submission right so so we've gone through an example of how to use the three core kind of chaining tools and features uh using simple remix examples but what if you need to now take those uh libraries and basically that code and and use it as part of your hackathon submission right so going back to what i was talking about before we've got our starter kits here that you can use which contain working smart contract examples um you know you know bundled up repository that you can use that you can kind of use as part of your submission if you've already got a repository started and just want to import and use the libraries in in kind of the way that we did in remix here you can go to the developer reference and go to install instructions and it kind of gives you the instructions you need to import the chain link library into your existing project right but for the purposes of this uh demo i think maybe we'll just choose one of the starter kits probably the the hard hat one because it's the the most beginner friendly probably and we'll kind of go through an example of that one right so if i just go to my visual studio here so let me just make this a bit bigger hopefully you guys can see so i've i've kind of um uh pulled a copy of the hard hat starter kit here to my local machine and um we'll kind of go through it quickly so we've got the three contracts that we just went through in remix we've got them here as well in the starter kit so your api consumer for accessing any api your price consumer for using price feeds and the random number consumer contract for using chaining vrf right in addition to this we've got some deployment scripts right so these scripts deploy those three contracts to the specified environment and they kind of give you some hints on how to then use the deployed contracts in addition to this we've got a bunch of uh what hardhat calls tasks so tasks kind of just repeatable steps that are kind of bundled up in a really easy to use reusable way and we've got tasks specific to each contract you know such as api consumer you know request data or read data or for a price consumer you know read the value of the price feed contract and then we've also got some generic tasks as well such as you know fund the contract with link for example so you'll see how the tasks work in a minute in addition to this we've got some tests uh for each of the contracts as well so if you're working locally and you want to do some unit tests there's an example of you know a couple simple tests on these contracts and you can kind of use them as inspiration to kind of create your own tests or you can modify these ones but yeah they're great for kind of verifying your your smart contract with logic especially in a local development environment speaking of hardhat offers what's called the hardhat network which is a local development environment that enables developers to um do do testing of their smart contracts in a local blockchain and not a live public one where you need to deal with tokens and this and that and the good news is the starter kits are set up in a way so that you can work on these local networks even though you're doing oracle requests and things like that um we use mocks and things to kind of mimic having a decentralized oracle network connected you know even though you don't so that actually is a good segue into the hardhat config file now so the hardhat config file is where you specify um first of all you specify what network you want to deploy and use your contracts on so you can use the local hard hat network like i said in this case we'll stick with a public coven test net in addition to this you need to specify certain environment variables right so you need to basically specify an rpc url you need to specify uh your mnemonic or private key which you can export from metamask so that you can kind of generate these transactions and uh if you're going to be forking the mainnet so hard has it has this forking feature where you can fork mainnet and run it in a local environment which is really cool if you're going to do that you can kind of specify the environment variable for this too and you can kind of get a key from alchemy for doing that if you want if you're not familiar with you know these environment variables you can use a emv file so there's an example file here where you can kind of just plug in your rpc url which you can get one from infuria um you can plug in your private key which you can get from metamask exporting it and if you're forking mainnet you can kind of plug in your alchemy rpc url too and then you kind of set that as your env and then you kind of don't need to mess around with the environment variables apart from that i think that's most of it so let's go ahead and now try and deploy sofa apx hardhat deploy what that will do is it will go through the deployment tasks or scripts here in the deploy folder and it will deploy each of the smart contracts to the given network so in our case we're doing it to the coven network and as you can see as it deploys then we kind of get some output as well uh which is which is pretty handy so for price feeds we can kind of see um we can kind of see a task here that says run price free contract with a command mpx hard hat read price feed and then you pass in a contract number and a network so basically what this does is uh it calls it runs the read price feed task which was over here and because we've deployed our price feed contract to the network we can kind of specify it here and if we run that here we should hopefully get some output and there we go so there's our the value of our price so exactly the same as what we were doing in remix just doing it using the hard hat starter kit here and we've got a working code repository you can kind of easily integrate this with your with other aspects of your hackathon such as your ui etc um let's go through one more example so let's do a vrf request so run random number consumer with the following command so we're going to run the request random number task now we're going to pass in our deployed vrf consumer contract address and we're going to specify a seed as well and the network as coven right so once again this will call the specified task so in this case um request random number which is here and there we go so it now says to read the returned random number once a result comes back we now run the read random number task and we pass in the contract that we just deployed the vrf consumer contract and we specify the cover network as well right so hopefully if i run that now should hopefully have a result and there we go so we've got a random number back in our contract right so as you can see really easy to use easily able to integrate it to your other parts of your hackathon submission and if we want to run the tests run an npx hardhead test and that will run through and execute any test script that is in the test folder here basically so we'll let that run through so as you can see uh it's got everything you need to kind of use chain link tools and features um in in a proper code repository format and not just you know a remix example that you can't connect to front ends and things like that easily so highly recommend having a play with these things modify the contracts copy the contracts do whatever you want um use deployment scripts and tests as as inspiration or you know as a guide for kind of doing your own and have a play with it so i think we're just about at time that's all i had i think so yeah i just wanted to say um i just wanted to say uh really look forward to what everyone's uh really look forward to seeing what everyone is going to build and if you have any questions like i said reach out to us in we're in the discord whether it's the chain link or the xerox discord um happy to help you in if there's any issues happy to answer questions and we can't wait to see um what kind of uh applications and smart contracts you create to kind of take smart contracts to the next level right so yeah really look forward to seeing everyone in the discord and can't wait to see what you guys come up with thank you thanks a lot corey um i saw some questions in the chat so um maybe you can take a look sure we had a questions from louie uh and he also posted some questions in discord which you can answer uh after the after the like this live session sure yep sorry guys it doesn't make an average of prices from each oracle input node okay great question so um it doesn't just uh just take an average it it aggregates the responses but it also takes other metrics into account such as um you know volume and liquidity and things like that because you don't want to you don't want to weigh a an exchange or an api um let's say it's an exchange you don't want to weigh that the same um when it's got a small amount of volume or liquidity as say an exchange with a high amount of volume and liquidity right because um in the end market coverage is kind of an aggregated and weighted uh result basically so you kind of need to take other things into account as well there okay let's see what was the next question does chaining services usage like price feed or vrf uh come with a a cost right so uh yeah definitely whenever you're doing any kind of external computation um or kind of external uh request for a chaining node to kind of either get some data or do any computation then yes you you need to kind of uh pay the node operator in the form of a link right because not only are they providing the service for you but they are also sending the result back to you on chain and you know they have to pay gas costs and things like that as well so there's definitely a cost uh with the price feeds um if you're just using the reference contracts um because you're not uh modifying the state of of the chain or anything like that uh you can actually use the reference contracts without having to fund the contracts uh your smart contract with link right so you don't need link for that one i think someone's asked to share the link to the 77 use cases article and i'll definitely put that in the chat now and once the youtube video uh is live or wherever it's live we'll we'll modify the description and put in um all the links that i discussed in the talk we'll make sure they're in the description as well cool was there any other questions yeah uh harry did you have a chance to read out all the questions uh i think i answered all of them let me see greg thanks a lot yeah if anyone if anyone has any other follow-up questions just yeah ask us in the discord we'll be happy to help yeah absolutely uh someone someone sorry someone just asked about um finance smart chain yes chain link you can use chain link with binance smart chain so if you go to our docs.chain.link and whether it's price feeds or vrf you'll see contract addresses and details for binance smart chain awesome uh so i guess uh those are all questions for today uh all right thank you very much for taking your time yeah thanks for um out on discord uh and thank you guys for watching um we're gonna start tomorrow at uh 11am uh we have a presentation from pedro from wallet connect um thanks again hari and thanks can't wait to see what everyone builds absolutely see you guys see you later bye
Automatic transcript — names and jargon may be misspelled.