/
expectTXTable.ts
180 lines (169 loc) Β· 6.06 KB
/
expectTXTable.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
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
import type { IdlErrorCode } from "@coral-xyz/anchor/dist/esm/idl.js";
import { matchError, matchErrorCode } from "@saberhq/anchor-contrib";
import type { TransactionEnvelope } from "@saberhq/solana-contrib";
import { parseTransactionLogs, printTXTable } from "@saberhq/solana-contrib";
import { formatInstructionLogsForConsole } from "./printInstructionLogs.js";
import { expectTX } from "./utils.js";
/**
* expectTXTable is a drop in replacement for expectTX with a cool table output like:
* ```
* 872 example message (tests/awesomeTest.spec.ts:583:29)
* βββββββ¬ββββ¬ββββ¬ββββ¬ββββββββββββ¬βββββββ¬ββββββ¬βββββββ¬ββββ
* βindexβisoβmarβcumβ programId βquota βused β left βCPIβ
* βββββββΌββββΌββββΌββββΌββββββββββββΌβββββββΌββββββΌβββββββΌββββ€
* β 0 β298β281β464β'ATokenG..'β200000β24270β175730β 1 β
* β 1 β298β 74β538β'ATokenG..'β178730β21270β157460β 1 β
* β 2 β298β 74β612β'ATokenG..'β157460β27277β130183β 1 β
* β 3 β298β 42β686β'ATokenG..'β130183β21270β108913β 1 β
* β 4 β338β265β951β'qExampL..'β108913β76289β 32624β 3 β
* βββββββ΄ββββ΄ββββ΄ββββ΄ββββββββββββ΄βββββββ΄ββββββ΄βββββββ΄ββββ
* ```
*
* - **index**: the array index of the instruction within the transaction
* - **iso**: the isolated size of the instruction (marginal cost of only the instruction)
* - **mar**: the marginal size cost of the instruction (when added to previous)
* - **cum**: the cumulative size of the instructions up until that instruction
* - **quota/used/left**: [BPF instruction compute unit info](https://docs.solana.com/developing/programming-model/runtime)
* - **CPI**: [the maximum depth of CPI](https://docs.solana.com/developing/programming-model/calling-between-programs) (current limit in Solana is 4)
*
* @param verbosity If "printLogs", will always print the logs of the simulation.
* It's a string and not a boolean so you can easily search and comment it out if needed
*/
export const expectTXTable = (
tx: TransactionEnvelope,
msg?: string,
{
verbosity = null,
formatLogs = true,
}: {
/**
* Logging verbosity.
*
* - `always` -- print logs whenever they exist
* - `error` -- print logs only if there is an error
* - `null` -- never print the full transaction logs
*/
verbosity?: "always" | "error" | null;
formatLogs?: boolean;
} = {
verbosity: null,
formatLogs: true,
},
): Chai.PromisedAssertion => {
if (tx === null) {
throw new Error();
}
let relativePath: string | undefined;
const callStack = new Error().stack?.split("\n");
if (callStack) {
const expectIndex = callStack.findIndex((l) =>
l.includes(`at ${expectTXTable.name}`),
);
if (expectIndex > 0) {
const targetLine = callStack[expectIndex + 1];
if (targetLine) {
const cwd = process.cwd();
// get the part of targetLine after cwd
const targetLineAfterCwd = targetLine.substring(
targetLine.indexOf(cwd) + cwd.length,
);
if (targetLineAfterCwd.length > 0) {
relativePath = targetLineAfterCwd.substring(1);
}
}
}
}
// Run the simulation before sending the tx to avoid a race condition.
void tx
.simulate()
.then((simulation) => {
if (simulation?.value?.logs) {
printTXTable(
tx,
simulation.value.logs,
`${msg ? msg + " " : ""}${relativePath ? `(${relativePath})` : ""}`,
);
}
const logs = simulation.value.logs;
if (logs) {
if (
verbosity === "always" ||
(verbosity === "error" && simulation.value.err)
) {
if (formatLogs) {
const parsed = parseTransactionLogs(logs, simulation.value.err);
const fmt = formatInstructionLogsForConsole(parsed);
console.log(fmt);
} else {
console.log(logs.join("\n"));
}
}
if (simulation.value.err) {
let lastLine = "";
for (let i = 0; i < logs.length; i++) {
const curLine = logs[i];
if (curLine) {
const errorCode = curLine.match(
/Program log: Custom program error: (0x[0-9a-f]*)/,
);
if (errorCode && errorCode[1]) {
const programIdMatch = lastLine.split(" ");
if (programIdMatch && programIdMatch[1]) {
console.log(
` Program ${programIdMatch[1]} error:`,
Number(errorCode[1]),
);
}
}
lastLine = curLine;
}
}
console.log(" ", JSON.stringify(simulation.value.err));
}
}
})
.catch((e) => {
if (e instanceof Error) {
console.log(e.message);
}
});
return expectTX(tx, (msg ?? "") + (relativePath ? ` (${relativePath})` : ""));
};
/**
* Assert that a transaction is successful.
* @param tx
* @param msg
* @returns
*/
export const assertTXSuccess = (
tx: TransactionEnvelope,
msg?: string,
): Chai.PromisedAssertion => {
return expectTX(tx, msg).to.be.fulfilled;
};
/**
* Assert that a transaction will throw the given error.
* @param tx
* @param msg
* @returns
*/
export const assertTXThrows = (
tx: TransactionEnvelope,
err: IdlErrorCode,
msg?: string,
): Chai.PromisedAssertion => {
return expectTX(tx, msg).to.be.rejectedWith(matchError(err));
};
/**
* Assert that a transaction will throw the given error.
* @param tx
* @param msg
* @returns
*/
export const assertTXThrowsCode = (
tx: TransactionEnvelope,
code: number,
msg?: string,
): Chai.PromisedAssertion => {
return expectTX(tx, msg).to.be.rejectedWith(matchErrorCode(code));
};