@@ -17,6 +17,7 @@ import {
17
17
type QueryOptions ,
18
18
} from "./decoder.types" ;
19
19
import { encodeEventTopics , type Abi } from "viem" ;
20
+ import { chunkify } from "../../utils/functions" ;
20
21
21
22
export class GoldRushDecoder {
22
23
private static configs : DecoderConfig = { } ;
@@ -28,7 +29,7 @@ export class GoldRushDecoder {
28
29
private static fileExtension : "js" | "ts" =
29
30
process . env . NODE_ENV !== "test" ? "js" : "ts" ;
30
31
31
- public static initDecoder = ( ) => {
32
+ public static initDecoder = ( ) : void => {
32
33
console . info ( "Initializing GoldrushDecoder Service..." ) ;
33
34
34
35
const protocolsDirectoryPath : string = join ( __dirname , "/protocols" ) ;
@@ -104,7 +105,7 @@ export class GoldRushDecoder {
104
105
chain_names : Chain [ ] ,
105
106
abi : Abi ,
106
107
decoding_function : DecodingFunction
107
- ) => {
108
+ ) : void => {
108
109
const [ protocol , event_name ] = event_id . split ( ":" ) ;
109
110
const [ topic0_hash ] = encodeEventTopics ( {
110
111
abi : abi ,
@@ -143,7 +144,7 @@ export class GoldRushDecoder {
143
144
event_name : string ,
144
145
abi : Abi ,
145
146
decoding_function : DecodingFunction
146
- ) => {
147
+ ) : void => {
147
148
const [ topic0_hash ] = encodeEventTopics ( {
148
149
abi : abi ,
149
150
eventName : event_name ,
@@ -155,7 +156,7 @@ export class GoldRushDecoder {
155
156
this . fallbacks [ lowercaseTopic0Hash ] = fallback_function_index ;
156
157
} ;
157
158
158
- public static native = ( native_decoder : NativeDecodingFunction ) => {
159
+ public static native = ( native_decoder : NativeDecodingFunction ) : void => {
159
160
this . native_decoder = native_decoder ;
160
161
} ;
161
162
@@ -164,55 +165,56 @@ export class GoldRushDecoder {
164
165
tx : Transaction ,
165
166
covalent_api_key : string ,
166
167
options : QueryOptions
167
- ) => {
168
+ ) : Promise < EventType [ ] > => {
168
169
const covalent_client = new CovalentClient ( covalent_api_key ) ;
169
- const events : ( EventType | null ) [ ] = [ ] ;
170
+ let events : ( EventType | null ) [ ] = [ ] ;
170
171
if ( tx . value ) {
171
172
const nativeEvent = this . native_decoder ( tx , options ) ;
172
173
events . push ( nativeEvent ) ;
173
174
}
174
-
175
- const decodedEvents = await Promise . all (
176
- ( tx . log_events ?? [ ] ) . map ( ( log_event ) => {
177
- const {
178
- raw_log_topics : [ topic0_hash ] ,
179
- sender_address,
180
- sender_factory_address,
181
- } = log_event ;
182
- const lowercaseChainName = chain_name . toLowerCase ( ) as Chain ;
183
- const lowercaseSenderAddress = sender_address ?. toLowerCase ( ) ;
184
- const lowercaseSenderFactoryAddress =
185
- sender_factory_address ?. toLowerCase ( ) ;
186
- const lowercaseTopic0Hash = topic0_hash ?. toLowerCase ( ) ;
187
-
188
- const decoding_index =
189
- this . decoders [ lowercaseChainName ] ?. [
190
- lowercaseSenderAddress
191
- ] ?. [ lowercaseTopic0Hash ] ??
192
- this . decoders [ lowercaseChainName ] ?. [
193
- lowercaseSenderFactoryAddress
194
- ] ?. [ lowercaseTopic0Hash ] ;
195
- const fallback_index = this . fallbacks [ lowercaseTopic0Hash ] ;
196
-
197
- const logFunction =
198
- decoding_index !== undefined
199
- ? this . decoding_functions [ decoding_index ]
200
- : fallback_index !== undefined
201
- ? this . fallback_functions [ fallback_index ]
175
+ const logChunks = chunkify ( tx . log_events ?? [ ] , 100 ) ;
176
+ for ( const logChunk of logChunks ) {
177
+ const decodedChunk = await Promise . all (
178
+ logChunk . map ( ( log_event ) => {
179
+ const {
180
+ raw_log_topics : [ topic0_hash ] ,
181
+ sender_address,
182
+ sender_factory_address,
183
+ } = log_event ;
184
+ const lowercaseChainName =
185
+ chain_name . toLowerCase ( ) as Chain ;
186
+ const lowercaseSenderAddress =
187
+ sender_address ?. toLowerCase ( ) ;
188
+ const lowercaseSenderFactoryAddress =
189
+ sender_factory_address ?. toLowerCase ( ) ;
190
+ const lowercaseTopic0Hash = topic0_hash ?. toLowerCase ( ) ;
191
+ const decoding_index =
192
+ this . decoders [ lowercaseChainName ] ?. [
193
+ lowercaseSenderAddress
194
+ ] ?. [ lowercaseTopic0Hash ] ??
195
+ this . decoders [ lowercaseChainName ] ?. [
196
+ lowercaseSenderFactoryAddress
197
+ ] ?. [ lowercaseTopic0Hash ] ;
198
+ const fallback_index = this . fallbacks [ lowercaseTopic0Hash ] ;
199
+ const logFunction =
200
+ ( decoding_index !== undefined &&
201
+ this . decoding_functions [ decoding_index ] ) ||
202
+ ( fallback_index !== undefined &&
203
+ this . fallback_functions [ fallback_index ] ) ||
204
+ null ;
205
+ return logFunction
206
+ ? logFunction (
207
+ log_event ,
208
+ tx ,
209
+ chain_name ,
210
+ covalent_client ,
211
+ options
212
+ )
202
213
: null ;
203
-
204
- return logFunction
205
- ? logFunction (
206
- log_event ,
207
- tx ,
208
- chain_name ,
209
- covalent_client ,
210
- options
211
- )
212
- : null ;
213
- } )
214
- ) ;
215
-
216
- return events . concat ( decodedEvents ) . filter ( Boolean ) as EventType [ ] ;
214
+ } )
215
+ ) ;
216
+ events = [ ...events , ...decodedChunk ] ;
217
+ }
218
+ return events . filter ( Boolean ) as EventType [ ] ;
217
219
} ;
218
220
}
0 commit comments