How I Compile, Test & Deploy my smart contracts using Truffle

Sneha Thakare
4 min readMay 6, 2022

--

Truffle is a development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM).

In this article, we will see how to start using Truffle to write, compile, test and deploy smart contracts that run on the blockchain.

Installing truffle

To get started, we need to install Truffle on our computer using npm. Open a terminal and use the following command to install it globally.

npm install -g truffle

Create a folder for your new truffle project and initialize it using the command listed below

truffle init
The folder structure in VS code after you initialize the project

Once this operation is completed, you’ll now have a project structure with the following items:

  • contracts/: Folder for Solidity contracts
  • migrations/: Folder for scriptable deployment files
  • test/: Folder for test files for testing your application and contracts
  • truffle.js: Truffle configuration file

For the moment, all we are going to do is uncomment the part of the configuration file (truffle-config.js) where we define the network to be used.

Write a smart contract

You can write a smart contract in a file with a .sol extension and save it under the contracts folder or you can paste an existing smart contract from Remix IDE.

I have another article on writing smart contracts in Solidity linked here.

Compile

Next, we will compile our code using the following command and you will notice a build folder in our project directory post compilation

truffle compile
Check the JSON file under build/contracts

Truffle’s contract artifacts are the large JSON files that get saved in your build/contracts/ directory. Truffle saves our artifacts when we compile and updates our artifacts with address information when we migrate.

Test

Truffle lets you write tests in Javascript and Solidity. It uses the Mocha testing framework for testing the Smart Contract. All test files should be located in the ./test directory. Truffle will only run test files with the following file extensions: .js, .ts, .es, .es6, .jsx,.sol. All other files are ignored.

Here’s a simple test file for Storage smart contract

We need to ask for contracts to interact with, within your test explicitly. We can do this by using the artifacts.require() method, a method provided by Truffle that allows us to request a usable contract abstraction for a specific Solidity contract.

To explain this test code in short

  1. ‘contract()’ is the same as ‘describe()’ in mocha.

2. ‘it()’ is used to create a test.

3. Deploys the contract.

4. Get the value of the variable ‘updatedData’.

5. Use assert.equal() to test if the expected output is the same as the actual output.

To run all tests, simply run:

truffle testortruffle test ./path/to/test/file.js

Deploy

Numbering in the filename denotes the order of deployment of the contract

Deployer function

All migrations must export a function via the module.exports syntax. The function exported by each migration should accept a deployer object as its first parameter. The deployer object is your main interface for staging deployment tasks.

To deploy our smart contracts, we’re going to need to connect to a blockchain. Truffle has a built-in personal blockchain that can be used for testing. This blockchain is local to your system and does not interact with the main Ethereum network.

You can create this blockchain and interact with it using Truffle Develop.

  1. Run Truffle Develop:
truffle develop
This shows accounts and their private keys that can be used when interacting with the blockchain.
migrate
Shows the transaction IDs and addresses of your deployed contracts. It also includes a cost summary.

You can use these Ids and addresses to create and connect to UI or develop a Dapp.

Done!!! That’s how to compile, test and deploy a Smart Contract.

You can find the complete code here.

Thanks for reading this article ❤

Clap 👏 If this article helps you.

Connect with me on LinkedIn and GitHub.

--

--