-
Notifications
You must be signed in to change notification settings - Fork 2
/
web3helpers.js
85 lines (74 loc) · 2.15 KB
/
web3helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const ethers = require('ethers');
function realtimeUpsertTxParams(trxData, confirmed, block) {
const filter = {
transaction_hash: trxData.transactionHash,
transaction_index: trxData.transactionIndex,
};
const update = {
...trxData,
confirmed,
block_number: block.number,
};
return { filter, update };
}
function realtimeUpsertParams(abi, trxData, confirmed, block) {
const block_number = block.number;
const address = trxData.address.toLowerCase();
const transaction_hash = (trxData.transactionHash).toLowerCase();
const log_index = trxData.logIndex;
const topics = [trxData.topic0, trxData.topic1, trxData.topic2, trxData.topic3];
const data = trxData.data;
const filter = {
transaction_hash,
log_index,
};
const rest = {
address,
block_number,
};
rest.confirmed = confirmed;
if (abi) {
const update = {
...filter,
...decodeWithEthers(abi, data, topics.filter(t => t !== null)),
...rest,
};
return { filter, update };
}
const update = {
...filter,
...rest,
data,
topic0: topics[0],
topic1: topics[1],
topic2: topics[2],
topic3: topics[3],
};
return { filter, update };
}
function decodeWithEthers(abi, data, topics) {
try {
const iface = new ethers.utils.Interface(abi);
const { args } = iface.parseLog({ data, topics});
const event = iface.getEvent(topics[0]);
const decoded = {};
event.inputs.forEach((input, index) => {
if (input.type === "uint256") {
decoded[`${input.name}_decimal`] = {
__type: "NumberDecimal",
value: ethers.BigNumber.from(args[index]._hex).toString()
};
decoded[input.name] = ethers.BigNumber.from(args[index]._hex).toString();
return;
}
decoded[input.name] = args[index];
});
return decoded;
} catch (error) {
return {};
}
}
module.exports = {
realtimeUpsertParams,
realtimeUpsertTxParams
}