WinDeveloper Coin Tracker

  • Home
  • Solidity
  • Getting Started with Solidity Programming for Ethereum Part 2

Getting Started with Solidity Programming for Ethereum Part 2

  • Published: Jul 29, 2020
  • Category: Ethereum, Solidity
  • Votes: 5.0 out of 5 - 1 Vote
Cast your Vote
Poor Excellent

Part 2: There are ample resources out there to get started with smart contract development. Many go into detail in regards to how Blockchains work, and the ins and outs of Ethereum. This article is intended for those who are less patient and want to create a first smart contract now.

Continuing from Part 1, we’ll now create the following functionality inside the smart contract:

  1. Upon uploading the smart contract, the parent will provide the twins' accounts to be set as the owners of the piggy bank.
  2. Anyone can put Ether into the Piggy Bank.
  3. 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.
  4. 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:

Delete all smart contracts

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:

Expand deploy section

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:

Dropdown list of accounts

You should see a list of accounts as follows:

List of accounts

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:

Copy account

After copying and pasting the account address into the Deploy section, you can then re-deploy the smart contract:

Deploy smart contract for twins

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.

Copyright 2024 All rights reserved. BlockchainThings.io