Events Assignment

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:

Here’s the event I added to to the transfer() function:

event transactionCompleted(uint amount, address sender, address recipient);

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 transactionCompleted(amount, msg.sender, recipient);
 }
1 Like

Hey Jon, could you give me a hand with my code, please? I really do not understand why I am not getting any logs here. I have added the screenshot of my device that shows my code. I am still messing with my code here in remix and I have looked at it a few times but still can not figure out the issue with it. Could you please give me a hand here?

Thank you in advance.

Joseph

Screen Shot 2021-05-21 at 2.00.03 PM

Screen Shot 2021-05-21 at 2.01.05 PM

Look Jon, this one fired event logs… what’s the deal with this?

Screen Shot 2021-05-21 at 2.31.39 PM

I got the events to log and I really did not do anything in particular. It eventually just fired. I worked on the other tutorial. It fired events and then I got the tutorial here to fire events.

Screen Shot 2021-05-21 at 2.37.21 PM

Hey Joseph,

I’m not entirely sure I understand the problem, but the event is emitted when you call the function with the emit statement, which in the Bank contract is addBalance(), and not when you call getBalance(). Is that what’s happened? In your first screen shot, it looks like you have called getBalance() and tried to find event data for that call. Whereas in your final screen shot you have found the event data in the transaction logs because you are looking at the transaction generated by the addBalance() function call.

We emit an event when an actual “event” happens i.e. some change in the contract state has occurred. We don’t emit events for getters, because when we call those, we are only retrieving data and the getter is only reading the contract state, not changing it.

Does that answer your question?

You didn’t get confused with your test contract, because you only have one function to call there :wink:

You will have noticed that when you call getters (e.g. the getBalance function) to just read data and retrieve it, you get CALL data in the Remix console. If you click on the down arrow to view their data, there won’t be any event logs. I think maybe that’s what you did at first.

But when you call a function which changes the contract state and generates an actual transaction, the confirmed transaction data appears with a green tick in the console. When you click on the down arrow with one of those that has fired an event, you will see the event data in the logs there.

Ok Jon, so after doing a few tutorials and getting some more hands-on, I can now speak a bit more educated on the topic, however, there still seems to be some confusion.

Referring to the tutorial that is relevant to the course, I can see that I actually did code the event and the trigger correctly. However, when I look for the log, I am not able to see the log so I am lead to believe that I did not code it correctly or that I made some mistakes somewhere along the road. Should the events be logged? I am under the impression that it is important, and that the event is logged for future reference. I have been lead to believe that the log is the actual proof that the event was both coded and triggered appropriately. Am I off?

1 Like

No, you are correct.

Is the problem that you are looking for the event data by clicking on the down arrow for CALL data (generated by a successful call to a getter), instead of clicking on the down arrow for the confirmed transaction data (generated by the function with the emit statement), where the logged event data will be…?

I’ve gone into more detail about this in my 2nd reply.

It’s easy to click on the wrong receipt in the console because the window is so narrow… When I’m looking at the detailed transaction data, I often drag the console section up so it’s much bigger. That way it’s easier to see what you’re doing and what you’re clicking on.

Also, sometimes the latest confirmation receipt doesn’t show at the bottom and you need to scroll down to see it. That can also cause you to click on the wrong down arrow :sweat_smile:

This is one the annoying things about the Remix interface. Remix has loads of great features, but this isn’t one of them! :upside_down_face:

I’m looking for the data in the log. That is what we were shown to do here in the tutorial, bro and that is what I am attempting to do. What strikes me as odd is that the actual log was shown after doing nothing different. It just showed up after doing the other tutorial in the log and then coming back.

But you’re telling me that it was coded in remix correctly, right? So, I should feel ok to move forward, right?

Yes, that’s what I’m telling you to do too… but you need to look in the logs for the right receipt, as in click on the down arrow for the receipt that was generated by the function call that emits the event. If you look in the logs for a CALL confirmation, and not a transaction confirmation, or if you look in the wrong transaction confirmation, then the event data won’t be there. I’m pretty sure from what you’ve explained and your screen shots, that that’s what has happened.

Yes, your code looks correct, but please save my eyes by posting your code formatted so I can clearly see it and also run it to test it :pray: otherwise it’s difficult to confirm that your code is definitely correct.

But your code does look correct from what I can see, and it’s really good that you have done your own little test contract for the events — that’s a great way to learn and check things :+1:

However, at some point you do really need to make sure you are able to find the confirmation receipt (in the console) that corresponds to the function call you want to check the data for. Before clicking on the down arrow, you can ensure that you are looking at the right receipt because the “receipt header” in the console (before expanding it) will include the function name e.g. Bank.addBalance(uint256)  or  Bank.getBalance()
You need to make sure you are looking at the logs in the receipt for Bank.addBalance(uint256)

1 Like

Great solution @Ernest_SG :muscle:

The only comment I would make is that TransactionCompleted isn’t really specific enough for an appropriate event name. “Transaction” could refer to either of our transactions (deposit or transfer) and any additional types of transaction that are added later. You could use this same “general” event for all transactions, and add an additional string parameter for the transaction type. The name of each type of transaction could then be added as a fixed string in each of the separate emit statements e.g.

event TransactionCompleted(string transaction, uint amount, address sender, address recipient);

emit TransactionCompleted("Transfer", amount, msg.sender, recipient);
emit TransactionCompleted("Desposit", _toAdd, msg.sender, address(0));

Notice, that the number of arguments and their values in the emit statements must correspond with the number of parameters and their data types in the event definition. This means that any of the arguments that aren’t relevant for a particular transaction would still need to be included and logged as zero values.

Or you could change the event name to something more specific to a transfer, and just use it as an event exclusive to transfers, and have separate event definitions for each type of transaction.

Let me know if you have any questions :slight_smile:

2 Likes