Events Assignment

Add Event:

event amountTransferred(uint amount, address Recipient, address Sender);

Emit the Event to function transfer …

emit amountTransferred(amount, recipient, msg.sender);

result:
image

1 Like

An excellently coded Bank contract @Saula_Atama :muscle:

Your transfer event and corresponding emit statement are both well coded. The emit statement is in the correct position in the transfer() function body and will log appropriate data when the transfer() function is successfully executed.

1 Like

Hi @Konzaih,

Your transfer event and corresponding emit statement are both well coded. As you have demonstrated, the emit statement logs appropriate data when the transfer() function is successfully executed :ok_hand:

Where an emit statement is positioned within the function body is also important. Where exactly did you place yours?

1 Like

Hey @jon_m

Thank you for pointing that out!
Next time I will keep in mind to properly display the location of the code aswell.

I’ve decided to place the emit statement at the bottom of the function.

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);
          
    }
1 Like

This is my code after adding the event in the transfer() function. Are the event and emit statements correct?

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

An excellently coded Bank contract @alexhupe :muscle:

Your transfer event and corresponding emit statement are both correct and well coded. The emit statement is in the correct position in the transfer() function body and will log appropriate data when the transfer() function is successfully executed.

Hi all,
I post my event assignment below:
// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

contract PlikiPliki {

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

event balanceAdded(uint amount, address indexed depositedTo);
event transferMade(address from, address to, uint amounTransered);

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

constructor(){
    owner = msg.sender;
}

function addBalance(uint _amount) public onlyOwner returns(uint){
    balance[msg.sender] += _amount;
    emit balanceAdded(_amount, msg.sender);
    return balance[msg.sender];
}
function getBalance() public view returns(uint){
    return balance[msg.sender];
}

function transfer(address recipient, uint amount) public{
    //some important checks
    require(msg.sender != recipient);
    require(balance[msg.sender] >= amount);
    
    uint balanceBeforeTransfer = balance[msg.sender];
    _transfer(msg.sender, recipient, amount);
    
    //some other contract logic;
    assert(balanceBeforeTransfer == balance[msg.sender] + amount);
    emit transferMade(msg.sender, recipient, amount);
}

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

}

1 Like

In both events, I placed the amount as the last argument

pragma solidity 0.7.5;

contract Bank {
 
    mapping (address=>uint) balance;
    address owner;
    
    event balanceAdded(address indexed depositTo, uint amount);
    event balanceTransferred(address indexed sender, address indexed recipient,uint amount);
    modifier onlyOwner{
        require(msg.sender == owner);
        _;
    }
    constructor(){
        owner = msg.sender;
    }

    function addbalance(uint _toAdd) public onlyOwner returns (uint){
        balance[msg.sender] += _toAdd;
        emit balanceAdded(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,"Balance not sufficient");
        require(msg.sender != recipient, "No point on transfering funds to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        _transfer(msg.sender,recipient,amount);
        emit balanceTransferred(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;
    }
}
1 Like
pragma solidity 0.7.5;

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

contract Bank {

    mapping(address => uint) balance;

    address owner;

    event balanceAdded(uint amount, address indexed depositedTo);
    
    event balanceTransfered(uint amount, address indexed sendedFrom, address indexed sendedTo);

    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 yourself");

        uint previousSenderBalance = balance[msg.sender];

        _transfer(msg.sender, recipient, amount);
        
        emit balanceTransfered(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 indexed depostiedTo);
event amountTransfered(uint amount, address indexed transferedTo, address indexed transferedFrom);

modifier onlyOwner {
require(msg.sender == owner, “Only 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 amountTransfered(amount, recipient, msg.sender);

assert(balance[msg.sender] == previousSenderBalance - amount);
  
  
  //event Logs and further checks

}

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

    balance[from] -= amount;
    balance[to] += amount;

}

}

1 Like

So much fun this one is!

pragma solidity 0.7.5;

contract Bank {
    
   mapping(address => uint) balance;
   
   address owner;
   
   event balanceAdded(uint amount, address depositedTo);
   
   event transferMade(uint amount, address depositedTo, address depositedFrom);
   
   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, "Dont send money to yourself");
       
       uint previousSenderBalance = balance[msg.sender];
       
       _transfer(msg.sender, recipient, amount);
       
       assert(balance[msg.sender] == previousSenderBalance - amount);
       
       emit transferMade(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 {

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

event balanceAdded(uint amount, address depositedTo);
event amountTransfered(uint amount, address trasferedFrom, address trasferedTo);

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 recipeint, uint amount) public {
     require(balance[msg.sender] >= amount, "Balance not sufficent!" );
     require(msg.sender != recipeint);
     
     uint previousSenderBalance = balance[msg.sender];
     
     _transfer(msg.sender, recipeint, amount);
     
     
     
     assert(balance[msg.sender] == previousSenderBalance - amount);
     //event logs and further checks
     
     emit amountTransfered(amount, msg.sender, recipeint);
 }
 
 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 transferred(uint amount, address fromAddress, address toAddress); 
    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, "Can't transfer money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
         emit transferred(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 MemoryAndStorage {

mapping(uint => User) users;

struct User{
    uint id;
    uint balance;
}

function addUser(uint id, uint balance) public {
    users[id] = User(id, balance);
}

 event newBalance(uint id, uint balance);

function updateBalance(uint id, uint bigBalance) public {
     users[id].balance=bigBalance;
     emit newBalance(bigBalance, id);
     
}



function getBalance(uint id) view public returns (uint balance) {
    return users[id].balance;
}

}

1 Like

Added an additional parameter to the transfer event, originAccount, and indexed both recipient and outgoing accounts, because both are accounts that can be queried if necessary.

pragma solidity 0.7.5;

contract Bank {
    
    mapping(address => uint) balance;
    address owner;
    
    event balanceAdded(uint amount, address depositedTo);
    
    // event transfer assignment
    event transferWhybroke(uint amount, address indexed transferredTo, address indexed originAccount);
    
    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, "You don't have enough moneys XD");
        require(msg.sender != recipient, "Stop slapping yourself XD");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
        
        emit transferWhybroke(amount, recipient, msg.sender); // response to assignment
    }
    
    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 transferTransaction(uint amount, address Sender, address Reciever );
    
    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;
        return balance[msg.sender];
        emit balanceAdded(_toAdd, msg.sender);
    }
    
    
    function getBalance()public view returns (uint){
        return balance[msg.sender];
    }
    
    function transfer(address recipient, uint amount) public {
        require(balance[msg.sender] >= amount, "Ballance not sufficient");
        require(msg.sender != recipient, "Don't transfer money to yourself!");

        _transfer(msg.sender, recipient , amount);
        emit transferTransaction(amount, msg.sender, recipient);

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

@filip @thecil hey guys this is my code. Anyway i’ve got a warning in line 24. It says unreachable code. Don’t know how to make it reachable. Wish you a great day!

Hey @a.l.a.i.n, hope you are ok.

I think the problem is on your return in this function.

The return method is the last one that you should use in your functions to end its execution, but the problem here is that after its execution (return balance[msg.sender]), you are emitting the event, which should be emitted before the return.

Hope it helps.

Carlos Z

1 Like
pragma solidity 0.7.5;

contract Bank {
    
    //storage - persistant data storage
    //memory - temporary data storage
    //calldata - advanced for gas and memory; similar to memory but READ-ONLY 
    
    //state variables
    
    
    mapping(address => uint) balance;
    
    address owner;
    
    event balanceAdded(uint indexed amount, address indexed depositedTo);
    event transferAmount(address from, address to, uint amount);
    
    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 yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
        
        emit transferAmount(recipient, msg.sender, amount);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }

}
1 Like

Yes thanks man this worked :+1:

1 Like