-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.js
71 lines (61 loc) · 1.82 KB
/
decode.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
const {
EthereumBundlerV2__factory,
} = require("@morpho-org/morpho-blue-bundlers/types");
const fs = require("fs");
const KNOWN_TX_ORIGINS = {
"00011111": "unknown interface deployment",
"00022222": "app.morpho.dev",
"00033333": "netlify",
"0000da44": "app.morpho.org",
"05afea44": "safe-app.morpho.org",
};
BigInt.prototype.toJSON = function () {
return this.toString();
};
const decodeCalls = (calls) => {
const bundlerIfc = EthereumBundlerV2__factory.createInterface();
return calls.map((_data) => bundlerIfc.parseTransaction({ data: _data }));
};
const decodeMulticall = (data) => {
const bundlerIfc = EthereumBundlerV2__factory.createInterface();
const [calls] = bundlerIfc.decodeFunctionData("multicall", data);
return decodeCalls(calls);
};
try {
let res, origin, submissionTimestamp;
if (process.argv.filter((d) => !d.startsWith("-")).length === 3) {
const data = process.argv[2];
res = decodeMulticall(data);
const metadataLength = (data.length - 10) % 64;
if (metadataLength !== 0) {
const encodedSubmissionTimestamp = data.slice(
-metadataLength,
-metadataLength + 8
);
origin = data.slice(-metadataLength + 8);
submissionTimestamp = Number("0x" + encodedSubmissionTimestamp);
}
} else {
res = decodeCalls(
process.argv.slice(2).filter((call) => call !== "--simple")
);
}
const path = `output/${Date.now()}.json`;
if (!fs.existsSync("output")) {
fs.mkdirSync("output");
}
if (process.argv.includes("--simple")) {
res = res.map((r) => r && { name: r.name, args: r.args });
}
fs.writeFileSync(
path,
JSON.stringify({
calls: res,
origin: KNOWN_TX_ORIGINS[origin] ?? origin,
submissionTimestamp,
})
);
console.log("saved in", path);
} catch (e) {
console.log("couldn't decode", e);
}