-
Notifications
You must be signed in to change notification settings - Fork 223
/
index.js
214 lines (186 loc) · 5.33 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { sha3, BN } = require("web3-utils");
const abiCoder = require("web3-eth-abi");
const state = {
savedABIs: [],
methodIDs: {},
};
function _getABIs() {
return state.savedABIs;
}
function _typeToString(input) {
if (input.type === "tuple") {
return "(" + input.components.map(_typeToString).join(",") + ")";
}
return input.type;
}
function _addABI(abiArray) {
if (Array.isArray(abiArray)) {
// Iterate new abi to generate method id"s
abiArray.map(function(abi) {
if (abi.name) {
const signature = sha3(
abi.name +
"(" +
abi.inputs
.map(_typeToString)
.join(",") +
")"
);
if (abi.type === "event") {
state.methodIDs[signature.slice(2)] = abi;
} else {
state.methodIDs[signature.slice(2, 10)] = abi;
}
}
});
state.savedABIs = state.savedABIs.concat(abiArray);
} else {
throw new Error("Expected ABI array, got " + typeof abiArray);
}
}
function _removeABI(abiArray) {
if (Array.isArray(abiArray)) {
// Iterate new abi to generate method id"s
abiArray.map(function(abi) {
if (abi.name) {
const signature = sha3(
abi.name +
"(" +
abi.inputs
.map(function(input) {
return input.type;
})
.join(",") +
")"
);
if (abi.type === "event") {
if (state.methodIDs[signature.slice(2)]) {
delete state.methodIDs[signature.slice(2)];
}
} else {
if (state.methodIDs[signature.slice(2, 10)]) {
delete state.methodIDs[signature.slice(2, 10)];
}
}
}
});
} else {
throw new Error("Expected ABI array, got " + typeof abiArray);
}
}
function _getMethodIDs() {
return state.methodIDs;
}
function _decodeMethod(data) {
const methodID = data.slice(2, 10);
const abiItem = state.methodIDs[methodID];
if (abiItem) {
let decoded = abiCoder.decodeParameters(abiItem.inputs, data.slice(10));
let retData = {
name: abiItem.name,
params: [],
};
for (let i = 0; i < decoded.__length__; i++) {
let param = decoded[i];
let parsedParam = param;
const isUint = abiItem.inputs[i].type.indexOf("uint") === 0;
const isInt = abiItem.inputs[i].type.indexOf("int") === 0;
const isAddress = abiItem.inputs[i].type.indexOf("address") === 0;
if (isUint || isInt) {
const isArray = Array.isArray(param);
if (isArray) {
parsedParam = param.map(val => new BN(val).toString());
} else {
parsedParam = new BN(param).toString();
}
}
// Addresses returned by web3 are randomly cased so we need to standardize and lowercase all
if (isAddress) {
const isArray = Array.isArray(param);
if (isArray) {
parsedParam = param.map(_ => _.toLowerCase());
} else {
parsedParam = param.toLowerCase();
}
}
retData.params.push({
name: abiItem.inputs[i].name,
value: parsedParam,
type: abiItem.inputs[i].type,
});
}
return retData;
}
}
function _decodeLogs(logs) {
return logs.filter(log => log.topics.length > 0).map((logItem) => {
const methodID = logItem.topics[0].slice(2);
const method = state.methodIDs[methodID];
if (method) {
const logData = logItem.data;
let decodedParams = [];
let dataIndex = 0;
let topicsIndex = 1;
let dataTypes = [];
method.inputs.map(function(input) {
if (!input.indexed) {
dataTypes.push(input.type);
}
});
const decodedData = abiCoder.decodeParameters(
dataTypes,
logData.slice(2)
);
// Loop topic and data to get the params
method.inputs.map(function(param) {
let decodedP = {
name: param.name,
type: param.type,
};
if (param.indexed) {
decodedP.value = logItem.topics[topicsIndex];
topicsIndex++;
} else {
decodedP.value = decodedData[dataIndex];
dataIndex++;
}
if (param.type === "address") {
decodedP.value = decodedP.value.toLowerCase();
// 42 because len(0x) + 40
if (decodedP.value.length > 42) {
let toRemove = decodedP.value.length - 42;
let temp = decodedP.value.split("");
temp.splice(2, toRemove);
decodedP.value = temp.join("");
}
}
if (
param.type === "uint256" ||
param.type === "uint8" ||
param.type === "int"
) {
// ensure to remove leading 0x for hex numbers
if (typeof decodedP.value === "string" && decodedP.value.startsWith("0x")) {
decodedP.value = new BN(decodedP.value.slice(2), 16).toString(10);
} else {
decodedP.value = new BN(decodedP.value).toString(10);
}
}
decodedParams.push(decodedP);
});
return {
name: method.name,
events: decodedParams,
address: logItem.address,
};
}
});
}
module.exports = {
getABIs: _getABIs,
addABI: _addABI,
getMethodIDs: _getMethodIDs,
decodeMethod: _decodeMethod,
decodeLogs: _decodeLogs,
removeABI: _removeABI,
};