pragma solidity 0.7.5;
contract Bank {
mapping (address => uint) balance;
address owner;
event balanceAdded(uint amount, address depositedTo) ;
event amountTransferred(uint amount, address toAddress, address fromAddress);
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);
assert(balance[msg.sender] == previousSenderBalance - amount);
emit amountTransferred(amount, recipient, msg.sender);
}
function _transfer(address from, address to, uint amount) private {
balance[from] -= amount;
balance[to] += amount;
}
}
I noticed others above indexing the events. While the video it talked about it to be able to look up addresses later, Filip said that amounts were not worth indexing.
I was curious why because there might be scenarios where you want to index an amount transferred, or if this can always be looked up on the blockchain itself afterwards?
Can’t addresses be looked up by having interacted with a smart contract? A block explorer shows things like the most used contracts and some transaction details with them from what I’ve seen.
My eth experience almost anything about a transaction of tokens can be seen from an address with a block explorer.
Thanks for anyone’s time!
edit: Question from the Event Quiz right after this assignment
It appears the first question gets the syntax incorrect:
Both how the video shows events and how jon_m above has the first word in an event as lower case not upper case.
from jon_m:
(2) A generally-accepted convention is to start the names of …
- Contracts, Interfaces, Events and Structs with a capital letter
- functions, variables, mappings, arrays, arguments, parameters and modifiers with a lower-case letter