forked from Shyft-to/solana-tx-parser-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
281 lines (257 loc) · 11.5 KB
/
helpers.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { utils } from "@coral-xyz/anchor";
import {
AccountMeta,
CompiledInstruction,
LoadedAddresses,
Message,
MessageCompiledInstruction,
ParsedInstruction as SolanaParsedInstruction,
ParsedTransactionWithMeta,
PartiallyDecodedInstruction,
PublicKey,
TransactionInstruction,
VersionedMessage,
VersionedTransactionResponse,
} from "@solana/web3.js";
import { LogContext } from "./interfaces";
export function hexToBuffer(data: string) {
const rawHex = data.startsWith("0x") ? data.slice(2) : data;
return Buffer.from(rawHex);
}
/**
* Parse transaction message and extract account metas
* @param message transaction message
* @returns parsed accounts metas
*/
export function parseTransactionAccounts<T extends Message | VersionedMessage>(
message: T,
loadedAddresses: T extends VersionedMessage ? LoadedAddresses | undefined : undefined = undefined,
): AccountMeta[] {
const accounts: PublicKey[] = message.version === "legacy" ? message.accountKeys : message.staticAccountKeys;
const readonlySignedAccountsCount = message.header.numReadonlySignedAccounts;
const readonlyUnsignedAccountsCount = message.header.numReadonlyUnsignedAccounts;
const requiredSignaturesAccountsCount = message.header.numRequiredSignatures;
const totalAccounts = accounts.length;
let parsedAccounts: AccountMeta[] = accounts.map((account, idx) => {
const isWritable =
idx < requiredSignaturesAccountsCount - readonlySignedAccountsCount ||
(idx >= requiredSignaturesAccountsCount && idx < totalAccounts - readonlyUnsignedAccountsCount);
return {
isSigner: idx < requiredSignaturesAccountsCount,
isWritable,
pubkey: new PublicKey(account),
} as AccountMeta;
});
const [ALTWritable, ALTReadOnly] =
message.version === "legacy" ? [[], []] : loadedAddresses ? [loadedAddresses.writable, loadedAddresses.readonly] : [[], []]; // message.getAccountKeys({ accountKeysFromLookups: loadedAddresses }).keySegments().slice(1); // omit static keys
if (ALTWritable) parsedAccounts = [...parsedAccounts, ...ALTWritable.map((pubkey) => ({ isSigner: false, isWritable: true, pubkey }))];
if (ALTReadOnly) parsedAccounts = [...parsedAccounts, ...ALTReadOnly.map((pubkey) => ({ isSigner: false, isWritable: false, pubkey }))];
return parsedAccounts;
}
/**
* Converts compiled instruction into common TransactionInstruction
* @param compiledInstruction
* @param parsedAccounts account meta, result of {@link parseTransactionAccounts}
* @returns TransactionInstruction
*/
export function compiledInstructionToInstruction<Ix extends CompiledInstruction | MessageCompiledInstruction>(
compiledInstruction: Ix,
parsedAccounts: AccountMeta[],
): TransactionInstruction {
if (typeof compiledInstruction.data === "string") {
const ci = compiledInstruction as CompiledInstruction;
return new TransactionInstruction({
data: utils.bytes.bs58.decode(ci.data),
programId: parsedAccounts[ci.programIdIndex].pubkey,
keys: ci.accounts.map((accountIdx) => parsedAccounts[accountIdx]),
});
} else {
const ci = compiledInstruction as MessageCompiledInstruction;
return new TransactionInstruction({
data: Buffer.from(ci.data),
programId: parsedAccounts[ci.programIdIndex].pubkey,
keys: ci.accountKeyIndexes.map((accountIndex) => {
if (accountIndex >= parsedAccounts.length)
throw new Error(
`Trying to resolve account at index ${accountIndex} while parsedAccounts is only ${parsedAccounts.length}. \
Looks like you're trying to parse versioned transaction, make sure that LoadedAddresses are passed to the \
parseTransactionAccounts function`,
);
return parsedAccounts[accountIndex];
}),
});
}
}
function parsedAccountsToMeta(accounts: PublicKey[], accountMeta: AccountMeta[]): AccountMeta[] {
const meta = accountMeta.map((m) => ({ pk: m.pubkey.toString(), ...m }));
return accounts.map((account) => {
const encoded = account.toString();
const found = meta.find((item) => item.pk === encoded);
if (!found) throw new Error(`Account ${encoded} not present in account meta!`);
return found;
});
}
export function parsedInstructionToInstruction(parsedInstruction: PartiallyDecodedInstruction, accountMeta: AccountMeta[]): TransactionInstruction {
return new TransactionInstruction({
data: utils.bytes.bs58.decode(parsedInstruction.data),
programId: parsedInstruction.programId,
keys: parsedAccountsToMeta(parsedInstruction.accounts, accountMeta),
});
}
/**
* Converts transaction response with CPI into artifical transaction that contains all instructions from tx and CPI
* @param transaction transactionResponse to convert from
* @returns Transaction object
*/
export function flattenTransactionResponse(transaction: VersionedTransactionResponse): (TransactionInstruction & { parentProgramId?: PublicKey })[] {
const result: (TransactionInstruction & { parentProgramId?: PublicKey })[] = [];
if (transaction === null || transaction === undefined) return [];
const txInstructions = transaction.transaction.message.compiledInstructions;
const accountsMeta = parseTransactionAccounts(transaction.transaction.message, transaction.meta?.loadedAddresses);
const orderedCII = (transaction?.meta?.innerInstructions || []).sort((a, b) => a.index - b.index);
const totalCalls =
(transaction.meta?.innerInstructions || []).reduce((accumulator, cii) => accumulator + cii.instructions.length, 0) + txInstructions.length;
let lastPushedIx = -1;
let callIndex = -1;
for (const CII of orderedCII) {
// push original instructions until we meet CPI
while (lastPushedIx !== CII.index) {
lastPushedIx += 1;
callIndex += 1;
result.push(compiledInstructionToInstruction(txInstructions[lastPushedIx], accountsMeta));
}
for (const CIIEntry of CII.instructions) {
const parentProgramId = accountsMeta[txInstructions[lastPushedIx].programIdIndex].pubkey;
result.push({
...compiledInstructionToInstruction(CIIEntry, accountsMeta),
parentProgramId,
});
callIndex += 1;
}
}
while (callIndex < totalCalls - 1) {
lastPushedIx += 1;
callIndex += 1;
result.push(compiledInstructionToInstruction(txInstructions[lastPushedIx], accountsMeta));
}
return result;
}
export function flattenParsedTransaction(
transaction: ParsedTransactionWithMeta,
): ((SolanaParsedInstruction | PartiallyDecodedInstruction) & { parentProgramId?: PublicKey })[] {
const result: ((SolanaParsedInstruction | PartiallyDecodedInstruction) & { parentProgramId?: PublicKey })[] = [];
if (!transaction) {
return result;
}
const txInstructions = transaction.transaction.message.instructions;
const orderedCII = (transaction?.meta?.innerInstructions || []).sort((a, b) => a.index - b.index);
const totalCalls =
(transaction.meta?.innerInstructions || []).reduce((accumulator, cii) => accumulator + cii.instructions.length, 0) + txInstructions.length;
let lastPushedIx = -1;
let callIndex = -1;
for (const CII of orderedCII) {
// push original instructions until we meet CPI
while (lastPushedIx !== CII.index) {
lastPushedIx += 1;
callIndex += 1;
result.push(txInstructions[lastPushedIx]);
}
for (const CIIEntry of CII.instructions) {
const parentProgramId = txInstructions[lastPushedIx].programId;
result.push({
...CIIEntry,
parentProgramId,
});
callIndex += 1;
}
}
while (callIndex < totalCalls - 1) {
lastPushedIx += 1;
callIndex += 1;
result.push(txInstructions[lastPushedIx]);
}
return result;
}
/**
* @private
*/
function newLogContext(programId: string, depth: number, id: number, instructionIndex: number): LogContext {
return {
logMessages: [],
dataLogs: [],
rawLogs: [],
errors: [],
programId,
depth,
id,
instructionIndex,
};
}
/**
* Parses transaction logs and provides additional context such as
* - programId that generated the message
* - call id of instruction, that generated the message
* - call depth of instruction
* - data messages, log messages and error messages
* @param logs logs from TransactionResponse.meta.logs
* @returns parsed logs with call depth and additional context
*/
export function parseLogs(logs: string[]): LogContext[] {
const parserRe =
/(?<logTruncated>^Log truncated$)|(?<programInvoke>^Program (?<invokeProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) invoke \[(?<level>\d+)\]$)|(?<programSuccessResult>^Program (?<successResultProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) success$)|(?<programFailedResult>^Program (?<failedResultProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) failed: (?<failedResultErr>.*)$)|(?<programCompleteFailedResult>^Program failed to complete: (?<failedCompleteError>.*)$)|(?<programLog>^^Program log: (?<logMessage>.*)$)|(?<programData>^Program data: (?<data>.*)$)|(?<programConsumed>^Program (?<consumedProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) consumed (?<consumedComputeUnits>\d*) of (?<allComputedUnits>\d*) compute units$)|(?<programReturn>^Program return: (?<returnProgramId>[1-9A-HJ-NP-Za-km-z]{32,}) (?<returnMessage>.*)$)|(?<insufficientLamports>^Transfer: insufficient lamports)/s;
const result: LogContext[] = [];
let id = -1;
let currentInstruction = 0;
let currentDepth = 0;
const callStack = [];
const callIds: number[] = [];
for (const log of logs) {
const match = parserRe.exec(log);
if (!match || !match.groups) {
throw new Error(`Failed to parse log line: ${log}`);
}
if (match.groups.logTruncated) {
result[callIds[callIds.length - 1]].invokeResult = "Log truncated";
} else if (match.groups.programInvoke) {
callStack.push(match.groups.invokeProgramId);
id += 1;
currentDepth += 1;
callIds.push(id);
if (match.groups.level != currentDepth.toString()) throw new Error(`invoke depth mismatch, log: ${match.groups.level}, expected: ${currentDepth}`);
result.push(newLogContext(callStack[callStack.length - 1], callStack.length, id, currentInstruction));
result[result.length - 1].rawLogs.push(log);
} else if (match.groups.programSuccessResult) {
const lastProgram = callStack.pop();
const lastCallIndex = callIds.pop();
if (lastCallIndex === undefined) throw new Error("callIds malformed");
if (lastProgram != match.groups.successResultProgramId) throw new Error("[ProramSuccess] callstack mismatch");
result[lastCallIndex].rawLogs.push(log);
currentDepth -= 1;
if (currentDepth === 0) {
currentInstruction += 1;
}
} else if (match.groups.programFailedResult) {
const lastProgram = callStack.pop();
if (lastProgram != match.groups.failedResultProgramId) throw new Error("[ProgramFailed] callstack mismatch");
result[callIds[callIds.length - 1]].rawLogs.push(log);
result[callIds[callIds.length - 1]].errors.push(match.groups.failedResultErr);
} else if (match.groups.programCompleteFailedResult) {
result[callIds[callIds.length - 1]].rawLogs.push(log);
result[callIds[callIds.length - 1]].errors.push(match.groups.failedCompleteError);
} else if (match.groups.programLog) {
result[callIds[callIds.length - 1]].rawLogs.push(log);
result[callIds[callIds.length - 1]].logMessages.push(match.groups.logMessage);
} else if (match.groups.programData) {
result[callIds[callIds.length - 1]].rawLogs.push(log);
result[callIds[callIds.length - 1]].dataLogs.push(match.groups.data);
} else if (match.groups.programConsumed) {
result[callIds[callIds.length - 1]].rawLogs.push(log);
} else if (match.groups.programReturn) {
if (callStack[callStack.length - 1] != match.groups.returnProgramId) throw new Error("[InvokeReturn]: callstack mismatch");
result[callIds[callIds.length - 1]].invokeResult = match.groups.returnMessage;
} else if (match.groups.insufficientLamports) {
result[callIds[callIds.length - 1]].rawLogs.push(log);
}
}
return result;
}