Events Assignment

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

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

    event addBalanceTransaction(uint amount, address depositedTo);
    event transferTransaction(uint amount, address Sender, address Receiver);
    
    constructor(){
        owner = msg.sender;
    } 
    
    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function addBalance(uint _toAdd) public returns (uint){
        balance[msg.sender] += _toAdd;
        emit addBalanceTransaction(_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;
         
   }    
}
2 Likes

My solution for transfer event

pragma solidity 0.7.5;

contract test {
    
    mapping(address => uint) balance;
    address owner;
    
    event balanceAdded(uint amount, address indexed depositedTo);
    event transferred(uint amount, address depositedFrom, address depositedTo);
    
    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 transferred(amount, msg.sender, recipient);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
    
}
2 Likes
pragma solidity 0.7.5;

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

contract SolidityBasic{
 
// mapping (Key => Value)
    mapping(address => uint) balance;
    address owner;
    
    event balanceAdded(uint amount, address indexed depositedTo);

    event transferDetails(address indexed depositedFrom, address indexed depositedToAddress, uint amount); // new event added for transfer function
    
    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, "Insufficient funds");
        require(msg.sender != recipient, "Don't send money to yourself");
        
        uint previousSenderBalance = balance[msg.sender];
        
        _transfer(msg.sender, recipient, amount);
        
        emit transferDetails(msg.sender, recipient, amount); // this logs the sender, recipient, and the amount to the event transferDetails, also the sender and recipient is indexed just for fun
        
        assert(balance[msg.sender] == previousSenderBalance - amount);
    }
    
    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}
2 Likes
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;
    
    event balanceAdded(uint amount, address indexed depositTo);
    
    event amountTransfered(uint amount, address recipientAddress, address senderAddress);
    
    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 sufficent");
        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(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 HelloWorld {
    
    mapping(address => uint) balance;
    
    address owner;
    
    event balanceAdded(uint amount, address indexed depositedTo); 
    event transferEvent(uint amount, address indexed transferredTo, address indexed transferredFrom);
    
    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 youself");       
        uint previousSenderBalance = balance[msg.sender];
        _transfer(msg.sender, recipient, amount);
        
        emit transferEvent(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;
    }
}
1 Like
pragma solidity 0.7.5;

// EVENTS

contract myBank {

mapping(address => uint) balance;

address owner;

event balanceAdded(uint amount, address depositedTo);
event transferred(uint amount, address depositedTo, address depositedFrom); 

constructor() {
    owner = msg.sender;
}

function addBalance(uint _toAdd) public returns(uint) {
    balance[msg.sender] = 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 {
    
    
    _transfer(msg.sender, recipient, amount);
    
}

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


}

I placed my emit at the end of the _transfer function, since it seemed all the elements were there, getting the job done.

Hi @Randallcrum,

You have correctly defined your transfer event, but the corresponding emit statement has the arguments in the wrong order: this will result in the senderā€™s address being logged as the depositedTo address, and the receiverā€™s address being logged as the depositedFrom address. Can you correct this?

We should emit events at the very end of the function that completes the transaction. As you only have a function call in your transfer() function, then it is appropriate to have your emit statement at the end of the _transfer() helper function. In fact, if the only code you have in your transfer() function is a call to the helper function, then all you really need is one transfer function anyway. We should always try to avoid unnecessarily repetitve code and aim for conciseness. However, you are missing the require and assert statements in transfer(). These are important to ensure the inputs and output are valid. If you add these, then having 2 separate functions for the transfer becomes relevant again, and so this will also affect where you should put the emit statement.

Another important factor to consider here is that if you emit the event in the helper function, you are actually restricting your ability to reuse it in the future. It will be more reuseable if we can call _transfer() from another function when we may not want to emit that same event ā€” either a different one, or no event at all. That way we can reuse it whenever we just want its functionality (i.e. the operations, or computation, that it performs).

Let us know if anything is unclear, or if you have any questions :slight_smile:

pragma solidity 0.7.5;
contract MemoryAndStorage {

    mapping(uint => User) users;
    event balanceAdded(uint amount, address depositedTo);
    event transferred(uint amount, address depositedFrom, address depositedTo); 
    
    struct User{
        uint id;
        uint balance;
    }

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

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

    function getBalance(uint id) view public returns (uint) {
        return users[id].balance;
    }
    
    
    function transfer(address recipient, uint amount) public {
        require(balance[msg.sender] >= amount, "Insufficient Balance Available"); //require
        require(address[msg.sender] != receipient, "You may not transfer to the same address.");//require
        uint priorSenderBalance = balance[msg.sender];
        _transfer(msg.sender, recipient, amount);
        emit transferred(amount, msg.sender, recipient);
        assert(balance[msg.sender] == priorSenderBalance - amount);//assert
    }

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

}

OK. I started over again because of how sloppy my initial coding was. Started with the code in github and made my changes with an eye on you critiques. Iā€™m struggling now, because I keep getting the same Declaration Error for anything relating back to msg.sender. I suspect I set up the environment wrong or something, but Iā€™m not sure what I missed.

Hi @Randallcrum,

You have somehow mixed up and merged the code for Bank and for MemoryAndStorage (the data location assignment code). You are getting all of the Declaration errors because you have the wrong mapping (users). The compiler is expecting a mapping called balance. Once youā€™ve changed your mapping from the one used in the data location assignment, to the one needed for Bank, most of the errors will be resolved.

Then, you need to remove the rest of the code relating to the data location assignment:

struct User {
    uint id;
    uint balance;
}

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

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

ā€¦ and replace the wrong getBalance function with the correct one for Bank:

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

You should then be left with a couple more compiler errors to resolve, which relate to your second require statement in the transfer function. See if you can work out what they are, using the error messages to help, where possible.

Your amendments in terms of the actual Event Assignment are mostly good:

  • The emit statement now has its arguments in the correct order.
  • The emit statement is in the correct function.
  • You have added the 2 require statements (one just needs correcting, as mentioned above).
  • You have added the assert statement.

The emit statement would be better placed at the very end of the transfer function, after assert(). If assert() throws, then the preceding operations in the function body will revert, but it is still safer to only log the transfer event once all of the other code has executed successfully.

Let us know if anything is unclear, or if you run into any further difficulties.

pragma solidity 0.7.5;
contract myBank {

    mapping(address => uint) balance;
    
    address owner;
    
    event balanceAdded(uint amount, address depositedTo);
    event transferred(uint amount, address depositedFrom, address depositedTo); 
    
    constructor() {
        owner = msg.sender;
    }
    

    function addBalance(uint _toAdd) public 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, "Insufficient Balance Available"); //require
        require(msg.sender != recipient, "You may not transfer to the same address.");//require
        uint priorSenderBalance = balance[msg.sender];
        _transfer(msg.sender, recipient, amount);
        assert(balance[msg.sender] == priorSenderBalance - amount);//assert
        emit transferred(amount, msg.sender, recipient);

    }

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

}

Whew! Got it.
Many thanks!

2 Likes

Nicely done! :raised_hands:
Well done for persevering with it!

event transferEvent(uint amount, address transferedFrom, address transferedTo); 

ā€¦

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

log solidity

1 Like

I added the following:

**event transferred(uint amount, address sender, address receiver);**

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];
    
    //balance[msg.sender] -= amount;
    //balance[recipient] += amount;
    _transfer(msg.sender, recipient, amount);
    
    assert(balance[msg.sender] == previousSenderBalance - amount);
    
    **emit transferred(amount, msg.sender, recipient);**
}
1 Like