@@ -7,7 +7,10 @@ import type {
77import { type Log , formatLog } from "viem" ;
88import { resolveContractAbi } from "../../contract/actions/resolve-abi.js" ;
99import type { ThirdwebContract } from "../../contract/contract.js" ;
10- import { getContractEvents as getContractEventsInsight } from "../../insight/get-events.js" ;
10+ import {
11+ type ContractEvent ,
12+ getContractEvents as getContractEventsInsight ,
13+ } from "../../insight/get-events.js" ;
1114import { eth_blockNumber } from "../../rpc/actions/eth_blockNumber.js" ;
1215import {
1316 type GetLogsBlockParams ,
@@ -156,6 +159,28 @@ export async function getContractEvents<
156159
157160 // if we have an abi on the contract, we can encode the topics with it
158161 if ( ! events ?. length && ! ! contract ) {
162+ if ( useIndexer ) {
163+ // fetch all events from the indexer, no need to get events from ABI
164+ const events = await getContractEventsInsight ( {
165+ client : contract . client ,
166+ chains : [ contract . chain ] ,
167+ contractAddress : contract . address ,
168+ decodeLogs : true ,
169+ queryOptions : {
170+ limit : 500 ,
171+ filter_block_hash : restParams . blockHash ,
172+ filter_block_number_gte : restParams . fromBlock ,
173+ filter_block_number_lte : restParams . toBlock ,
174+ } ,
175+ } ) . catch ( ( ) => {
176+ // chain might not support indexer
177+ return null ;
178+ } ) ;
179+ if ( events ) {
180+ return toLog ( events ) as GetContractEventsResult < abiEvents , TStrict > ;
181+ }
182+ }
183+
159184 // if we have a contract *WITH* an abi we can use that
160185 if ( contract . abi ?. length ) {
161186 // @ts -expect-error - we can't make typescript happy here, but we know this is an abi event
@@ -246,6 +271,10 @@ async function getLogsFromInsight(options: {
246271 } ,
247272 } ) ;
248273
274+ return toLog ( r ) ;
275+ }
276+
277+ function toLog ( r : ContractEvent [ ] ) {
249278 const cleanedEventData = r . map ( ( tx ) => ( {
250279 chainId : tx . chain_id ,
251280 blockNumber : numberToHex ( Number ( tx . block_number ) ) ,
@@ -257,7 +286,18 @@ async function getLogsFromInsight(options: {
257286 address : tx . address ,
258287 data : tx . data as Hex ,
259288 topics : tx . topics as [ `0x${string } `, ...`0x${string } `[ ] ] | [ ] | undefined ,
289+ ...( tx . decoded
290+ ? {
291+ eventName : tx . decoded . name ,
292+ args : {
293+ ...tx . decoded . indexed_params ,
294+ ...tx . decoded . non_indexed_params ,
295+ } ,
296+ }
297+ : { } ) ,
260298 } ) ) ;
261299
262- return cleanedEventData . map ( ( e ) => formatLog ( e ) ) ;
300+ return cleanedEventData
301+ . map ( ( e ) => formatLog ( e ) )
302+ . sort ( ( a , b ) => Number ( ( a . blockNumber ?? 0n ) - ( b . blockNumber ?? 0n ) ) ) ;
263303}
0 commit comments