avatarAlex Roan

Summary

This context provides a step-by-step guide on how to deploy an Ethereum Smart Contract using the Truffle Suite, a popular framework for developing full-stack Decentralized Applications on the Ethereum Network.

Abstract

The context is a comprehensive tutorial that explains how to use the Truffle Suite to develop and test a Smart Contract locally before deploying it to a public Ethereum testnet. The tutorial is divided into six steps: setting up, creating a project, writing the Smart Contract, compiling and migrating, testing, and deploying to the testnet. The tutorial provides detailed instructions on how to set up the development environment, including installing NodeJS, NPM, a text editor, Truffle, and Ganache. It also explains how to create a new Truffle project, write a simple Smart Contract in Solidity, compile and migrate the contract, test it using Solidity tests, and deploy it to the Kovan testnet using Infura. The tutorial includes code snippets, screenshots, and links to additional resources.

Bullet points

  • The context is a tutorial on how to deploy an Ethereum Smart Contract using the Truffle Suite.
  • The tutorial is divided into six steps: setting up, creating a project, writing the Smart Contract, compiling and migrating, testing, and deploying to the testnet.
  • The tutorial provides detailed instructions on how to set up the development environment, including installing NodeJS, NPM, a text editor, Truffle, and Ganache.
  • The tutorial explains how to create a new Truffle project and write a simple Smart Contract in Solidity.
  • The tutorial provides instructions on how to compile and migrate the contract, test it using Solidity tests, and deploy it to the Kovan testnet using Infura.
  • The tutorial includes code snippets, screenshots, and links to additional resources.

Deploy an Ethereum Smart Contract in 6 Steps

Tutorial: How To Develop, Test & Deploy An Ethereum Smart Contract To Public Testnet

Truffle Logo by www.trufflesuite.com

The full working project code is available on Github.

Introduction

This article explains step-by-step instructions on using the Truffle Suite to develop and test a Smart Contract locally. Codebase checkpoints throughout this tutorial let you see what your code should look like at each stage. They look like this:

Codebase checkpoint: version x.x.x

Once we’ve tested our code locally, we will deploy it to a public Ethereum testnet and check it out on Etherscan!

We’ll do this in 6 steps:

  1. Setup
  2. Creating Our Project
  3. Writing The Smart Contract
  4. Compile and Migrate
  5. Testing
  6. Deploy To Testnet

1. Setting Up

Node & NPM

Follow instructions for installing NodeJS on your machine. NPM comes bundled with Node by default. For this walkthrough, I’m using Node version 9.10.0 and NPM version 5.10.0.

Text Editor

Ensure you have an up to date text editor installed. I’m using VSCode.

Truffle

Truffle Suite is a popular framework for developing full-stack Decentralised Applications on the Ethereum Network.

We want to make sure we have Truffle installed globally, so in the terminal run:

npm install -g truffle

Ganache

Ganache acts as our local Blockchain to deploy and test the contract functionality locally before deploying it to a public testnet. Follow the install instructions on the Ganache Webpage.

2. Creating Our Project

Navigate to your preferred workspace and create a new directory in which our Truffle project will reside, I’m using the name truffle-smart-contract/:

mkdir truffle-smart-contract
cd truffle-smart-contract/
truffle init

When finished, you should see something similar to the output in figure 1.

Figure 1: Initializing truffle

Folder Structure

Open up the new project directory in your text editor. Your folder structure should look like figure 2. There should be 3 subdirectories: contract/, migrations/ and test/; and a file: truffle-config.js.

Figure 2: Folder structure
  • contract/ is where our Solidity Smart Contract code is stored. Truffle knows to look here for .sol files to compile and migrate to the Blockchain.
  • migrations/ is where our migration logic resides. Here we can describe the steps needed when deploying our contracts so that they are correctly deployed.
  • test/ is where we will write for our Smart Contracts to ensure they function as expected.
  • truffle-config.js contains information about networks, compilers, file locations and other custom configurations for the Truffle framework to know where our things are. More on this file later…

Codebase checkpoint: version 0.0.1

Connecting Ganache

Open up Ganache, which we installed earlier, then click “Quickstart”. This will start up our local Blockchain. Your window should look something like figure 3.

Figure 3: Our local Ganache Blockchain dashboard

The public addresses you can see are the first 10 addresses that Ganache has provided to interact with the Blockchain. Ganache has loaded each of them up with 100 ETH. We’ll use these later.

Along the darker navigation portion at the top of the window, you can see a row displaying details about the Blockchain: Current Block, Gas Price, Gas Limit, etc. Make a note of the Network ID and RPC Server details and head back over to your text editor.

Open up truffle-config.js and delete everything currently in there. Paste in the following from figure 4.

In figure 4, we define where Truffle needs to look to find our local Blockchain. On line 3 we define a network called development . Use the Network ID and RPC Server details we made a note of from Ganache to fill in host, port and network_id.

Let’s go back to our terminal to make sure that Truffle can connect to our Blockchain. Run the following:

truffle test

You should see something similar to the output in figure 5.

Figure 5: Testing the Blockchain connection.

Great stuff. Our workspace is set up to start interacting with our local Blockchain. We can now start developing our Smart Contract.

Codebase checkpoint: version 0.0.2

3. Writing The Smart Contract

Let’s head back to our text editor and create a new file in the contracts/ folder called HelloWorld.sol. Figure 6 shows the HelloWorld code, so paste this in.

Line 1: Version

pragma solidity defines the version of Solidity to use when compiling the Contract. I’m using solidity version 0.5.0 and above, you may need to check which version you’re using so that yours compiles correctly. Do this by heading back to the terminal and running truffle version.

Line 3: Contract

Here we define our Smart Contract name: HelloWorld. All functionality for this contract is contained within the trailing curly bracket.

Line 4: State Variables

Here we define a state variable called greeting, of the string type. This is the greeting that our getGreeting function will return.

Lines 6–8: Constructor

The constructor is called when the Smart Contract is initialized. In ours, we’re setting the greeting state variable to be “Hello World”.

Lines 10–12: getGreeting() Function

Our getGreeting function returns the value of the greeting state variable, which was set in the constructor. The view keyword means that the function doesn’t make any state changes during its execution.

Codebase Checkpoint: version 0.0.3

4. Compile & Migrate

This section will get our new Smart Contract compiled into machine code and deployed to our local Blockchain.

Compile

Head over to your terminal and run the following:

truffle compile

You should see something similar to the output in figure 7.

Figure 7: Compiling the Smart Contract

There should now be a new folder in your root directory called build/. This is where your compiled code sits.

Migrating

Migrating is the process of deploying compiled Smart Contracts to the Blockchain.

In your text editor, create a new file in the migrations/ folder called 2_deploy_contract.js. Here we will instruct Truffle on how to deploy our newly created Smart Contract. Paste the code from figure 8 into 2_deploy_contract.js:

Run the following in your terminal to migrate your Contract:

truffle migrate

You should see something similar to the output in figure 9:

Figure 9: Output from truffle migrate

Awesome. Your contract has been deployed to your local Blockchain! Head over to ganache and take a look at the first row from the accounts table. You should see that the ETH balance has dropped slightly and that the number of transactions has increased above 0, shown in figure 10.

Figure 10: First account

The ETH balance drops because it costs GAS to deploy to the blockchain, and the increased transaction count represents the transactions that were used deploying the contract.

Codebase checkpoint: version 0.0.4

5. Testing

To ensure our Smart Contract works properly, we need to write some tests. We’re going to write a Solidity test for our HelloWorld Contract to ensure that when we call getGreeting() the correct response is returned.

Head over to your text editor and create a new file inside the test/ folder called TestHelloWorld.sol. Paste the code from figure 11 into the file.

Let’s run the test! Run:

truffle test

You should see output similar to figure 12 confirming the test has passed:

Figure 12: Tests passing

Codebase checkpoint: version 0.0.5

6. Deploy To Testnet

To deploy Smart Contracts to a network, we need to own Ether on that network. Fortunately on testnets, obtaining Ether doesn’t cost anything.

Obtaining Ether

We’re going to use the Kovan testnet. To obtain Kovan Ether head over to the Kovan Faucet and sign in. Open up Ganache and copy the first account's public key address in the list, you can see mine in figure 10. (Note, the public key is the one that is displayed in plain bold text, as seen in figure 10, do not copy the private key). Paste the address into the chat, as shown in figure 13 and send.

Figure 13: Pasting Ether Address

You might need to wait a few minutes, but eventually, you will see a response like the one seen in figure 14 informing you that Ether has been sent to your address.

Figure 14: Confirmation of Ether Sent

Infura

Now we need Truffle to be able to access the Kovan network. A service called Infura makes this easy for us. Go to infura.io and create an account.

Once logged in, create a new project. I’m calling mine truffle-smart-contract. Click View Project in the dashboard. There should be a section called KEYS, as seen in figure 15. Make sure that you select KOVAN in the ENDPOINT dropdown.

Figure 15: Project View

The ENDPOINT data greyed out in figure 15, is what we need, but we need to add a few dependencies to our truffle project.

In your project's root folder, create a new file called package.json and paste the contents from figure 16 into it.

Head back to the terminal and run:

npm install

This may take some time, but once finished head over to your text editor and open truffle-config.js. Edit the file so that it looks like the contents of figure 17.

On line 2, change the text value to equal the private key for the first account in your Ganache accounts list (use your private key this time. you get this from clicking on the key icon on the left side of the row. Seen in figure 10).

On line 3, take the ENDPOINT data copied from Infura and paste into the text value replacing endpoint-goes-here.

Notice the new network configuration on lines 12–24.

Please note: it is NOT good practice to place private keys in version controlled files, we are only doing so here for the purpose of this tutorial.

Migrate

Now we’ve got everything in place to migrate, let’s do it! Run:

truffle migrate —-network kovan

Initially, the migration runs a dry run to ensure the account has enough gas and ether to deploy the contracts. Once that’s finished, it’ll go straight onto deploying your contracts. You should see output similar to figure 18.

Figure 18: Deployment to Kovan output

The highlighted contract address is the address that our Smart Contract has been deployed to. Go to kovan.etherscan.io and search for that contract address and you will see your newly created contract on the Kovan Blockchain! Well done! Check out mine here.

Test

Much like the previous migration, testing on the Kovan network requires us to pass the network name when running the terminal test command. Run:

truffle test --reset —-network kovan

Codebase checkpoint: version 0.0.6

Done

Well done, you developed, tested and deployed your very own Smart Contract to the Kovan Ethereum testnet. I hope this will inspire you to experiment more with Truffle, Ethereum and Blockchain in general.

Happy coding.

Additional Resources

Ethereum
Smart Contracts
Blockchain Development
Blockchain
Technology
Recommended from ReadMedium