Navigating through the intricacies of testing smart contract on the Ethereum testnet.

by Kevin Wong


Welcome to the third and last article for this short series. Instead of using using our application on a local blockchain, we want to utilize the testnet. The testnet introduces a new set of challenges for us. Which we will address throughout this article. Let us begin immediately.

Refer to the repository here:

What is a Public Testnet?

As we all know, Ethereum is a public blockchain that can be accessed by anyone at anytime. In a similar vein, a public tesnet for the Ethereum blockchain allows anyone to test their code and smart contracts in a public environment. Ethereum tokens on the testnet has no monetary value and is obtained free of charge from faucets.

The testnets behave exactly like the main Ethereum chain, with the exception that testnets could be an upgrade or two ahead of the main chain. That means that like the mainnet, there will be variables such as block times, gas, miners etc affecting your smart contracts. No worries, these variables will not come into play for our simple tutorial, besides the longer test times due to block times of course.

HD Wallet

First of all, we need testnet Ether tokens and a wallet to store them.

What is a HD Wallet? Well, I suppose you can read more about it here, but tl;dr, it is just a way to generate a lot of wallets using a single seed. And in our case, we are using mnemonics to generate our seed.

Before, we were using the wallets supplied by Ganache, which had 100 Ether in every wallet. These wallets only existed on the local blockchain and not the public testnet. Let’s install the package needed for a wallet. Make sure the directory is the project directory.

>npm i truffle-hdwallet-provider@web3-one

Now, navigate to the truffle-config.js file and add these lines into the file.

Obviously, we are missing the variables for the infura key and the mnemonics. Generate a mnemonic using MyCrypto. Then obtain a set of infura key from Infura via registration in the Infura website.

Then, add the following Ropsten testnet settings below the local development network settings. I chose Ropsten as our test network, but there are other choices such as Rinkeby and Kovan.

If you are using truffle v5 and above, you might see this block pre generated but commented out.

Uncomment. (I changed the confirmations to 0 to match the default.)

Understanding the values above will require a deeper understanding of the Ethereum blockchain itself. But we will leave it as the default (generated) values for now.

Faucet and Testnet Tokens

Use and and send some Ropsten testnet Ethers into your own private wallet (You will need to look for other faucets for other testnets). Remember the address that you have generated using MyCrypto in the previous section? Use the address as the destination address.

Testnet tokens only exists in the testnet and cannot be exchanged for monetary value. So don’t get too many interesting ideas now.

Migrating to the Testnet

So in the previous sections we have setup the HD wallet, configured the settings and obtained the necessary resources. Now, all that is left for us is to migrate the contract to the public testnet. Use the console command shown below.

>truffle migrate –network ropsten

You will have to wait for a few minutes for this process to be completed. In the earlier sections of the tutorial, we had the luxury of using a personal blockchain that is instantly mined, giving us quick test times on our project. Testing on a public tesnet can be time consuming, so make sure that your code/contract works locally before taking it out for a spin on the public testnet.

Using the command will publish your contract to the Ropsten testnet. Fortunately, the outputs are pretty self explanatory so I don’t think it will be necessary to cover it here. I have included the sample output in the folder labeled “outputs” found in the project github repository. Note that newer version of truffle might have differing outputs compared to the ones found.

Using Blockchain Explorers

Your contract is published to the testnet? Great!

I understand that going outside of truffle is beyond the scope of this series, but having other tools at your disposal will help you understand how to look for information from transactions.

Get acquainted with because it will be one of your most used tool in exploring transactions in your Ethereum journey. There are different versions of the website for the mainnet and other testnets.

The search box in Etherscan allows you to search addresses, transaction hash, block number, token symbol and ENS name. Go ahead and paste your contract creation transaction hash into the search box.

You will see that Etherscan provides a detailed breakdown of the entire transaction for you. You can look at the amount of Gas Used and the Gas Price for the transaction, the created contract address, so on and so forth. Remember that contract function calls are transactions too? You can actually determined the function that was called by looking at the input data field of the transaction page.

You can even upload the source code for your contract onto Etherscan for all to refer to, but we will save that topic for another time.

Naturally, there are alternatives such as . Feel free to use the alternative if that is to your liking.

Using the Testnet for Tests

Migrating to the testnet does not do much for us now does it? We want to test our smart contract on the testnet too. It’s easy.

>truffle test –network ropsten

Truffle will publish a new set of contracts for a clean slate and then run the set of tests on it. Which will take slightly longer than migrating in the previous step.

A simple output is displayed. Since this is ran on the ropsten testnet, you can use the blockchain explorer to explore the latest transaction in the wallet that you have just utilized.

And You’re Done

This third article is the shortest amongst the 3 in terms of length, but probably the most time consuming exercise. There are going to be some bumps and setbacks. You will have to go slightly above and beyond to understand the information presented to you from the console and from the block explorers.

Parting Words

So here we are at the end of this journey. Hopefully I have provided a good enough launchpad for you to get started on playing with smart contracts. I also hope that this has pique your interests enough to venture deeper with blockchain technology. Remember that what we have covered here barely covers the surface of what blockchain and smart contract can do. This is just the beginning.