@ethersproject/providers#BlockTag TypeScript Examples

The following examples show how to use @ethersproject/providers#BlockTag. 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: rpcCall.ts    From defillama-sdk with GNU Affero General Public License v3.0 6 votes vote down vote up
export async function call(provider: BaseProvider, data: Deferrable<TransactionRequest>, block: BlockTag, chain?: string) {
  if (!chain) chain = 'noChain'
  const counter: Counter = getChainCounter(chain)
  const currentId = counter.requestCount++
  const eventId = `${chain}-${currentId}`

  if (counter.activeWorkers > maxParallelCalls) {
    counter.queue.push(eventId)
    await once(emitter, eventId)
  }

  counter.activeWorkers++

  if (DEBUG_MODE_ENABLED) {
    const showEveryX = counter.queue.length > 100 ? 50 : 10 // show log fewer times if lot more are queued up
    if (currentId % showEveryX === 0) console.log(`chain: ${chain} request #: ${currentId} queue: ${counter.queue.length} active requests: ${counter.activeWorkers}`)
  }

  let response
  try {
    response = await provider.call(data, block)
    onComplete()
  } catch (e) {
    onComplete()
    throw e
  }

  return response

  function onComplete() {
    counter.activeWorkers--
    if (counter.queue.length) {
      const nextRequestId = counter.pickFromTop ? counter.queue.shift() : counter.queue.pop()
      counter.pickFromTop = !counter.pickFromTop
      emitter.emit(<string> nextRequestId)
    }
  }
}
Example #2
Source File: index.ts    From ccip-read with MIT License 5 votes vote down vote up
async function handleCall(
  provider: CCIPReadProvider,
  params: { transaction: TransactionRequest; blockTag?: BlockTag },
  maxCalls = 4
): Promise<{ transaction: TransactionRequest; result: BytesLike }> {
  for (let i = 0; i < maxCalls; i++) {
    let result;
    let bytes: Uint8Array;
    try {
      result = await provider.parent.perform('call', params);
      bytes = arrayify(result);
    } catch (e) {
      if (isRevertError(e)) {
        bytes = arrayify(e.error.data.originalError.data);
      } else {
        return logger.throwError('The error message does not contain originalError', Logger.errors.UNKNOWN_ERROR);
      }
    }
    if (bytes.length % 32 !== 4 || hexlify(bytes.slice(0, 4)) !== CCIP_READ_INTERFACE.getSighash('OffchainLookup')) {
      return { transaction: params.transaction, result: bytes };
    }
    const { sender, urls, callData, callbackFunction, extraData } = CCIP_READ_INTERFACE.decodeErrorResult(
      'OffchainLookup',
      bytes
    );
    if (params.transaction.to === undefined || sender.toLowerCase() !== params.transaction.to.toLowerCase()) {
      return logger.throwError('OffchainLookup thrown in nested scope', Logger.errors.UNSUPPORTED_OPERATION, {
        to: params.transaction.to,
        sender,
        urls,
        callData,
        callbackFunction,
        extraData,
      });
    }
    const response = await sendRPC(provider.fetcher, urls, params.transaction.to, callData);
    const data = hexConcat([
      callbackFunction,
      defaultAbiCoder.encode(CCIP_READ_INTERFACE.getFunction('callback').inputs, [response, extraData]),
    ]);
    params = Object.assign({}, params, {
      transaction: Object.assign({}, params.transaction, { data }),
    });
  }
  return logger.throwError('Too many redirects', Logger.errors.TIMEOUT, { to: params.transaction.to });
}