2

i am having quite a hard time reading an array that is passed through an event, this is the event:

event SaleCreated(
        address seller,
        address indexed token,
        uint256[] indexed tokenId,
        uint256 price,
        uint256 indexed indexOrder,
        uint256[] amount,
        uint256 orderType,
        uint256 orderTime
    );

I have no problem reading any parameter, whether it is indexed or not, the problem is reading the tokenId array or the amount array, for example inserting this in the tokenId array: [150]

I get this: 0x6aa7ec8ac2a999a90ce6c78668dffe4e487e2576a97ca366ec81ecb335af90d0

how can i go from that to the original array?

My personal solution

I found a workaround for this problem, although I know it may not work for everyone. In my case the data that was put into the array and shown in the event (tokenId and amount) were both arrays inserted as input to the function call, so when the event is captured I get the transaction hash , and with web3.py I call:

contractObject = web3.eth.contract(address=addressContract, abi=abiContract)
transaction = web3.eth.get_transaction(tx_hash)
inputs = contractObject.decode_function_input(transaction.input)

thus getting clean input data.

I didn't find these web3.py functions in web3.js, I hope it will be useful for you

2
  • Did you find an answer to this problem? I'm facing a similar issue
    – ofir
    Mar 14 at 18:39
  • @ofir I updated my question with my personal workaround Mar 15 at 23:36

2 Answers 2

2
+50

how can i go from that to the original array?

You actually can't go from 0x6aa7ec8ac2a999a90ce6c78668dffe4e487e2576a97ca366ec81ecb335af90d0 to [150].

From the solidity docs:

You can add the attribute indexed to up to three parameters which adds them to a special data structure known as “topics” instead of the data part of the log. A topic can only hold a single word (32 bytes) so if you use a reference type for an indexed argument, the Keccak-256 hash of the value is stored as a topic instead.

All parameters without the indexed attribute are ABI-encoded into the data part of the log.

Topics allow you to search for events, for example when filtering a sequence of blocks for certain events. You can also filter events by the address of the contract that emitted the event.

In the case of your event, if you want to know the tokenId and amount arrays, you'd have to not index them.

1
  • it is possible to read the arguments of indexed data if they are not arrays, and the situation for arrays is the same if they are not indexed, the value in the "data" parameter remains the same whether they are indexed or not, i'm updating my question with my personal solution Mar 15 at 23:29
0

It looks like you're trying to decode the event log data in Ethereum, and you're having difficulty decoding the arrays (tokenId and amount) within the event. Your current solution involves decoding the input data from the transaction using web3.py. This works, but there's a more direct way of decoding the event logs using web3.js.

Here's a solution using web3.js that doesn't involve decoding the transaction input:

contract.events.SaleCreated({
    fromBlock: 0
}, (error, event) => {
    if (error) {
        console.error(error);
    } else {
        const tokenIdArray = event.returnValues.tokenId;
        const amountArray = event.returnValues.amount;

        console.log('Token ID Array:', tokenIdArray);
        console.log('Amount Array:', amountArray);
    }
});
1
  • The problem is that indexed parameters are limited to 32 bytes, so dynamic arrays aren't stored in the event as they are instead keccak256(array) is stored.
    – Ismael
    Mar 16 at 15:25

Your Answer

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

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