web3j-evm icon indicating copy to clipboard operation
web3j-evm copied to clipboard

Bug with ethGetBalance in the EmbeddedWeb3jService

Open bbertolucci opened this issue 3 years ago • 0 comments

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 !!)

bbertolucci avatar Sep 29 '22 02:09 bbertolucci