Thomas Hodges ~ Build Smart Contracts that Connect to Real World Data, Events & APIs
Ethereum Denver·Wed, Oct 9, 2019, 12:00 AM
Transcript
[Music] all right hi everyone my name is Thomas I work on chain-link which is which is a decentralized oracle network I also write the documentation and I help node operators get started running nodes and I write chain link contracts which is what we're going to be doing here today so as a brief overview we're gonna talk about what chain link is we're gonna prevent present a sample problem and we're actually gonna deploy and use a chain link contract then we'll end with questions so in order to understand chain link we need to first understand that smart contracts cannot access external data on their own and this is a big limiting factor for a smart contract adoption because it means essentially right now smart contracts can only do token transfers so one of the key problems that we're solving with chain link is getting external data to smart contracts so there are like thousands of api's right now that power web applications and what we do is we enable smart contract developers to get the data from those api's to their smart contracts so that now you can develop decentralized applications powered by the same reliable api's that currently power existing web applications then there's also the fact that not everyone wants to be paid in a cryptocurrency just yet so we make it possible to pay out through traditional payments providers upon the execution of a smart contract and then along those same lines we make it easy to connect to other block chains so that you can execute a smart contract based on the event of another blockchain or you can send an output to another blockchain based on the execution of a smart contract so we enabled this connectivity through what we call external adapters and external adapters can be written in any programming language that you want they simply need to respond to civic request from a chain link node and respond with a data payload in a way that the chain link node can consume so because of that it's really easy to get started on extending the capabilities of chain link without having to know anything about aetherium or even solidity so to get a bigger picture chain link is a decentralized oracle network where you can select as many Oracle nodes or chain link nodes as we call them as you like and then as such you can select as many data sources as you like to trigger the execution of your smart contract and then upon that smart contracts execution you can also use chain link to payout in a traditional fiat currency through whatever a payment provider you prefer so we're going to use a use case here today that retrieves the current price of etherium and feeds that into a smart contract so when a smart contract knows the market price of some asset it can do things a little bit more interesting like basing a payment off of the value of that asset instead of just blindly doing a token transfer for some amount so for example let's say that Alice has a website and she's contracted Bob to do some SEO work for her if Bob can get Alice's website to the first page of most major search engines then she'll pay him $50 worth of aetherium in order for this contract work we need to retrieve the ranking of Alice's site across multiple sources and then depending on that result we need to then look up the current price of aetherium once that price is fed back into the smart contract then we can pay out Bob so they'll make a smart contract that essentially says check the current ranking of Alice's site and if that value is less than let's just say 10 for an example then retreat the current market price of etherium when that market price of etherium is fed back into the contract paid Bob at such-and-such address $50 worth of etherium and then we can send the remaining over to Alice so Alice will fund the smart contract with some aetherium so that all parties can know how much of their name is present on the contract and then a chain link report request can be made which notifies the chain link network that this request came in so chain link can retrieve the ranking of Alice's site across multiple data sources an aggregate answer can be sent back to their smart contract where the logic of that smart contract can determine if that was valid for their agreement or not and if so then the current price of aetherium can then be retrieved through another chain link request again that answer can be aggregated aggregated and sent back to their smart contract where we can fulfill the payment to Bob and send the remaining over to Alice so this smart contract enables chain link connectivity by being a chain linked contract which means that it inherits the chain link contract that we have developed that gives it the ability to notify the chain link network that a request came in and then it also fulfills the methods necessary to receive the answer so we'll commonly refer to that as a consuming contract we're going to be working on using a chain link contract here today so there are several ways to create a chain link contract you can use a web-based IDE like remix or superblocks or you can use a local development environment like Zeppelin OS or truffle a web-based IDE is really great for getting started without a lot of setup because you don't need a development computer to write and deploy and use smart contracts and setting up a local development environment is really great if you have like a more complex project and you want to in like integrate some testing into your solidity project so the focus of today's talk we're just going to be using remix to deploy a smart contract and then you can use what we learn here today to whatever development environment you're most comfortable with so before we get started I'm going to display several links and things that you would need to copy on the screen instead of having to worry about that on each individual slide this is the only link that you really need to copy or take a picture of whatever you need to do I have on this page it's broken down per slide anything that you would need to to copy will be on the slide so y'all can get pictures what's a good all right so we're going to use the R obstinate s Network for this example because it behaves a lot like maintenance and we can take advantage of ENS so the first thing that you're going to want to do is install meta mask if you don't already have it and switch to the rob stand test network meta mask is a wallet that acts as an extension in your browser it just makes it really easy to change the to the different test networks and get started using contracts right away then once you have mended masks configured you're going to want to obtain some Rob's tonneau theorem which we have multiple Rob stand faucets available again these links are on that workshop page and then once you have Rob stone etherium you need to obtain some robson link which we have a faucet set up for that it's really straightforward you just paste in your address you solve a CAPTCHA and you click the button and it'll send you 100 tests Rob stone link on the test networks we use one link per one request just to make it easy and straightforward so yeah once you uh once you have the link token you need to add it in meta mask so to do that you should be using that workshop link because you can just easily copy the address to our link token and you add a custom token into meta mask also since we use ens you can click send and type in the ENS address on the screen and it should automatically resolve to the link token address but again you should be using that workshop page then in your browser you're going to navigate to remix which is a web-based development environment for solidity if you use the link on our workshop page it should automatically select the compiler version for you otherwise you'll need to manually select compiler version 0.4 0.2 4 because that's the version that we've locked the pragma line in our chain link contract to then once remix is opened up you want to grab the example contract that we have in our chain link repo remix opens up with an example contract ballot dot Sol you can overwrite the code in that and it'll still work fine or you can just create a new contract and remixes interface the end result is the same you just want to make sure that you get all that code into remix then once you have the code in there you want to click on compile and you should see the green contract names on the sidebar in remix if not then double check your compiler version double check that you can that you copy and pasted everything in there correctly but otherwise it should compile fine and it should say like should have all the green contract names in the side Bower in remix so then once the contract is compiled you'll go to the run tab and remix and you want to check and make sure that the environment is set to injected web 3 because I think if it's like the first time that you open a remix it may default to the JavaScript VM but if you use injected web 3 then that's ensuring that you're gonna use meta mask and then essentially all you have to do is just click on deploy because a Ralston consumer is should already be selected at the drop-down once you click on deploy you'll just have to approve that contract deployment in meta mask and then you'll have to wait a little while for that contract to deploy but once it does deploy remix this interface will update to display the contract name and then there's the little page icon button that you can use to copy and I was playing around with it this morning and it doesn't seem that that button is copying it correctly so you may actually have to open up that transaction in like ether scan to obtain that contracts deployed address just so that we're aware for this workshop now let me go back one and then once that contracts deployed you're gonna want to send at least one linked token to that contract address because the requesting contract has to be funded with at least on RuPt in one link to make a request and then while you're waiting on that token transfer to complete you can grab the input parameter for our request etherion price method basically what we're doing here is we're using a job ID and then we're specifying a currency for the price of aetherium to be converted to and we're gonna get into what that job ID means here in a little bit but essentially once that token transfer is complete you should be able to click the request etherium price method in remix and if you don't get this error message then you made a chain link request this is the most common error that I see when using remix and it essentially means that that contract has not been funded with enough link to make the request so if you do ever see this error message check and make sure that that token transfer was successful so that that requesting contract is funded with link so now assuming that the request didn't go through this is what just happened you're requesting contract made a chain link request to the Oracle contract residing on Rob stone and the chain link network is monitoring that Oracle contract for that job ID that you specified so that when a request comes in with that job ID it knows to perform some work in our case that work is to retrieve the current price of aetherium which it will do and then return that value back to the Oracle contract which then returns the answer back to your chain link contract so by now as long as the Rob's 10 network is working correctly chain link network should have fulfilled that request and you should be able to see the updated price of aetherium by clicking on the current price button while interacting with your contract so now that your contract knows how much the price of aetherium is it can do interesting things like deposits and transfers based on that value similar to the example that we talked about with Alice and Bob so now let's actually do this in a virtual machine see the screen there we go so this is the it's gonna be awkward this is the documentation page and this looks like this is unreadable this is the interface for remix and I've essentially cheated a little bit here because I don't want to type in my passwords live because I'm just gonna mess that up so I've already got meta Mask installed and configured and set up for the Rob stand test Network essentially all that we need to do now is grab the Rob stand consumer contract and paste it in to remix so this is where we compile it and that is completely unreadable but we can see all the green contracts here so we know that it compiled successfully this kind of looks right from what I can read and we just hit deploy and then we'll see some nuances with meta masks that are affecting us today so I just have to hit edit and select a price and save so that it'll actually deploy so right now what's happening is I signed a transaction to deploy our example chain link contract and I would really have liked to be able to show you the the code within here but that's that's completely unreadable right there and then another problem is once this interface updates I'm not going to be able to click on that little page icon so I'm just gonna middle click on this link here for the deployment and we're just gonna watch this transaction confirm and then this is how I'll get the actual deployed contract address yeah oh yeah just an margin yeah thanks awesome so here we got the deployed contract address and it's like it's bigger but it's still not really readable so essentially here now we just want to send one link to it there's no way that I can enlarge this but I think that it's pretty easy to figure out what I'm doing another nuance here is you have to click on the transaction fee for metamath to update the gas amount before a token transfer and just confirming that transaction and then while we're waiting on that transaction to confirm let's see if I can it's a little bit better anyway while I'm waiting on that transaction to confirm I'll go back over to the workshop page where I can just copy this input parameter and I really can't I think it's this one we'll see let's make sure that transfers gone through and hasn't gone through yet so I can show you this error message the contracts not funded yet but then once this token transfer completes while we're waiting on that to confirm then I should be able to make the request I'm usually really unlucky with Robson sometimes when I really need it to confirm fast there it goes so now we can see now that the contracts funded we can make the request immediately again I'm just updating that transaction fee so it actually goes through now what's happening is that request is being sent over to the Robson Oracle contract and we have a Oracle node that we can use during this hackathon to monitor that Oracle contract and it'll fulfill requests so I still can't really make it too big here right now I'm waiting on the Oracle contracts transaction to confirm on the blockchain so even though that request just confirmed it doesn't mean that we got a response right away because the Oracle has to see that response or see that request reach out to the endpoint and make a new transaction and that's what we're waiting on now to confirm but we can see here that we're just specifying the URL within the solidity of request method and then now assuming everything's working right it hasn't updated yet but at least that shows that I haven't hard-coded a number in there and this is the actual request method that we called so with the chain link contract you can specify at URL and then you can also specify a path which is the path of the JSON response that you want the chain link node to parse to return back to your smart contract so in our case we just want the currency and still waiting on that con on that confirmation in our case we're specifying the currency and that's that the actual path that we want the chain link node to parse and then we're actually going to multiply that value by a hundred since solidity doesn't handle decimal values and again I'm getting really unlucky with Robson confirmation times oh yeah essentially once the chain link nodes fulfillment does come back on chain we'll see a value that's been multiplied by 100 here should be any second now No we can give it a few more minutes let's uh let's open up to questions now while we're waiting on that confirmation yes the he asked who's paying the gas for the for the Oracles and it's the Oracle Note operator that pays the gas and they take the payment in link still no another question now alright I'm gonna call them just really unlucky with this confirmation yeah oh got a question so the question is how do you trust the Oracle data so our method of doing that is the more Oracle nodes that you select the greater assurance that you have that the data has been retrieved correctly because you have multiple independent node operators that if they've even even if they've only reached out to a single endpoint you you get a better assurance that the majority of them are not lying if you have multiple data sources you get a better assurance that the data is correct because you have multiple node operators each gathering answers from multiple sources yeah I'm gonna call that do we have yeah so he has a hard-coded the URLs into the contract and what happens if they change yeah in this example you would have to redeploy but the like the more modular approach would be to use that URL as like an input parameter and then you could also specify the path as an input parameter as well since usually when you change URL you may want a different path to be returned that basically everything there could be an input parameter or that could be hard-coded into the job that we're specifying yeah we're not gonna get a response oh no we got it just took a while Thank You Robson so any other questions so the question is how do you deal with multiple nodes worldwide trying to get something like price data so in the bigger picture as a requester you're going to like select these nodes that they are saying that they are making this data available so like they you will essentially propose a service agreement using our own terminology they will accept that service agreement or they will reject it based on additional parameters that you specify so once they've agreed to it then they have actually made a signed transaction that they will fulfill those requests and if they don't fulfill the request then they get penalized like they can lose that deposit they can get their reputation harm things like that the comment is each note is asking for a price at a different oh so you mean like they're they're retrieving at price and because of volatility the price changes so yeah when the nodes answer back like this is just a single note but when multiple notes answer back you as a requester also select a aggregation function for all those answers to be ran through so you can take an average you can take the first responder anything you want any other questions no all right if not then that's the end of the talk we're doing a hackathon so we are here to help you if you're making a chain link contract for the hackathon we'll be at the booth this example is like a really good place to get started if you have some custom like endpoint that you want just let us know and we'll see if we can get it set up in time for you to be able to demo for your project so that's all that I got thank you very much
Automatic transcript — names and jargon may be misspelled.