Skip to content

Commit 24ff47b

Browse files
authored
Merge pull request #52 from rsksmart/refactor-decodeLogs-to-reuse-logic
Refactors decodeLogs and creates a new decodeLog function to reuse lo…
2 parents 16bb6b7 + c7cc7f5 commit 24ff47b

File tree

5 files changed

+67
-32
lines changed

5 files changed

+67
-32
lines changed

index.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ interface BridgeEvent {
2222
arguments: {}
2323
}
2424

25+
export interface Log {
26+
data: string;
27+
topics: string[];
28+
};
29+
30+
export interface AbiElement {
31+
type: string;
32+
name: string;
33+
inputs: {name: string, type: string}[];
34+
outputs: {name: string, type: string}[];
35+
signature: string;
36+
}
37+
2538
export default class BridgeTransactionParser {
2639

2740
/**
@@ -59,5 +72,21 @@ export default class BridgeTransactionParser {
5972
* @returns Object - A transaction object
6073
*/
6174
decodeBridgeTransaction(web3Tx: Web3Transaction, bridgeTxReceipt: TransactionReceipt): Promise<Transaction>;
75+
76+
/**
77+
* Decodes logs from a transaction receipt
78+
* @param txReceipt
79+
* @return {BridgeEvent[]}
80+
*/
81+
decodeLogs(txReceipt: TransactionReceipt): BridgeEvent[];
82+
83+
/**
84+
* Decodes a log data using the given abiElement
85+
* @param {Log} log
86+
* @param {AbiElement} abiElement
87+
* @returns {BridgeEvent}
88+
*/
89+
decodeLog(log: Log, abiElement: AbiElement): BridgeEvent;
90+
6291
}
6392

index.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

149155
module.exports = BridgeTransactionParser;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rsksmart/bridge-transaction-parser",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"description": "A tool to find interactions with the Bridge on Rootstock",
55
"main": "index.js",
66
"scripts": {

tool/live-monitor/live-monitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const BridgeTransactionParser = require('../../index');
2-
const Bridge = require('@rsksmart/rsk-precompiled-abis-lovell700').bridge;
2+
const Bridge = require('@rsksmart/rsk-precompiled-abis').bridge;
33
const EventEmitter = require('node:events');
44
const { MONITOR_EVENTS, defaultParamsValues } = require('./live-monitor-utils');
55
const Web3 = require('web3');

0 commit comments

Comments
 (0)