-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.ts
37 lines (34 loc) · 930 Bytes
/
lib.ts
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
import * as fs from "fs";
import * as cbor from "cbor";
type Row = {
exMem: string;
exCPU: string;
script: string;
};
export function printExUnitsFromTxBody(filename: string): void {
const file = fs.readFileSync(filename, "utf-8");
const json = JSON.parse(file);
if (json.type !== "TxBodyAlonzo") {
throw new Error(`Expect file type TxBodyAlonzo, get ${json.type}`);
}
const tx = cbor.decodeAllSync(json.cborHex);
let totalMem = 0,
totalCPU = 0;
const rows: Row[] = tx[0][3].map((a: any, i: number): Row => {
const exMem = a[3][0];
const exCPU = a[3][1];
totalMem += exMem;
totalCPU += exCPU;
return {
exMem: exMem.toLocaleString(),
exCPU: exCPU.toLocaleString(),
script: `${tx[0][1][i][1].length} bytes`,
};
});
rows.push({
exMem: totalMem.toLocaleString(),
exCPU: totalCPU.toLocaleString(),
script: "Total",
});
console.table(rows);
}