@@ -11,18 +11,21 @@ class BridgeTransactionParser {
11
11
throw new Error ( `web3Client is required` ) ;
12
12
}
13
13
this . web3Client = web3Client ;
14
+ this . bridge = Bridge . build ( this . web3Client ) ;
15
+ this . jsonInterfaceMap = this . bridge . _jsonInterface . reduce ( ( map , item ) => {
16
+ map [ item . signature ] = item ;
17
+ return map ;
18
+ } , { } ) ;
14
19
}
15
20
16
21
getBridgeTransactionByTxHash = async ( transactionHash ) => {
17
22
utils . verifyHashOrBlockNumber ( transactionHash ) ;
18
23
19
24
let transaction ;
20
25
const txReceipt = await this . web3Client . eth . getTransactionReceipt ( transactionHash ) ;
21
-
22
26
if ( txReceipt ?. to === Bridge . address ) {
23
- const bridge = Bridge . build ( this . web3Client ) ;
24
27
const tx = await this . web3Client . eth . getTransaction ( txReceipt . transactionHash ) ;
25
- transaction = await this . createBridgeTx ( bridge , tx , txReceipt ) ;
28
+ transaction = await this . createBridgeTx ( tx , txReceipt ) ;
26
29
}
27
30
28
31
return transaction ;
@@ -76,8 +79,7 @@ class BridgeTransactionParser {
76
79
throw new Error ( `Given bridgeTxReceipt is not a bridge transaction` ) ;
77
80
}
78
81
79
- const bridge = Bridge . build ( this . web3Client ) ;
80
- return this . createBridgeTx ( bridge , bridgeTx , bridgeTxReceipt ) ;
82
+ return this . createBridgeTx ( bridgeTx , bridgeTxReceipt ) ;
81
83
}
82
84
83
85
decodeBridgeMethodParameters = ( methodName , data ) => {
@@ -97,10 +99,10 @@ class BridgeTransactionParser {
97
99
return args ;
98
100
}
99
101
100
- createBridgeTx = async ( bridge , tx , txReceipt ) => {
102
+ createBridgeTx = async ( tx , txReceipt ) => {
101
103
const txData = tx . input ;
102
- const method = bridge . _jsonInterface . find ( i => i . signature === txData . substr ( 0 , 10 ) ) ;
103
- const events = this . decodeLogs ( txReceipt , bridge ) ;
104
+ const method = this . jsonInterfaceMap [ txData . substring ( 0 , 10 ) ] ;
105
+ const events = this . decodeLogs ( txReceipt ) ;
104
106
const block = await this . web3Client . eth . getBlock ( txReceipt . blockNumber ) ;
105
107
106
108
let bridgeMethod = '' ;
@@ -118,32 +120,36 @@ class BridgeTransactionParser {
118
120
) ;
119
121
} ;
120
122
121
- decodeLogs = ( tx , bridge ) => {
123
+ decodeLogs = ( txReceipt ) => {
122
124
const events = [ ] ;
123
- for ( let txLog of tx . logs ) {
124
- const bridgeEvent = bridge . _jsonInterface . find ( i => i . signature === txLog . topics [ 0 ] ) ;
125
-
126
- if ( bridgeEvent ) {
127
- const args = { } ;
128
- const dataDecoded = this . web3Client . eth . abi . decodeParameters ( bridgeEvent . inputs . filter ( i => ! i . indexed ) , txLog . data ) ;
129
- let topicIndex = 1 ;
130
- for ( let input of bridgeEvent . inputs ) {
131
- let value ;
132
- if ( input . indexed ) {
133
- value = this . web3Client . eth . abi . decodeParameter ( input . type , txLog . topics [ topicIndex ] ) ;
134
- topicIndex ++ ;
135
- } else {
136
- value = dataDecoded [ input . name ] ;
137
- }
138
- args [ input . name ] = value ;
139
- }
140
- events . push ( new BridgeEvent ( bridgeEvent . name , bridgeEvent . signature , args ) ) ;
125
+ for ( let log of txReceipt . logs ) {
126
+ const abiElement = this . jsonInterfaceMap [ log . topics [ 0 ] ] ;
127
+ if ( ! abiElement ) {
128
+ continue ;
141
129
}
130
+ const event = this . decodeLog ( log , abiElement ) ;
131
+ events . push ( event ) ;
142
132
}
143
-
144
133
return events ;
145
134
}
146
135
136
+ decodeLog = ( log , abiElement ) => {
137
+ const args = { } ;
138
+ const dataDecoded = this . web3Client . eth . abi . decodeParameters ( abiElement . inputs . filter ( i => ! i . indexed ) , log . data ) ;
139
+ let topicIndex = 1 ;
140
+ for ( let input of abiElement . inputs ) {
141
+ let value ;
142
+ if ( input . indexed ) {
143
+ value = this . web3Client . eth . abi . decodeParameter ( input . type , log . topics [ topicIndex ] ) ;
144
+ topicIndex ++ ;
145
+ } else {
146
+ value = dataDecoded [ input . name ] ;
147
+ }
148
+ args [ input . name ] = value ;
149
+ }
150
+ return new BridgeEvent ( abiElement . name , abiElement . signature , args ) ;
151
+ }
152
+
147
153
}
148
154
149
155
module . exports = BridgeTransactionParser ;
0 commit comments