Search code examples
javascriptnode.jsweb3js

web3js Batch Request response doesn't have any context


I am sending batch requests to a moralis avalanche node which (half the time) returns token prices, but it only returns prices to the callback, I can't see the token addresses or anything related to the request I sent. Is there anyway to have some context sent to the callback? Or maybe a better way to send batch requests?

here is the callback:

const callbackMethod = (error, result) => {
  if (error) {
    console.log("error");
  } else {
    result.map((res) => {
      console.log(res.toString())
    })
  }
}

and the requests:

const generateBatch = () => {
  var batch = new web3.BatchRequest();
  for (let i = 0; i < tokens.length; i++) {
    for (let j = 0; j < tokens.length; j++) {
      if (i != j) {
        batch.add(contract.methods
                .function(params)
                .call.request({from: web3.eth.accounts.wallet.accounts["0"].address}, callbackMethod));
      }
    }
  }
  return batch;
}

Solution

  • My implementation was to write a wrapper around the function I was using (I was already going to deploy a contract for this project) and make it return both the prices and the path.