Chris: What is Truffle and What Can you Do With It? | ETHDenver
Ethereum Denver·Tue, Oct 9, 2018, 12:00 AM
#BUIDL the future at https://ethdenver.com This talk was part of the ETH Stage at ETHDenver 2018 on 02.16.2018. ETH Stage is ETHDenver’s Main Stage which features talks and workshops by top leading blockchain impactors, influencers and experts. ETHDenver is the largest Web3 #BUIDLathon in the world (fka hackathon) for Ethereum and other blockchain protocol enthusiasts, designers and developers. The community attending ETHDenver shows up to contribute to the global blockchain ecosystem, whether by #BUIDLing decentralized applications (dApps), contributing to or starting infrastructure level projects, writing documentation/tutorials/white-papers, designing UI/UX, or info-graphics, or to learn, connect, and get more involved. So bring your inner awesomeness and some friends. We’ll provide mentors, resources, and all of the educational content you can consume. Follow ETHDenver on Twitter: https://twitter.com/ethereumdenver Follow ETHDenver on Instagram: https://instagram.com/ethdenver Join the Bufficorn #BUIDL Brigade on Opensea: https://opensea.io/collection/bufficornbuidlbrigade Join the conversation on Discord: https://discord.gg/sporkdao Subscribe to our channel: https://www.youtube.com/ethdenver
Transcript
who here is with consensus who here has come out and is with consensus awesome and who here at these consensus is awesome so in my mind one of the coolest things about them is they are building a lot of the infrastructure that is gonna make aetherium truly scalable whether it's meta masks or you port for for browser and you use your IT management all this good stuff today we're gonna hear from Chris from consensus about tooling so very fundamental things that programmers need to more effectively and easily write programs and probably a lot more on top of that so great Thank You Kent that was not fun hopefully everyone's day's going well so my role at consensus is as a back-end and smart contract developer I don't actually work on the truffle team but I use truffle every day in my development practice so I was going to talk a little bit about what truffle is what you can do with it and then show you some live coding examples which is why it was so important that we get the computer set up one of the big things that I think is extremely important as we just saw with all the security vulnerabilities that are possible with solidity is testing and truffle helps you do testing as well so truffle is a aetherium and smart contract development framework it contains tools for compiling linking deploying managing your contracts automating your testing deploying and running migration scripts network management so keeping track of which contracts are deployed on your local networks which contracts are deployed on test networks which test networks they're deployed to and also installing running external packages and interacting with your contract so you can actually interact with them on your local blockchain one of the great things about truffle is that it also comes with boxes so boxes are premade projects you can start with the vanilla box completely blank slate where you can also do a truffle unboxing to start with a repo that has some tools built into I'm gonna be using one called truffle token today but for the hackathon I would definitely recommend checking out the truffle website for their boxes they have some react boxes they have some react Redux boxes that can help you get your project going quickly so I think jump right into coding I can get to the right place and make things how do we fill that that size good ok so starting from scratch we are going to make a new directory and I mentioned the truffle unboxing command this is the command for that tutorial token one that we need while that's downloading as I said there are ones for react there are ones for using react and redux truffle also make several other tools that the truffle team makes ganache which is a local test block chain that you can deploy to I'll show you that a little bit later and also a new tool that they made called drizzle which I had don't have any experience with myself but it handles state updates with react and redux interacting with the blockchain so it basically listens to that for you and as expected we don't have internet so when you unbox truffle box you get several things as I said out of the out of the box it starts out with a truffle configuration file this is where you can configure information for how you want to connect to your networks for purposes for this presentation the most important one is just the development Network you can see that we're using the host one two seven port seventy-five eighty forty-five I am actually gonna change that to eighty-five because we'll need that later when I connect to ganache like a lot of NPM projects it comes with your package JSON which you can set up to run custom scripts and also keep track of your dependencies it has a basic contracts folder a basic migrations folder and also a test placeholder folder in the source folder for this truffle box it has a basic front-end app that if I have time I'll get into and show you the web three interactions that come with this one but it's a good starter place that's why I mentioned the react ones earlier they come it comes with a react starter app and can get you started on on building your react app very quickly so I want to start with a basic token sorry let me rearrange my windows here and if the talk earlier this morning about solidity development hopefully some of this will look familiar but just setting up the basic solidity token contract structure here and initially it's not going to do anything for me so this is a very boring project we want to use I'm going to base this off of ERC 20 thank you if anyone has ever live coded in front of an audience it is a lot of fun your fingers stop working for some reason the Zeppelin opens Zeppelin people presented earlier and I also want to utilize their opens Upland contracts for this because I'm building an e rc 20 token since I don't want to rely on the internet I have already installed those modules under my in this project when I set it up so I can simply import those contract or the the ERC 20 contracts here and then inherit it in my contract I want to set this contract up with some basic information about it and then also create my constructor function to do the initial allocation of tokens so this total supply that I'm using here is something that I'm inheriting from the open Zeppelin structure and I'm setting that to my initial supply that I'm setting in my contract so the contract or the ERC 20 token is set up and then initially allocating all of those tokens to the account that instantiated the contract so now I have a very basic contract set up still haven't really done anything with truffle yet so let's get into what truffle does for us other than setting up the project as I mentioned earlier truffle comes with migrations that we can run to launch our contract and deploy it so this artifacts dot require is I believe it comes from truffle and it basically is allowing it to go back and pull that contract code that I've written into the contracts directly directory in that file in the same way that you would do an import or require for an NPM package so that should be all the migration code that I need to get a basic contract setup truffle compile is how I'm going to compile that solidity code if I can and put it into a bill directly so it's going to go through my contracts run so I said I had installed Zeppelin and I may not have actually done that hopefully that works so look looking at this command and the output it has here you can see that it compiles my initial denver token contract it also compiles a base migrations solidity contract that comes with the truffle box and then pulls in all of those dependencies that I imported from the open Zeppelin library to build the ERC 20 standard token and it writes those to the build contracts folder that now appears in my project as JSON files that hasn't done anything yet other than compile those contracts into the JSON objects so that's where running a local blockchain comes into play as I mentioned ganache is another tool that's made by the truffle team it is a local blockchain user interface and will run you can see that I'm running on localhost here 8545 which is why I changed that truffle j/s file earlier and I have a network of the ID of 1337 so now when I run over to my console I can do truffle migrate and that will launch those contracts on to my blockchain running locally on my computer so I can interact with them you can see that it runs the migrations that I wrote the initial migration is the one that comes with the truffle initial project and then that deploy contracts is the one that I just set up and here it deployed my denver token contract to I believe this is the transaction ID and then this is the address where my contract is deployed and if we pop back over into ganache you can see those transactions having taken place here on the blockchain creating those contracts and calling them so now I have contracts living on a local blockchain with truffle console I can interact with those contracts truffle console uses a uses something similar to what you might find with web 3 if you're familiar with that so i want to do denver token dot deployed and we've got a promise so then result sorry I jumped lines in my notes so all I did there was I essentially set that instance variable that I set up initially as my deployed contract so now I can interact with instance as the contract that's running on my local blockchain and if you remember I set up the total supply and set that to the total supply variable that the ERC 20 interface provides for me so I want to check what my total supply is for this contract to confirm that we've launched our token and it's the 20,000 that ice our 200,000 that I set up initially I can also check the balance of the accounts that I'm using so that initial account on ganache is the one that would have deployed the contracts that would be this address right here I need to grab without the return so here I'm calling the ERC 20 balance of function and you'll notice that I'm doing the dot call at the end of it so I'm not actually doing a transaction on the blockchain I'm just calling the and getting the response and as I said when we set up the contract I apply or I allocated all of that initial supply to that first contract that instantiated the contract so you can see that we've confirmed that contract has 20,000 here nothing too interesting other than we have confirmed that those balances are set up the way we wanted it to but because we're using the ERC 20 interface we can transfer tokens to other accounts so let's try that real fast actually I feel like that kind of shows you the functionality of truffle console does anyone want to see blockchain transactions happening on truffle console or should I move into truffle of testing and interacting with the truffle suite that way testing okay because we're using the ERC 20 token contract and interface I don't feel that testing that's really all that interesting so what I want to do is I want to test something special something custom that I'm building to this contract so we set in an initial name for the contract but let's say that someone owns this token contract and they want to be able to change that name so we want to be able to have first off ownership of the contract which is thankfully another open Zeppelin standard so let's import that and get that set up so now my contract is standard token and ownable if I wanted to I could do my great reset from within the truffle console here to redeploy that contract reset the migration and now I can interact with that contract as though it's ownable again there's not much functionality that we've built in there so I'm gonna exit out of truffle console and go over into my testing and build some tests sorry I'm not sure about that I didn't if I had to recompile the code or if I can just do my great reset I think my great reset recompiles those JSON objects something that I find I do every now and then with truffle is go through and delete the build folder and recompile to reset things in there so the question is if I delete build would I delete where the contract is deployed on the chain I don't believe so I believe truffle keeps track of that someplace else relative to the migrations I don't think that's in the build folder truffle allows working with two different types of testing you can do solidity tests or you can do JavaScript tests today I'll show you an example of a JavaScript test so similar to how I set up my migration I'm gonna require that artifact in at the top of my test and then to interact with it if you're familiar with mocha testing this will probably look similar so you see here I'm passing in accounts this would this will allow me to interact with the accounts that are in existence on the blockchain that my test is running on and I want to set up a couple of initial constants here for us to use so my owner is going to be my first account and then we'll have an odd owner account that we'll use later kind of similar to how I set it up on the in the console I do a let contract equals null and then later in each of my tests I need to deploy the contract so I want to be able to set that contract to the instance that gets deployed with each test and here instead of using promises I'm doing a sink and a weight because I find that to be a little bit cleaner syntax similar interaction from what you might be familiar with with web 3 I'm just creating and deploying a new contract in the test before each contract or before each test runs and I'm saying this is from the owner account so that will set that owner account as the owner when the contract gets instantiated I'm practicing a little bit of test-driven development here because we haven't actually written the function changed name but we know that it should allow the owner to change the name the token name you okay so what we have here is a simple test that should call the contract get the original name here on the current name or no I'm sorry we're gonna set a new name and then we're gonna call the contract to change the name from the owner account to this new name so here's my contract instance here's the function that we're going to write change name and here's the value that I'm passing in I want to call this from the owner so that gets passed the only owner modifier that we'll add into that function later then we call the same contract to get the name variable and we assert that the current name equals the new name proving that we have changed that contract so if I run truffle tests right now this would fail because the contract does not have this function built into it so let's go add that function into our contract so I switch back over to trying to give us a little more space here here we go okay so back into our contract code here change name function that accepts a new name we wanted to be public we want only owner because we're inheriting the ownable contract we have that modifier available to us from the open Zeppelin contract standard and we're going to return whether or not it was successful and this is a very simple function we want to take the contract name and set it to the new name that is getting passed in and return true so now that is built into our contract we should be able to switch over to our console and run truffle tests now as I said before the truffle test is not dependent it on any local blockchain or I'm sorry it is dependent on a local blockchain so if I didn't have ganache running I would get an error saying it couldn't find test RPC but it is not dependent on the contracts already being deployed on that blockchain it will deploy the contracts themselves now we get to play a little bit of so I passed in counts here but forgot to use that see if that works better for us so you can see the contraire the test suite initially compiles the contracts and deploys them and then just like your mocha tests runs them we get one passing test it should allow the owner to change the name great now we know that our functions doing what it's supposed to do on the happy path but on the sad path what happens if someone other than the owner tries to change the name let's write a real quick function to test or a real quick test for that so I think it's very important especially coming right after the security talk that you'd think about how to test bad things happening in your contract and make sure that your contracts protect you from those bad things from happening so we're gonna need to throw an error message let's set that up here so this would be if the non owner was allowed to change the name and we're gonna have a new name again and we want original name to equal what the contract gets called with or it gets instantiated with now we're going to try to change the contract name from the knot owner account that we set up so this is going to be our second account that gets passed into our test I'll come back to this line once get the rest of this built out and explain what that's doing I also need to add that function in at the top so I said I'd come back we need - right is EVM exception function up here that's going to test if we're throwing an EVM exception so all this is doing is taking the error that gets passed into that catch block and checking to see if it includes the word revert if our contract fails for some reason it's going to include that revert back to our to our test suite because the etherium are the smart contract kicked it out for whatever reason in this case because it was not the owner that was sending it so we take we try the to change the name if there's an error we're gonna end up in this catch block pass that air into our EVM exception function to check if it contains the word revert because then we know that it's not some I miss named a variable or JavaScript error but rather an EVM exception if it's not then we still want to double check to make sure that something hasn't happened so we're you know putting some extra safety safety boundaries around our test here so I'm gonna get the current name and make sure that the current name does not or still equals the original name and if it does then we're gonna return if not then we want to make sure that we don't somehow end up at this part of the test so we're just asserting false and then passing in that error message that I said up here is not owner was allowed to change the name because then the the name will have changed at this point so let's see how many typos I have in this test so we do have a failing test here and the output of that I'm seeing an assertion error and that it doesn't trigger a solidity revert statement does anyone spot that I made a typo along the way and point me in the right direction so most likely I have a typo somewhere in here that I have a JavaScript error the error message is that the assertion error status for the code that is right here thank you very much in my I didn't have the curly brackets so I didn't have a return statement so that is that was the problem for those of you not familiar with es6 syntax without these this statement automatically returns with those you need to put in the return statement otherwise you get strange errors so that's a very basic how do you use truffle to start a project write your code deploy to a test car test blockchain network interact with it through the truffle console and then write tests I think truffle is a really really great tool it's something that especially around the testing I use every day I think it was a really great way of getting you started for your projects for this hackathon and I am not entirely sure when my talk was supposed to end but that's kind of the end of the coding piece if there are any questions I can try and answer them I should be floating around all weekend for any questions that you have while you're writing stuff yeah so the question is that in some solidity coding patterns with contracts you can use shell contracts or proxy contracts is how I've heard them called before and if whether or not truffle has a way of managing that for you and helping you keep track of of those contracts honestly I'm not sure in in the truffle documentation it says it helps you with linking contracts together and also helps you with libraries so I think it probably does but I haven't dug down to that point thank you I had lots of prep yeah I'll be floating around this weekend I also have some resources for you that I meant to show at the end there's a little bonus in there of soul hint which is a linting and hint tool that you can use to check the quality of your code it looks for problems looks for some security issues I will see where I can post these there's a really great video tutorial that I built are based a lot of this off of that was done by the truffle team by the experts that probably had a lot more smooth coding than I have
Automatic transcript — names and jargon may be misspelled.