1

I am working in a ethereum project and i want to retrieve some data from an event. Let me show the details of what i am trying to do ;

    event fooev( address indexed sender, address indexed requesteeContractAddr, string indexed abc);

The fooev is an event that the contract emits

function foo(address requesteeContractAddr, string abc) public
{
    fooev(msg.sender, requesteeContractAddr, abc);
}

Foo is a function, when someone sends a transaction & calls foo, the fooev' event is emitted

assume abc = 'hey'

Now i am getting event data like this :

AttributeDict({'sender': '0xfc63c3aB84fc49bfDd7aB7FAcB6A80BE890eA062',
'requesteeContractAddr': '0x4cd53503e7Ab59D2aFB379195569424Df88f5BF6',
 'abc': b'&\xa2\x082:\x8c!\xa11#\xc1#\xa8\xdd|qDD\xc5a\x10;\x00b\xd1\xb8\xb6\xa9\xe9\x08\x9e*'})

notice that abc, which was input in the smart contract in a string form, is returned in a bytes form.

When i try to convert it from bytes to string by doing something like :

Web3.toText(AttributeDict.args.resourceType)

i get the error :

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 1: invalid start byte

can someone please help me ? I just want to convert abc BACK to the string form i had originally input it in

1 Answer 1

1

Did you figure this out? Strings are encoded as Bytes32 in events:

https://docs.ethers.io/v5/api/utils/strings/#Bytes32String

p.s. you will not want to index your strings in the event. They are not null terminated and you will get an error.

I decided to go with the interface pattern in ethers:

https://github.com/ethers-io/ethers.js/issues/422

1
  • 1
    It is not correct that an indexed string will cause an error. For dynamic parameters like string and bytes it will index its keccak256 value.
    – Ismael
    Commented Sep 21, 2022 at 13:35

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.