-
Hi, Is there any way to get coin and token balances in the same account with one query? For example, I have ETH account and in there, three ERC20 Tokens A,B,C. As far as I know, I can use BatchRequest for multiple request but I have no idea how to apply ERC20 balance query in it. Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can use this class - https://github.com/web3j/web3j/blob/master/core/src/main/java/org/web3j/protocol/core/BatchRequest.java |
Beta Was this translation helpful? Give feedback.
-
I have used batch call to the miner to simultaneously get a list of receipts. You can create similar for tokens or coins. public List<Object> getReceiptList(List<String> hashes) throws ServerInternalError {
if (hashes == null || hashes.isEmpty()) {
return Collections.emptyList();
}
BatchRequest batchRequest = web3j.newBatch();
for (String hash : hashes) {
batchRequest.add(web3j.ethGetTransactionReceipt(hash));
}
List<Object> listOfReceipt = new ArrayList<>(Collections.nCopies(hashes.size(), null));
BatchResponse batchResponse;
try {
batchResponse = batchRequest.send();
} catch (IOException e) {
throw new ServerInternalError("Unable to connect to miner!");
}
List<? extends Response<?>> response = batchResponse.getResponses();
if (response != null && !response.isEmpty()) {
for (int i = 0; i < hashes.size(); i++) {
Response<?> eachResp = response.get(i);
if (eachResp instanceof EthGetTransactionReceipt ethGetTransactionReceipt) {
if (ethGetTransactionReceipt.getTransactionReceipt().isPresent()) {
listOfReceipt.set(i, ethGetTransactionReceipt.getTransactionReceipt().get());
}
}
}
}
return listOfReceipt;
} |
Beta Was this translation helpful? Give feedback.
You can use this class - https://github.com/web3j/web3j/blob/master/core/src/main/java/org/web3j/protocol/core/BatchRequest.java
Create a new BatchRequest, add requests to this and lastly use send function to send the batchRequest.