Description:
Smart contract deployed on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title PiggyBank
* @dev A simple piggy bank contract for depositing and withdrawing ETH
*/
contract PiggyBank {
// Mapping to track each user's balance
mapping(address => uint256) public balances;
// Events to track deposits and withdrawals
event Deposit(address indexed user, uint256 amount);
event Withdrawal(address indexed user, uint256 amount);
/**
* @dev Function 1: Deposit ETH into your piggy bank
*/
function deposit() public payable {
require(msg.value > 0, "Must send some ETH!");
balances[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}
/**
* @dev Function 2: Withdraw your ETH from the piggy bank
* @param amount Amount to withdraw in wei
*/
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance!");
require(amount > 0, "Amount must be greater than 0!");
balances[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
emit Withdrawal(msg.sender, amount);
}
/**
* @dev Function 3: Check your balance
* @return Your current balance in wei
*/
function getMyBalance() public view returns (uint256) {
return balances[msg.sender];
}
/**
* @dev Function 4: Say hello and show your balance
* @return A friendly greeting with your balance
*/
function sayHello() public view returns (string memory) {
uint256 balance = balances[msg.sender];
if (balance == 0) {
return "Hello! Your piggy bank is empty. Time to save some ETH!";
} else if (balance < 0.01 ether) {
return "Hello! You're just getting started. Keep saving!";
} else if (balance < 0.1 ether) {
return "Hello! Nice savings! You're doing great!";
} else {
return "Hello! Wow! You're a crypto saver superstar!";
}
}
/**
* @dev Get the total contract balance
*/
function getTotalContractBalance() public view returns (uint256) {
return address(this).balance;
}
/**
* @dev Fallback function to receive ETH
*/
receive() external payable {
deposit();
}
}
Submitted on: 2025-10-26 13:05:18
Comments
Log in to comment.
No comments yet.