Linking and initializing contracts on deploy automatically
First, you should get familiar with the truffle migrate command that helps you deploy your contracts but also upgrade and redeploy the changed contracts only.
But what about the contracts that depend on other contracts that you are deploying as well? Contracts that are dependencies should be updated with the newly deployed address. You can do this manually during production but what about tests? Let’s dive into a solution.
For example, we have a smart contract called Application that interacts with Storage smart contract. The goal is to deploy them both and update Application contract with the Storage contract’s address.
Here is our Application.sol file:
And our migration file:
The code above creates 4 transactions:
- Deployment of the Application contract
- Deployment of the Storage contract
- Updating storage address in Application contract
- Updating Truffle’s Migrations contract that tracks migrations onchain
To save up some gas, you could add constructor to Application contract which would accept address of storage contract as function parameter. Then our example would look like this:
And migration of
This way we will make one transaction less than before and thus save some gas.
Testing time based functions
Often you will find yourself writing time-dependent code i.e. checking if a certain time has passed before withdraw/deposit. If you are using ganache-cli for local network and testing, then you can use special RPC function evm_increasetime during your tests.
Here is how it can be used:
To wait during tests for 24 hours you can call:
await utils.increaseTime(24 * 60 * 60);
Mocha multi reporters
If you are like us and you use some sort of CI (Continuous Integration) like CircleCi, you would probably want your test results in some common format. Most CI tools require test results in JUnit XML format.
Truffle by default uses spec reporter which nicely formats test result onto console window. You could easily change this to mocha-junit-reporter, it’s just matter of running
yarn install mocha-junit-reporter and adding following mocha config entry in your truffle.js config file:
This solution is nice for CI but it’s not very user friendly when it comes to running test locally and then checking xml file for test results. Luckily there is something called mocha-multi-reporters.
For configuring multi reporters, first install dependencies:
yarn install ---dev mocha-multi-reporters mocha-junit-reporter
Add following mocha configuration to your truffle.js config:
You’ve probably noticed that we are using mocha-reporter-config.json. Using this file, we can configure multi reporter to use both spec and mocha-junit-reporter.
As you might have probably noticed, you can even decide where you want your test results to be stored.
Developing dApps using Ethereum requires complex setup with whole bunch of frameworks and tools. Because of these we created starter repository which has all tools and frameworks configured for you to develop smart contracts with Node.Js as backend server. Everything is put in containers using Docker so with only one command you can start your private network, deploy contracts, run (database) migrations, run web server, tests and whatever you like.
If you want to try it, you can check it out here:
You can let us know if you would like some more features in this repo or feel free to contribute!