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

Contract Address Must Be Trimmed In EthFilter #405

Closed
cerberus17 opened this issue Mar 3, 2018 · 9 comments
Closed

Contract Address Must Be Trimmed In EthFilter #405

cerberus17 opened this issue Mar 3, 2018 · 9 comments

Comments

@cerberus17
Copy link

Web3j Version: 3.3.1
TestRPC: 4.1.3

Subscribing to events using an EthFilter created with a contract address does not receive any events.

Web3j web3j = Web3j.build(new HttpService());

SimpleContract contract = SimpleContract.load(...);

EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress());

web3j.ethLogObservable(filter).subscribe(log -> System.out.println(log.toString()));

Nothing will be printed when activity occurs in the contract.

However, if you change the EthFilter creation to below, the events are correctly logged.

EthFilter filter = new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contract.getContractAddress().substring(2));

It appears that when a non-null address is provided to EthFilter, there is an attempt to add '0x' to the beginning somewhere which is causing the filter to not match any results.

@tjanson
Copy link

tjanson commented Nov 5, 2018

Thanks. I think this is an important issue because apparently many people run into it (see StackOverflow) and there’s no clear error message (in fact, no indication that anything’s gone wrong at all).

I suppose simply calling cleanHexPrefix in the EthFilter constructor would do the trick.

Or is this behavior intentional in order to enforce some spec? @conor10
In that case it would be good to add this caveat to the documentation. (It correctly omits the "0x" prefix – but that may not be obvious.)

@tjanson
Copy link

tjanson commented Nov 5, 2018

Apparently this does not affect all clients, so it might be “their fault” (e.g., Truffle’s ganache-cli). But regardless it would be helpful to deal with this issue in Web3j.

@tjanson
Copy link

tjanson commented Nov 7, 2018

I’ve now noticed that geth requires the 0x; without it, I get this:

org.web3j.protocol.core.filters.FilterException: Invalid request: invalid argument 0: invalid address at index 0: hex string without 0x prefix

@ice09
Copy link

ice09 commented Nov 25, 2018

It would be great if this get fixed OR is mentioned somewhere, this would be fine as well.
But as the webj-maven-plugin generates sources with the wrong EthFiler creation, so this is misleading and costs people a lot of time.

Just to sum up:
Versions: web3j: 3.5, 3.6
web3j-maven-plugin: 0.3.5, 0.3.7

With TestRPC/Ganache, the address submitted to EthFilter must exclude the beginning "0x", for Geth it must include "0x".

The problem with the Maven generated sources is the inclusion of "0x" in the specific filter add method, so the method
(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock)
will never work with Ganache/TestRPC, but
(EthFilter)
has to be used as the address (with beginning "0x" or not) must be added on filter cretation.

This might even be a bug in Ganache/TestRPC, but it should be noted somewhere in web3j or the Maven plugin.

@tjanson
Copy link

tjanson commented Nov 25, 2018

I think ganache-cli is misbehaving.

Here are relevant quotes from the spec:

When encoding UNFORMATTED DATA (byte arrays, account addresses, hashes, bytecode arrays): encode as hex, prefix with "0x", two hex digits per byte. [1]

address: DATA|Array, 20 Bytes - (optional) Contract address or a list of addresses from which logs should originate. [2]

"address": "0x8888f1f195afa192cfee860698584c030f4c9db1",

I’ll take it to the truffle guys, should be an easy fix.

@tjanson
Copy link

tjanson commented Nov 25, 2018

While trying to look into this I discovered that ganache-cli v6.2.x doesn’t seem to have the problem anymore, though I don’t immediately see what changed.

Edit: It was fixed here, while adding support for filtering with an array of addresses.

It doesn’t matter anymore, but the issue was as follows: Even single addresses were passed as an array ["0x1234…"] to eth_newFilter. ganache-cli delegates filter creation to MetaMask’s web3-provider-engine, which at the time did not support arrays (even of a single address). Instead, this line of the normalizeHex function misbehaves in a way only Javascript can:

  • It expects a string and slices it to get the first two characters. Since Array also has a slice, the comparison returns false but does not error.
  • Concatenating a String and an Array also “works” (yay!) and returns "0x0x1234…".

So close and yet so far.


Also, unrelatedly: they changed something about the txHash algorithm which makes transactions throw.

@iikirilov
Copy link
Contributor

Is this still an issue?

@ice09
Copy link

ice09 commented Mar 15, 2019

No, tested with Ganache-1.3.1 and both versions (with prefix "0x" and without) do work now.

From my side, this can be closed, can't reproduce the issue.

@adneerav
Copy link

adneerav commented May 23, 2019

Hello,

Not sure why but I am facing same issue with web3j. I tried by using contract addres 0x prefix and without it and result below error

Invalid request: invalid argument 0: invalid address at index 0: hex string without 0x prefix

val filter: EthFilter =
            EthFilter(
                DefaultBlockParameterName.EARLIEST,
                DefaultBlockParameterName.LATEST,
                CONTRACT_ADDRESS_HERE
            )
                .addOptionalTopics("logs", "log", "StatusCallbackEvent", "PrintLog").addSingleTopic("PrintLog")
        val filterResponse = BaseApplication.instance.web3j?.ethLogFlowable(filter)?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe({ logdata ->
                Log.e("StatusCallbackEvent", logdata.data)
            }, { throwable ->
                Log.e("StatusCallbackEvent", throwable.message)
            }
            )

Tried address with 0x : 0x163897f32f08cdf177126ba47e1d156e667e1392
And without 0x : 163897f32f08cdf177126ba47e1d156e667e1392

Always getting error in throwable

Android Web3j Version : 'org.web3j:core:4.2.0-android'
Geth Version : Geth/v1.8.27-stable-4bcc0a37/linux-amd64/go1.10.4

I am not using ganache or any wrapper.

Contracts methods are executing successfully. But the logs which I've added in methods are getting cached in above source.Fetching transaction details working fine as per below

 val subscription = EVM.web3jObj?.transactionFlowable()?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())?.subscribe { tx ->
                Log.e("TransactionDetails", tx.toString()) // Working
            }

I am not using ganache or any wrapper.

Can anyone please help me or guide me to make this working?

Thanks

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

5 participants