Continuing from Part 1, we’ll now create the following functionality inside the smart contract:
- Upon uploading the smart contract, the parent will provide the twins' accounts to be set as the owners of the piggy bank.
- Anyone can put Ether into the Piggy Bank.
- Each twin (using their account) can decide that they want to ‘smash’ the piggy bank, but the piggy bank will only be smashed once both twins decide to do so.
- Once the Piggy Bank is smashed the twins can pick up or rather withdraw their funds.
Let's add-in the upload functionality. This is done by using a constructor function which is executed when the smart contract is uploaded.
The updated code follows:
pragma solidity >=0.5.0 <0.7.0;
contract SharedPiggyBank {
address payable twin1;
address payable twin2;
constructor (address payable twin1address, address payable twin2address) public {
twin1 = twin1address;
twin2 = twin2address;
}
}
The two address payable variables are account addresses to which we can send Ether to. If we didn't specify the payable keyword, then we would only be able to keep track of the address but not send it funds.
The public keyword used in the constructor definition implies that the function can be called by anyone.
When uploading the smart contract the two address parameters must be provided by the uploader to successfully upload the smart contract.
Let's try to upload the smart contract. But first, let's remove any other smart contracts that we have deployed in remix. In the Run and Deploy Transactions tab click the delete all smart contracts button as below:
Now, when we come to deploy the smart contract we need to provide two parameters, the addresses of the twins' accounts. Expand the Deploy section for the SharedPiggyBank smart contract as below:
Now, we need to provide the account addresses for the twins. Remix by default creates 15 accounts that you can use to test, which you can see by clicking on the Account dropdown:
You should see a list of accounts as follows:
TIP: One idea for you to keep track of which account belongs to the various simulated users in your test is to keep track of the last few hexadecimal digits and associate it to a user (on a piece of paper or your computer).
For these articles, I'll be using the last two hexadecimal digits of the accounts where: 36 will be the parent; e1 will be twin1; and Ea will be twin2.
I'll copy twin1 (e1) and twin2's (Ea) account addresses from the dropdown and enter them into the parameters below. To copy an address select the copy button once the account is selected as follows:
After copying and pasting the account address into the Deploy section, you can then re-deploy the smart contract:
If successful, that’s great! If not, please make sure the code is written as above (be careful of copying text as the web/browser may alter some characters). But, we still do not have any functionality implemented yet which we can test. We'll continue in Part 3.