pragma solidity 0.7.5;
contract Bank {
mapping(address => uint) balance;
address owner;
event balanceAdded(uint amount, address indexed depositedTo);
event transferDone(uint amount, address indexed sender, address indexed recipient);
modifier onlyOwner {
require(msg.sender == owner);
_;
}
modifier costs(uint price) {
require(msg.value >= price);
_;
}
constructor(){
owner = msg.sender;
}
function addBalance(uint _toAdd) public onlyOwner returns (uint){
balance[msg.sender] += _toAdd;
emit balanceAdded(_toAdd, msg.sender);
return balance[msg.sender];
}
function getBalance() public view returns (uint) {
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Do not transfer money to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
emit transferDone(amount, msg.sender, recipient);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
//Private function that will manage the transfer
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
1 Like
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract Zamalek {
mapping (address=> uint) balance;
address owner;
event balanceAdded(uint amount, address depositedTo);
event transferDone(uint amount, address transferredFrom ,address transferredTo);
modifier onlyOwner{
require(msg.sender == owner);
_;
}
constructor(){
owner = msg.sender;
}
function addBalance(uint _toAdd) public onlyOwner returns (uint){
balance[msg.sender] += _toAdd;
emit balanceAdded(_toAdd, msg.sender);
return balance[msg.sender];
}
function getBalance() public view returns (uint) {
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "Don't transfer money to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
emit transferDone(amount, recipient, msg.sender);
assert(balance[msg.sender] == previousSenderBalance - amount);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
Sorry if the comment lines make it look messy, this program has my notes in it.
pragma solidity 0.7.5;
contract Bank {
mapping(address => uint) balance;
address owner;
modifier onlyOwner { // a small piece of code which you place in a function. When the function call happens it always executes first. Helps reduce code repetition in functions.
require(msg.sender == owner, "Only the contract owner can perfeorm this function");
_; //this means the modifier is done, go ahaed and run the function. Under the hood the VM will replace the _ with the code in the body of the fucntion.
}
constructor () {
owner = msg.sender;
}
event BalanceAdded(uint amount, address indexed depositedTo);
event TransferComplete(uint amount, address indexed recipient);
function addToBalance(uint _toAdd) public onlyOwner returns (uint) { //you call the modifier in the header of the function. Just like normal functions, modifiers can have input paramenters
balance[msg.sender] += _toAdd;
emit BalanceAdded(_toAdd, msg.sender);
return balance[msg.sender];
}
function getBalance() public view returns (uint) {
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public {
//don't need the sender to give details, you can just get it from msg.sender.
//check balance of msg.sender
require(balance[msg.sender] >= amount, "Balance not sufficient");
require(msg.sender != recipient, "You can't transfer money to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount); //call the internal/private function where the computation takes place.
assert(balance[msg.sender] == previousSenderBalance - amount);
emit TransferComplete(amount, recipient);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
Here is my solution:
//SPDX-License-Identifier: MIT
pragma solidity 0.7.5;
contract HelloWorld {
mapping(address => uint) balance;
address owner;
event balanceAdded(uint amount, address indexed depositedTo); //indexed by ETH nodes
constructor() {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_; //run the function
}
function addBalance(uint _toAdd) public onlyOwner returns (uint){
balance[msg.sender] += _toAdd;
emit balanceAdded(_toAdd, msg.sender);
return balance[msg.sender];
}
function getBalance() public view returns (uint) {
return balance[msg.sender];
}
function transfer(address recipient, uint amount) public {
require(balance[msg.sender] >= amount, "You don't have enough funds"); //check balance of msg.sender
require(msg.sender != recipient, "You can't transfer funds to yourself");
uint previousSenderBalance = balance[msg.sender];
_transfer(msg.sender, recipient, amount);
assert(balance[msg.sender] == previousSenderBalance - amount);
emit balanceAdded(amount, recipient);
//event logs and further checks
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
1 Like