Events Assignment

contract Bank{

mapping (address => uint) balance;
address owner;

event balanceAdded(uint amount, address depositedTo);
event transferAmount(uint amount, address transferredFrom, address transfferedTo);

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 recepient, uint amount) public {
    require(balance[msg.sender]>=amount);
    require(msg.sender != recepient);
    
    uint previousSenderBalance = balance[msg.sender];
    
    _transfer(msg.sender,recepient, amount);
    
    assert(balance[msg.sender] == previousSenderBalance - amount);
    
    emit transferAmount(amount, msg.sender, recepient);
}

function _transfer(address from, address to, uint amount) private {
    balance[from] -= amount;
    balance[to] += amount; 
    
}

}

I’ve written this contract from scratch, in order to train myself with all the concepts explained in the previous lessons! :smiley:

pragma solidity 0.7.5;

contract tokenTrasfer{
    
    mapping (address => uint) balance;
    
    address owner;
    
    constructor(){
        owner = msg.sender;
    }
    
    modifier onlyOwner{
        require(msg.sender == owner);
        _;
    }
    
    event balanceTransferred(address from, address to, uint value);
    
    function addBalance(address _to, uint _valueToAdd) public onlyOwner{
        balance[_to] += _valueToAdd;
    }
    
    function transferBalance(address from, address to, uint valueToTransfer) public onlyOwner{
        uint previousSenderBalance = balance[from];
        _transfer(from, to, valueToTransfer);
        assert (balance[from] == previousSenderBalance - valueToTransfer);
        emit balanceTransferred(from, to, valueToTransfer);
    }
    
    function _transfer(address from, address to, uint valueToTransfer) private{
        balance[from] -= valueToTransfer;
        balance[to] += valueToTransfer;
    }
    
    function getBalance(address whom) public view returns(uint){
        return balance[whom];
    }
    
    
}
2 Likes
pragma solidity 0.7.5;

contract IshoboyBank {
    
    mapping(address => uint) balance;
    
    address owner;
    
    address recipient;
    
    event balanceAdded(uint amount, address depositedTo);
    
    event balanceTransfer(uint amount, address depositedFrom, address recipient);
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; //run the function
    }
    
    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 your self");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        emit balanceTransfer(amount, msg.sender, _recipient);
        
        assert(balance[msg.sender]== previousSenderBalance - amount);

    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}
1 Like
pragma solidity 0.7.5;

contract Bank {
 
    mapping(address => uint) balance;
    address owner;
    
    event balanceAdded (uint amount, address depositedTo);
    event balanceTransfered (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 to yourself");
        
       uint previousSenderBalance = balance[msg.sender];
       
       _transfer (msg.sender, recipient, amount);
       
       assert(balance[msg.sender] == previousSenderBalance - amount);
       
            emit balanceTransfered(amount, recipient, msg.sender);
    }
    
    function _transfer (address from, address to, uint amount) private{
        balance [from] -= amount;
        balance [to] += amount;
    }
    
}
1 Like
pragma solidity 0.7.5;

contract Bank{
    mapping (address=>uint)balance;
    address owner;
    
    event balanceAdded (uint amount, address depositedTo);
    event balanceTransferred (uint amount, address sender,address recipient);
    
    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, "Dont transfer money to yourself");
       
       uint previousSenderBalance = balance[msg.sender];
       
        _transfer(msg.sender, recipient, amount);
        
        assert(balance[msg.sender]== previousSenderBalance - amount);
    }
    
    function _transfer(address from, address to, uint amount) private {

        balance[from] -= amount;
        balance[to] += amount;
        emit balanceTransferred (amount, from, to);
    }
}
1 Like

contract Bank {

mapping(address => uint) balance;
address owner;

event balanceAdded(uint amount, address depositedTo);
event transferMade(uint amount, address transeredTo);

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 transferMade(amount, recipient);
    assert(balance[msg.sender] == previousSenderBalance - amount);
}

function _transfer(address from, address to, uint amount) private{
    balance[from] -= amount;
    balance[to] += amount;
}

}

1 Like
pragma solidity 0.7.5;
contract Bank {
    
    mapping(address => uint) balance;
    
    address owner;
    
    event balanceAdded(uint amount, address indexed depositedTo);
    event balanceTransferred(uint amount, address transferredFrom, address transferredTo);
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; // run the functions
    }
    
    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);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
        emit balanceTransferred (amount, from, to);
    }
}

pragma solidity 0.7.5;

contract Bank Kyle{
mapping (address=>uint)balance;
address owner;

event balanceAdded (uint amount, address depositedTo);
event balanceTransferred (uint amount, address sender,address recipient);

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, "Dont transfer money to yourself");
   
   uint previousSenderBalance = balance[msg.sender];
   
    _transfer(msg.sender, recipient, amount);
    
    assert(balance[msg.sender]== previousSenderBalance - amount);
}

function _transfer(address from, address to, uint amount) private {

    balance[from] -= amount;
    balance[to] += amount;
    emit balanceTransferred (amount, from, to);
}

}

1 Like
pragma solidity 0.7.5;

contract Bank {

    mapping(address => uint) balance;
    
    address owner;
    
    event balanceAdded(uint amount, address indexed depositedTo);
    
    event amountTransferred(address sender, address recipient, uint amount);

    modifier onlyOwner {
        //only a specific address can call the function this modifier is included in
        require(msg.sender == owner);
        _; //run the function
    }

    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 {
        //check balance of msg.sender
        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);
        
        //event logs and further checks
        assert(balance[msg.sender] == previousSenderBalance - amount);
        
        emit amountTransferred(msg.sender, recipient, amount);
    }

    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }

}
1 Like
pragma solidity 0.7.5;

contract HelloWorld {
    mapping(address => uint) balance;
    address owner;
    event balanceAdded(uint amount, address depositedTo);
    event balanceTransferred(uint amount, address from, address to);
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; // runs the function
    }
    
    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 balanceTransferred(amount, msg.sender, recipient);
    }
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}
1 Like
pragma solidity 0.7.5;
contract Bank {
    
    //storage - permanent storage of data (state variables)
    //memory - temporary storage used in function execution
    //calldata - save arguments/inputs to our functions
    //strings, arrays, mappings, structs

    mapping(address => uint) balance;
    
    address owner;
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _; //run the function
    }
    
    event balanceAdded(uint amount, address indexed depositedTo);
    event transfers(address indexed recipient, uint amount);
    
    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 transfers(recipient, amount);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
    

}
1 Like
pragma solidity 0.7.5;

contract Bank{
    
    mapping(address => uint) balance;
    address owner;

modifier onlyAdmin {
  require(msg.sender == owner);
  _;
}

constructor(){
  owner = msg.sender;
}

event balanceAdded(uint amount, address indexed depositedTo);
event balanceTransferred(address indexed recipient, uint amount);

function addBalance(uint _toAdd) public onlyAdmin 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 transferBalance(address recipient, uint amount) public {
    require(balance[msg.sender] >= amount, "Insufficient amount!");
    require(msg.sender != recipient, "Cannot send to yourself!");
    emit balanceTransferred(recipient, amount);    
    uint previousBalance = balance[msg.sender];
    _transfer(msg.sender, recipient, amount);
    assert(balance[msg.sender] == previousBalance - amount);
}
    
    function _transfer (address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}
1 Like

pragma solidity 0.7.5;

contract Bank {

mapping (address => uint) balance;
address owner;
event balanceAdded(uint amount, address indexed depositedTo);
//creates a log of who sent, received and what amount//
event balanceTransfer(uint amount, address recipient, address sender);

modifier onlyOwner {
    require(msg.sender == owner);
    _; // means run the function
}

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);
  //emits the log//
   emit balanceTransfer(amount, recipient, msg.sender);
}
function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }

}

1 Like

Why create a log? Doesn’t the information in the blockchain block give us these details anyway?

Hi @Toddness

Doesn’t the information in the blockchain block give us these details anyway?

Events is the way to fetch info from the blockchain, they will indeed tell you a lot about a transaction.

A transaction without event just tells you that addressA sent x ERC20 to addressB.

A transaction with events tells you that addressA sent x ERC20 to addressB due to an exchange on uniswap.
Or tells you that addressA is a contract that minted 100 tokens to addressB.

Cheers,
Dani

1 Like

got it. wonderful, thanks for the explanation !

1 Like
pragma solidity 0.7.5;

contract Bank{
    
  mapping(address=>uint)balance;
  
  address owner;
  modifier onlyOwner{
     require( msg.sender == owner);
     _;
  }
  
  constructor(){
      owner=msg.sender;
  }
  
  event transferTo(uint amount , address whoGets , address whoSent);
  
  function addBalance(uint _toAdd)public onlyOwner returns(uint){
      require(msg.sender==owner, "not the owner account");
      balance[msg.sender]+= _toAdd;
      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);
      require(msg.sender != recipient);
      emit transferTo (amount , recipient , msg.sender);
  _transfer(msg.sender , recipient , amount);
  }
  
  function _transfer(address from, address to, uint amount)private {
      balance[from]-=amount;
      balance[to]+=amount;
  }
  
}
3 Likes
pragma solidity 0.8.2;
contract Bank{
    mapping (address => uint) balance;
    address owner;
    event balanceAdded(uint amount, address depositedTo);
    event transferTo(uint amount, address intoAcc, address fromAcc);
    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);
        uint previousSenderBalance = balance[msg.sender];
        _transfer(msg.sender, recipient, amount);
        emit transferTo(amount, recipient, msg.sender);
        
    }
    function _transfer(address from, address to, uint amount) public{
        balance[from] -=amount;
        balance[to] +=amount;
    }
} 
1 Like

pragma solidity 0.7.5;
contract Bank{
mapping (address => uint) balance;
address owner;
event balanceAdded(uint amount, address depositedTo);
event transferred(address owner, address recipient, uint amount);
modifier onlyOwner {
require(msg.sender == owner, ‘only owner can send’);
_;
}
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);
    _transfer(msg.sender, recipient, amount);
    emit transferred(msg.sender, recipient, amount);
    
}
function _transfer(address from, address to, uint amount) public{
    balance[from] -=amount;
    balance[to] +=amount;
}

}

Hey @bigbill

Well done!
I leave here an FAQ that can help you to post readable code in the forum for your next exercises :slight_smile:

Cheers,
Dani