web3dart
web3dart copied to clipboard
RangeError: Value not in range: 32
When I call the balanceOf function of a ERC-20 contract i get the Error 'RangeError: Value not in range: 32'. After some debug fun it seems that there is some kind of problem with decoding the return value of the call, but am not entierly sure why there is a problem. Here is the code im working with:
`ethereumTokenBalance(String contractAddress) async { var httpClient = Client(); Web3Client client = Web3Client(global.publicRpcEthereumMainNet, httpClient); var contract = await ContractParser.fromAssets('abi.json', contractAddress); var function = contract.function('balanceOf'); var balance = await client.call( contract: contract, function: function, params: [EthereumAddress.fromHex(global.ownAddress)]);
return balance;
}
class ContractParser {
static Future<DeployedContract> fromAssets(
String path, String contractAddress) async {
final contractJson =
jsonDecode(await rootBundle.loadString('assets/abi.json'));
return DeployedContract(
ContractAbi.fromJson(jsonEncode(contractJson), 'token'),
EthereumAddress.fromHex(contractAddress));
} }`
the abi.json is a norml ERC-20 contract.
I think the problem is your RPC URL for example, I get the same error with Ankr public Rpc Url but worked with Infura
Thanks, was indeed this. not sure why it doesnt workfor some RPC urls but changing indeed fixed it.
This issue needs attention.
@kaumudpa What is your url endpoint?
@xclud I am using injected metamask as my custom client.
Interstingly If we use direct RPC client - things work just fine.
Injected Metamask as a custom client throws this issue.
Issue is also seen on the prior plugin - https://github.com/simolus3/web3dart/issues/59
With the help of alchemy explorer i found the following-
While using Alchemy API as Direct RPC Client the request to alchemy is
{"id":"d17acd83-3c00-432a-b3cd-1036985e9c2d","jsonrpc":"2.0","method":"eth_call","params":[{"to":"0x7872eF9f1187bae5a522DDd0f29258e09976CEaE","data":"0x70a08231000000000000000000000000fad3b616bcd747a12a7c0a6203e7a481606b12e8"},"0x1f1b14b"]}
Response
{"jsonrpc":"2.0","id":"d17acd83-3c00-432a-b3cd-1036985e9c2d","result":"0x00000000000000000000000000000000000000000000000fc26804b0a01683f7"}
While using injected metamask as custom client for the same request.
{"id":2465261209,"jsonrpc":"2.0","method":"eth_call","params":[{},"0x1f1b2ad"]}
Response
{"jsonrpc":"2.0","id":2465261209,"result":"0x"}
Which one is the correct/expected Request & Response?
@xclud The first one is the correct request and hence we get the correct response.
For some reason with custom client the request doesn't send the right params
as you might have observed and hence we get just 0x
as response and that's the reason web3dart throws RangeError: Value not in range: 32
@xclud Any Idea what could be wrong? How do we resolve this?
@kaumudpa I am investigating. Your hints to the problem are pretty helpful.
@xclud any luck with this? If you have anything to share please do share.
Hi, is there any new info on this? thanks
For me, the issue was the DeployedContract
address. I was putting in the wallet address, instead of the token address.
final _usdcPolygonContract = DeployedContract(
ContractAbi.fromJson(_erc20ContractAbi, 'USDC'),
EthereumAddress.fromHex('0x9999f7Fea5938fD3b1E26A12c3f2fb024e194f97'),
);
client.call(
contract: _usdcPolygonContract,
function: _usdcPolygonContract.function('balanceOf'),
params: [ownAddress],
)
in case you're wondering, the contract's abi I used was
[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}]
and I'm hitting the https://rpc-mumbai.maticvigil.com/
rpc url