1- import type { BlockType } from '../types' ;
2-
3- import { type PublicClient , parseAbiItem } from 'viem' ;
4- import { getBlock , getBlockNumber , getLogs } from 'viem/actions' ;
1+ import type { GetEventArgs } from 'viem' ;
2+ import { ERC5564AnnouncerAbi } from '../../abi' ;
3+ import { fetchLogsInChunks } from '../../helpers/logs' ;
54import { handleViemPublicClient } from '../../stealthClient/createStealthClient' ;
65import type {
7- AnnouncementLog ,
6+ AnnouncementArgs ,
87 GetAnnouncementsParams ,
98 GetAnnouncementsReturnType
109} from './types' ;
1110
11+ type AnnouncementFilter = GetEventArgs <
12+ typeof ERC5564AnnouncerAbi ,
13+ 'Announcement'
14+ > ;
15+
1216/**
1317 * This function queries logs for the `Announcement` event emitted by the ERC5564 contract.
1418 *
@@ -29,127 +33,33 @@ async function getAnnouncements({
2933} : GetAnnouncementsParams ) : Promise < GetAnnouncementsReturnType > {
3034 const publicClient = handleViemPublicClient ( clientParams ) ;
3135
32- const fetchParams = {
33- address : ERC5564Address ,
34- args
35- } ;
36-
3736 const logs = await fetchLogsInChunks ( {
38- publicClient,
39- fetchParams,
37+ publicClient : publicClient ,
38+ abi : ERC5564AnnouncerAbi ,
39+ eventName : 'Announcement' ,
40+ address : ERC5564Address ,
41+ args : convertAnnouncementArgs ( args ) ,
4042 fromBlock,
4143 toBlock
4244 } ) ;
4345
44- // Extract the relevant data from the logs
45- const announcements : AnnouncementLog [ ] = logs . map ( log => {
46- const { args } = log ;
47-
48- return {
49- schemeId : args . schemeId ,
50- stealthAddress : args . stealthAddress ,
51- caller : args . caller ,
52- ephemeralPubKey : args . ephemeralPubKey ,
53- metadata : args . metadata ,
54- ...log
55- } ;
56- } ) ;
57-
58- return announcements ;
46+ return logs . map ( log => ( {
47+ schemeId : log . args . schemeId ,
48+ stealthAddress : log . args . stealthAddress ,
49+ caller : log . args . caller ,
50+ ephemeralPubKey : log . args . ephemeralPubKey ,
51+ metadata : log . args . metadata ,
52+ ...log
53+ } ) ) ;
5954}
6055
61- /**
62- * Fetches logs in chunks to handle potential large range queries efficiently.
63- *
64- * @param {Object } params - The parameters for fetching logs in chunks.
65- * - `publicClient`: An instance of the viem `PublicClient`.
66- * - `fetchParams`: Parameters for the log fetch query.
67- * - `fromBlock`: The starting block number for the fetch.
68- * - `toBlock`: The ending block number for the fetch.
69- * - `chunkSize`: The number of blocks to query in each chunk.
70- * @returns {Promise<GetLogsReturnType> } A flattened array of all logs fetched in chunks.
71- */
72- const fetchLogsInChunks = async ( {
73- publicClient,
74- fetchParams,
75- fromBlock,
76- toBlock,
77- chunkSize = 5000 // Default chunk size, can be adjusted
78- } : {
79- publicClient : PublicClient ;
80- fetchParams : {
81- address : `0x${string } `;
82- // biome-ignore lint/suspicious/noExplicitAny: TODO handle better
83- args : any ;
84- fromBlock ?: BlockType ;
85- toBlock ?: BlockType ;
86- } ;
87- fromBlock ?: BlockType ;
88- toBlock ?: BlockType ;
89- chunkSize ?: number ;
90- } ) => {
91- const resolvedFromBlock =
92- ( await resolveBlockNumber ( {
93- publicClient,
94- block : fromBlock ?? 'earliest'
95- } ) ) || BigInt ( 0 ) ;
96-
97- const resolvedToBlock = await resolveBlockNumber ( {
98- publicClient,
99- block : toBlock ?? 'latest'
100- } ) ;
101-
102- let currentBlock = resolvedFromBlock ;
103- const allLogs = [ ] ;
104-
105- while ( currentBlock <= resolvedToBlock ) {
106- // Calculate the end block for the current chunk
107- const endBlock =
108- currentBlock + BigInt ( chunkSize ) < resolvedToBlock
109- ? currentBlock + BigInt ( chunkSize )
110- : resolvedToBlock ;
111-
112- const logs = await getLogs ( publicClient , {
113- ...fetchParams ,
114- event : parseAbiItem (
115- 'event Announcement(uint256 indexed schemeId,address indexed stealthAddress,address indexed caller,bytes ephemeralPubKey,bytes metadata)'
116- ) ,
117- fromBlock : currentBlock ,
118- toBlock : endBlock ,
119- strict : true
120- } ) ;
121- allLogs . push ( ...logs ) ;
122- currentBlock = endBlock + BigInt ( 1 ) ;
123- }
124-
125- return allLogs ;
126- } ;
127-
128- /**
129- * Resolves a block number from a given block type (number, tag, or bigint).
130- *
131- * @param {Object } params - Parameters for resolving the block number.
132- * - `publicClient`: An instance of the viem `PublicClient`.
133- * - `block`: The block number or tag to resolve.
134- * @returns {Promise<bigint> } The resolved block number as a bigint or null.
135- */
136- export async function resolveBlockNumber ( {
137- publicClient,
138- block
139- } : {
140- publicClient : PublicClient ;
141- block ?: BlockType ;
142- } ) : Promise < bigint > {
143- if ( typeof block === 'bigint' ) {
144- return block ;
145- }
146-
147- const { number } = await getBlock ( publicClient , { blockTag : block } ) ;
148- // Get the latest block number if null, since it is the pending block
149- if ( ! number ) {
150- return getBlockNumber ( publicClient ) ;
151- }
152- return number ;
56+ // Helper function to convert AnnouncementArgs to the array format Viem expects
57+ function convertAnnouncementArgs ( args : AnnouncementArgs ) {
58+ return [
59+ args . schemeId === undefined ? undefined : args . schemeId ,
60+ args . stealthAddress === undefined ? undefined : args . stealthAddress ,
61+ args . caller === undefined ? undefined : args . caller
62+ ] as AnnouncementFilter ;
15363}
15464
15565export default getAnnouncements ;
0 commit comments