@@ -13,14 +13,18 @@ import {
13
13
type EventType ,
14
14
type DecoderConfig ,
15
15
type Fallbacks ,
16
+ type NativeDecodingFunction ,
16
17
} from "./decoder.types" ;
17
18
import { encodeEventTopics , type Abi } from "viem" ;
18
19
19
20
export class GoldRushDecoder {
20
21
private static configs : DecoderConfig = { } ;
21
22
private static decoders : Decoders = { } ;
22
23
private static fallbacks : Fallbacks = { } ;
24
+ private static native_decoder : NativeDecodingFunction ;
23
25
private static decoding_functions : DecodingFunctions = [ ] ;
26
+ private static fileExtension : "js" | "ts" =
27
+ process . env . NODE_ENV !== "test" ? "js" : "ts" ;
24
28
25
29
public static initDecoder = ( ) => {
26
30
console . info ( "Initializing GoldrushDecoder Service..." ) ;
@@ -34,12 +38,10 @@ export class GoldRushDecoder {
34
38
let configFile : string | null = null ,
35
39
decodersFile : string | null = null ;
36
40
files . forEach ( ( file ) => {
37
- const fileExtension =
38
- process . env . NODE_ENV !== "test" ? "js" : "ts" ;
39
- if ( file . endsWith ( `.configs.${ fileExtension } ` ) ) {
41
+ if ( file . endsWith ( `.configs.${ this . fileExtension } ` ) ) {
40
42
configFile = file ;
41
43
}
42
- if ( file . endsWith ( `.decoders.${ fileExtension } ` ) ) {
44
+ if ( file . endsWith ( `.decoders.${ this . fileExtension } ` ) ) {
43
45
decodersFile = file ;
44
46
}
45
47
} ) ;
@@ -68,9 +70,7 @@ export class GoldRushDecoder {
68
70
const files = readdirSync ( fallbackPath ) ;
69
71
let fallbackFile : string | null = null ;
70
72
files . forEach ( ( file ) => {
71
- const fileExtension =
72
- process . env . NODE_ENV !== "test" ? "js" : "ts" ;
73
- if ( file . endsWith ( `.fallback.${ fileExtension } ` ) ) {
73
+ if ( file . endsWith ( `.fallback.${ this . fileExtension } ` ) ) {
74
74
fallbackFile = file ;
75
75
}
76
76
} ) ;
@@ -80,6 +80,13 @@ export class GoldRushDecoder {
80
80
}
81
81
}
82
82
83
+ const nativeDecoderPath : string = join (
84
+ __dirname ,
85
+ "native" ,
86
+ `native.decoder.${ this . fileExtension } `
87
+ ) ;
88
+ require ( join ( nativeDecoderPath ) ) ;
89
+
83
90
const decodersCount = Object . keys ( this . decoding_functions ) . length ;
84
91
const configsCount = Object . values ( this . configs ) . reduce (
85
92
( chainCount , chain ) => {
@@ -93,6 +100,7 @@ export class GoldRushDecoder {
93
100
0
94
101
) ;
95
102
103
+ console . info ( "native decoder added" ) ;
96
104
console . info ( `${ protocolsCount . toLocaleString ( ) } protocols found` ) ;
97
105
console . info ( `${ configsCount . toLocaleString ( ) } configs generated` ) ;
98
106
console . info ( `${ decodersCount . toLocaleString ( ) } decoders generated` ) ;
@@ -148,6 +156,10 @@ export class GoldRushDecoder {
148
156
this . fallbacks [ topic0_hash ] = decoding_function_index ;
149
157
} ;
150
158
159
+ public static native = ( native_decoder : NativeDecodingFunction ) => {
160
+ this . native_decoder = native_decoder ;
161
+ } ;
162
+
151
163
public static decode = async (
152
164
chain_name : Chain ,
153
165
tx : Transaction ,
@@ -156,6 +168,10 @@ export class GoldRushDecoder {
156
168
const covalent_client = new CovalentClient ( covalent_api_key ) ;
157
169
const events : EventType [ ] = [ ] ;
158
170
const logs = ( tx . log_events ?? [ ] ) . reverse ( ) ;
171
+ if ( tx . value ) {
172
+ const nativeEvent = this . native_decoder ( tx ) ;
173
+ events . push ( nativeEvent ) ;
174
+ }
159
175
for ( const log of logs ) {
160
176
const {
161
177
raw_log_topics : [ topic0_hash ] ,
@@ -169,10 +185,10 @@ export class GoldRushDecoder {
169
185
this . decoders [ chain_name ] ?. [ contract_address ] ?. [ topic0_hash ] ;
170
186
const fallback_index = this . fallbacks [ topic0_hash ] ;
171
187
if ( decoding_index !== undefined || fallback_index !== undefined ) {
172
- const event = await this . decoding_functions [
188
+ const logEvent = await this . decoding_functions [
173
189
decoding_index ?? fallback_index
174
190
] ( log , tx , chain_name , covalent_client ) ;
175
- events . push ( event ) ;
191
+ events . push ( logEvent ) ;
176
192
}
177
193
}
178
194
return events ;
0 commit comments