Events Assignment

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

Hey Randall, Could you share the location of the Github file. Also, when was this file shared? Was it shared in the video that corresponds to this activity? I reckon Iā€™m a bit lost. Iā€™d really appreciate it. Thanks in advance.

1 Like

I want to create an event that displays the following data:

  1. the date it was transferred
  2. the address it was transferred from
  3. the address it was transferred to
  4. the amount that was transferred

pragma solidity 0.7.5;

contract ContractOne {

event newTransfer
( uint date,
address from,
address to,
uint amount,

) ;

function trade(address to, uint amount)
external {
emit NewTransfer
(now,
msg-sender, to, amount) }

}

1 Like

Hi Joseph,

There isnā€™t actually any solution code in the GitHub repository for this assignment. In the Events video, Filip asks you to create an event for the transfer function in the Bank contract you have been building. The idea is to use the same method that was demonstrated in the video for the addBalance function, where the event BalanceAdded was first defined, and then the corresponding emit statement was included within the addBalance function body.

Once you have added the code for your event to your Bank contract, and it has compiled without any errors, you should test it by deploying your contract and performing some transactions, which should include some transfer function calls. This will enable you to check that your transfer event is successfully emitted and that it logs data which is both correct and appropriate for each successfully executed transfer. In the video, Filip shows you where to find this data in the transaction logs, in Remix.

You should post your solution code here in this discussion thread, and we will review it for you.

Let me know if anything is still unclear, or if you have any further questions about this assignment :slight_smile:

1 Like

Hi again, Joseph,

Youā€™ve definitely got the right idea here :ok_hand: but your event and emit statement need to be implemented in your Bank contract for the transfer() function. This way you can also demonstrate that you know where to place your emit statement within the function body. You should be able to use the same code that you have already written and posted here, within your Bank contractā€¦ except for the following errors, which will be highlighted by the compiler when you add your code to Bank in Remix:

  • A few syntax errors:
    (i)ā€‚An extra comma where there shouldnā€™t be one
    (ii)ā€‚A missing semi colon
    (iii) msg.senderā€‚is written with a dot (.) not a hyphen (-)
    (iv) Inconsistent use of capital v lower-case letter for the event name. Both should be written exactly the same. You can start the event name with either a lower case or a capital letter, but it is standard practice to start event, struct and contract names with a capital letter.

  • nowā€‚ can be used to return the block timestamp in versions of Solidity prior to v0.7, but one of the changes from v0.7 is that it can no longer be used. You have to useā€‚block.timestampā€‚instead. You can find this kind of information in the Solidity documentation. For example, here is the relevant section on the Solidity v0.7.0 Breaking Changes page. The 2nd bullet point refers to the block timestamp:
    https://docs.soliditylang.org/en/v0.7.0/070-breaking-changes.html#changes-to-the-syntax
    As you have correctly defined within your event, the block timestamp will be returned as an unsigned integer. It is actually expressed as the number of seconds which have elapsed since a specific point in time in the past (not a date). This timestamp can be converted into various different date and/or time formats, in the frontend using JavaScript.

Just one other thingā€¦ you should format your code before posting it here in the forum. This will make it clearer and more readable. It should also make it easier to spot the kinds of syntax errors that Iā€™ve highlighted above.
Follow the instructions in this FAQ: How to post code in the forum
For example, this is what your event will look like when formatted in this wayā€¦
(Notice that Iā€™ve made a couple of corrections for some of the points mentioned above)

event NewTransfer(uint date, address from, address to, uint amount);

2 Likes

Thank you. I will redo it today. Because coding is absolutely new to me, I find doing additional research on Solidity very helpful. I will make it a priority to do it before the end of the week. Thanks for looking out man. You are definitely A-list.

1 Like

Youā€™re very welcome, Joseph! :smiley: