Skip to content

Bug with ethGetBalance in the EmbeddedWeb3jService #88

@bbertolucci

Description

@bbertolucci

There is a bug in the manner of setting the result of ethGetBalance in the EmbeddedWeb3jService.kt

I tested on an alchemy ethereum branch :

val balance = web3j.ethGetBalance(accountAddress, DefaultBlockParameterName.LATEST).send()

And I got the result :
image

Now I tested the same code on embedded service and got :
image

The difference is coming from the 0 prefixing the value 0x 00000000000 ....
The object return is an EthGetBalance object with a getBalance()
getBalance will use Numeric.decodeQuantity(getResult())
Well I will not write all the details but it checks the validity of the string.
You can see the code here
One of the test is : value.length() > 3 && value.charAt(2) == '0'
So the value is incorrect and will return an Exception when having 0 following 0x

The error is coming from this code :
https://github.com/web3j/web3j-evm/blob/a40b044df37a0c3426dc24f71589b03327b3a8bc/src/main/kotlin/org/web3j/evm/EmbeddedEthereum.kt#L219-L227

and
https://github.com/web3j/web3j-evm/blob/a40b044df37a0c3426dc24f71589b03327b3a8bc/src/main/kotlin/org/web3j/evm/EmbeddedWeb3jService.kt#L220-L230

.map(Wei::toHexString) will add all those 0
The solution is to use: .map(Wei::toShortHexString) (Edit : Tested with success !!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions