1

Hey guys I have a simple Solidity code, which emits events shown in the code bellow. What I don't understand however is that on Etherscan I get many sections with events that I have never coded. Why is that happening?

Contract address: 0x5b6c5f2032C2483251C36DA4EAd2EEe9504694dd

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract CallTestContract {

event NewTrade(
    string message,
    uint indexed date,
    address indexed from,
    address indexed to, // Only three indexed variables are allowed in an event
    uint amount,
    uint transaction
);
function Trade(string calldata _message, address to, uint amount) external payable{
    emit NewTrade(_message, block.timestamp, msg.sender, to, amount, msg.value);
}
}

enter image description here

1 Answer 1

2

The output your seeing corresponds to how solidity turns emitted events into logs.

First of all - you can have up to three indexed parameters in your event. These indexed parameters appear as topic1 - topic3. Topic0 is the hash of the event signature.

topic0 = keccack256("NewTrade(string,uint256,address,address,uint256,uint256)")
topic1 = date
topic2 = from
topic3 = to

Now for the un-indexed parameters. You may be expecting to find the un-indexed parameters packed together in order with the remaining log data. But what really happens is the rest of the parameters are encoded using abi.encode. This specification with similar examples to what you are dealing with can be found here.

In this case, since you are using a dynamic type, a string, the hex represents the following:

[line 1] = since the first non-indexed param is a string which is a dynamic type, this is an offset to the start of where the `message` parameter begins = 32*3 bytes = 96 = 0x60
[line 2] = amount 
[line 3] = transaction 
[line 4] = length of string (in hex)
[line 5 & 6] = your string in hex padded to 32 bytes on the right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.