Ethereum smart contract tutorial [lunch-break series] (1): Custodian Contract

From my rough understanding, and the wisdom of the crowds, it is unpractical to record/count the times that a contract has been instantiated. But what if this information is indispensable for the project to proceed? That is when the Custodian Contract comes into play.

Apologize upfront for naming this simple concept as such a strong phrase ;p

Custodian Contract: Noun. A solidity contract who is eligible to supervise or oversee the other contract, or a collection of contracts. The simplest usage is to count the existences(number of instances) of the contract that is under custody.

What you need?

Solidity, truffle and testrpc

Get started!

Create a new directory named custody-contract/ or any name you prefer.

Start off testrpc in custody-contract/ by terminal and open the other terminal to do truffle init inside custody-contract/

# Note: If you think I start to babble on meaningless words and probably go crazy, then you might want to visit my previous tutorial asap. :)

Create child contract

Child contract is the contract that doomed to be oversaw. It has only one method that proves its liveness.

pragma solidity ^0.4.13;
contract Child {
function alive() public returns (bool) {
return true;


  1. pragma: This annotation prevents the compilation of future-version compiler which might lead to incompatibility of codes.

Create custody contract

Custody contract looks like below:

pragma solidity ^0.4.13;
import "./Child.sol";
contract Custodian {
uint public counter;
event Birth(uint _counter, address _child);
function Monitor() public {
counter = 0;
function createChild() public returns (address) {
Child child = new Child();
Birth(counter, child);
return child;


  1. event: It is always a good habit to do logging. Event provides similar functionality that allows being triggered within contract method, and being captured on the side of Javascript (not included in this tutorial).
  2. import: By importing the other contract, Child.sol in this case, this contract can interact with the imported contract.


Change the migrations/2_deploy_contracts.js to the following:

var Custodian = artifacts.require("./Custodian.sol");
var Child = artifacts.require("./Child.sol");
module.exports = function(deployer) {

Let’s run truffle console, and deploy to finish up the deployments.

Try out

This gonna be the fun part of this tutorial. To begin with, let’s get the instance of Custodian.sol first.>custodian=obj)

We know the codes below will return the address of the new Child instance:


# Note: web3.eth.coinbase is the address of the first account, as well as the address where the reward of mining will go to.

Check whether the counter of custodian has incremented by 1.

In order to retrieve the Child instance and change the state of custodian at a time, we should do:{from:web3.eth.coinbase})).then(obj=>child=obj)

Now let’s check whether the child is working by:    // Should return true

Lastly check the counter of custodian to see if the afore-created Child are successfully recorded.    // Should be 2

Wrap up

This tutorial offers only extremely basic idea on how two contracts can interact unilaterally, in order to mimic the custody relationship. There could be more use cases for you to explore and realize.

Thanks for your time~

All the codes can be found here.