How to Test and Debug Your Ethereum Smart Contracts

Roy Xie
57Blocks
Published in
3 min readOct 16, 2018

Testing smart contracts is a critical step when building blockchain applications. For Ethereum, smart contract can be executed on the main network, test networks (Rinkeby, Ropsten, Kovan), or locally.

Running smart contracts on MainNet require the expenditure of real ether, which should be reserved for your production environment. For test networks, we can get free ether manually for testing, but the deployment takes minutes. TestNets are good for QA but still not the best choice for day to day testing during the development process.

Testing with Truffle

Running a local blockchain is our preferred way to testing, because ether is free and contracts can be deployed quickly. At 57Blocks, we use truffle to manage smart contract development and testing.

Truffle Installation

To install truffle, make sure you have node.js 5.0+ installed, then install truffle with:

$ sudo npm install -g truffle

After installing truffle, we can check truffle version and solc-js version with:

$ truffle version
Truffle v4.1.14 (core: 4.1.14)
Solidity v0.4.24 (solc-js)

Create Truffle Project

To create a truffle project, first create a directory, change into it and then type:

$ truffle init

The above commend will create a standard truffle project directory layout and configuration files.

$ tree .
.
├── contracts
│ └── Migrations.sol
├── migrations
│ └── 1_initial_migration.js
├── test
├── truffle-config.js
└── truffle.js

If you are building something that is tokenized, use the following command to create a project with a sample token smart contract and test scripts to quickly start development and testing.

$ truffle unbox metacoin

This command create the following structure:

$ tree .
.
├── contracts
│ ├── ConvertLib.sol
│ ├── MetaCoin.sol
│ └── Migrations.sol
├── migrations
│ ├── 1_initial_migration.js
│ └── 2_deploy_contracts.js
├── test
│ ├── metacoin.js
│ └── TestMetacoin.sol
├── truffle-config.js
└── truffle.js

Run the following command to start the smart contract test:

$ truffle test
Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./contracts/Migrations.sol...
Compiling ./test/TestMetacoin.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...


TestMetacoin
✓ testInitialBalanceUsingDeployedContract (70ms)
✓ testInitialBalanceWithNewMetaCoin (42ms)

Contract: MetaCoin
✓ should put 10000 MetaCoin in the first account
✓ should call a function that depends on a linked library
✓ should send coin correctly (51ms)


5 passing (709ms)

Write and Run Tests Using Truffle

Truffle supports testing smart contracts using JavaScript and Solidity. At 57Blocks, we prefer testing with JavaScript, which is similar to how our smart contracts interact with the other parts of our system. Let’s check out an example test at test/metacoin.js:

There are 2 styles we can write our tests: using .then or using async/await. Below is the above test in async/await style:

Compared to .then method which chains together checks, writing tests in the async/await style is easier to read and maintain. So at 57Blocks, we prefer writing our tests in the async/await style.

Debugging Smart Contracts

Developing and testing smart contracts is hard. There is no built-in way to debug code like you would when developing traditional software. Here’s a small tip to help debug issues in your smart contracts.

Take the meta coin smart contract as example.

If we found sendCoin method is not sending correct amount of token to the receiver. here’s how we can identify where the bug might be located.

Let’s modify the method as shown in below:

Here we defined a Log event with a string message and an integer value, then at everywhere we want to inspect the value of amount, we emit a Log event to the blockchain. Let’s run the test again:

We also need a failed test to show the events from the blockchain. Add a line to the test:

assert.fail();

Running the test again produces the following results:

Events are only printed out on console if there is a failed test. With this method, we can debug our smart contract more easily.

57Blocks is a blockchain innovation lab helping companies turn blockchain ideas into reality. If your company is working with smart contracts and want feedback or help, give us a shout at hello@57blocks.io

--

--