|
| 1 | +import type { AbiEvent } from "abitype"; |
| 2 | +import { |
| 3 | + type BlockNumber, |
| 4 | + type Chain, |
| 5 | + type Client, |
| 6 | + type GetLogsParameters, |
| 7 | + type GetLogsReturnType, |
| 8 | + HttpRequestError, |
| 9 | + type Transport, |
| 10 | +} from "viem"; |
| 11 | +import { getLogs } from "viem/actions"; |
| 12 | + |
| 13 | +interface BlockRange { |
| 14 | + fromBlock: bigint; |
| 15 | + toBlock: bigint; |
| 16 | +} |
| 17 | + |
| 18 | +export async function getLogsSafe< |
| 19 | + chain extends Chain | undefined, |
| 20 | + const abiEvent extends AbiEvent | undefined = undefined, |
| 21 | + const abiEvents extends |
| 22 | + | readonly AbiEvent[] |
| 23 | + | readonly unknown[] |
| 24 | + | undefined = abiEvent extends AbiEvent ? [abiEvent] : undefined, |
| 25 | + strict extends boolean | undefined = undefined, |
| 26 | +>( |
| 27 | + client: Client<Transport, chain>, |
| 28 | + params: GetLogsParameters< |
| 29 | + abiEvent, |
| 30 | + abiEvents, |
| 31 | + strict, |
| 32 | + BlockNumber, |
| 33 | + BlockNumber |
| 34 | + > = {}, |
| 35 | +): Promise< |
| 36 | + GetLogsReturnType<abiEvent, abiEvents, strict, BlockNumber, BlockNumber> |
| 37 | +> { |
| 38 | + try { |
| 39 | + const events = await getLogs< |
| 40 | + chain, |
| 41 | + abiEvent, |
| 42 | + abiEvents, |
| 43 | + strict, |
| 44 | + BlockNumber, |
| 45 | + BlockNumber |
| 46 | + >(client, params); |
| 47 | + return events; |
| 48 | + } catch (e) { |
| 49 | + const fromBlock = params.fromBlock as bigint; |
| 50 | + const toBlock = params.toBlock as bigint; |
| 51 | + const bisected = tryBisectBlockRange({ fromBlock, toBlock }, e); |
| 52 | + if (!bisected) { |
| 53 | + throw e; |
| 54 | + } |
| 55 | + |
| 56 | + const [left, right] = await Promise.all([ |
| 57 | + getLogs(client, { ...params, ...bisected[0] } as any), |
| 58 | + getLogs(client, { ...params, ...bisected[1] } as any), |
| 59 | + ]); |
| 60 | + return [...left, ...right] as GetLogsReturnType< |
| 61 | + abiEvent, |
| 62 | + abiEvents, |
| 63 | + strict, |
| 64 | + BlockNumber, |
| 65 | + BlockNumber |
| 66 | + >; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function tryBisectBlockRange( |
| 71 | + { fromBlock, toBlock }: BlockRange, |
| 72 | + e: any, |
| 73 | +): [BlockRange, BlockRange] | undefined { |
| 74 | + const alchemyMid = checkForAlchemyBlockRange(e); |
| 75 | + if (alchemyMid && alchemyMid > fromBlock && alchemyMid < toBlock) { |
| 76 | + return [ |
| 77 | + { fromBlock, toBlock: alchemyMid }, |
| 78 | + { fromBlock: alchemyMid + 1n, toBlock }, |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + const blockRangeErrors = [ |
| 83 | + "query exceeds max block", |
| 84 | + "range is too large", |
| 85 | + "eth_getLogs is limited to", |
| 86 | + "eth_getLogs requests with up to", |
| 87 | + ]; |
| 88 | + |
| 89 | + if ( |
| 90 | + e instanceof Error && |
| 91 | + blockRangeErrors.some(errorText => e.message.includes(errorText)) |
| 92 | + ) { |
| 93 | + const middle = (fromBlock + toBlock) / 2n; |
| 94 | + return [ |
| 95 | + { fromBlock, toBlock: middle }, |
| 96 | + { fromBlock: middle + 1n, toBlock }, |
| 97 | + ]; |
| 98 | + } |
| 99 | + return undefined; |
| 100 | +} |
| 101 | + |
| 102 | +const ALCHEMY_BLOCK_RANGE_REGEX = |
| 103 | + /this block range should work: \[(0x[0-9a-fA-F]+),\s*(0x[0-9a-fA-F]+)\]/; |
| 104 | + |
| 105 | +function checkForAlchemyBlockRange(e: any): bigint | undefined { |
| 106 | + if (e instanceof HttpRequestError) { |
| 107 | + try { |
| 108 | + // exmple of alchemy error: |
| 109 | + // Details: {"code":-32600,"message":"You can make eth_getLogs requests with up to a 10000 block range. Based on your parameters, this block range should work: [0x9538b4, 0x955fc3]"} |
| 110 | + const err = JSON.parse(e.details); |
| 111 | + if (typeof err.message === "string") { |
| 112 | + const match = err.message.match(ALCHEMY_BLOCK_RANGE_REGEX); |
| 113 | + if (match) { |
| 114 | + return BigInt(match[2]); |
| 115 | + } |
| 116 | + } |
| 117 | + } catch {} |
| 118 | + } |
| 119 | + return undefined; |
| 120 | +} |
0 commit comments