explorer: parse compute budget instructions (#24703)

This commit is contained in:
Justin Starry 2022-04-27 01:09:00 +08:00 committed by GitHub
parent 506ee0014a
commit 2df1c1cd52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,109 @@
import React from "react";
import {
ComputeBudgetInstruction,
SignatureResult,
TransactionInstruction,
} from "@solana/web3.js";
import { InstructionCard } from "./InstructionCard";
import { SolBalance } from "utils";
import { Address } from "components/common/Address";
import { reportError } from "utils/sentry";
import { useCluster } from "providers/cluster";
export function ComputeBudgetDetailsCard({
ix,
index,
result,
signature,
innerCards,
childIndex,
}: {
ix: TransactionInstruction;
index: number;
result: SignatureResult;
signature: string;
innerCards?: JSX.Element[];
childIndex?: number;
}) {
const { url } = useCluster();
try {
const type = ComputeBudgetInstruction.decodeInstructionType(ix);
switch (type) {
case "RequestUnits": {
const { units, additionalFee } =
ComputeBudgetInstruction.decodeRequestUnits(ix);
return (
<InstructionCard
ix={ix}
index={index}
result={result}
title="Compute Budget Program: Request Units"
innerCards={innerCards}
childIndex={childIndex}
>
<tr>
<td>Program</td>
<td className="text-lg-end">
<Address pubkey={ix.programId} alignRight link />
</td>
</tr>
<tr>
<td>Requested Compute Units</td>
<td className="text-lg-end">{units}</td>
</tr>
<tr>
<td>Additional Fee (SOL)</td>
<td className="text-lg-end">
<SolBalance lamports={additionalFee} />
</td>
</tr>
</InstructionCard>
);
}
case "RequestHeapFrame": {
const { bytes } = ComputeBudgetInstruction.decodeRequestHeapFrame(ix);
return (
<InstructionCard
ix={ix}
index={index}
result={result}
title="Compute Budget Program: Request Heap Frame"
innerCards={innerCards}
childIndex={childIndex}
>
<tr>
<td>Program</td>
<td className="text-lg-end">
<Address pubkey={ix.programId} alignRight link />
</td>
</tr>
<tr>
<td>Requested Heap Frame (Bytes)</td>
<td className="text-lg-end">{bytes}</td>
</tr>
</InstructionCard>
);
}
}
} catch (error) {
reportError(error, {
url: url,
signature: signature,
});
}
return (
<InstructionCard
ix={ix}
index={index}
result={result}
title="Compute Budget Program: Unknown"
innerCards={innerCards}
childIndex={childIndex}
defaultRaw
/>
);
}

View File

@ -1,6 +1,7 @@
import React from "react"; import React from "react";
import { ErrorCard } from "components/common/ErrorCard"; import { ErrorCard } from "components/common/ErrorCard";
import { import {
ComputeBudgetProgram,
ParsedInnerInstruction, ParsedInnerInstruction,
ParsedInstruction, ParsedInstruction,
ParsedTransaction, ParsedTransaction,
@ -46,6 +47,7 @@ import { isMangoInstruction } from "../instruction/mango/types";
import { useAnchorProgram } from "providers/anchor"; import { useAnchorProgram } from "providers/anchor";
import { LoadingCard } from "components/common/LoadingCard"; import { LoadingCard } from "components/common/LoadingCard";
import { ErrorBoundary } from "@sentry/react"; import { ErrorBoundary } from "@sentry/react";
import { ComputeBudgetDetailsCard } from "components/instruction/ComputeBudgetDetailsCard";
export type InstructionDetailsProps = { export type InstructionDetailsProps = {
tx: ParsedTransaction; tx: ParsedTransaction;
@ -236,6 +238,8 @@ function InstructionCard({
return <WormholeDetailsCard key={key} {...props} />; return <WormholeDetailsCard key={key} {...props} />;
} else if (isPythInstruction(transactionIx)) { } else if (isPythInstruction(transactionIx)) {
return <PythDetailsCard key={key} {...props} />; return <PythDetailsCard key={key} {...props} />;
} else if (ComputeBudgetProgram.programId.equals(transactionIx.programId)) {
return <ComputeBudgetDetailsCard key={key} {...props} />;
} else if (anchorProgram) { } else if (anchorProgram) {
return ( return (
<ErrorBoundary fallback={<UnknownDetailsCard {...props} />}> <ErrorBoundary fallback={<UnknownDetailsCard {...props} />}>

View File

@ -24,6 +24,7 @@ import { TokenInfoMap } from "@solana/spl-token-registry";
export enum PROGRAM_NAMES { export enum PROGRAM_NAMES {
// native built-ins // native built-ins
ADDRESS_MAP = "Address Map Program", ADDRESS_MAP = "Address Map Program",
COMPUTE_BUDGET = "Compute Budget Program",
CONFIG = "Config Program", CONFIG = "Config Program",
STAKE = "Stake Program", STAKE = "Stake Program",
SYSTEM = "System Program", SYSTEM = "System Program",
@ -116,6 +117,10 @@ export const PROGRAM_INFO_BY_ID: { [address: string]: ProgramInfo } = {
name: PROGRAM_NAMES.ADDRESS_MAP, name: PROGRAM_NAMES.ADDRESS_MAP,
deployments: ALL_CLUSTERS, deployments: ALL_CLUSTERS,
}, },
ComputeBudget111111111111111111111111111111: {
name: PROGRAM_NAMES.COMPUTE_BUDGET,
deployments: ALL_CLUSTERS,
},
Config1111111111111111111111111111111111111: { Config1111111111111111111111111111111111111: {
name: PROGRAM_NAMES.CONFIG, name: PROGRAM_NAMES.CONFIG,
deployments: ALL_CLUSTERS, deployments: ALL_CLUSTERS,