11import { type Static , Type } from "@sinclair/typebox" ;
22import type { FastifyInstance } from "fastify" ;
33import { StatusCodes } from "http-status-codes" ;
4- import { getContract } from "../../../../shared/utils/cache/get-contract " ;
4+ import { BigNumber } from "ethers " ;
55import {
66 contractEventSchema ,
77 eventsQuerystringSchema ,
@@ -10,7 +10,10 @@ import {
1010 contractParamSchema ,
1111 standardResponseSchema ,
1212} from "../../../schemas/shared-api-schemas" ;
13+ import { thirdwebClient } from "../../../../shared/utils/sdk" ;
14+ import { getChain } from "../../../../shared/utils/chain" ;
1315import { getChainIdFromChain } from "../../../utils/chain" ;
16+ import { getContract , getContractEvents } from "thirdweb" ;
1417
1518const requestSchema = contractParamSchema ;
1619
@@ -82,20 +85,67 @@ export async function getAllEvents(fastify: FastifyInstance) {
8285 const { fromBlock, toBlock, order } = request . query ;
8386
8487 const chainId = await getChainIdFromChain ( chain ) ;
85- const contract = await getContract ( {
86- chainId,
87- contractAddress,
88- } ) ;
8988
90- let returnData = await contract . events . getAllEvents ( {
91- fromBlock ,
92- toBlock ,
93- order ,
89+ const contract = getContract ( {
90+ client : thirdwebClient ,
91+ address : contractAddress ,
92+ chain : await getChain ( chainId ) ,
9493 } ) ;
94+ const returnData = mapEventsV4ToV5 (
95+ await getContractEvents ( {
96+ contract : contract ,
97+ fromBlock : BigInt ( fromBlock ) ,
98+ toBlock : BigInt ( toBlock ) ,
99+ } ) ,
100+ order ,
101+ ) ;
95102
96103 reply . status ( StatusCodes . OK ) . send ( {
97104 result : returnData ,
98105 } ) ;
99106 } ,
100107 } ) ;
101108}
109+
110+ /**
111+ * Mapping of events v5 response to v4 for backward compatiblity.
112+ * Clients may be using this api and dont want to break things.
113+ * @param eventsV5 events data returned by v5 sdk
114+ * @param order asc or desc
115+ * @returns {Type.Array(contractEventSchema) }
116+ */
117+ export function mapEventsV4ToV5 ( eventsV5 = [ ] , order = "desc" ) {
118+ if ( ! eventsV5 ?. length ) return [ ] ;
119+
120+ return eventsV5
121+ . map ( ( event ) => {
122+ const eventName = event . eventName ;
123+ const data = { } ;
124+
125+ // backwards compatibility of BigInt(v5) to BigNumber(v4)
126+ Object . keys ( event . args ) . forEach ( ( key ) => {
127+ let value = event . args [ key ] ;
128+ if ( typeof value == "bigint" ) {
129+ value = BigNumber . from ( value . toString ( ) ) ;
130+ }
131+ data [ key ] = value ;
132+ } ) ;
133+
134+ delete event . eventName ;
135+ delete event . args ;
136+ const transaction = event ;
137+ transaction . blockNumber = parseInt ( transaction . blockNumber ) ;
138+ transaction . event = eventName ;
139+
140+ return {
141+ eventName,
142+ data,
143+ transaction,
144+ } ;
145+ } )
146+ . sort ( ( a , b ) => {
147+ return order === "desc"
148+ ? b . transaction . blockNumber - a . transaction . blockNumber
149+ : a . transaction . blockNumber - b . transaction . blockNumber ;
150+ } ) ;
151+ }
0 commit comments