Skip to content

Instantly share code, notes, and snippets.

@renso3x
Created March 10, 2026 12:54
Show Gist options
  • Select an option

  • Save renso3x/47fc3ebf608e949215e7720f2904d01c to your computer and use it in GitHub Desktop.

Select an option

Save renso3x/47fc3ebf608e949215e7720f2904d01c to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.31;
contract Bank {
/**
ACTIVITY 1 — First Payable Function
Instructions:
Create a mapping:
mapping(address => uint) public balances;
Create a payable function deposit() that:
Requires msg.value > 0
Adds the received amount to balances[msg.sender]
Create a function getBalance() that returns the user’s balance.
*/
event Deposit(address indexed sender, uint amount);
event Withdrawn(address indexed recipient, uint amount);
mapping(address => uint) public balances;
function deposit() external payable {
require(msg.value > 0, "Need some gas!");
balances[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}
function getBalance() external view returns(uint) {
return address(this).balance;
}
/**
ACTIVITY 2 — Withdraw Your Funds
Instructions:
Add a withdraw(uint amount) function that:
Requires the sender has enough balance.
Subtracts the amount from their balance first (Effect).
Sends Ether to them using call{value: amount}("").
Requires success (require(success, "Withdraw failed")).
Emit a Withdrawn(address user, uint amount) event.
*/
function withdraw(uint amount) external {
require(balances[msg.sender] >= amount, "Insufficient balance");
// Effect
balances[msg.sender] -= amount;
// Interaction
(bool success,) = payable(msg.sender).call{value: amount}("");
require(success, "Withdraw Failed");
emit Withdrawn(msg.sender, amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment