how chainlink oracles workCode a REAL WORLD dApp with Chainlink - Ethereum, Solidity, Web3.js

Biggest Movers: Cardano Moves to 6-Week High, Climbing by Apr 8, · This move saw cardano climb to its strongest point since February 21, when the token reached a high of $0.4061. Overall, the surge came as the 14-day relative strength index RSI moved beyond a ceiling at the 60.00 mark. At the time of writing, the index is tracking at 63.67, which is close to a highest point of resistance at 64.00. Cardano Price Prediction for , 2024, 2025, 2030Invezz What will Cardano be worth in 2024? Most predictions expect a price of $4 to $5 by 2024. The continued growth relies on Cardano establishing itself as a genuine how chainlink oracles work Code a REAL WORLD dApp with Chainlink - Ethereum, Solidity, Web3.js
how chainlink oracles work Where can I pay with ADA? : cardano... Code a REAL WORLD dApp with Chainlink - Ethereum, Solidity, Web3.js
in this video im going to show you how to build a real world blockchain application with chain link so if you want to up your blockchain skills and build an app like a pro then you need to understand this stuff but dont worry you dont have to be a pro yet or you know an advanced developer in order to follow along with this video im going to explain what chain link is and how blockchain works as we go ill show you everything in this video so before we get into that if youre new around here hey im gregory and on this channel i turn you into a blockchain master so if thats something that youre interested in then smash that like button down below and subscribe to this channel and if you wanna learn how to build pro blockchain apps step by step from start to finish with chain link then head on over to dap university dot com forward slash boot camp to get started today all right so lets get into this so before we start building the application let me talk about chain link and why its so important for you to understand if you want to build a real world blockchain app so blockchains are closed systems that dont have access to outside data so they only know about you know cryptocurrency transactions you know peoples account balances you know how much cryptocurrency they hold and data inside of smart contracts okay they dont understand like what the current weather is uh what the outcome of an election is or what the bitcoin price is all right so these are all examples of real world data that you might want inside of a blockchain application and thats the problem that chain link solves it provides external data to smart contracts and thats exactly what im going to show you how to do in this tutorial were going to focus on fetching real world cryptocurrency prices with chain link because thats by far the most common use case for the chain link protocol inside of blockchain apps especially when youre talking about like decentralized finance for example in this tutorial were going to integrate a real-time price checker into this dice rolling game so this is an app that we previously built on my channel a few weeks ago so you can check out that video if youd like to ill put a link down in the description below but you dont have to in order to follow along ill show you exactly how it works with this one so heres a quick explainer uh basically this is an as is a blockchain application uh its a betting game where you can bet on the outcome of this dice roll and if you guess it correctly then you can double your money so this game is powered by ethereum smart contracts that run on the blockchain and it uses chain link uh to implement randomness for the dice roll and were gonna upgrade this smart contract to check the cryptocurrency prices whenever we place the bet and were gonna use chain link to do this heres a diagram of how the application will work so the user is going to make a bet directly with the ui which we saw a second ago and thats going to talk directly to the ethereum blockchain because theyre going to connect to it with metamask in their browser and thats going to talk to this betting game smart contract that weve coded in this tutorial okay and thats going to talk with the chain link smart contracts that we can implement into our project im going to show you how to do that so its going to fetch the price from the chain link smart contract its going to return that price to our betting contract which will allow us to uh also get the randomness from chain link and you know use that data when we play the game now lets go ahead and install the dependencies we need to build the project first you need node.js so you can see if you have node already installed by going to your terminal typing node v like this and if you dont have node already installed you can install it directly from the node.js website or you can always use a package manager to install it directly from your terminal next we need to turn our web browser into a blockchain browser so that we can use the betting game so well do this with the metamask chrome extension so just google metamask for google chrome go to the google chrome web store and click install right here however you do you should see the fox icon up in the top right hand corner of your screen now lets start coding the project im going to go to this github repository and clone the final code solution so ill put a link to this down in the description below but im going to clone a very specific branch because like i said we built this app in a previous video and were going to update it to include this new feature with chain link so if you go to the branches here and lets just take a look well see this update branch all right so click on this and im going to copy this url and well download the code to our computer like this well say git clone well paste in the url here well say dash b update okay and thats going to be the specific branch that we want to clone because we dont want to clone the master or the main branch we want to come this clone the specific branch because this is the update for this specific tutorial so thatll download and once its finished use a cd chain link betting game all right and uh now lets install all the packages for the project like this well say npm install and wait for this to finish and once thats finished youll see your success message here and ill go ahead and open this in my text editor im using sublime text but you can use whatever text editor you like lets take a really quick tour of the project code here so everything is inside of the source directory alright so weve got the contracts here so this is the bettinggame.sol okay this is going to be the smart contract that were going to deploy to the network all right im gonna explain all the solidity code here in a minute uh but just notice that this is where the smart contract is and the components directory is where all the code for the react js application is gonna live that we use to play the betting game all right so app.js is the main components of the application and this is what actually loads the app up so that we can play the game so well go over all this momentarily but i want to first start with a smart contract because the first thing we want to do is actually put this on a blockchain so were going to copy this and were going to head on over to okay so just do a remix and you should see a file browser over here click the plus icon thatll allow you to create a new file and then just call it bettinggame.sol okay and then paste the code in and then were gonna go ahead and put this on a test network so this is gonna be a blockchain that doesnt exist on our computer itll be out there uh in the real world but its gonna be a test blockchain so it wont be real cryptocurrency uh itll be a fake cryptocurrency that we can use for testing purposes so its not worth anything in the real world uh were gonna use the rinkaby test network for this so make sure you uh connect to the rink b test network inside your metamask account here and then go ahead and copy your address all right because in order to deploy the smart contract to the blockchain we need some ether some test ether to pay the gas fee for doing that all right so head on over to faucet dot all right this is a website that will give you test ether for free all you have to do is create a social network url for your uh for your request so youll follow these steps here basically you can use twitter or facebook and you can make a post that contains your ethereum address say you know requesting faucet funds so basically click tweet and you can see a tweet like this so what youll want to do is actually put your address in here tweet it out copy that url go back to here and then say give me ether you can select the amount uh the more you request the longer you have to wait before you can request any again so ive already done this step uh but ill just leave that to you next we also want to get some test uh link tokens in order to use the application um so go to in your web browser all right and this is the chain link faucet so you can just paste your address in here for the test net you know do the captcha and say send me 100 test link and whenever thats finished you should have some link in your account so you can go to meta mask and view your special tokens so basically go in here lets see here go to your account click assets all right and then you can go to the bottom and click uh you know add token and you can paste in the link address here uh from the transaction you can just copy it put it back in here add the custom token and you should see uh the link balance here so ive done this step before so i have you know 180 link but after you do this you should have 100 link in your wallet that way now that we have some cryptocurrency in our wallet lets go ahead and put this smart contract on the blockchain so uh you know make sure you have selected the wrinkly test network inside of metamask again and uh go over here inside of remix uh to this the solidity icon this will compile the smart contracts were using version zero at 6.6 so that should be good if you dont see this step you can go to the plugins and search for the compile and run transactions step and then also the deploy step that will give you these two icons okay so go ahead and click that click compile all right and then next well go to this uh deploy run transactions uh just as a side note these are the names of the plugins deploy and run transactions and also solidity compiler all right so anyways were going to select the injected web 3 okay thats going to use metamask so you can see rinkaby network was selected here heres your account with your ether balance uh were going to go ahead and deploy it so we click this deploy button all right and uh click confirm im going to go ahead and raise the gas price that it happens fast 50 way uh all right it should happen pretty quick you can see your transaction here all right with that to finish all right now once thats done we actually want to send some cryptocurrency to the smart contract because thats how this game works um basically it needs two different cryptocurrencies it needs ether you know the ethereum cryptocurrency um in order to pay out the bets right so we have to send ether to this smart contract it can hold cryptocurrency just like our meta mask wallet can so you want to get the smart contract address you can get it from a few places you can copy this address from here right or you can get it from this transaction okay so im just going to copy this way click this copy button ill go to my meta mask wallet and ill click send paste in the account here and ill just send it one eth for now okay click next click confirm all right and the next thing we want to do is actually send it link tokens as well because thats how we use chain link we pay the fees in link we want the contract to hold some link so you can go down to this link token here that we added a minute ago click send paste in your account here im going to send it just like 20 link for now click next click confirm and then once this transactions uh mined then the smart contract will be funded so you can see that ill just wait for this to finish wait wait wait whenever its finished youll be able to see the balance on ether scan so as soon as its done ill uh ill show you what that looks like so wherever thats finished you can just click on the smart contract address here and you can see that it has a balance of one ether and that it also has um some tokens it has 20 link tokens okay so now that our contract is properly funded uh lets explain the code and then demonstrate how it works all right so lets look at the new way that were going to use chain link in this tutorial so we use it in a few different ways but i want to first focus on uh the the main point of this tutorial which is to fetch the ethereum price uh from chain link because were going to use it as the minimum bet all right so whenever someone bets on the dice roll uh we want them to they need to bet at least one dollar okay and theyre betting ether you know ethereum cryptocurrency in this case and we need to understand what the ethereum price is so that we can calculate one u.s dollar okay because we dont know that information directly on chain uh were gonna rely upon chain link to do that so were going to use uh this get latest price you can see that here the chain link documentation for solidity so we need to import this uh aggregator all right and uh provided the address of the cryptocurrency price fee that we want uh in this case its eth usd all right heres the address for that well do it for our specific network uh here all right so basically what we need to do is uh you know get the price feed uh like this all right well do this inside of our code and then well have a function that will get the latest price okay whenever it does that uh well get some variables here like the round id the price started at time stamp answered your mouth you dont have to worry about what all these things mean what we really want to focus on is this uh price right here because thats the data that were going to use in our app right try to make it simple at first so its easy to understand so lets look at how we implement that inside of our smart contract for this tutorial so we start by uh looking at the updated parts we have an update flag you can see that all throughout the code here okay ill go through those one by one so we start here by importing the aggregator uh from the github repository okay just like they do in the in the documentation all right and then next uh we create a variable to store the etherusd price all right thats here this is a state variable and then lets see here next update were going to add this to our constructor okay so the etherusd price feed thats this right here just like we saw in the documentation next what were going to do is um create a function here to fetch eth usd so this will basically just be a function and anytime we call this its going to get the eth usd price from chain link so thats the exact same code as uh this right here so get latest price thats going to be this ethan usd and then we also have a function for getting way in usd because sometimes you want to get the uh you know the smallest amount of ether in whey like this all right so thats the bare bones infrastructure those are all the components that we need in order to fetch the actual price now we fetch the actual price here because when we call the game function this allows the user to make a bet and whenever they make the bet we had a requirement in the solidity code here uh require that the value that theyre sending in is equal to the minimum wave value so away in usd it must be one us dollar and of course you could update this and change the minimum bet to whatever youd like youd have to do some math some actual calculations here but its nice and easy for us to see the one dollar conversion because thats the exact same data that comes back from the price feed here so we just make the minimum bet one dollar makes it nice and easy solidity has this special require function that is a uh basically whatevers passed into here whether its true or false its important so basically if its true then the function resumes execution and if it false sorry if its false then uh this basically returns an error and then nothing else in the function is uh called sorry nothing in this function executes after this line and we get this error back so error you know message value must be greater than or equal to one dollar so now we can see this uh in action okay you can go to your smart contract here and then make sure you uh expand this menu here and uh we can call the functions so we can like look at the actual you know prices like this so we can get the uh ethan usd price okay all right so here it is now we can get the whey and usd price like this boom all right and then we can also uh you know call the game function which um allows us to make a bet okay so lets uh lets try this the bet is uh zero and the seed is well just make something up one two three four five six okay well do a real seed later well click transact and uh oh actually we need to uh pass in some value because thats how it works you send in some cryptocurrency theres a transaction thats what this value field up here is okay so uh it needs to be at least one dollar so lets make it fail first lets just do one way we know thats its way less than one dollar okay its click transact all right and then uh yeah we can see our error here msg value must be greater than one dollar see thats awesome so remix is returning the error on our smart contract code okay thats awesome lets actually make it like a like you know one dollar and fifty cents or something like that so lets look up the ethereum price all right so here is the uh heres the ether price in way so one usd equals this many way so we can just take this and uh take the decimal places out in our text editor like this or sorry the commas out and then well pass this into the now lets make it a little bit higher lets just say like you know three we know thatll be higher all right so ill kill that and go back here paid some for the value and then click uh transact and then there we go all right we can see the uh amount going in right here before we confirm the transaction that your math was correct click confirm and then there we go theres our bet now lets do a quick recap of the rest of the smart contract i first wanted to talk about the chain link integration with the price feeds thats the main purpose of this video is to show you how to integrate that into your application you know like we did uh here with the get price all right um but now i want to quickly recap how the rest of the contract worked if you want a full explanation again i highly recommend checking out that previous video ill put a link down the description below where i show you how the betting game function works um but well do a quick recap and heres the function that we call its a game we do a bet which would be one or a zero thats basically uh is the number on the dice gonna be uh you know one through three or is it gonna be four through six okay zero is one through three one is four through six thats the bet and then the seed is the random seed used for randomness okay so that calls this get random number uh function right here uh and so we do link we request randomness thats the thats the function here and thats what we get from the chain link contract okay so ill point you the documentation for that this is the get random number uh we can see here you just have to add these parts we get the consumer contract like this we have at the top of our file and then we implement this get random number function that works just like the documentation and we fulfill that request with this call back here and so uh we have all those components inside the application to call that for our game and then if the uh if they bet correctly then theyre eligible to win that cryptocurrency and if theyre not then they they dont okay so again check out that previous video if you havent already but thats a quick recap of how this game function works and how we also implement uh randomness with chain link now lets boot up the client side application to play the game in real time with the user interface alright so uh im going to open a new terminal tab here and say npm run start okay so this will uh boot up a web server that we should automatically launch a new tab in our web browser here okay so whenever thats finished you should see it a message like this okay now that were connected you want to make sure that youre on the rinkeby test network so you know make sure youve got wrinkly selected metamask and then we can play the game okay so we can enter the bet amount uh you can see the max bet and also the uh minimum bet is one dollar okay so were going to do it in eth so lets look at you know the eighth price in usd ethan usd so one dollar is 0.021 ether at the time record this video so you can look that up in google if you want to well just do 0.0001 and say its low and should give us an error all right so thats what we expect so yeah its going to throw an error so we dont want that so its its uh its requesting the price correctly so lets um lets do this lets say 0.01 eth should be a lot more than a dollar so lets just bet low well play the game and it should work so lets do the gas price lets raise it to 50. uh click confirm and we should see a message here on whether or not we won the game and then once your transaction is mined youll see uh your message here whether you won or lost so we won in this case so we see a nice uh happy message here all right so thats how you fetch cryptocurrency prices inside your smart contracts with chain link you add them to your uh application like this lets take a quick tour of the client side application um again highly recommend checking out the previous video for a fuller explanation but the main part of the app is this app.js file here this is react component um so we made some updates in here in here on how we load web3 to make sure its compatible with the uh current version of metamask all right and also that we have the right network but basically we get the smart contract api and then the contract address okay on rinkaby so really you want to take your address from remix okay copy it and put it inside of here uh replace this address so lets go ahead and show you how to do that uh click see here sometimes the copy button goes off screen so copy that go back to sublime text change it here all right so then we at create a javascript version of the smart contract with web3 like this okay and then we load all the data into our app like the accounts all right i explained all this in the previous video but um you know we set restore it to the state object next we add this on accounts changed we listen for that event so like if we swap account to metamask that will reload the app and then also if we change the blockchain like to the test network versus the net itll also reload the app as well okay so heres the main function the make bet function where we give the bet and then the amount and this basically calls this um game function right with the bet and also the random seed in this case we just hard coded a random seed you could make this dynamic if you wanted to uh or you know use a different random seed for the randomness in the game as well and then we uh you know execute the transaction on the blockchain like this and then whenever its finished we trigger the window alert uh with the state of the uh game so if you won we give you this message and if it you lose we give you this message okay so anyways uh heres the react state that stores all the data inside the front end application and then we pass all the data all the data and the props down to this uh these two components im going to explain that more in the previous video but heres the main component that you know has all the markup and the display logic for the game and then also this loader okay all right so thats how you implement cryptocurrency prices into your blockchain applications with chain link so i hope you like this video as always you know smash the like button down below subscribe to this channel if you havent already and theres a lot of different things you can do with chain link its a really exciting technology and you need to understand this if you want to build real world blockchain applications particularly on top of the 3m platform right now this is really in demand skill you can do a lot of different things with this you know you can integrate cryptocurrency prices into a defy app or something like that so i actually have a brand new uh section about chain link inside the block chain boot camp so if you want to learn uh more in depth on how to do that uh you know just click this link go to dap university dot com forward slash boot camp and sign up today all right so thats all ive got and until next time thanks for watching dap diversity Become an in-demand blockchain MASTER: YzCbaR15Xi4 Subscribe to this channel: Instagram: Twitter: Email: Need to hire a blockchain talent? List your job here: