- Consolidated duplicate UndoManagers to single instance - Fixed connection promise to only resolve on 'connected' status - Fixed WebSocketProvider import (WebsocketProvider) - Added proper doc.destroy() cleanup - Renamed isPresenceInitialized property to avoid conflict Co-Authored-By: Paperclip <noreply@paperclip.ing>
219 lines
6.6 KiB
TypeScript
219 lines
6.6 KiB
TypeScript
import type { AbiEvent, Address } from 'abitype'
|
|
|
|
import type { Client } from '../../clients/createClient.js'
|
|
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
import type { ErrorType } from '../../errors/utils.js'
|
|
import type { BlockNumber, BlockTag } from '../../types/block.js'
|
|
import type { Chain } from '../../types/chain.js'
|
|
import type {
|
|
MaybeAbiEventName,
|
|
MaybeExtractEventArgsFromAbi,
|
|
} from '../../types/contract.js'
|
|
import type { Log } from '../../types/log.js'
|
|
import type { Hash, LogTopic } from '../../types/misc.js'
|
|
import type { RpcLog } from '../../types/rpc.js'
|
|
import type { DecodeEventLogErrorType } from '../../utils/abi/decodeEventLog.js'
|
|
import {
|
|
type EncodeEventTopicsErrorType,
|
|
type EncodeEventTopicsParameters,
|
|
encodeEventTopics,
|
|
} from '../../utils/abi/encodeEventTopics.js'
|
|
import { parseEventLogs } from '../../utils/abi/parseEventLogs.js'
|
|
import type { RequestErrorType } from '../../utils/buildRequest.js'
|
|
import {
|
|
type NumberToHexErrorType,
|
|
numberToHex,
|
|
} from '../../utils/encoding/toHex.js'
|
|
import {
|
|
type FormatLogErrorType,
|
|
formatLog,
|
|
} from '../../utils/formatters/log.js'
|
|
|
|
export type GetLogsParameters<
|
|
abiEvent extends AbiEvent | undefined = undefined,
|
|
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,
|
|
//
|
|
_eventName extends string | undefined = MaybeAbiEventName<abiEvent>,
|
|
> = {
|
|
/** Address or list of addresses from which logs originated */
|
|
address?: Address | Address[] | undefined
|
|
} & (
|
|
| {
|
|
event: abiEvent
|
|
events?: undefined
|
|
args?: MaybeExtractEventArgsFromAbi<abiEvents, _eventName> | undefined
|
|
/**
|
|
* Whether or not the logs must match the indexed/non-indexed arguments on `event`.
|
|
* @default false
|
|
*/
|
|
strict?: strict | undefined
|
|
}
|
|
| {
|
|
event?: undefined
|
|
events: abiEvents
|
|
args?: undefined
|
|
/**
|
|
* Whether or not the logs must match the indexed/non-indexed arguments on `event`.
|
|
* @default false
|
|
*/
|
|
strict?: strict | undefined
|
|
}
|
|
| {
|
|
event?: undefined
|
|
events?: undefined
|
|
args?: undefined
|
|
strict?: undefined
|
|
}
|
|
) &
|
|
(
|
|
| {
|
|
/** Block number or tag after which to include logs */
|
|
fromBlock?: fromBlock | BlockNumber | BlockTag | undefined
|
|
/** Block number or tag before which to include logs */
|
|
toBlock?: toBlock | BlockNumber | BlockTag | undefined
|
|
blockHash?: undefined
|
|
}
|
|
| {
|
|
fromBlock?: undefined
|
|
toBlock?: undefined
|
|
/** Hash of block to include logs from */
|
|
blockHash?: Hash | undefined
|
|
}
|
|
)
|
|
|
|
export type GetLogsReturnType<
|
|
abiEvent extends AbiEvent | undefined = undefined,
|
|
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,
|
|
//
|
|
_eventName extends string | undefined = MaybeAbiEventName<abiEvent>,
|
|
_pending extends boolean =
|
|
| (fromBlock extends 'pending' ? true : false)
|
|
| (toBlock extends 'pending' ? true : false),
|
|
> = Log<bigint, number, _pending, abiEvent, strict, abiEvents, _eventName>[]
|
|
|
|
export type GetLogsErrorType =
|
|
| DecodeEventLogErrorType
|
|
| EncodeEventTopicsErrorType
|
|
| FormatLogErrorType
|
|
| NumberToHexErrorType
|
|
| RequestErrorType
|
|
| ErrorType
|
|
|
|
/**
|
|
* Returns a list of event logs matching the provided parameters.
|
|
*
|
|
* - Docs: https://viem.sh/docs/actions/public/getLogs
|
|
* - Examples: https://stackblitz.com/github/wevm/viem/tree/main/examples/logs_event-logs
|
|
* - JSON-RPC Methods: [`eth_getLogs`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_getlogs)
|
|
*
|
|
* @param client - Client to use
|
|
* @param parameters - {@link GetLogsParameters}
|
|
* @returns A list of event logs. {@link GetLogsReturnType}
|
|
*
|
|
* @example
|
|
* import { createPublicClient, http, parseAbiItem } from 'viem'
|
|
* import { mainnet } from 'viem/chains'
|
|
* import { getLogs } from 'viem/public'
|
|
*
|
|
* const client = createPublicClient({
|
|
* chain: mainnet,
|
|
* transport: http(),
|
|
* })
|
|
* const logs = await getLogs(client)
|
|
*/
|
|
export async function getLogs<
|
|
chain extends Chain | undefined,
|
|
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,
|
|
>(
|
|
client: Client<Transport, chain>,
|
|
{
|
|
address,
|
|
blockHash,
|
|
fromBlock,
|
|
toBlock,
|
|
event,
|
|
events: events_,
|
|
args,
|
|
strict: strict_,
|
|
}: GetLogsParameters<abiEvent, abiEvents, strict, fromBlock, toBlock> = {},
|
|
): 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[]
|
|
}
|
|
|
|
let logs: RpcLog[]
|
|
if (blockHash) {
|
|
logs = await client.request({
|
|
method: 'eth_getLogs',
|
|
params: [{ address, topics, blockHash }],
|
|
})
|
|
} else {
|
|
logs = await client.request({
|
|
method: 'eth_getLogs',
|
|
params: [
|
|
{
|
|
address,
|
|
topics,
|
|
fromBlock:
|
|
typeof fromBlock === 'bigint' ? numberToHex(fromBlock) : fromBlock,
|
|
toBlock: typeof toBlock === 'bigint' ? numberToHex(toBlock) : toBlock,
|
|
},
|
|
],
|
|
})
|
|
}
|
|
|
|
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 any,
|
|
logs: formattedLogs,
|
|
strict,
|
|
}) as unknown as GetLogsReturnType<
|
|
abiEvent,
|
|
abiEvents,
|
|
strict,
|
|
fromBlock,
|
|
toBlock
|
|
>
|
|
}
|