Ethereum Development On Windows — Web3, and Truffle Debugging

Andy Watt
edgefund
Published in
6 min readMay 5, 2018

The first post in this series saw us do some basic environment configuration, and then some very simple operations with the Truffle suite. In this tutorial I’ll take things a little further by demonstrating a couple of interactions with the contract, and going through an example with the Truffle debugger.

Make sure that you’ve followed through the steps in part 1. At the end of that tutorial, we had shown how to deploy and test a contract, but hadn’t really done much with it yet.

Open up VS Code, and open the console again with Ctrl+’. In the console, enter one after the other:

truffle develop
migrate --reset
test

These steps will take us back to the place that we left off in the previous tutorial, showing the contracts are deployed, and that the five tests have passed.

Getting started with Web3

Web3 is a JavaScript library that allows interactions with smart contracts on the Ethereum blockchain (or test chains). It comes bundled into the Truffle development console, and so is available to use to interact with the contracts that have been deployed.

When the Truffle development console is started, it automatically configures 10 addresses, and assigns each of the addresses 1000 test Eth to get going with. You can check the ten available addresses by entering:

web3.eth.accounts

This will return an array of the available addresses. Note that these are the addresses that were displayed when entering the Truffle development console.

You can pick out individual elements in the array in the usual way:

You can check the balance of each address in fake-Eth by typing:

web3.eth.getBalance(web3.eth.accounts[1]).toNumber() * 10e-18;

Note, that the getBalance call returns the value in Wei, so convert to Eth.

The full web3 capabilities are described here.

Interacting with the contract

So far we have compiled, deployed, and tested the MetaCoin example, but we have barely even looked at the actual code, let alone interacted with it.

Lets take a quick look at the code, and think about a couple of interactions that may be useful.

As you can see, there are four functions available. They are:

  • MetaCoin, the constructor. Called when the contract is deployed.
  • sendCoin, for transferring coins between addresses.
  • getBalanceInEth, to convert between MetaCoins, and Ethereum.
  • getBalance, to show the balance in the requested address.

The concept of constructors is of course well understood to anyone familiar with object orientated programming, but its use in blockchain coding is a little less obvious. The constructor is called only once, and this is when the contract is deployed to the blockchain. In these examples, that is when the truffle command migrate is called. Deploying a contract to a blockchain is analogous to calling ‘new’ in traditional OO coding, and so once deployed, the constructor cannot be called again.

Inspecting the code in the constructor shows only a single line:

balances[tx.origin] = 10000;

This sets the balance of the address which has initiated the transaction to 10000 MetaCoin. Note, this is the balance in MeteCoin, not Eth, which will remain ‘1000’. In these examples, the initiating address is the one found at:

web3.eth.accounts[0]

This can be shown by inspecting the balance at that address by entering:

MetaCoin.deployed().then(function(instance){return instance.getBalance.call(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});

The balance at account[0] is shown to contain 10000 MetaCoins, as per the value set in the constructor.

The function ‘sendCoin’ can be used to move the coins around between accounts. Note that the sender in this case is accounts[0], and we are sending the balance to accounts[1].

MetaCoin.deployed().then(function(instance){return instance.sendCoin(web3.eth.accounts[1], 100);});

The transaction can be shown to be successful by checking the balance of both accounts. Note that this is the MetaCoin balance, NOT the Eth balance:

MetaCoin.deployed().then(function(instance){return instance.getBalance.call(web3.eth.accounts[0]);}).then(function(value){return value.toNumber()});MetaCoin.deployed().then(function(instance){return instance.getBalance.call(web3.eth.accounts[1]);}).then(function(value){return value.toNumber()});

We have used the Truffle development console, and the Web3 libraries to deploy and interact with the contract. Next, we’ll look into the actual execution of the function, by starting the Truffle debugger, and stepping through the code line by line.

Debugging the Transaction

The final piece of the Truffle-puzzle that I want to demonstrate, is the Truffle debugger.

In the above examples, a call was placed to the sendCoin() function, which resulted in a transaction occurring on the blockchain. This transaction can be stepped through line by line, using the Truffle debugger. To do this the Truffle debug command is used, passing in the transactionHash. This can be found in the console output following the sendCoin function call. Note that your transaction hash will differ from mine.

Copy the transaction hash and enter the Truffle debugging environment with:

debug YOUR_TRANSACTION_HASH

This will start the Truffle debugger. The instructions for interacting with the debugger are printed to the console:

You should start by adding a watch to the passed variables. do this by entering:

+: receiver
+: amount

As you step through the code, the values passed into the function will be shown. Note, these are ‘undefined’ at the start of the function call.

You can hit enter a few times to step through the code that was executed in this transaction.

The debug commands can be used to inspect the variables, and add watched variables

The experience debugging with Truffle is not quite as ‘rich’ an experience as you may be used to, say coming from a .NET background. But, it’s still a fantastic tool to use to make your life significantly easier when writing your contracts.

What’s Next?

Part one of this series described some basic environmental setup. In this tutorial, we have interacted with the MetaCoin contract, and worked through a transaction with the debugger. However, all of this has been carried out against the build in Truffle ‘fake’ blockchain. In the next post, I’ll go though the alternative, and more ‘realistic’ test environments.

I am currently working on EdgeFund, an open-source platform which offers a decentralised shared bankroll on the Blockchain. To learn more about EdgeFund, please visit our website or join our telegram group to chat to the team and be sure to follow us on Twitter!

--

--

Andy Watt
edgefund

Technical Lead and co-founder at Avalone Consultants. Angular, .NET, and blockchain developer.