-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const blockNotFoundMessage = "block not found"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/block-logs-stream/src/getLogsFromLoadBalancedRpc.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
AbiEvent, | ||
BlockNumber, | ||
BlockTag, | ||
GetLogsParameters, | ||
GetLogsReturnType, | ||
EncodeEventTopicsParameters, | ||
LogTopic, | ||
RpcLog, | ||
encodeEventTopics, | ||
formatLog, | ||
numberToHex, | ||
parseEventLogs, | ||
toHex, | ||
Block, | ||
} from "viem"; | ||
import { blockNotFoundMessage } from "./common"; | ||
|
||
/** | ||
* Mostly equivalent to viem's `getLogs` action, but using a batch rpc call to check if the RPC has the requested block. | ||
* If the RPC doesn't have the requested block, it will throw a "block not found" error. | ||
* Expects a HTTP RPC endpoint that supports batch requests. | ||
*/ | ||
export async function getLogsFromLoadBalancedRpc< | ||
const abiEvent extends AbiEvent | undefined = undefined, | ||
const abiEvents extends readonly AbiEvent[] | readonly unknown[] | undefined = abiEvent extends AbiEvent | ||
? [abiEvent] | ||
: undefined, | ||
strict extends boolean | undefined = undefined, | ||
fromBlock extends BlockNumber | BlockTag | undefined = undefined, | ||
toBlock extends BlockNumber | BlockTag | undefined = undefined, | ||
>({ | ||
rpcUrl, | ||
address, | ||
fromBlock, | ||
toBlock, | ||
event, | ||
events: events_, | ||
args, | ||
strict: strict_, | ||
}: Omit<GetLogsParameters<abiEvent, abiEvents, strict, fromBlock, toBlock>, "blockHash"> & { rpcUrl: string }): Promise< | ||
GetLogsReturnType<abiEvent, abiEvents, strict, fromBlock, toBlock> | ||
> { | ||
const strict = strict_ ?? false; | ||
const events = events_ ?? (event ? [event] : undefined); | ||
|
||
let topics: LogTopic[] = []; | ||
if (events) { | ||
const encoded = (events as AbiEvent[]).flatMap((event) => | ||
encodeEventTopics({ | ||
abi: [event], | ||
eventName: (event as AbiEvent).name, | ||
args: events_ ? undefined : args, | ||
} as EncodeEventTopicsParameters), | ||
); | ||
// TODO: Clean up type casting | ||
topics = [encoded as LogTopic]; | ||
if (event) topics = topics[0] as LogTopic[]; | ||
} | ||
|
||
const requests = [ | ||
{ | ||
method: "eth_getBlockByNumber", | ||
params: [typeof toBlock === "bigint" || typeof toBlock === "number" ? toHex(toBlock) : toBlock, false], | ||
id: 1, | ||
jsonrpc: "2.0", | ||
}, | ||
{ | ||
method: "eth_getLogs", | ||
params: [ | ||
{ | ||
address, | ||
topics, | ||
fromBlock: typeof fromBlock === "bigint" ? numberToHex(fromBlock) : fromBlock, | ||
toBlock: typeof toBlock === "bigint" ? numberToHex(toBlock) : toBlock, | ||
}, | ||
], | ||
id: 2, | ||
jsonrpc: "2.0", | ||
}, | ||
]; | ||
|
||
const results: [Block | undefined, RpcLog[]] = await fetch(rpcUrl, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(requests), | ||
}).then((res) => res.json()); | ||
|
||
const [block, logs] = results; | ||
|
||
if (!block) { | ||
// Throw an explicit error so the caller can retry instead of silently returning an empty array | ||
throw new Error(blockNotFoundMessage); | ||
} | ||
|
||
const formattedLogs = logs.map((log) => formatLog(log)); | ||
if (!events) return formattedLogs as GetLogsReturnType<abiEvent, abiEvents, strict, fromBlock, toBlock>; | ||
return parseEventLogs({ | ||
abi: events, | ||
args: args as never, | ||
logs: formattedLogs, | ||
strict, | ||
}) as unknown as GetLogsReturnType<abiEvent, abiEvents, strict, fromBlock, toBlock>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters