DEV Community

prince-joel
prince-joel

Posted on

Building a piggy bank smart contract with solidity

Introduction

The piggy bank concept has been around for a long time; it's been a method of saving from the days of continuously putting coins in a box until you've saved up your desired amount and then breaking the box to get your money to now, where mobile and web apps are built to save money that can't be withdrawn until the stated date before it's broken.

Currently, most fintech projects using web2 languages are now built using web3 languages, with the increase in demand for web3 and blockchain developers, it is necessary as a Solidity developer to learn methods that are useful in the field.

In this article, you'll learn how to use Solidity to create a simple smart contract that accomplishes the aforementioned task. Anyone can transfer tokens to the bank, but only the owner can withdraw from it, and this withdrawal will terminate the contract by calling the self-destruct method.

Getting started

To successfully write a smart contract that allows anyone to send in a token, only the owner can withdraw and destroy itself after withdrawal. This article is the perfect guide for you.

Construct a .sol file using the Remix IDE and set it up by mentioning the licensing identification and version, then create your contract with the name you want.
The next step is to create an event. This contract has two events a withdrawal, and a deposit event.

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

contract Pbank{
    event deposit(uint amount);
    event withdraw(uint amount);
}
Enter fullscreen mode Exit fullscreen mode

The above code will trigger the receive function and the withdraw function you will create later.

Next, you declare your state variable and modifier. modifiers are code that are run before or after a function call. They are used to modify the behavior of a function.

address public owner;

    modifier onlyowner(){
        require(msg.sender == owner, "not the owner");
        _;
    }
Enter fullscreen mode Exit fullscreen mode

You'll declare your state variable, which will serve as the contract's owner, and then use the modifier method to create an onlyowner that will alter the behavior of the function you'll write.

After that, you must allow this contract to accept tokens. This can be accomplished by using the receive function and ensuring that it is a payable function. The contract will then be able to accept tokens from anyone in this method.

Let’s implement this functionality in Solidity.

receive() external payable{
        emit deposit(msg.value);
    }
Enter fullscreen mode Exit fullscreen mode

When we get a token in this function, the event deposit is emitted with the amount sent.

Next, the most important function in this contract, the withdrawal function, must be created. Imagine saving money and then being unable to withdraw it when the time comes. Scary right?. Luckily, we can avoid this nightmare and give owners the access they deserve.

Using the onlyowner modifier we developed, you can design a withdrawal function that only allows the owner to withdraw from the contract. When this withdrawal event is then emitted, all tokens in the address.

Finally, we need to destroy our piggy bank by starting the self-destruct method. What is a piggy bank that does not destroy itself after withdrawal? We must only trigger the self-destruct method when all tokens have been given to the owner, then the contract can be terminated
Take a look at the implementation of the function in Solidity

function Withdraw() external onlyowner {
        emit withdraw(address(this).balance);
        selfdestruct(payable(msg.sender));
    }
Enter fullscreen mode Exit fullscreen mode

Conclusion

Depositing, withdrawing, and destroying your piggy bank are all important parts of saving with it. This article showed you how to write a simple smart contract that accepts tokens from anyone, allows only the owner of the contract to withdraw from it, and self-destructs when it’s empty.

Top comments (0)