@@ -11,18 +11,21 @@ class BridgeTransactionParser {
1111 throw new Error ( `web3Client is required` ) ;
1212 }
1313 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+ } , { } ) ;
1419 }
1520
1621 getBridgeTransactionByTxHash = async ( transactionHash ) => {
1722 utils . verifyHashOrBlockNumber ( transactionHash ) ;
1823
1924 let transaction ;
2025 const txReceipt = await this . web3Client . eth . getTransactionReceipt ( transactionHash ) ;
21-
2226 if ( txReceipt ?. to === Bridge . address ) {
23- const bridge = Bridge . build ( this . web3Client ) ;
2427 const tx = await this . web3Client . eth . getTransaction ( txReceipt . transactionHash ) ;
25- transaction = await this . createBridgeTx ( bridge , tx , txReceipt ) ;
28+ transaction = await this . createBridgeTx ( tx , txReceipt ) ;
2629 }
2730
2831 return transaction ;
@@ -76,8 +79,7 @@ class BridgeTransactionParser {
7679 throw new Error ( `Given bridgeTxReceipt is not a bridge transaction` ) ;
7780 }
7881
79- const bridge = Bridge . build ( this . web3Client ) ;
80- return this . createBridgeTx ( bridge , bridgeTx , bridgeTxReceipt ) ;
82+ return this . createBridgeTx ( bridgeTx , bridgeTxReceipt ) ;
8183 }
8284
8385 decodeBridgeMethodParameters = ( methodName , data ) => {
@@ -97,10 +99,10 @@ class BridgeTransactionParser {
9799 return args ;
98100 }
99101
100- createBridgeTx = async ( bridge , tx , txReceipt ) => {
102+ createBridgeTx = async ( tx , txReceipt ) => {
101103 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 ) ;
104106 const block = await this . web3Client . eth . getBlock ( txReceipt . blockNumber ) ;
105107
106108 let bridgeMethod = '' ;
@@ -118,32 +120,36 @@ class BridgeTransactionParser {
118120 ) ;
119121 } ;
120122
121- decodeLogs = ( tx , bridge ) => {
123+ decodeLogs = ( txReceipt ) => {
122124 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 ;
141129 }
130+ const event = this . decodeLog ( log , abiElement ) ;
131+ events . push ( event ) ;
142132 }
143-
144133 return events ;
145134 }
146135
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+
147153}
148154
149155module . exports = BridgeTransactionParser ;
0 commit comments