Skip to content

Instantly share code, notes, and snippets.

@wivern-co-uk
Created April 28, 2021 13:10
Show Gist options
  • Select an option

  • Save wivern-co-uk/e3e2d3435c02766b0bd36947b89f4865 to your computer and use it in GitHub Desktop.

Select an option

Save wivern-co-uk/e3e2d3435c02766b0bd36947b89f4865 to your computer and use it in GitHub Desktop.
Batch RPC calls
updatePrices() {
const batch = new web3.eth.BatchRequest()
this.pairs = []
const requests = [];
serializedPairs.forEach(lp => {
requests.push(new Promise((resolve, reject) => {
const contract = new web3.eth.Contract(pancakePairAbis, lp.pair.address)
batch.add(contract.methods.getReserves().call.request((error, reserves) => {
this.requestCount++
if (error) return reject(error)
this.pairs.push(
this.newPairFromSerializedPairAndReserves(lp, reserves)
)
resolve(reserves);
}))
}))
});
batch.execute()
return Promise.all(requests);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment