web3-core#BatchRequest TypeScript Examples
The following examples show how to use
web3-core#BatchRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: generateBatchRequests.ts From multisig-react with MIT License | 5 votes |
generateBatchRequests = <ReturnValues>({
abi,
address,
batch,
context,
methods,
}: Props): Promise<ReturnValues> => {
const contractInstance = new web3.eth.Contract(abi, address)
const localBatch = new web3.BatchRequest()
const values = methods.map((methodObject) => {
let method,
type,
args: MethodsArgsType = []
if (typeof methodObject === 'string') {
method = methodObject
} else {
;({ method, type, args } = methodObject)
}
return new Promise((resolve) => {
const resolver = (error, result) => {
if (error) {
resolve(undefined)
} else {
resolve(result)
}
}
try {
let request
if (type !== undefined) {
request = web3[type][method].request(...args, resolver)
} else {
if (address === null) {
resolve(undefined)
return
}
request = contractInstance.methods[method](...args).call.request(resolver)
}
// If batch was provided add to external batch
batch ? batch.add(request) : localBatch.add(request)
} catch (e) {
console.warn('There was an error trying to batch request from web3.', e)
resolve(undefined)
}
})
})
// TODO fix this so all batch.execute() are handled here
// If batch was created locally we can already execute it
// If batch was provided we should execute once we finish to generate the batch,
// in the outside function where the batch object is created.
!batch && localBatch.execute()
// @ts-ignore
return Promise.all([context, ...values])
}