1- /**
2- * 1. blockTime + contract (with abi) + no events -> logs with types and parsing *if* contract has abi defined
3- * 2. blockTime + contract (no abi) + no events -> logs with NO types but *with* parsing
4- * 3. blockTime + no contract + events -> logs with types and parsing (across all "addresses") (no contract filter)
5- * 4. blockTime + contract + events -> logs with types and parsing (filtered by contract address + event topics)
6- */
7-
81import type {
92 Abi ,
103 AbiEvent ,
114 ExtractAbiEvent ,
125 ExtractAbiEventNames ,
136} from "abitype" ;
147import { type Log , formatLog } from "viem" ;
15- import type { Chain } from "../../chains/types.js" ;
168import { getChainServices } from "../../chains/utils.js" ;
17- import type { ThirdwebClient } from "../../client/client.js" ;
189import { resolveContractAbi } from "../../contract/actions/resolve-abi.js" ;
1910import type { ThirdwebContract } from "../../contract/contract.js" ;
11+ import { getContractEvents as getContractEventsInsight } from "../../insight/get-events.js" ;
2012import { eth_blockNumber } from "../../rpc/actions/eth_blockNumber.js" ;
2113import {
2214 type GetLogsBlockParams ,
@@ -25,9 +17,7 @@ import {
2517} from "../../rpc/actions/eth_getLogs.js" ;
2618import { getRpcClient } from "../../rpc/rpc.js" ;
2719import { getAddress } from "../../utils/address.js" ;
28- import { getThirdwebDomains } from "../../utils/domains.js" ;
2920import { type Hex , numberToHex } from "../../utils/encoding/hex.js" ;
30- import { getClientFetch } from "../../utils/fetch.js" ;
3121import type { Prettify } from "../../utils/type-utils.js" ;
3222import { type PreparedEvent , prepareEvent } from "../prepare-event.js" ;
3323import { isAbiEvent } from "../utils.js" ;
@@ -189,7 +179,6 @@ export async function getContractEvents<
189179 ...restParams ,
190180 address : getAddress ( contract . address ) ,
191181 topics : e . topics ,
192- signature : `${ e ?. abiEvent . name } (${ e ?. abiEvent . inputs . map ( ( i ) => i . type ) . join ( "," ) } )` ,
193182 } ) )
194183 : // otherwise we want "all" events (aka not pass any topics at all)
195184 [ { ...restParams , address : getAddress ( contract . address ) } ] ;
@@ -203,12 +192,12 @@ export async function getContractEvents<
203192 logsParams . map ( ( p ) =>
204193 getLogsFromInsight ( {
205194 params : p ,
206- chain : contract . chain ,
207- client : contract . client ,
195+ contract,
208196 } ) ,
209197 ) ,
210198 ) ;
211- } catch {
199+ } catch ( e ) {
200+ console . warn ( "Error fetching from insight" , e ) ;
212201 // fetch from rpc
213202 logs = await Promise . all (
214203 logsParams . map ( ( ethLogParams ) => eth_getLogs ( rpcRequest , ethLogParams ) ) ,
@@ -232,95 +221,55 @@ export async function getContractEvents<
232221
233222async function getLogsFromInsight ( options : {
234223 params : GetLogsParamsExtra ;
235- chain : Chain ;
236- client : ThirdwebClient ;
237- signature ?: string ;
224+ contract : ThirdwebContract < Abi > ;
238225} ) : Promise < Log [ ] > {
239- const { params, chain , client , signature } = options ;
226+ const { params, contract } = options ;
240227
241- const chainServices = await getChainServices ( chain ) ;
228+ const chainServices = await getChainServices ( contract . chain ) ;
242229 const insightEnabled = chainServices . some (
243230 ( c ) => c . service === "insight" && c . enabled ,
244231 ) ;
245232
246233 if ( ! insightEnabled ) {
247- throw new Error ( `Insight is not available for chainId ${ chain . id } ` ) ;
234+ throw new Error (
235+ `Insight is not available for chainId ${ contract . chain . id } ` ,
236+ ) ;
248237 }
249238
250- try {
251- let baseUrl = `https://${ getThirdwebDomains ( ) . insight } /v1/events` ;
252- if ( params . address ) {
253- baseUrl += `/${ params . address } ` ;
254- if ( signature ) {
255- baseUrl += `/${ signature } ` ;
256- }
257- }
258- const url = new URL ( baseUrl ) ;
259-
260- url . searchParams . set ( "chain" , chain . id . toString ( ) ) ;
261- url . searchParams . set ( "limit" , "500" ) ; // this is max limit on insight
239+ const fromBlock =
240+ typeof params . fromBlock === "bigint" ? Number ( params . fromBlock ) : undefined ;
262241
263- if ( params . blockHash ) {
264- url . searchParams . set ( "filter_block_hash" , params . blockHash ) ;
265- } else {
266- if ( params . fromBlock ) {
267- const fromBlock =
268- typeof params . fromBlock === "bigint"
269- ? numberToHex ( params . fromBlock )
270- : params . fromBlock ;
271-
272- url . searchParams . set ( "filter_block_number_gte" , fromBlock ) ;
273- }
274- if ( params . toBlock ) {
275- const toBlock =
276- typeof params . toBlock === "bigint"
277- ? numberToHex ( params . toBlock )
278- : params . toBlock ;
279-
280- url . searchParams . set ( "filter_block_number_lte" , toBlock ) ;
281- }
282- }
242+ const toBlock =
243+ typeof params . toBlock === "bigint" ? Number ( params . toBlock ) : undefined ;
283244
284- if ( params . topics ) {
285- const args = params . topics . slice ( 1 ) ;
286- for ( const [ i , a ] of args . entries ( ) ) {
287- if ( a ) {
288- url . searchParams . set ( `filter_topic_${ i + 1 } ` , a as Hex ) ;
289- }
290- }
291- }
245+ const r = await getContractEventsInsight ( {
246+ client : contract . client ,
247+ chains : [ contract . chain ] ,
248+ contractAddress : contract . address ,
249+ queryOptions : {
250+ limit : 500 ,
251+ filter_block_hash : params . blockHash ,
252+ filter_block_number_gte : fromBlock ,
253+ filter_block_number_lte : toBlock ,
254+ filter_topic_0 : params . topics ?. [ 0 ] as Hex | undefined ,
255+ filter_topic_1 : params . topics ?. [ 1 ] as Hex | undefined ,
256+ filter_topic_2 : params . topics ?. [ 2 ] as Hex | undefined ,
257+ filter_topic_3 : params . topics ?. [ 3 ] as Hex | undefined ,
258+ } ,
259+ } ) ;
292260
293- const clientFetch = getClientFetch ( client ) ;
294- const result = await clientFetch ( url . toString ( ) ) ;
295- const fetchedEventData = ( await result . json ( ) ) as {
296- data : {
297- chain_id : number ;
298- block_number : number ;
299- block_hash : string ;
300- block_timestamp : string ;
301- transaction_hash : string ;
302- transaction_index : number ;
303- log_index : number ;
304- address : string ;
305- data : string ;
306- topics : string [ ] ;
307- } [ ] ;
308- } ;
309- const cleanedEventData = fetchedEventData . data . map ( ( tx ) => ( {
310- chainId : tx . chain_id ,
311- blockNumber : numberToHex ( tx . block_number ) ,
312- blockHash : tx . block_hash as Hex ,
313- blockTimestamp : tx . block_timestamp ,
314- transactionHash : tx . transaction_hash as Hex ,
315- transactionIndex : numberToHex ( tx . transaction_index ) ,
316- logIndex : numberToHex ( tx . log_index ) ,
317- address : tx . address ,
318- data : tx . data as Hex ,
319- topics : tx . topics as [ `0x${string } `, ...`0x${string } `[ ] ] | [ ] | undefined ,
320- } ) ) ;
261+ const cleanedEventData = r . map ( ( tx ) => ( {
262+ chainId : tx . chain_id ,
263+ blockNumber : numberToHex ( Number ( tx . block_number ) ) ,
264+ blockHash : tx . block_hash as Hex ,
265+ blockTimestamp : tx . block_timestamp ,
266+ transactionHash : tx . transaction_hash as Hex ,
267+ transactionIndex : numberToHex ( tx . transaction_index ) ,
268+ logIndex : numberToHex ( tx . log_index ) ,
269+ address : tx . address ,
270+ data : tx . data as Hex ,
271+ topics : tx . topics as [ `0x${string } `, ...`0x${string } `[ ] ] | [ ] | undefined ,
272+ } ) ) ;
321273
322- return cleanedEventData . map ( ( e ) => formatLog ( e ) ) ;
323- } catch {
324- throw new Error ( "Error fetching events from insight" ) ;
325- }
274+ return cleanedEventData . map ( ( e ) => formatLog ( e ) ) ;
326275}
0 commit comments