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

Loading player…

Empowering Smart Contracts with Decentralized Data - Patrick Collins

Ethereum DenverSat, Oct 9, 2021, 12:00 AM

This talk is from ETHDenver & ColoradoJam , the world’s largest Web3 BUIDLathon and Community Gathering. ETHDenver’s content is crypto and blockchain focused. Learn more: ethdenver.com Subscribe to our channel to learn more + connect on Twitter: https://twitter.com/EthereumDenver. Speaker: Patrick Collins This talk was recorded Sunday, February 7, 2021. ETHDenver 2021’s content themes included DeFi & Money Legos, NFTs & the Creator Economy, ColoradoJam & the Next Frontier of Statehood, Protocols & Interoperability, Open Source & Public Goods, The Ownership Economy & Digital Orgs, Usability Design & Adoption and Legal, Privacy & Identity. Industry experts also weighed in on topics like decentralization, privacy, identity, non-fungible tokens, scaling, governance, diversity, and arts and culture in blockchain technology. The 2021 event was February 5-12 and hosted virtually in 2021 with 5000+ blockchain enthusiasts, developers and community members from 80 countries and 50 U.S. states. 🔴 SUBSCRIBE ETHDenver’s YouTube https://www.youtube.com/channel/UCgz9NU06t_FkT0Py1Vadczw 💡 LEARN ETHDenver’s Official Website https://www.ethdenver.com/ 👋 CONNECT ETHDenver Twitter https://twitter.com/EthereumDenver 📖 READ ETHDenver News on Medium https://medium.com/ethdenver 🛍️ SHOP ETHDenver Official Merchandise bufficorn.com 💬 CHAT Telegram https://t.me/Ethdenver Discord https://discord.gg/AxFmNdCVnU 📱 FOLLOW LinkedIn https://www.linkedin.com/company/ethdenver Instagram https://www.instagram.com/ethdenver/ Facebook https://www.facebook.com/ETHDenver/

Transcript

okay so up next we have chain link patrick hello hello how's it going another youth denver success story another eat denver successful that's right this was my first uh blockchain hackathon and really where i met the majority of uh of the people that i know in the space so really excited to be back in this year as a speaker as opposed to um you know when i was first here when i was like what is it what's the theorem what's going on what is all this stuff been uh been very um down the hole into the ether if you will so excited to jump in thanks for having me back john oh you got it did you get my email yesterday uh i might have what was the uh what was the email it was about the state it was about the state yeah oh well i'll have to check that out after this get out after you talk well i won't don't burden yourself now we'll talk later all right cool anyways uh you're ready to talk about decentralized data i'm ready to do it i'm ready to talk about decentralized data john all right man it's all yours all right cool thanks so much hey hello everybody how's it going um and yeah like i was just saying to john uh really excited to be back especially here at ethereum denver last year i was a hacker here and uh this is pretty much where i learned solidity um i learned everything about kind of the space got to see who the who the makers and shakers are um so this is a fantastic hackathon that you guys are at so um you've chosen a great hackathon to be at um you've chosen a uh a great place to be and just really excited to help everybody on the journey and kind of follow the same journey that i've basically taken in the last year so really excited to be here especially for that reason but let's jump into it the uh the name of the game right now is decentralized data and empowering your smart contracts to do uh really whatever you want um and because really at this point in our life and especially in my last year from east denver to denver uh we have this extraordinary opportunity in front of us right the world of smart contracts is really knocking at our doorstep right now and history is going to show who's going to be the pioneers to push us into this fourth industrial revolution where we use these superior digital agreements these superior smart contracts uh built on top of the blockchain uh like ethereum and all the other uh all the other chains that are sponsoring this hackathon so you know these hackathons really are the stomping grounds for innovation so uh really excited that you're all here you know especially at east denver um and uh you know excited that uh excited to take that next phase to help us move forward but uh yeah we're gonna talk about connecting your smart contracts to off-chain data uh financial definitive truth apis proven random numbers and and everything chain-link so we're going to talk about chain link we're going to go into the oracle problem uh definitive truth verifiable randomness use cases accessing an api and then hopefully we'll have some time to get to some technical demos here as well so in order to really understand chain link we must first understand that smart contracts are not able to connect to external data or really any off chain resources on their own and this is a huge limiting factor right because for smart contract adoption because it means the only thing we can really do is we can do token transfers right and this already has been this monumental breakthrough doing these token transfers and having this the store of value but smart contracts can do way more than that and it would be great if these uh external um uh these smart contracts actually interact with the real world right because we have contracts that are great but we would love these math based these these trust minimized contracts so uh then the question becomes okay well why can't a blockchain just make an api call that's what every other system does right like what's going on and that was the first thing that i thought you know coming in as a software engineer i said okay i'll just make an api call and blockchains themselves actually can't do this they can't make api calls and the question then is okay well why not and the simple answer is that blockchains are deterministic right and this is really important for consensus because every node needs to be able to validate every other node and if nodes are calling apis and you know maybe they call an api at a different time that api goes down it's depreciated now the nodes are going to say hey you know what was the response for that api they're going to say different things and there's no way that they're ever going to be able to be in sync they're never going to reach a consensus uh and and this basically makes it impossible to have that part to be uh the consensus protocol so how do we interact with the outside world and the answer to this is using oracle's and an oracle is any device that interacts with the off chain world to provide data to smart contracts and cause the real world to do things or send data from the blockchain to the real world so oracles are bi-directional right bring data into the blockchain send data or or do stuff outside of the blockchain and they report the results when they're bringing on chain they report the result of data on chain uh so that the blockchain can stay deterministic and they do this through an external transaction uh themselves so this already is is hugely powerful right these oracles are really what allow you to interact with the outside world from the blockchain now here's where it gets a little bit more important uh and a little bit more interesting that the problem becomes a little bit more complicated we have our our logic layer on chain is all decentralized right if we're working with ethereum solana manic you know any other the uh the block chains that are you know here sponsoring this event we know that that logic layer is decentralized but if we're getting our data from a centralized point then it's it's no better than if we just wrote our whole contract in python or java or a centralized service right because if there's a single point of failure in our in our in our decentralized application if there's a single person who can twist the lever and turn it off your application is now centralized and this is a huge issue so the oracle problem can be summed up with these two problems right number one blockchains themselves can't make api calls and number two using a single centralized oracle is going to mean your entire application is centralized so you want the logic layer and the data layer to be uh to be decentralized so and chain link is the answer to all this right chain link is a decentralized oracle network and it's designed to be modular it's designed to be a modular decentralized or oracle network for getting reliable decentralized data so if you want to get centralized data you can absolutely do that too you can run a single chain-link oracle or chain-link node um which is i'll use them a little bit interchangeably there but really if you're if you're looking to keep a decentralized application you want to use a decentralized network of chain link nodes here and with this we can use these these networks to really take any input move through the blockchain and any output uh as as well so we can really chain link oracle's allow you to take your smart contracts from being able to do a lot of things on chain to be able to do anything in the real world any type of contract any type of agreement that exists in the traditional world can be done with these decentralized oracles so they really unlock the power of smart contracts to go from you know doing everything kind of in this walled garden to breaking through that wall and having cryptographic math proven contracts in the real world which is really really exciting and to get you started with defy uh chain link has actually already gone ahead and made some pre-box solutions for this so like i was saying chain links 100 customizable you can get any data that you want through chain link any input any output do whatever you like um but chin link has gone ahead and already done some work for you right some of the most popular data sources right now especially in d5 is cryptographic uh excuse me it's cryptography prices or cryptocurrency prices excuse me like the price of ethereum and usd like the price of um dot like the price of uh any token or or any fiat currency as well so the chain team has actually gone ahead found many of the best node operators on the planet and put together what's called price feeds or data feeds and these are decentralized points of reference that anybody can can call in their smart contracts and considered source of truth so they have decentralized data in their smart contracts so here's a visualization of exactly what that looks like here right each one of these nodes in this little circle here independently retrieves data from some of the highest quality apis and reports their answers on chain you can see their answers that each provider reports so that everything is completely transparent right and most of the most popular cryptocurrency or fiat currency pairs are currently supported and in addition there's other pieces of data like how much how much is in the reserves of tusd what's the current fast gas you can check out feeds.chain.link or data.chain.link to see all the some of the well some of the supported pairs and there's more in the documentation which i will show in a little bit so uh these are massive massive for d5 and have been kind of the backbone for a lot of different projects and here's just a little bit more um information on kind of the approach that the chain link oracles and the chain link networks take right this massive decentrality provably secure high quality apis and you know one of the other things that's incredible is the blockchain oracles excuse me the the chain link oracles are blockchain agnostic meaning that chainlink can run on any blockchain uh that needs data and there's a reason why i stress kind of the decentrality so much especially in d5 in november alone we saw over five attacks that were over a million dollars worth of damage um and each and the total combination was over 30 million right so there were a lot of attacks that came from centralized oracle attacks right a lot of people call them flash loan attacks um but they the tool was flash alone and they were using it to manipulate oracle prices so um it's really really important especially in d5 when you're working with people's livelihoods that you work with decentralized really secure uh really provably honest data and that's what chain link delivers so uh and d5 again you know is constantly one of the major areas pushing for projects and looking for this this fantastic data that's really easy to use um and chain link is that data that that's the backbone of a lot of these projects so right now uh last time i checked i think d5 was at like 25 billion it it changes so quickly um it might be even higher than that but uh chain link is the backbone of a lot of these massive projects you know like synthetics which last time i checked was 1.

5 billion assets under management i think it's actually a lot more than that at this point uh loopering set protocol ave last time i checked was 3.5 billion right so these are battle tested price fees these are tried and true decentralized price feeds um that are really going to power your d5 applications to do uh you know everything that we've seen in traditional finance but in the decentralized finance world so um and again check out feeds.chain.link um also data.chain.

link because uh chain link is expanding to more different decentralized data types um and as the protocol matures even more and more we're going to see uh more different types of data i'm really excited for when i can check on the blockchain instead of like the news you know what what the temperature outside is or what the weather is going to be like i think that that's going to be uh really really exciting so so that's for that's the the pre-box solution there is these price fees is these data feeds right but chain link has even more than that right because again chain link allows you to get any input and any output so what does this mean well a a huge common problem in uh computers and and systems that are deterministic you know like a blockchain is getting random numbers if you do a random number you know in your python in your javascript whatever it's actually pseudo-random right it's actually pulling a number from like one of your files or from some bytes or something but it's not actually creating a truly random number right it's getting that random number from somewhere um and this is an issue on a deterministic system like the blockchain uh some of the most common patterns right now for getting decentralized data are are hashing the block height right and and this is actually a major issue because it actually gives the miners the control over uh the delivery of your descent of your number right so if you're looking for a random number an admirer a miner gets it but maybe you know they have uh they have some stake in whatever lottery or game or whatever you're running and they go ha if i publish this block i will lose and they just throw it out and they just keep throwing it out until they they publish a blo or until they they mine the block that would win and then they publish that one uh well basically this sucks away any and all fairness of your application right so chain-link vrf is a way to get provably random numbers right so you actually interact with a chain-link oracle it generates a random number um and through a lot of cryptographic magic and there's a smart contract on chain that actually verifies whether the number is random or not you can get a truly provably random number uh into your smart contract and we're seeing more and more adoption uh with this as well you know pull together being one of the one of the major leaders and kind of using the chain link vrf to get their random number in their default application um but everything you need for this is in the documentation and um and yeah the documentation link is right there and we'll go over that in just a minute and then last but not least again uh chain link is capable of being used to work with the apis that run the rails on the internet right now so everything that makes the internet run is kind of basically off of apis and working with any api is is a cinch width with chain link and it allows you to not only work with these already existing apis but excuse me customize them and make them do even more than what they already do not only that but you can also integrate chain link with any other blockchain using external initiators um and then this this massive customization feature is with external adapters so we've written ton of blogs on all this um and we we have a hackathon resources list that we'll go into at the end of the certain we can show you guys where you can see some of these resources and where you can customize your smart contracts to do literally anything and and yeah and here's uh here's the uh just a quick snapshot of the documentation page which shows getting an http get request and we'll go to the documentation in a second um but before we jump into that i wanted to jump in some of the use cases enabled by chain links so there's an article out there called 77 used 77 smart contract use cases enabled by chain link definitely go check it out um if you're looking for ideas if you're trying to get started uh it goes through way more than i'm going to go through here but i'm just going to briefly uh just run through a couple of them defy applications i mean we're basically speed running the traditional finance history uh and there's still so much to do so there's a ton of default applications and obviously getting data is the cornerstone of a lot of these d5 applications so um definitely take a look for some defy ideas fantasy sports or really any type of gaming that needs randomness or or any interaction with the outside world tokenizing your assets or insurance insurance is kind of a huge one for me and one that i'm really ex personally really excited for we're seeing more and more adoption on this as well um you know let's say your crops get all messed up due to the weather and your insurance doesn't want to pay because of some pre-existing condition right with a decentralized smart contract you don't have to trust the insurance company is going to do the right thing since the right thing is going to be infrastructural with chain link and getting that data so incredibly exciting here so let's go through a couple of demos here [Music] and hopefully we can see the docs yes we can great so here's the the chain link documentation for those of you who are looking to get started i would say just come down to hackathon resources right there's there's a ton of information down here um you can go through some of the frameworks um that have chain link natively in them you can come to the different um the different support channels discord is there's a massive following of chain link and smart contract engineers who are really helpful looking to help each other out and then we have a bunch of tutorials here as well if you've no idea what any of this ethereum is but you're really excited you heard you know a little bit about trust minimize smart contracts or uh or you're really into the tokenomics or whatever we have some beginner tutorials that are good for anybody just looking to get into the space and looking to work with data um and there's also some some past winners in here some some other blockchains but this is a great place the other great place to look at it's gonna be the chain link blog that's at blog.chain.link you can check out the developers channel and there's just a ton of tutorials in here nfts insurance different blockchains different different languages python hard hat truffle um there's there's a ton of information here if you're just looking to get started you're looking for some more information but let's go ahead and we're gonna actually start with the price feeds and getting the latest price of a of a currency pair into your smart contract so we're gonna come right here so it's gonna be using price feeds get the latest price and all the code that you need is right here so this is for um this is set up for your truffle or your hard hat or your your brownie we'll go to we'll actually deploy with remix because it's a little bit quicker it's a little bit faster for us to uh to try out here um you will notice that the syntax on remix is a little bit different um and i've done this a lot so i have a lot of stuff but but normally you'll just get uh you'll just get one contract here so the only difference with remix is that you actually pull directly from github in remix versus with like truffle hard hat brownie you pull from like the mpm package um so we're actually gonna uh we're gonna pull right from github and we can see in here i'm just being conscious of the time that's why i keep looking up uh and we can see in here uh this is all the code that we need so we we have our contract we're gonna i'm gonna move this over just a hair cool we have our contract um that's called uh price consumer v3 uh and what we're going to do is we're going to uh import this aggregator v3 interface this is the the chain link contract that actually aggregates the data of all those nodes and we need the address of it so so we need this address right here and we can actually see this on the coven or we can actually see what what these price fields look like you know this is obviously the maintenance example but if you want to see it on chain right you can go to ether scan you can see exactly the contract um and and just read everything off of it so um yeah we can view advanced mode see a whole bunch of the different uh the different transactions that are going out to keep these price feeds up to date um but yeah so we're on the we're gonna use the coventestnet to test this out uh and all we need to do is call this get latest price function and this is a view function so it doesn't cost us any gas to actually call it because we're just reading something off the chain and it's going to return us the price so we're going to go ahead and compile this which i just hit ctrl s to compile oops compile it and we're going to deploy it inject a web 3 i might be on mainnet um [Music] so let's uh let's not be on maintenance oh you guys can't see my metamask because uh it doesn't let me it doesn't it doesn't show the application window but my metamask just popped up i am in coven uh we can't even see that i'm in covenant right here we're going to use inject web3 which means we're we're plugging in our metamask right into here and we're going to deploy metamask again popped up you guys can't see it i'm going to hit confirm and we're going to get this transaction down here saying okay cool you're deploying your smart contract so again this is this is just deploying a smart contract that once it's deployed it's going to read from this or even this address this decentralized data on chain and we're gonna pull it and we're just gonna read from right so now we've deployed our contract and we hit get latest price and we can see the latest price of ethereum uh the question then is oh well why is it a weird number um and the answer is because uh ethereum actually can't understand decimals it doesn't work with decimals so uh we actually just multiply it by um [Music] uh what is it one two three four five six seven eight eight zeros and we see the price is actually 1560 right now which is really exciting this is how price feed works and we'll jump back into the next one and again head over to data.chain.link um in the price feeds themselves um if you want to see you know all the different addresses for you know right now it's ethereum buying a smart chain and matic we can jump here and we can see all the different price feeds um there's a ton and on the different networks as well and you just grab an address you pull it in the other thing that's really cool is you can actually work with ens the ethereum name service to get this uh decentralized data um instead of like grabbing the address you just do you know you look up the fusd um i think um yeah fusd so here's the name fuc.

data.eth and here's the address here so you can use this instead of of always having the the hard-coded addresses but you can use whatever one you want so that is price feeds and that is like i would say that's one of the most powerful features is these pre-box decentralized solutions um that you can use to get um decentralized points of truth in your smart contract you don't have to worry about figuring out where to get data from the next thing that we're going to look at is getting random numbers into our smart contracts and and this is huge and we saw the use of this actually at another denver event um the the game jam colorado's game jam which was really cool we saw a number of projects using these random numbers um to help kind of redefine and rebuild some of the different things in the um in the current like web 2 like lottery world so we can have these provably fair lotteries which is uh just kind of an insane concept that that's even a thing and again you know all the code is here this is kind of the the truffle hard hat version we can deploy with remix like so and again i've done this a hundred times so i have a ton of contracts in here but what we're going to do here is we're going to import vrf consumer and that's the the code that's going to really um it's going to prove that's going to provably check that our number is truly random with the oracle that we use so a couple of additional things going here so we need the key hash of the oracle that we're going to work with uh we need the fee because in order to work with a chain-link oracle we actually have to pay a um a gas fee or excuse me an oracle fee and then we're gonna have just a global variable called a random result so in our constructor here uh we we're inherited the vrf consumer base and we give it the vrf coordinator this is the smart the vrf coordinator address this is the smart contract that actually cryptographically checks that our number is provably random we have the link token which is just the address of the chain token and then we have our key hash and this combined with the vrf coordinator and the seed phrase which we'll get to in a second are all the pieces that the smart contract needs in order to prove that the um that the random number given is is truly random so and then we have our fee which we're going to set to 0.1 link to work with the the chain link oracle here now what's going to happen we have a function called get random number which we give it a user provided seed and this is something that we come up with we've seen stuff like uh protocols like pool together just use the um just use the uh the hashed block height and this is a seed that we give it so that again the chain link oracle can use that kind of as a base to create the random number and and the vrf will actually take this seed and use it to prove that the random number uh generated by the oracle is is truly random and then we just call this request randomness function and we send you know give the keyhouse uh the fee the user provided seed and this is imported from that vrf consumer base which we have at the top here so and once so this actually makes uh to do this we actually use do two transactions so we we call this get random number function and um it emits a log that the chain link node off off chain reads it picks up the law goes ah okay they want a random number great generates the random number and in a second transaction it actually calls this fulfill randomness function calls it back returns the randomness number and also the request id but returns the randomness number and you can do whatever you want with that random number here you know if you want like a number one through ten you can use module module 10 which this actually makes it a random number zero through nine um we're just gonna set it to random result so that then we can read it so um so this is the callback function that the chain link node uh returns with so same thing we're gonna compile it we're gonna move to injected web three and we're going to deploy again metamask pops up i'm hitting confirm you guys can't see it uh and yeah same thing we have the the coven um the coven ether scan link so we can see it actually being deployed uh and we get the consumer the random number consumer here now i'm act and now i'm actually going to call it wrong intentionally just to show you guys something that you'll probably run into um and you'll be like what's going on so we get this gas estimation failed um and the reason that we get this is again you know to work directly with the chain link oracles we need to send them a link token we need to send them this oracle gas so we're actually going to copy this address and in my metamask i'm going to send it excuse me i'm going to send it some link and apologies metamask doesn't show up when i'm doing the applications uh screen but i'm sending it i'm saying edit a couple link here's the um here's the the transaction for it and if you want like coven link uh coven lincoln it looks like it's already done uh you can just like google coven link faucet right and it'll pop up uh same thing with you know cove and eat faucet or whatever um or whatever you know network you're working on you can get that into your smart contract uh no problem so now it's funded with link now we can call this get random number and it doesn't give us that error again metamask does pop up you guys can't see it here but i'm going to go ahead and hit confirm so it's going to make two transactions right once this one completes our chain link node is going to read it pick up this request and and then say ah okay cool they need a round number and send it back and if it's not random this vrf uh consumer this vrf coordinator is actually going to reject it so hopefully the the chain link node actually sends us a proper a proper random number here and um uh yeah and then we also specify which um which oracle that we're gonna work with uh which is um oh which is which is imported from the bureau of consumer base so once we hit random result now now that it's actually responded we can see we have this massive random number here so and i have a couple minutes left so i'm going to try to speed through the last example here which is making an http get request which again um if you're interested in kind of customizing this so your smart contract can really do anything definitely scroll down to the external adapters we've got a blog we've got a video on the whole thing like building and using internal adapters uh the blog here there's a video here we've got a whole bunch of videos in the on the chain link youtube this is how you can kind of customize it to do anything if you're interested in building kind of your own solution with other chains check out external initiators um and yeah again hackathon resources is a great place to go you can see some example winners from the past um there's there's a lot of really good stuff in here but yeah for those of you looking for support uh definitely jump into the chain link discord but yeah i'm going to speed through this one as well because i have a minute and a half left and this is just making an api call so again we're going to pop up metamask and all the information that you need is in here if you have a question feel free to google it or ask the discord or ask somewhere this is pretty much the same as getting a random number we're going to choose we're going to import some code we're going to choose the oracle the only difference here is we're going to choose a job id which is going to specify what we want that job to do the return type that we want to get and we're going to call this request volume data which is actually going to call this make this api call right here and we're going to look for the last 24 hours we're looking to get this into our smart contract so uh to our chain link request here which you build it like this we're going to add this http get what's called an adapter and we're going to say hey we want you to make this api call and we say off of this api we only want to get the path of raw fusd volume 24 hour which is going to be raw f usd volume 24 hour we're saying we only want this data obviously if we grab this whole thing and that's going to be very gas expensive and remember ethereum doesn't work with decimals so we're going to also add this this times adapter which is going to multiply that number um by this and then we're going to send the request to that off-chain oracle and then it's going to return with this fulfill function and yep just speeding right through because i have zero minutes left we're going to deploy up we're going to deploy [Music] confirm and then once it deploys we're going to grab it send it some link because again it's the same thing as getting the random number uh we need to send it some oracle gas again apologies you cannot see because the the stream art isn't letting me um let me show the whole screen here but i'm going to be sending it some link and then once that is confirmed [Music] still sending once it's confirmed i can now request the volume data metamask pops up says are you sure you want to do that we're going to say yes and once the transaction goes through right now if we click this it's going to be zero because we have to wait for the oracle to just see the request make it off chain and then return it back and once this returns it then we'll get the volume and you can see how very easily um if you make this the request to a network of these nodes you can get decentralized data very easily there are no listing services like market.link where you can check out all the different nodes that are on the network you can choose you can mix and match and make your decentralized network of nodes of whatever you want and again uh we get the number here uh which is uh multiplied by um this massive number so we have the volume in our smart contract so really excited for everyone to build that ethereum denver you know like i said this was my first blockchain hackathon so uh this is of particular interest to me john uh you know you guys always put on a phenomenal event um so just really excited to be back and giving a talk this time so i will be in the discord we will be in the discord the denver discord uh feel free to stop by say hello i was in the castle for a while yesterday i will probably be there again today feel free to stop by and say hello and tell me about your projects and what you guys are building cool thanks patrick it's great seeing you man all right great seeing you too john i'll check out that email right after this sounds good all right take care guys see ya

Automatic transcript — names and jargon may be misspelled.