Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
explorer: Pretty print instruction data hex strings (solana-labs#19813)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry authored Sep 12, 2021
1 parent d9674f7 commit ab152f1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
44 changes: 44 additions & 0 deletions explorer/src/components/common/HexData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { ReactNode } from "react";
import { Buffer } from "buffer";
import { Copyable } from "./Copyable";

export function HexData({ raw }: { raw: Buffer }) {
if (!raw) {
return <span>No data</span>;
}

const chunks = [];
const hexString = raw.toString("hex");
for (let i = 0; i < hexString.length; i += 2) {
chunks.push(hexString.slice(i, i + 2));
}

const spans: ReactNode[] = [];
for (let i = 0; i < chunks.length; i += 4) {
const color = i % 8 === 0 ? "text-white" : "text-gray-500";
spans.push(
<span key={i} className={color}>
{chunks.slice(i, i + 4).join(" ")}&emsp;
</span>
);
}

function Content() {
return (
<Copyable text={hexString}>
<pre className="d-inline-block text-left mb-0 data-wrap">{spans}</pre>
</Copyable>
);
}

return (
<>
<div className="d-none d-lg-flex align-items-center justify-content-end">
<Content />
</div>
<div className="d-flex d-lg-none align-items-center">
<Content />
</div>
</>
);
}
4 changes: 2 additions & 2 deletions explorer/src/components/instruction/RawDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from "react";
import { TransactionInstruction } from "@solana/web3.js";
import { Address } from "components/common/Address";
import { HexData } from "components/common/HexData";

export function RawDetails({ ix }: { ix: TransactionInstruction }) {
const data = ix.data.toString("hex");
return (
<>
{ix.keys.map(({ pubkey, isSigner, isWritable }, keyIndex) => (
Expand All @@ -28,7 +28,7 @@ export function RawDetails({ ix }: { ix: TransactionInstruction }) {
Instruction Data <span className="text-muted">(Hex)</span>
</td>
<td className="text-lg-right">
<pre className="d-inline-block text-left mb-0 data-wrap">{data}</pre>
<HexData raw={ix.data} />
</td>
</tr>
</>
Expand Down
18 changes: 2 additions & 16 deletions explorer/src/pages/inspector/InstructionsSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TableCardBody } from "components/common/TableCardBody";
import { AddressWithContext, programValidator } from "./AddressWithContext";
import { useCluster } from "providers/cluster";
import { programLabel } from "utils/tx";
import { HexData } from "components/common/HexData";

export function InstructionsSection({ message }: { message: Message }) {
return (
Expand All @@ -30,19 +31,6 @@ function InstructionCard({
const programId = message.accountKeys[ix.programIdIndex];
const programName = programLabel(programId.toBase58(), cluster) || "Unknown";

let data: string = "No data";
if (ix.data) {
data = "";

const chunks = [];
const hexString = bs58.decode(ix.data).toString("hex");
for (let i = 0; i < hexString.length; i += 2) {
chunks.push(hexString.slice(i, i + 2));
}

data = chunks.join(" ");
}

return (
<div className="card" id={`instruction-index-${index + 1}`} key={index}>
<div className={`card-header${!expanded ? " border-bottom-none" : ""}`}>
Expand Down Expand Up @@ -104,9 +92,7 @@ function InstructionCard({
Instruction Data <span className="text-muted">(Hex)</span>
</td>
<td className="text-lg-right">
<pre className="d-inline-block text-left mb-0 data-wrap">
{data}
</pre>
<HexData raw={bs58.decode(ix.data)} />
</td>
</tr>
</TableCardBody>
Expand Down
2 changes: 1 addition & 1 deletion explorer/src/scss/_solana.scss
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ div.inner-cards {

pre.data-wrap,
pre.json-wrap {
max-width: 23rem;
max-width: 22rem;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
Expand Down

0 comments on commit ab152f1

Please sign in to comment.