@@ -12,20 +12,24 @@ import {
12
12
type DecodingFunctions ,
13
13
type EventType ,
14
14
type DecoderConfig ,
15
+ type Fallbacks ,
15
16
} from "./decoder.types" ;
16
17
import { encodeEventTopics , type Abi } from "viem" ;
17
18
18
19
export class GoldRushDecoder {
19
20
private static configs : DecoderConfig = { } ;
20
21
private static decoders : Decoders = { } ;
22
+ private static fallbacks : Fallbacks = { } ;
21
23
private static decoding_functions : DecodingFunctions = [ ] ;
22
24
23
25
public static initDecoder = ( ) => {
24
- const directoryPath : string = join ( __dirname , "/protocols" ) ;
25
- const protocols = readdirSync ( directoryPath ) ;
26
+ console . info ( "Initializing GoldrushDecoder Service..." ) ;
27
+
28
+ const protocolsDirectoryPath : string = join ( __dirname , "/protocols" ) ;
29
+ const protocols = readdirSync ( protocolsDirectoryPath ) ;
26
30
let protocolsCount : number = 0 ;
27
31
for ( const protocol of protocols ) {
28
- const protocolPath = join ( directoryPath , protocol ) ;
32
+ const protocolPath = join ( protocolsDirectoryPath , protocol ) ;
29
33
const files = readdirSync ( protocolPath ) ;
30
34
let configFile : string | null = null ,
31
35
decodersFile : string | null = null ;
@@ -56,6 +60,27 @@ export class GoldRushDecoder {
56
60
}
57
61
}
58
62
63
+ const fallbacksDirectoryPath : string = join ( __dirname , "/fallbacks" ) ;
64
+ const fallbacks = readdirSync ( fallbacksDirectoryPath ) ;
65
+ let fallbacksCount : number = 0 ;
66
+ for ( const fallback of fallbacks ) {
67
+ const fallbackPath = join ( fallbacksDirectoryPath , fallback ) ;
68
+ const files = readdirSync ( fallbackPath ) ;
69
+ let fallbackFile : string | null = null ;
70
+ files . forEach ( ( file ) => {
71
+ const fileExtension =
72
+ process . env . NODE_ENV !== "test" ? "js" : "ts" ;
73
+ if ( file . endsWith ( `.fallback.${ fileExtension } ` ) ) {
74
+ fallbackFile = file ;
75
+ }
76
+ } ) ;
77
+ if ( fallbackFile ) {
78
+ fallbacksCount ++ ;
79
+ require ( join ( fallbackPath , fallbackFile ) ) ;
80
+ }
81
+ }
82
+
83
+ const decodersCount = Object . keys ( this . decoding_functions ) . length ;
59
84
const configsCount = Object . values ( this . configs ) . reduce (
60
85
( networkCount , network ) => {
61
86
return (
@@ -68,11 +93,10 @@ export class GoldRushDecoder {
68
93
0
69
94
) ;
70
95
71
- const decodersCount = Object . keys ( this . decoding_functions ) . length ;
72
-
73
- console . info (
74
- `Created ${ configsCount } configs and ${ decodersCount } decoders for ${ protocolsCount } protocols!`
75
- ) ;
96
+ console . info ( `${ protocolsCount . toLocaleString ( ) } protocols found` ) ;
97
+ console . info ( `${ configsCount . toLocaleString ( ) } configs generated` ) ;
98
+ console . info ( `${ decodersCount . toLocaleString ( ) } decoders generated` ) ;
99
+ console . info ( `${ fallbacksCount . toLocaleString ( ) } fallbacks generated` ) ;
76
100
} ;
77
101
78
102
public static on = (
@@ -82,7 +106,7 @@ export class GoldRushDecoder {
82
106
decoding_function : DecodingFunction
83
107
) => {
84
108
const [ protocol , event_name ] = event_id . split ( ":" ) ;
85
- const [ topic0 ] = encodeEventTopics ( {
109
+ const [ topic0_hash ] = encodeEventTopics ( {
86
110
abi : abi ,
87
111
eventName : event_name ,
88
112
} ) ;
@@ -101,12 +125,27 @@ export class GoldRushDecoder {
101
125
Object . keys ( this . configs [ network ] [ protocol ] ) . forEach ( ( address ) => {
102
126
this . decoders [ network ] ??= { } ;
103
127
this . decoders [ network ] [ address ] ??= { } ;
104
- this . decoders [ network ] [ address ] [ topic0 ] =
128
+ this . decoders [ network ] [ address ] [ topic0_hash ] =
105
129
decoding_function_index ;
106
130
} ) ;
107
131
} ) ;
108
132
} ;
109
133
134
+ public static fallback = (
135
+ event_name : string ,
136
+ abi : Abi ,
137
+ decoding_function : DecodingFunction
138
+ ) => {
139
+ const [ topic0_hash ] = encodeEventTopics ( {
140
+ abi : abi ,
141
+ eventName : event_name ,
142
+ } ) ;
143
+ this . decoding_functions . push ( decoding_function ) ;
144
+ const decoding_function_index : number =
145
+ this . decoding_functions . length - 1 ;
146
+ this . fallbacks [ topic0_hash ] = decoding_function_index ;
147
+ } ;
148
+
110
149
public static decode = async (
111
150
network : Chain ,
112
151
tx : Transaction ,
@@ -123,17 +162,18 @@ export class GoldRushDecoder {
123
162
// !ERROR: add factory_contract_address in the log_event(s)
124
163
// factory_contract_address,
125
164
} = log ;
126
- const function_index =
165
+ const decoding_index =
127
166
// !ERROR: add factory_contract_address in the log_event(s)
128
167
// factory_contract_address ||
129
168
this . decoders [ network ] [ contract_address ] ?. [ topic0_hash ] ;
130
- if ( function_index !== undefined ) {
131
- const event = await this . decoding_functions [ function_index ] (
132
- log ,
133
- tx ,
134
- network ,
135
- covalent_client
136
- ) ;
169
+ const fallback_index = this . fallbacks [ topic0_hash ] ;
170
+ if (
171
+ decoding_index !== undefined ||
172
+ fallback_index !== undefined
173
+ ) {
174
+ const event = await this . decoding_functions [
175
+ decoding_index ?? fallback_index
176
+ ] ( log , tx , network , covalent_client ) ;
137
177
events . push ( event ) ;
138
178
}
139
179
}
0 commit comments