chainlink fencce future for chainlink github smartcontract chainlink Building and Using External Adapters Chainlink Engineering Tutorials
in our last video we made an api call brought it into our smart contract and explained exactly how it works oftentimes though when building smart contracts youll want some additional functionality like api authentication off-chain computation to save on gas connecting to other blockchains and anything else not covered in the core adapters chain link external adapters allow for unlimited customizability when it comes to building these in this video were going to go over what an external adapter is how to build one who should host it and then how to use one theres a lot to cover in this video so lets build when we talk about adapters tasks there are two types there are core adapters and external adapters core adapters are the adapters that come already installed with the chain link software http get copy json parse are all examples of core adapters external adapters are where you can build your own additional functionality most external adapters are executed entirely off-chain so they can be built and tested entirely off-chain as well now when building these we need to keep in mind that other nodes need to be able to run these external adapters as well and its not just for us this is so data can stay decentralized think of external adapters as open source packages but for chain-link nodes this is great because we can build an external adapter and add this customizable functionality but also not have to run the node that hosts it ourself this way smart contract engineers can focus on smart contract development and node operators can focus on the node operating you can add your external adapter to a node listing service like market.link or join the ask a node-operated discord and ask a node operator to host it for you but if you want to run your own node then of course you should one of the simplest ways to make an external adapter is to make it an api itself this way we can customize the computation any way we like and use any language there are tons of tutorials out there on how to build your own api server were going to build an external adapter using a node.js template we also have a python template in the description as well if you want to get started there youre more than welcome to build your own api server from scratch but the template that were going to use makes it really easy to spin up external adapters youll need yarn and node.js installed to proceed to get started were going to use this repo to check if we have yarn and node installed well do a quick yarn dash v no dash v links in the description to download these now we want to clone the repository copy git clone and cd into the repo we can now see all the files in the repo well type yarn to install the packages and then well do yarn start to start the server well see here the server is listening on port 8080 it is now listening for entities to call it when a chain-link node is running through its adapters when it gets to an external adapter it makes a call to that adapter what we are about to do is going to mimic what a chain link node is going to do i have a second terminal up so we can see the results of the server from our call and we can mimic it locally with this command this is a curl command which is basically an api call its going to be an http post using json to our localhost which is where our server is currently running and were adding this data flag to it well talk about that in a minute now we make the call and we can see that the server responded and we got an answer right here lets look at whats in this data object chain-link nodes send json requests that have an id and a data object for one to be valid it could be as simple as this in the code of our external adapter we specified that were looking for from and two parameters and we place them inside of our data object a valid external adapter always needs to have an id and a data object this is what our server responded with all external adapters need to respond with the job id and the data object ideally youll have a status and result field as well this will make error handling and getting the result you want much easier as we can see here the result is 353.32 and this is the current price of ethereum were getting this data because our external adapter is actually calling an api as well in the code youll see this url which calls an api we can test to see what this responds by posting it into the browser so this external adapter is actually an api thats wrapped around an api looking at this api url we can see that the last endpoint is price and the parameters are fsim and t sims our adapter makes it a little easier to read by accepting from and to as parameters now that we know what it does lets see how its done and well do this by updating it to our own new external adapter we want our new adapter to get weather data we want to get the temperature of boston to be returned through this external adapter for this were going to use the open weather api this will also require an api key so well get to see some simple api authentication first lets sign up for a free key for this api you have to wait for the key to become active so after you sign up feel free to take a quick 10-minute break and come back once we have our account verified we can find our api key here well need this later lets look at the documentation to see what the url looks like by looking at the documentation we can see the url is quite simple we can see the endpoint is weather it has a queue parameter which is going to be the city and an app id which is going to be our api key lets see what this api responds with great we can see that we get a lot of good weather data here were going to be looking at this temperature field which returns the temperature in kelvin so now lets adjust our existing external adapter to account for this now there are a lot of files in here but there are two that we really care about app.js which is some simple boilerplate for running an express node.js server we can ignore this file since we wont be making any changes to it all of our adjustments are going to be in index.js the external adapters package adds some nice functionality to make it easier to spin these up this is a helpful function for getting custom errors now were getting into the interesting part previously we used base and quote or from and two to get the price from ethereum to usd in our new api we found that we only need to worry about the city name were going to ignore the api key for now because we want to set this as an environment variable ill talk about that in a little bit so this is where we can set some custom parameters we want our external adapter to be able to take a city and use q city or town to define it now we get to our create request function this will be how we call our new weather api validator object helps you validate chain link request data job or an id make sure we have a job run id then we add our new endpoint which is going to be weather we add our new url which is going to be our open api weather we get our new parameter which is going to be our city parameter and now were going to add our api key were using this process.env syntax so that our api key is an environment variable this way we dont have to store our api key on chain and let other people see it youll want to create a envrc file which has your api key and remember dont put it in source code heres an example of a dot envrc file when we run our server we can make the api key active by by running this command you can learn more about environment variables in the description now we can change our parameters to the two new parameters that we just added our config is going to stay the same and this is how we handle the request remember that we said that we wanted to bring up the result that we wanted and add it to our result response this is where we do that previously the result response was in the json mapping of the two symbol lets look at our api again and find out where it is we can find one of the temperature maps by going through the json we can see that we first have to go to the main key and inside the temp key of that result thats how we get the temperature in kelvin so in our code all we have to do is update this with the path that we need to take to get to our result and thats it there are a couple functions below which help deploy to serverless architectures like the gcp and aws we can ignore those for now but great it looks like were actually all done lets test it out well start our server up with yarn start in our new terminal lets write our new curl command to test it so its going to be the same thing were going to call our local server our data object is going to be a little different though we can use any id we want were just going to use 0 and all we have to do in the data object is add the city of boston and remember since our api key is currently an environment variable the server should pick it up automatically and great when we hit enter we can see we got a response we can see the result is 296.71 degrees kelvin and this is exactly what we wanted when making really robust external adapters you want to add some tests as well this test file is really easy to update to and you can run it with yarn test so now that we have our external adapter done how do we give it to other nodes that we can pull this data into our smart contracts theres a few different things that we can do we can add it to a node listing service like market.link you can ask a node operator or network of node operators to host it or you can run a node yourself now lets say a node is hosting your external adapter lets find out how to use it and get the data into our smart contracts for this example lets say that we built the alpha vantage external adapter alpha vantage is a stock and crypto data api that requires an api key as well lets see if we can get the price of tesla through an external adapter so well come down go to adapters and look for the alpha vantage external adapter were going to be on the coven network here as you can see a couple other people have made outfantage external adapters as well lets try this one we can see all the documentation for the external adapter here and how to use it lets see what nodes support this adapter looks like on the coven network link pool is the only node that supports this adapter to use this oracle and adapter were going to grab the oracle address and the node job id just like in getting any api if youre not sure how to make an api call solidity be sure to check the documentation or one of our earlier videos but again were going to grab the node job id and put it where it says id in our job id were going to grab the oracle address place it where it says oracle i already had this pre-populated as you can see the code for using this external adapter is almost identical to using any api the only difference is we dont see a url posted in our request price method so we build our request tesla price method which would be equivalent to our request ethereum price in earlier examples instead of passing a url we just pass the parameters and path of this new external adapter we can get the parameters of the url by going to the documentation of the adapter or we can go to the documentation of the actual api these parameters that were going to be adding is what is going to show up in our data package the copy path is where in the response string we want to get the data from which we can see in the api return its going to be global quote price because we want to get the price of tesla in this case we used ibm were going to add that path and then were going to multiply it by 1000 to get rid of all the decimals and then were going to send it lets compile this and run it as always add link and then request the price and we can see we have the price for tesla in our smart contract now using an external adapter in your smart contract is no different than using any other adapter you can deploy this code in remix in the description below if youre looking to see a good list of existing adapters that a lot of professional node operators use also be sure to check out the external adapters.js model repo it contains support for a number of popular apis already and has tons of documentations below to get deployed quickly as you can see external adapters allow for unlimited flexibility and customizability to your smart contracts and remember if you build a really cool external adapter be sure to tag it with chain link and share it on social media theres unlimited external adapters that have yet to be built now external adapters are really good for getting really customized data but what if you dont really want customized data maybe youre thinking you wish there was just a default solution for really common data that a lot of people would use for example pricing data or other pieces of data used in defy maybe you want to use the same simple easy to use tool thats powering a lot of the top projects in d5 right now well im glad you asked because well be explaining one of chain links simplest and yet most powerful and effective tools in the next video ill see you there you Chainlink is an infrastructure designed to be unlimited in how customizable it can be. External adapters are open sourced packages that allows Chainlink nodes to transform and receive data however they like. With external adapters you can do: 1. API Authentication keep private API password keys private 2. Private, low latency, and/or high throughput off-chain computation to save gas. 3. Write data to other blockchains interoperability. 4. Any desired functionality that isn’t covered with the core adapters This allows your solidity smart contracts to have access to data that they never would have before. This video goes over: 1. What an external adapter is 2. How to build one 3. Who should host it 4. How to use one Blog post: -----Updates----- None, Happy Coding! Chainlink is a decentralized oracle network that enables smart contracts to securely access off-chain data feeds, web APIs, and traditional bank payments. Chainlink is critical to connecting the blockchain ecosystem to the rest of the world. Code setup: Install Yarn: Install nodejs: Nodejs External Adapter Template: Nodejs External Adapter Template with the completed weather API code: External adapter documentation: Learn more about Environment Variables: Remix kovan from ending: Important Commands/APIs: curl -X POST -H content-type:application/json --data { id: 0, data: { from: ETH, to: USD } } API: Node Listing Service: Market.link: APIs used: OpenWeatherMap: Alpha Vantage: CryptoCompare: Learn more about Chainlink : Documentation: Discord: - Very active and developer focused! Website Building DeFi Applications: Twitter Telegram ChainlinkEngineeringTutorials,
movimientoantipiquetero.com.ar