360 Guide on How to Create Piggy Bank Smart Contract Using Solidity Language

ยท

4 min read

360 Guide on How to Create Piggy Bank Smart Contract Using Solidity Language

Photo by Braลˆo on Unsplash

The piggy bank of the digital age is no longer made of ceramic or cast iron; rather, it's built on solidity language. This innovative language emerged in 2014 due to the increasing demand for smart contracts on the Ethereum blockchain.

Solidity was created to enable developers to write decentralized applications, or "dapps," that can execute automatically and securely without intermediaries.

The digital piggy bank built on solidity language allows users to save and track their digital assets, such as cryptocurrency, in a transparent and tamper-proof way.

Unlike traditional piggy banks, digital ones built on solidity can't be smashed open, and money is stolen; blockchain technology's decentralized and cryptographic nature protects them.

As cryptocurrencies and decentralized finance (DeFi) have grown, solidity-based piggy banks need. Today, these digital piggy banks are used to store and track assets, earn interest, trade, and even borrow against them.

Here are the steps we will follow:

  • Set up a new Solidity file.

  • Define the PiggyBank contract.

  • Create the deposit and withdraw functions.

  • Add a fallback function to receive Ether.

  • Compile and deploy the contract.

  • Test the contract.

Step 1: Set up a new Solidity file

To start, open your favorite text editor and create a new Solidity file. Let's name it PiggyBank.sol.

We will be using Remix IDE to write the contract.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

Step 2: Define the PiggyBank contract

The PiggyBank contract is the heart of the application. It will hold the balance of the piggy bank and allow users to deposit and withdraw Ether.

Here is the code to define the PiggyBank contract:

contract Piggybank {
    event Deposit(uint amount);
    event Withdraw(uint amount);

    address public owner = msg.sender;

    receive() external payable {
        emit Deposit(msg.value);
    }

This code defines a new contract called PiggyBank. We also define a public variable called balance, which will hold the balance of the piggy bank.

We also declared a receive fallback function to receive tokens into the contract. Bear in mind that the received fallback emits a Deposit event.

The deposit function is defined as a public function that is payable. This means that it can receive Ether as a parameter when called. When the deposit function is called, it will add the value of msg.value (the amount of Ether sent with the function call) to the piggy bank balance.

Step 3: Create the deposit and withdraw functions

Now that we have our PiggyBank contract defined, we can add the deposit and withdraw functions. These functions allow users to deposit and withdraw Ether from the piggy bank.

Here is the code to create the deposit and withdraw functions:

  function withdraw() external payable {
        require(msg.sender == owner, "not owner");
        emit Withdraw(address(this).balance);
    }

The Withdraw function is also defined as a public function. It will transfer the balance of the piggy bank to the address of the user calling the function.

We use the payable modifier on the msg.sender address to indicate that it can receive Ether. After the transfer is complete, we set the piggy bank balance to zero.

Lastly, in real life, if you want to withdraw from a piggy bank, you have to destroy it. in solidity, also you have to include self-destruct in your line of code to destroy the piggy bank.

 selfdestruct(payable(msg.sender));

Here is the full code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract PiggyBank {
    event Deposit(uint amount);
    event Withdraw(uint amount);

    address public owner = msg.sender;

    receive() external payable {
        emit Deposit(msg.value);
    }
        function withdraw() external {
            require(msg.sender == owner, "not owner");
            emit Withdraw(address(this).balance);
            selfdestruct(payable(msg.sender));
    }
}

Conclusion

The piggy bank has come a long way from its humble clay origins. With the evolution of the solidity language, it's now a secure, decentralized, and versatile tool for saving and managing digital assets. As crypto and decentralized finance (DeFi) has grown, so need solidity-based piggy banks.

With all the above guides I have given, I firmly believe it's time for you to create your solidity-based piggy bank. What are you waiting for? open remix and start creating your piggy bank now!

Visit my GitHub.

ย