WinDeveloper Coin Tracker

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

Getting Started with Solidity Programming for Ethereum Part 3

  • Published: Aug 13, 2020
  • Category: Ethereum, Solidity
  • Votes: 5.0 out of 5 - 5 Votes
Cast your Vote
Poor Excellent

Part 3: 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 2, now that we have managed to deploy a smart contract let's add the following functionality (for now):

  1. The ability to deposit (put) Ether (money) into the shared piggy bank.
  2. A way to check how much Ether has been put in the shared piggy bank.

The updated code follows - the added lines are coloured red:

pragma solidity >=0.5.0 <0.7.0;

contract SharedPiggyBank {

    address payable twin1;
    address payable twin2;
    uint public amountPutIn;

    constructor (address payable twin1address, address payable twin2address) public {
        twin1 = twin1address;
        twin2 = twin2address;
    function deposit() payable public {
        amountPutIn += msg.value;

We've added in a variable amountPutIn which we'll use to keep track of the amount deposited into the shared piggy bank. It's an unsigned integer (uint) which stores a 256-bit integer (if you do not know what this is then I recommend looking up a tutorial on programming and data types). The public keyword here means that the amountPutIn variable can be viewed from the outside (remix will create a button for us which will allow us to request the value in the variable).

NOTE: Even if a variable is not marked as public, all values stored in a smart contract are stored in the blockchain which means anyone can inspect the value, so never store confidential information in a smart contract.

The deposit function has been added which has been marked as payable which means that people (and other smart contracts) can send Ether into the smart contract using this function. The public keyword specified here (when used for functions) means that the outside world (i.e. people and other smart contracts) can call/invoke/execute this function. Remix will create a button that allows for us to invoke this function.

The msg.value keyword (or rather globally available variable) is used to retrieve the amount of Ether that has been sent into the smart contract (through the deposit function). So the line of code increases the value of amountPutIn by the amount of Ether passed into the smart contract when invoking deposit.

You can now re-deploy this code (remember to insert the two addresses of the twins that will own the shared piggy bank). Expand the smart contract by pressing the following button:

Expand deployed smart contract

You should now see the following two buttons have been added, one to invoke the function deposit and one to return the value of amountPutIn.

Buttons created in remix

Press the amountPutIn button and you should see the value is returned (0 right now because we haven't put anything in):

Public variable amountPutIn

Now let's deposit some Ether into the shared piggy bank. In remix, to send Ether to the deposit function, we need to tell remix how much Ether we would like to send. This is set in the Value field as circled in red below:

Ether value

1 Ether can be broken down into smaller denominations (Just like 1 USD can be broken down into cents). wei is the smallest denomination of Ether, 1 wei is 0.000000000000000001 Ether. This is not too important for us yet, but you should read up more on Ether denominations. Above the denomination is set in the drop down field circled in blue.

Let's deposit 1000 wei, by inserting 1000 in the value field circled in red below:

Deposit Ether

Now, let's send the 1000 wei (a small amount of Ether) to the deposit function by pressing the button circled in blue above. If the transaction was successulfy you should see the green checkmark icon appear in the console on the right.

Now, let's check to see that we are correctly keeping track of the amount sent into the smart contract by pressing the amountPutIn button again, and we should see the 1000 wei has been recveived in the smart contract as below:

Check amount again

Excellent, so now we have a piggy bank where we can store funds. This is just a test smart contract and it's important that we do not upload this smart contract to the real Ethereum network (we are using a simulated network in remix) - since this smart contract as is, will only allow for Ether to be sent in, but it offers no way to get those funds out. They will be locked away in the smart contract for ever!

Next time we'll cover how to break the piggy bank.

Copyright 2024 All rights reserved.