WinDeveloper Coin Tracker

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

Getting Started with Solidity Programming for Ethereum Part 5

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

Part 5: 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 4, now that we have managed both to deposit funds into our shared piggy bank and break the piggy bank, let's now work on ensuring that the piggy bank cannot be broken before 10 years pass as per the original requirements we set out in Part 1.

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;
    bool twin1brokeIt;
    bool twin2brokeIt; 
    uint canBreakAfter;    

    constructor (address payable twin1address, address payable twin2address) public {
        twin1 = twin1address;
        twin2 = twin2address; 
        canBreakAfter = now + (10 * 365 days);    
    }
    
    function deposit() payable public {
        amountPutIn += msg.value;
    }
    
    function breakPiggyBank() public {
        require(msg.sender == twin1 || msg.sender == twin2, 
                "Only a twin can try to break the piggy bank!"); 
        require(canBreakAfter <= now, 
                "You have to have waited ten years before trying to break the piggy bank!");    
        
        if (msg.sender == twin1) {
            twin1brokeIt = true;
        }
        
        if (msg.sender == twin2) {
            twin2brokeIt = true;
        }
        
        if (!twin1brokeIt || !twin2brokeIt) {
            return;
        }
        
        uint half = amountPutIn / 2;
        twin1.transfer(half);
        twin2.transfer(half);
    }
    
}

We've added the canBreakAfter variable to keep track of the date after which the piggy bank can be broken, and in the constructor (uploading deploying the smart contract) we set this date to be 10 years after the current date, where the current date is retrieved using now and we calculate 10 years as 365 days x 10.

The only thing left to do is then to check to make sure that when a twin tries to break the piggy bank that the 10 years have passed. A require statement is added which checks exactly that.

To test out the code though, we do not want to wait ten years. Instead you can change days to seconds and this would then keep the piggy bank locked for around 1 hour.

IMPORTANT: It's important to note that Ethereum's notion of time is far from accurate and can also be manipulated (to some extent). now will return the time as reported by the miner who is finalising the block within which your code is executed. Read more about this elsewhere before deploying any real smart contracts that make use of time.

Great, so we've implemented a smart contract which can only be broken by the twins after 10 years of uploading the smart contract. However, there is a subtle vulnerability issue in the way we transfer funds out to the twins. See if you can figure out the problem before the next post where we will shed light on the issue.

Copyright 2020 All rights reserved. BlockchainThings.io