Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BatchRequest Timeout Issue When Querying Token Balances #6967

Open
NongChen1223 opened this issue Apr 12, 2024 · 1 comment
Open

BatchRequest Timeout Issue When Querying Token Balances #6967

NongChen1223 opened this issue Apr 12, 2024 · 1 comment
Labels
4.x 4.0 related Bug Addressing a bug

Comments

@NongChen1223
Copy link

I am encountering a persistent issue with web3.BatchRequest where it consistently times out regardless of the query made to fetch token balances. Below are the steps and configurations I've tried to resolve the issue without success:

  • I have tried switching among multiple RPC nodes, but the timeout error persists.
  • I reduced the number of tokens queried in the batch to just one, but still, the timeout issue occurs.
  • I have explicitly set a timeout parameter in the batch.execute() method.
const newTokenContract = (address: string) => {
    const contract = new web3.eth.Contract(ERC20ABI, address);
    return contract;
};
const createRequest = (item: TokensType, index: number,wallet:string): JsonRpcOptionalRequest => {
    const baseData: Partial<JsonRpcOptionalRequest> = {
        jsonrpc: "2.0",
        id: index,
    };
    return {
        ...baseData,
        method: "eth_call",
        params: [
            {
                form: "0x514910771af9ca656af840dff83e8264ecf986ca",
                to: item.address,
                data: newTokenContract(item.address).methods.balanceOf(wallet).encodeABI(),
            },
            "latest",
        ],
    };
};
const createTokenJsonRpcList = async <
    R extends JsonRpcResult = JsonRpcResult,
    E extends JsonRpcResult = JsonRpcResult,
>(list:any[],wallet:string):Promise<HoldType[] | []> => {
    try {
        const web3 = new Web3(ETH_RPC_URL);
        const batch = new web3.BatchRequest();
        list.forEach((item, i) => {
            batch.add(createRequest(item, i,wallet));
        });
        const jsonResponse = (await batch.execute({
            timeout:3000
        })) as JsonRpcBatchResponse<R, E>;
        const holdList =  jsonResponse.map<HoldType>(waitMapped).filter(predicate);
        return holdList
    }catch (err){
        console.log('createTokenJsonRpcList error!',err)
        return  []
    }
};
const createchunkJsonRpc = async (list:any,chunkSize:number)=>{
    const waitMapFn = (_: (typeof list)[number], index: number) => list.slice(index * chunkSize, (index + 1) * chunkSize)
    const chunkList:any[] = Array.from({length: Math.ceil(list.length / chunkSize)}, waitMapFn)
    return chunkList
}
const getWalletBalance = async (list:TokensType[],wallet:string,chunkSize:number = 50) => {
    const priceList = []
    const { name,balance } = await getEthBalance(wallet)
    priceList.push({
        name,
        balance
    })
    const chunkList = await createchunkJsonRpc(list,chunkSize)
    const promiseList = chunkList.map(async (item:any,index:number) => {
        return await createTokenJsonRpcList(item,wallet)
    })
    const result = await Promise.allSettled(promiseList)
    result.forEach(result => {
        if (result.status === "fulfilled") {
            result.value.length > 0 && priceList.push(...result.value)
        }
    });
}
getWalletBalance(Tokens,WALLET_ADDRESS,20)

Logs

\node_modules\web3-core\src\web3_batch_request.ts:125
promise.reject(new OperationAbortError(msg));
^
OperationAbortError: Batch request timeout
at Web3BatchRequest._abortAllRequests (\webDeveloper\node_modules\web3-core\src\web3_batch_request.ts:125:19)
at (\webDeveloper\node_modules\web3-core\src\web3_batch_request.ts:68:10) {
innerError: undefined,
code: 204
}

Environment

"@uniswap/v3-sdk": "^3.11.0",
"axios": "^1.6.8",
"ethers": "^6.10.0",
"https-proxy-agent": "^7.0.4",
"isomorphic-ws": "^5.0.0",
"tsx": "^4.7.1",
"typescript": "^5.3.3",
"web3": "^4.6.0",
"ws": "^8.16.0"
@SantiagoDevRel
Copy link
Member

Hey @NongChen1223 thanks for sharing this issue, just to double check regarding the RPCs, did u make sure u are not exceeding the allowed requests?

  • Have you tried making the call 1 by 1 interacting with the contract object instead? (just to see if it works)
  • Make sure you change the word form to from in the eth_call params

Lmk if still doesn't work! thank u

@mconnelly8 mconnelly8 added 4.x 4.0 related Bug Addressing a bug labels Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x 4.0 related Bug Addressing a bug
Projects
None yet
Development

No branches or pull requests

3 participants