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?
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;
}
}
<Tips>:
- 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) {
++counter;
Child child = new Child();
Birth(counter, child);
return child;
}}
<Tips>:
- 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).
- import: By importing the other contract,
Child.sol
in this case, this contract can interact with the imported contract.
Deploy
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) {
deployer.deploy(Custodian);
deployer.deploy(Child);
};
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.new().then(obj=>custodian=obj)
We know the codes below will return the address of the new Child instance:
custodian.createChild.sendTransaction({from:web3.eth.coinbase})
# 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.
custodian.counter.call()
In order to retrieve the Child instance and change the state of custodian
at a time, we should do:
Child.new(custodian.createChild.sendTransaction({from:web3.eth.coinbase})).then(obj=>child=obj)
Now let’s check whether the child
is working by:
child.alive.call() // Should return true
Lastly check the counter
of custodian
to see if the afore-created Child are successfully recorded.
custodian.counter.call() // 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.