Hello. I am using web3.js to connect to BSC mainnet.
I’m connecting to a Websocket URL and sending multiple requests (getTransactionReceipt) to Promise.all at once. Usually it works fine, but at some point I noticed that the script stops, and when I looked at the Quicknode dashboard, I saw that there was a -1 in the Response status.
I’ve looked through the documentation and can’t find anything about -1. Do you have any information that might help?
A standard -1 response on a Websocket response can mean you are hitting a rate limit amongst a few other things. Can you provide us with your code so we can take a look deeper? You can also open a ticket up with our team internally so we can investigate - https://support.quicknode.com/hc/en-us/requests/new
Thanks Nick!
But I’m still not sure why the script is stuck. I’m guessing it’s because the Web3.js library can’t handle a -1 response. When a -1 error occurs, does it return a failure message to the requestor or no value?
Here is my script.
async function main() {
const web3 = new Web('wss://...');
const block = await web3.eth.getBlock(n);
const batch: Promise<void>[] = [];
for await (const transaction of block.transaction) {
batch.push(handleTransaction(web3, transaction));
if (batch.length === 20) {
await Promise.all(batch);
batch = [];
await pause(150)
}
}
if (batch.length > 0) {
await Promise.all(batch):
await pause(150)
}
}
async function handleTransaction(web3: Web3, transaction: Transaction): Promise<void> {
const receipt = await web3.eth.getTransactionReceipt(transaction.hash);
//
// decodes events in receipt...
}
async function pause(duration: number) {
return new Promise((resolve) => {
const timeoutRef = setTimeout(resolve, duration);
});
}