Banking Contract Key Concepts
Understand the banking contract in depth
Last updated
Understand the banking contract in depth
Last updated
Owner Management: Teaches how to restrict access to certain functions for contract owners.
Banking Functions: Demonstrates how users can deposit and withdraw Ether.
Balance and Loan Tracking: Illustrates keeping track of user balances and loan amounts.
Events: Shows how to log contract actions for transparency using events.
Modifiers: Demonstrates using function modifiers for access control and logical checks.
These contracts can be used to teach the basic functionality associated with each concept in the original SimpleBank
contract.
A contract where only the owner can execute certain functions.
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract OwnerContract {
address public owner;
constructor() {
owner = msg.sender; // Set the owner as the account that deploys the contract
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
function changeOwner(address newOwner) public onlyOwner {
owner = newOwner;
}
function ownerOnlyFunction() public onlyOwner {
// Only owner can execute this function
}
}
A simple contract where users can deposit Ether, and only the owner can withdraw it.
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract DepositWithdraw {
address public owner;
mapping(address => uint256) public balances;
constructor() {
owner = msg.sender;
}
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw() public {
require(balances[msg.sender] > 0, "No balance to withdraw");
uint256 amount = balances[msg.sender];
balances[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}
function ownerWithdraw() public {
require(msg.sender == owner, "Only the owner can withdraw all funds");
payable(owner).transfer(address(this).balance);
}
}
A contract that tracks balances and loans for users.
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract LoanTracker {
mapping(address => uint256) public balances;
mapping(address => uint256) public loans;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function takeLoan(uint256 amount) public {
require(amount > 0, "Loan must be greater than 0");
loans[msg.sender] += amount;
}
function repayLoan() public payable {
require(loans[msg.sender] > 0, "No outstanding loan");
require(msg.value == loans[msg.sender], "Must repay exact loan amount");
loans[msg.sender] = 0;
}
function getLoanAmount() public view returns (uint256) {
return loans[msg.sender];
}
}
A contract that demonstrates emitting events to log actions such as deposits and withdrawals.
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EventLogger {
event DepositMade(address indexed user, uint256 amount);
event WithdrawalMade(address indexed user, uint256 amount);
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
emit DepositMade(msg.sender, msg.value);
}
function withdraw(uint256 amount) public {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
emit WithdrawalMade(msg.sender, amount);
}
}
A contract that demonstrates the use of function modifiers for access control.
solidityCopy code// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ModifierExample {
address public owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "You are not the owner");
_;
}
function restrictedFunction() public onlyOwner {
// Only the owner can call this function
}
function openFunction() public {
// Anyone can call this function
}
}