-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathInstructionsSection.tsx
More file actions
executable file
·319 lines (298 loc) · 11.8 KB
/
Copy pathInstructionsSection.tsx
File metadata and controls
executable file
·319 lines (298 loc) · 11.8 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { BaseInstructionCard } from '@components/common/BaseInstructionCard';
import { useAnchorProgram } from '@entities/idl';
import { isParsedInstruction, toParsedTransaction, useInstructionParser } from '@entities/instruction-parser';
import { MetaplexTokenMetadataDetailsCard } from '@features/mpl-token-metadata';
import { useCluster } from '@providers/cluster';
import {
AddressLookupTableAccount,
type CompiledInnerInstruction,
ComputeBudgetProgram,
type TransactionInstruction,
TransactionMessage,
type VersionedMessage,
} from '@solana/web3.js';
import { getProgramName } from '@utils/tx';
import React, { useMemo } from 'react';
import { ErrorBoundary } from 'react-error-boundary';
import { useProgramMetadataIdl } from '@/app/entities/program-metadata';
import { isTokenBatchInstruction, resolveInnerBatchInstructions, TokenBatchCard } from '@/app/features/token-batch';
import { useAddressLookupTables } from '@/app/providers/accounts';
import { FetchStatus } from '@/app/providers/cache';
import { ErrorCard } from '../common/ErrorCard';
import { InspectorInstructionCard as InspectorInstructionCardComponent } from '../common/InspectorInstructionCard';
import { LoadingCard } from '../common/LoadingCard';
import AnchorDetailsCard from '../instruction/AnchorDetailsCard';
import { ComputeBudgetDetailsCard } from '../instruction/ComputeBudgetDetailsCard';
import {
PROGRAM_METADATA_PROGRAM_ID,
ProgramMetadataDetailsCard,
} from '../instruction/program-metadata/ProgramMetadataDetailsCard';
import { ProgramMetadataIdlInstructionDetailsCard } from '../instruction/program-metadata-idl/ProgramMetadataIdlInstructionDetailsCard';
import { SystemDetailsCard } from '../instruction/system/SystemDetailsCard';
import { TokenDetailsCard } from '../instruction/token/TokenDetailsCard';
import { AssociatedTokenDetailsCard } from './associated-token/AssociatedTokenDetailsCard';
import { UnknownDetailsCard } from './UnknownDetailsCard';
const INSPECTOR_RESULT = { err: null };
const INSPECTOR_SIGNATURE = '';
export function InstructionsSection({
message,
compiledInnerInstructions,
}: {
message: VersionedMessage;
compiledInnerInstructions?: CompiledInnerInstruction[];
}) {
const hydratedTables = useAddressLookupTables(
message.addressTableLookups.map(lookup => lookup.accountKey.toString()),
);
for (let i = 0; i < hydratedTables.length; i++) {
const table = hydratedTables[i];
if (table && table[1] === FetchStatus.FetchFailed) {
return (
<ErrorCard
text={`Failed to fetch address lookup table: ${message.addressTableLookups[
i
].accountKey.toString()}`}
/>
);
}
}
const allDefined = hydratedTables.every(
table => table !== undefined && table[0] instanceof AddressLookupTableAccount,
);
if (!allDefined) {
return <LoadingCard />;
}
const addressLookupTableAccounts = (hydratedTables as any as Array<[AddressLookupTableAccount, FetchStatus]>).map(
table => table[0],
);
const transactionMessage = TransactionMessage.decompile(message, { addressLookupTableAccounts });
const batchByIndex = compiledInnerInstructions
? resolveInnerBatchInstructions(
compiledInnerInstructions,
message.getAccountKeys({ addressLookupTableAccounts }),
message,
)
: {};
return (
<>
{transactionMessage.instructions.map((ix, index) => {
const batchInnerCards = batchByIndex[index]?.map((innerIx, childIndex) => (
<ErrorBoundary key={childIndex} fallback={null}>
<TokenBatchCard index={index} childIndex={childIndex} ix={innerIx} result={INSPECTOR_RESULT} />
</ErrorBoundary>
));
return (
<InspectorInstructionCard
key={index}
index={index}
ix={ix}
message={message}
innerCards={batchInnerCards}
/>
);
})}
</>
);
}
function InspectorInstructionCard({
message,
ix,
index,
innerCards,
}: {
message: VersionedMessage;
ix: TransactionInstruction;
index: number;
innerCards?: React.ReactNode[];
}) {
const { cluster, url } = useCluster();
const dispatcher = useInstructionParser();
const programId = ix.programId;
const programName = getProgramName(programId.toBase58(), cluster);
const anchorProgram = useAnchorProgram(programId.toString(), url, cluster);
const { programMetadataIdl } = useProgramMetadataIdl(programId.toString(), url, cluster);
const parsedIx = useMemo(() => dispatcher.fromTransactionInstruction(ix), [dispatcher, ix]);
const parsedTx = useMemo(
() => (isParsedInstruction(parsedIx) ? toParsedTransaction(ix, message, [parsedIx]) : undefined),
[ix, message, parsedIx],
);
if (anchorProgram.program) {
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName="Unknown Program" />}
>
<AnchorDetailsCard
anchorProgram={anchorProgram.program}
index={index}
innerCards={undefined}
ix={ix}
result={INSPECTOR_RESULT}
signature={INSPECTOR_SIGNATURE}
/>
</ErrorBoundary>
);
}
if (isTokenBatchInstruction(ix)) {
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<TokenBatchCard index={index} ix={ix} result={INSPECTOR_RESULT} />
</ErrorBoundary>
);
}
// Compute Budget instructions are not RPC-pre-parsed and its DetailsCard
// decodes raw bytes directly, so no parser entry is needed today. Phase 3
// of the unification will fold this into the registry.
if (ComputeBudgetProgram.programId.equals(programId)) {
return (
<ComputeBudgetDetailsCard
key={index}
ix={ix}
index={index}
result={INSPECTOR_RESULT}
signature={INSPECTOR_SIGNATURE}
InstructionCardComponent={BaseInstructionCard}
/>
);
}
if (programId.toBase58() === PROGRAM_METADATA_PROGRAM_ID) {
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<ProgramMetadataDetailsCard
key={index}
ix={ix}
index={index}
result={INSPECTOR_RESULT}
InstructionCardComponent={BaseInstructionCard}
raw={ix}
/>
</ErrorBoundary>
);
}
// Parse instructions of programs that publish an IDL via the Program Metadata Program.
if (programMetadataIdl) {
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<ProgramMetadataIdlInstructionDetailsCard
key={index}
ix={ix}
index={index}
result={INSPECTOR_RESULT}
idl={programMetadataIdl}
/>
</ErrorBoundary>
);
}
if (!parsedIx) {
return (
<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} innerCards={innerCards} />
);
}
if ('unknown' in parsedIx) {
if (parsedIx.programLabel === 'mpl-token-metadata') {
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<MetaplexTokenMetadataDetailsCard
key={index}
ix={ix}
index={index}
result={INSPECTOR_RESULT}
InstructionCardComponent={BaseInstructionCard}
/>
</ErrorBoundary>
);
}
return (
<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} innerCards={innerCards} />
);
}
// `parsedTx` is non-null here by construction (it's built whenever `parsedIx`
// is a ParsedInstruction, which the guards above guarantee). This guard exists
// to narrow its type for the switch below — TS can't relate the two useMemos.
if (!parsedTx) {
return (
<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} innerCards={innerCards} />
);
}
switch (parsedIx.program) {
case 'system':
return (
<SystemDetailsCard
key={index}
ix={parsedIx}
tx={parsedTx}
index={index}
result={INSPECTOR_RESULT}
raw={ix}
/>
);
case 'spl-associated-token-account':
return (
<AssociatedTokenDetailsCard
key={index}
ix={parsedIx}
raw={ix}
message={message}
index={index}
result={INSPECTOR_RESULT}
/>
);
case 'spl-token':
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<TokenDetailsCard
key={index}
ix={parsedIx}
tx={parsedTx}
index={index}
result={INSPECTOR_RESULT}
InstructionCardComponent={InspectorInstructionCardComponent}
message={message}
raw={ix}
/>
</ErrorBoundary>
);
case 'spl-token-2022':
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<TokenDetailsCard
key={index}
ix={parsedIx}
tx={parsedTx}
index={index}
result={INSPECTOR_RESULT}
InstructionCardComponent={InspectorInstructionCardComponent}
message={message}
raw={ix}
/>
</ErrorBoundary>
);
case 'mpl-token-metadata':
return (
<ErrorBoundary
fallback={<UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} />}
>
<MetaplexTokenMetadataDetailsCard
key={index}
ix={ix}
parsedIx={parsedIx}
index={index}
result={INSPECTOR_RESULT}
InstructionCardComponent={BaseInstructionCard}
/>
</ErrorBoundary>
);
}
return <UnknownDetailsCard key={index} index={index} ix={ix} programName={programName} innerCards={innerCards} />;
}