Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error processing empty data field in event #114

Closed
yashh opened this issue Oct 10, 2022 · 1 comment
Closed

Error processing empty data field in event #114

yashh opened this issue Oct 10, 2022 · 1 comment

Comments

@yashh
Copy link
Contributor

yashh commented Oct 10, 2022

Hello, First off want to thank the authors and maintainers so bringing this project to life.

logs = ExW3.get_logs(
        %{
          fromBlock: "0x" <> Integer.to_string(fromBlock, 16),
          toBlock: "0x" <> Integer.to_string(toBlock, 16),
          # [A, B] topics means A and B so lets send [[A, B]] which means OR
          topics: [["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                         "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0"]]
        }
      )

abi = File.read!(Path.join(Application.app_dir(:myapp, "priv"), "static/721abi.json"))
       |> Jason.decode!()
       |> ABI.parse_specification(include_events?: true)

events =
      logs
      |> Enum.map(fn log ->
        [topic1, topic2, topic3, topic4] =
          case length(log["topics"]) do
            4 -> log["topics"]
            3 -> log["topics"] ++ [nil]
            2 -> log["topics"] ++ [nil, nil]
            _ -> raise "Invalid topics"
          end
       abi
        |> ABI.Event.find_and_decode(
          convert_hex_to_binary(topic1),
          convert_hex_to_binary(topic2),
          convert_hex_to_binary(topic3),
          convert_hex_to_binary(topic4),
          log["data"]
        )

The above code fetches logs for a block range and tries to extract event from it. But it fails on log when its data is 0x.

** (MatchError) no match of right hand side value: "0x"
    (ex_abi 0.5.13) lib/abi/type_decoder.ex:371: ABI.TypeDecoder.decode_uint/2
    (ex_abi 0.5.13) lib/abi/type_decoder.ex:190: anonymous fn/3 in ABI.TypeDecoder.do_decode_raw/2
    (elixir 1.14.0) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ex_abi 0.5.13) lib/abi/type_decoder.ex:185: ABI.TypeDecoder.do_decode_raw/2
    (ex_abi 0.5.13) lib/abi/type_decoder.ex:179: ABI.TypeDecoder.decode_raw/2
    (ex_abi 0.5.13) lib/abi/event.ex:84: ABI.Event.find_and_decode/6
    (Myapp 0.1.0) lib/myapp/chain/events.ex:60: anonymous fn/1 in myapp.Chain.Events.get_logs/3
    (elixir 1.14.0) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
    (elixir 1.14.0) lib/enum.ex:1658: Enum."-map/2-lists^map/1-0-"/2
    iex:2: (file)

Try for block range 15714764, 15714774 on mainnet and you can reproduce it. When i converted data 0x to integer the function succeeds but it returns get the wad / tokenId wrong in the event. Can the library handle topics and data in hex itself? Curious to get your thoughts as well.

Just found this https://github.com/wmitsuda/topic0/blob/main/docs/gotchas.md

So %ABI.FunctionSignature{} is querying by methodId. Can we figure out a way to add args to that calculation and query ? That way we can use the right ABI to decode the log.

@ayrat555
Copy link
Collaborator

resolved by #115

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants