airalab / hs-web3

Web3 API for Haskell
http://hackage.haskell.org/package/web3
Apache License 2.0
190 stars 68 forks source link

Fix how indexed event parameters of dynamically-sized types are handled #132

Open dhruvio opened 2 years ago

dhruvio commented 2 years ago

Description

When an event has an indexed parameter of type bytes, the abi and abiFrom template Haskell helpers produce Tagged n Bytes types inside an event's *Indexed and event types. Instead, they should produce Tagged n (BytesN 32) types for these indexed parameters instead given the documentation states that the keccak256 hash of dynamically-sized types is what is indexed, not the underlying bytes value.

For my use-case, I was trying to get logs from Ethereum for events with an indexed bytes parameter. However, I received a run-time error because decoding the event was failing due to a EventParseFailure "[\"Failed reading: getBytes: negative length requested\\nEmpty call stack\\n\"]" error. I manually dumped the splices for my contract's ABI .hs module into a separate module and imported that directly instead of the quasi-quoted version (after adding imports and pacifying the compiler). Then, I changed the indexed parameter in the file to use BytesN 32 instead of Bytes, and the event parse succeeded.

From the documentation:

Source: https://docs.soliditylang.org/en/v0.8.13/abi-spec.html#indexed-event-encoding

Indexed event parameters that are not value types, i.e. arrays and structs are not stored directly but instead a keccak256-hash of an encoding is stored. This encoding is defined as follows:

Requested Changes

Network.Ethereum.Contract.TH should be updated to fix the types generated for bytes and string indexed parameters for events. It may be worthwhile using a richer type, like Digest Keccak256, instead of BytesN 32.

Also, the above-linked documentation states that indexed array and struct event parameters are also hashed using keccak256. Those may need to be addressed as a part of this issue too.

akru commented 2 years ago

Thank you for report! Feel free to create PR with fix if you wish.