forked from solana-foundation/explorer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnchorDetailsCard.tsx
More file actions
307 lines (273 loc) · 13.3 KB
/
AnchorDetailsCard.tsx
File metadata and controls
307 lines (273 loc) · 13.3 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
import { Address } from '@components/common/Address';
import { BorshEventCoder, BorshInstructionCoder, Idl, Instruction, Program } from '@coral-xyz/anchor';
import { IdlEvent, IdlField, IdlInstruction, IdlTypeDefTyStruct } from '@coral-xyz/anchor/dist/cjs/idl';
import { useTransactionDetails } from '@providers/transactions';
import { SignatureResult, TransactionInstruction } from '@solana/web3.js';
import {
decodeEventWithCustomDiscriminator,
decodeInstructionWithCustomDiscriminator,
FlattenedIdlAccount,
getAnchorAccountsFromInstruction,
getAnchorNameForInstruction,
getAnchorProgramName,
instructionIsSelfCPI,
mapIxArgsToRows,
} from '@utils/anchor';
import { camelToTitleCase } from '@utils/index';
import { extractEventsFromLogs } from '@utils/program-logs';
import { useMemo, useState } from 'react';
import { ChevronDown, ChevronUp, CornerDownRight } from 'react-feather';
import { toBase64 } from '@/app/shared/lib/bytes';
import { InstructionCard } from './InstructionCard';
import { ProgramEventsCard } from './ProgramEventsCard';
export default function AnchorDetailsCard(props: {
ix: TransactionInstruction;
index: number;
result: SignatureResult;
signature: string;
innerCards?: JSX.Element[];
childIndex?: number;
anchorProgram: Program<Idl>;
}) {
const { ix, anchorProgram, signature, index } = props;
const programName = getAnchorProgramName(anchorProgram) ?? 'Unknown Program';
const ixName = getAnchorNameForInstruction(ix, anchorProgram) ?? 'Unknown Instruction';
const cardTitle = `${camelToTitleCase(programName)}: ${camelToTitleCase(ixName)}`;
// Extract events from transaction logs
const details = useTransactionDetails(signature);
const eventCards = useMemo(() => {
const transactionWithMeta = details?.data?.transactionWithMeta;
if (!transactionWithMeta) return undefined;
const logMessages = transactionWithMeta.meta?.logMessages;
if (!logMessages) return undefined;
// Extract event data for this specific instruction
const eventDataList = extractEventsFromLogs(logMessages, index);
if (eventDataList.length === 0) return undefined;
// Create event cards
return [
<ProgramEventsCard
key="events"
eventDataList={eventDataList}
program={anchorProgram}
instructionIndex={index}
/>,
];
}, [details, index, anchorProgram]);
return (
<InstructionCard title={cardTitle} {...props} eventCards={eventCards}>
<AnchorDetails ix={ix} anchorProgram={anchorProgram} />
</InstructionCard>
);
}
function AnchorDetails({ ix, anchorProgram }: { ix: TransactionInstruction; anchorProgram: Program }) {
const { ixAccounts, decodedIxData, ixDef } = useMemo(() => {
let ixAccounts: FlattenedIdlAccount[] | null = null;
let decodedIxData: Instruction | null = null;
let ixDef: IdlInstruction | undefined;
if (anchorProgram) {
let coder: BorshInstructionCoder | BorshEventCoder;
const encodedInstructionData = toBase64(ix.data.slice(8));
if (instructionIsSelfCPI(ix.data)) {
// Try custom discriminator decoder first (handles variable-length discriminators)
decodedIxData = decodeEventWithCustomDiscriminator(encodedInstructionData, anchorProgram);
// Fallback to standard Anchor event decoder
if (!decodedIxData) {
coder = new BorshEventCoder(anchorProgram.idl);
decodedIxData = coder.decode(encodedInstructionData);
}
const ixEventDef = anchorProgram.idl.events?.find(
ixDef => ixDef.name === decodedIxData?.name,
) as IdlEvent;
const ixEventFields = anchorProgram.idl.types?.find((type: any) => type.name === ixEventDef.name);
// Remap the event definition to an instruction definition by force casting to struct fields
ixDef = {
...ixEventDef,
accounts: [],
args: ((ixEventFields?.type as IdlTypeDefTyStruct).fields as IdlField[]) ?? [],
};
// Self-CPI instructions have 1 account called the eventAuthority
// https://github.com/coral-xyz/anchor/blob/04985802587c693091f836e0083e4412148c0ca6/lang/attribute/event/src/lib.rs#L165
ixAccounts = [{ isMut: false, isSigner: true, name: 'eventAuthority' }];
} else {
// Try custom discriminator decoder first (handles variable-length discriminators)
decodedIxData = decodeInstructionWithCustomDiscriminator(ix.data, anchorProgram);
// Fallback to standard Anchor decoder
if (!decodedIxData) {
coder = new BorshInstructionCoder(anchorProgram.idl);
decodedIxData = coder.decode(ix.data);
}
if (decodedIxData) {
ixDef = anchorProgram.idl.instructions.find(ixDef => ixDef.name === decodedIxData?.name);
if (ixDef) {
ixAccounts = getAnchorAccountsFromInstruction(decodedIxData, anchorProgram);
}
}
}
}
return {
decodedIxData,
ixAccounts,
ixDef,
};
}, [anchorProgram, ix.data]);
// Initialize collapsed groups with all group headers collapsed by default
// Must be called before early return to satisfy React Hooks rules
const [collapsedGroups, setCollapsedGroups] = useState<Set<number>>(() => {
const groupIndices = new Set<number>();
if (ixAccounts) {
ixAccounts.forEach((account, index) => {
if (account.isGroupHeader) {
groupIndices.add(index);
}
});
}
return groupIndices;
});
if (!ixAccounts || !decodedIxData || !ixDef) {
return (
<tr>
<td colSpan={3} className="text-lg-center">
Failed to decode account data according to the public Anchor interface
</td>
</tr>
);
}
const programName = getAnchorProgramName(anchorProgram) ?? 'Unknown Program';
const toggleGroup = (groupIndex: number) => {
setCollapsedGroups(prev => {
const newSet = new Set(prev);
if (newSet.has(groupIndex)) {
newSet.delete(groupIndex);
} else {
newSet.add(groupIndex);
}
return newSet;
});
};
return (
<>
<tr>
<td>Program</td>
<td className="text-lg-end" colSpan={2}>
<Address pubkey={ix.programId} alignRight link raw overrideText={programName} />
</td>
</tr>
<tr className="table-sep">
<td>Account Name</td>
<td className="text-lg-end" colSpan={2}>
Address
</td>
</tr>
{(() => {
const rows: JSX.Element[] = [];
let skipUntilLevel: number | null = null;
let accountInfoIndex = 0; // Track position in ixAccounts array
// First, insert group headers and track actual account indices
const accountMap = new Map<number, number>(); // keyIndex -> ixAccountsIndex
let actualAccountCount = 0;
if (ixAccounts) {
for (let i = 0; i < ixAccounts.length; i++) {
if (ixAccounts[i].isGroupHeader) {
// Group headers don't consume an actual account slot
continue;
}
accountMap.set(actualAccountCount, i);
actualAccountCount++;
}
}
// Render group headers and accounts
ix.keys.forEach(({ pubkey, isSigner, isWritable }, keyIndex) => {
// Check if there are group headers to render before this account
if (ixAccounts) {
while (accountInfoIndex < ixAccounts.length) {
const currentInfo = ixAccounts[accountInfoIndex];
if (currentInfo.isGroupHeader) {
// Render group header
const groupHeaderIndex = accountInfoIndex;
const isExpanded = !collapsedGroups.has(groupHeaderIndex);
skipUntilLevel = isExpanded ? null : (currentInfo.nestingLevel ?? 0);
rows.push(
<tr key={`group-${groupHeaderIndex}`} className="table-group-header">
<td colSpan={2}>{camelToTitleCase(currentInfo.name)}</td>
<td className="text-lg-end" onClick={() => toggleGroup(groupHeaderIndex)}>
<div className="c-pointer">
{isExpanded ? (
<>
<span className="text-info me-2">Collapse</span>
<ChevronUp size={15} />
</>
) : (
<>
<span className="text-info me-2">Expand</span>
<ChevronDown size={15} />
</>
)}
</div>
</td>
</tr>,
);
accountInfoIndex++;
} else {
// This is an actual account, break to process it
break;
}
}
}
// Get the account info for this actual account
const accountInfo =
accountMap.has(keyIndex) && ixAccounts ? ixAccounts[accountMap.get(keyIndex)!] : null;
// Skip nested accounts if parent group is collapsed
if (
skipUntilLevel !== null &&
accountInfo?.nestingLevel !== undefined &&
accountInfo.nestingLevel > skipUntilLevel
) {
accountInfoIndex++;
return;
}
// Reset skip flag when we exit the collapsed group
if (
skipUntilLevel !== null &&
accountInfo?.nestingLevel !== undefined &&
accountInfo.nestingLevel <= skipUntilLevel
) {
skipUntilLevel = null;
}
rows.push(
<tr key={keyIndex} className={accountInfo?.isNested ? 'table-nested-account' : ''}>
<td>
<div className="d-flex flex-row align-items-center">
{accountInfo?.isNested && <CornerDownRight className="me-2 mb-1" size={14} />}
<div className="me-2 d-md-inline">
{accountInfo
? `${camelToTitleCase(accountInfo.name)}`
: ixAccounts
? `Remaining Account #${keyIndex + 1 - actualAccountCount}`
: `Account #${keyIndex + 1}`}
</div>
{isWritable && <span className="badge bg-danger-soft me-1">Writable</span>}
{isSigner && <span className="badge bg-info-soft me-1">Signer</span>}
</div>
</td>
<td className="text-lg-end" colSpan={2}>
<Address pubkey={pubkey} alignRight link />
</td>
</tr>,
);
accountInfoIndex++;
});
return rows;
})()}
{decodedIxData && ixDef && ixDef.args.length > 0 && (
<>
<tr className="table-sep">
<td>Argument Name</td>
<td>Type</td>
<td className="text-lg-end">Value</td>
</tr>
{mapIxArgsToRows(decodedIxData.data, ixDef, anchorProgram.idl)}
</>
)}
</>
);
}