Piggy Bank

Implement a piggy bank.

Anyone can send Ether to this contract. However only the owner can withdraw, upon which the contract will be deleted.

Tasks

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

contract PiggyBank {
   
    event Deposit(uint amount);
    event Withdraw(uint amount);
    address public owner;
    
     constructor() {
        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));
    }
}

Last updated

Was this helpful?