Skip to content

Conversation

@Shelvak
Copy link

@Shelvak Shelvak commented Dec 1, 2021

ExW3.Contract.get_logs function to get formatted logs between blocks for a given "contract".

Need

The get_filter_changes is useful but has 2 problems (at least for me):
1- The query doesn't return logs between 2 given block numbers, just the "new" (like the name specify =) )
2- After a few manual tests, the filter_id disappear after a minute or so without quering. If the supervisor die for some reason you can't get the "missing changes" You have to create a new filter id and start from "now".

To work around these 2 issues, I prefer directly ask for a block range and process the returned logs, then ask again from where I left.

If anything can be improved let me know =)
I'll let a few comments in the PR to let you know why I had to make changes.

Manually tested on Polygon

ExW3.Contract.register(:DAI, abi: ExW3.Abi.load_abi("./dai.abi")) # only with Transfer event
ExW3.Contract.at(:DAI, "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063")
{:ok, logs} = ExW3.Contract.get_logs(:DAI, %{fromBlock:  21986395, toBlock: 21986397}) # logs are properly formatted

raise "Number of provided arguments to constructor is incorrect. Was given #{
arg_count
} args, looking for #{input_types_count}."
raise "Number of provided arguments to constructor is incorrect. Was given #{arg_count} args, looking for #{input_types_count}."
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mix format

Comment on lines +351 to +354
event_data[key]
|> Integer.to_string(16)
|> String.downcase()
|> String.replace_prefix("", "0x")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change these lines because most RPC don't accept numbers with leading 0's like:

  Errors encountered in param 0: Invalid value "0x014f7c5b" supplied to : RpcFilterRequest/fromBlock: (QUANTITY | "earliest" | "latest" | "pending") | undefined/0: QUANTITY, Invalid value "0x0000000000000000000000000000000000000000000000000000000000000001" supplied to
: RpcFilterRequest/fromBlock: (QUANTITY | "earliest" | "latest" | "pending") | undefined/1: "earliest" | "latest" | "pending", Invalid value "0x0000000000000000000000000000000000000000000000000000000000000001" supplied to : RpcFilterRequest/toBlock: (QUANTITY | "earlie
st" | "latest" | "pending") | undefined/0: QUANTITY, Invalid value "0x0000000000000000000000000000000000000000000000000000000000000002" supplied to : RpcFilterRequest/toBlock: (QUANTITY | "earliest" | "latest" | "pending") | undefined/1: "earliest" | "latest" | "pendin
g"

Tested against hardhat native node & https://polygon-rpc.com & alchemy.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember going about adding the 0x prefix a long time ago to fix other bugs. Maybe it isn't needed for logs.

@rupurt do you think this would break on other eth clients?

assert {:ok, accounts} = ExW3.Client.call_client(:eth_accounts)
assert Enum.count(accounts) > 0
assert ["0x" <> _ = account] = accounts
assert ["0x" <> _ = account | _] = accounts
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When hardhat node or any other pre-populated you always have more than 1 account so, just ignore it =)

assert log["transactionHash"] == simple_tx_hash
end

test "Testing formatted get logs", context do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on get_filter_changes test

@hswick
Copy link
Owner

hswick commented Dec 20, 2021

@Shelvak Thanks! These are welcome changes. Your approach to block watching is certainly the safest, glad that exw3 can be helpful for you.

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

Successfully merging this pull request may close these issues.

2 participants