The Truffle framework - Discover Ethereum & Solidity | Ludu

The Truffle framework

by Tristan Edwards Tristan Edwards

4 thanks

  • While it's good to know how to manually compile and run Solidity code, it gets tedious for larger apps. Thankfully, there are som great DApp frameworks that take care of this for you – the most popular one being Truffle.

    +
  • The Truffle website
    +
  • Throughout the rest of the course, we'll be using Truffle to build our DApp following their best practices.

    +
  • Creating a Truffle project

    +
  • To install Truffle globally on your computer, run the following command:

    +
  • Loading code...
    +
  • After the installation is done, you should be able to run truffle version to see your installed version.

    +
  • Make sure that your Truffle version is at least v4.0.0.
    +
  • Alright, now let's again re-create our HelloWorld example app from scratch (don't worry, we'll soon build something new) – this time using Truffle!

    +
  • Loading code...
    +
  • Once you've created a new folder for your project and navigated to it, the truffle init command will automatically generate a boilerplate file structure for your DApp.

    +
  • What your generated file structure should look like
    +
  • Let's go through the generated files and folders one by one so that we know what's going on!

    +
    • 1

      contracts: As you could've guessed, this is where your app's Solidity contracts (.sol files) should go.

    • 2

      migrations: The files in this folder will orchestrate the deployment process, similarly to what we did in the last chapter. Thanks to Truffle however, we get a lot of functionality for free so that the files can be written in a much simpler way.

    • 3

      test: Having automated tests for your contracts is crucial! They go in this folder, and can be written either in Solidity or JavaScript.

    • 4

      truffle.js: A configuration file for your project's settings.

    • 5

      truffle-config.js: This is actually exactly the same file as truffle.js. Some Windows systems can run into conflict problems when executing the generic truffle command, and should then use this file instead.

    +
  • HelloWorld in Truffle

    +
  • Let's add our HelloWorld.sol contract that we wrote earlier in the contracts folder. Again, here's what it looks like, in case you had forgotten:

    1
  • Loading code...
    +
  • Next, as we saw in the previous chapter, we need to deploy this contract to some address in order to interact with it. With Truffle, we just add a new file to the migrations folder to take care of this process. It should be prefixed with a 2 (so that it runs after 1_initial_migrations.js), so let's call it 2_deploy_hello.js:

    +
  • Loading code...
    1
  • Ah, isn't that nice? The whole process of compiling your contract, connecting to the network, and spending ether to deploy the bytecode is summarized in just a few lines! That certainly saves us some time!

    4
    Tristan Edwards
  • Now, in order to deploy our contract, we need to first specify which network we want to connect to. Remember, there are several Ethereum networks out there (the real one being the "mainnet"), but in our case we simply want to connect to our own local testrpc network.

    +
  • To specify this, we open the file truffle.js and add the details of our network in JSON format, as documented on Truffle's website.

    +
  • Loading code...
    +
  • After this, we simply run truffle migrate in our console (make sure that your testrpc instance is still running in the background as usual so that Truffle can connect to it)!

    +
  • Loading code...
    +
  • Success!
    +
  • You should now see the address that your contract was deployed to. In my case, the address for the HelloWorld contract is 0x729a7bd62c506ce8dfb3a41086b533d0bdb064d5, but yours will obviously be something different.

    3
    Tristan Edwards
  • Using the Truffle console

    +
  • Finally, if we want to interact with our contract, we can use the Truffle console:

    +
  • Loading code...
    +
  • +
  • By entering the console, you will have the HelloWorld contract object available as a global variable, the web3 library is automatically included and connected, and all promises will automatically be resolved.

    +
  • When inside the console, you can access your deployed contract instance by typing HelloWorld.at(YOUR_CONTRACT_ADDRESS). In my case, I would access the contract by running HelloWorld.at("0x729a7bd62c506ce8dfb3a41086b533d0bdb064d5").

    +
  • +
  • Using that contract instance, we can then call our getMyName function using the syntax HelloWorld.at(YOUR_ADDRESS).getMyName.call().

    +
  • We use the .call() method whenever we call a function that does not write anything to the contract. In other words, for functions that use the view or pure modifier, just like getMyName.

    +
  • Loading code...
    +
  • And there's the expected result of the function!
    +
  • Testing your Truffle code

    +
  • As I mentioned before, testing your Solidity code is crucial. You don't want to run into a situation where funds get stolen because of a bug in your contract.

    +
  • In Truffle, you can write tests for your smart contracts in both JavaScript and Solidity, each with their pros and cons.

    +
  • I've found that Solidity tests are usually great for simple unit tests that call specific parts of your code, whereas JavaScript tests are great for putting yourself in the user's shoes and testing wider use cases (a.k.a. integration tests).

    +
  • In this chapter, we'll stick to JavaScript testing. In your test folder, create a new file called helloworld.js (you can name it whatever you want).

    +
  • +
  • Truffle uses the Mocha testing framework behind the scenes, but uses contract() instead of describe() as its main function.

    +
  • In our code, we simply fetch the deployed contract instance using HelloWorld.deployed() (this works similarly to HelloWorld.at(YOUR_ADDRESS) in the console). Then we call the getMyName function and match it against the expected value ("Tristan").

    +
  • Loading code...
    2
    Tristan Edwards
  • Running our tests is just as simple as you'd expect:

    +
  • Loading code...
    +
  • +
  • To prove that the test is working as it should, you can try changing the expected value in your test to something else (like Bob) and watch the test fail as you run it again:

    +
  • Loading code...
    +
  • +
  • Well done! Let's write one more test, this time for the writable changeMyName function, so that we get the hang of it.

    +
  • This time, when we call changeMyName, we cannot use the .call() method, since it doesn't have either the view or pure modifier.

    +
  • Loading code...
    +
  • Run truffle test again, and you should now see both tests passing:

    +
  • Hooray!
    +
  • We've now gone through the basics of setting up a Truffle project. In the next chapter, we will start working on a more serious application, a decentralised Twitter clone (you can see the final result here), to see what pitfalls to avoid when dealing with more advanced smart contracts.

    +

Did it help you? Click to say "thanks" to the author!

Next lesson: Creating Tweether

Want to create your own course on Ludu?

Back to lesson