From ed470564e6fc63799a76140cc666f2ccc326e3fd Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sun, 5 Apr 2020 17:47:57 +0800 Subject: [PATCH] Improve the UI of the transaction modal --- explorer/src/components/TransactionModal.tsx | 89 ++++++++++++++++---- explorer/src/providers/blocks.tsx | 35 +++++++- 2 files changed, 105 insertions(+), 19 deletions(-) diff --git a/explorer/src/components/TransactionModal.tsx b/explorer/src/components/TransactionModal.tsx index 0d3759f8d5..1d4b77000a 100644 --- a/explorer/src/components/TransactionModal.tsx +++ b/explorer/src/components/TransactionModal.tsx @@ -6,6 +6,7 @@ import { Selected } from "../providers/transactions"; import { useBlocks } from "../providers/blocks"; +import { LAMPORTS_PER_SOL } from "@solana/web3.js"; function TransactionModal() { const { selected } = useTransactions(); @@ -16,14 +17,16 @@ function TransactionModal() { const renderContent = () => { if (!selected) return null; return ( -
-
-
e.stopPropagation()}> - - × - +
+
e.stopPropagation()}> +
+
+

Transaction Details

-

Transaction Details

+ +
@@ -33,10 +36,7 @@ function TransactionModal() { }; return ( -
+
{renderContent()}
); @@ -45,16 +45,73 @@ function TransactionModal() { function TransactionDetails({ selected }: { selected: Selected }) { const { blocks } = useBlocks(); const block = blocks[selected.slot]; - if (!block) return {"block not found"}; + if (!block) + return {"Transaction block not found"}; if (!block.transactions) { - return loading; + return ( + + + Loading + + ); } - const tx = block.transactions[selected.signature]; - if (!tx) return {"sig not found"}; + const details = block.transactions[selected.signature]; + if (!details) + return {"Transaction not found"}; - return {JSON.stringify(tx)}; + if (details.transfers.length === 0) + return {"No transfers"}; + + let i = 0; + return ( + <> + {details.transfers.map(transfer => { + return ( +
+ {i > 1 ?
: null} +
+
+
+
+
+
From
+
+
+ {transfer.fromPubkey.toBase58()} +
+
+
+ +
+
+
+
To
+
+
+ {transfer.toPubkey.toBase58()} +
+
+
+ +
+
+
+
Amount (SOL)
+
+
+ {`◎${(1.0 * transfer.lamports) / LAMPORTS_PER_SOL}`} +
+
+
+
+
+
+ ); + })} + + ); } export default TransactionModal; diff --git a/explorer/src/providers/blocks.tsx b/explorer/src/providers/blocks.tsx index 3462909b70..711b3d08cd 100644 --- a/explorer/src/providers/blocks.tsx +++ b/explorer/src/providers/blocks.tsx @@ -1,6 +1,12 @@ import React from "react"; import bs58 from "bs58"; -import { Connection, Transaction } from "@solana/web3.js"; +import { + Connection, + Transaction, + TransferParams, + SystemProgram, + SystemInstruction +} from "@solana/web3.js"; import { useCluster, ClusterStatus } from "./cluster"; import { useTransactions } from "./transactions"; @@ -10,7 +16,12 @@ export enum Status { Success } -type Transactions = { [signature: string]: Transaction }; +export interface TransactionDetails { + transaction: Transaction; + transfers: Array; +} + +type Transactions = { [signature: string]: TransactionDetails }; export interface Block { status: Status; transactions?: Transactions; @@ -155,8 +166,26 @@ async function fetchBlock(dispatch: Dispatch, slot: number, url: string) { block.transactions.forEach(({ transaction }) => { const signature = transaction.signature; if (signature) { + const transferInstructions = transaction.instructions + .filter(ix => ix.programId.equals(SystemProgram.programId)) + .filter( + ix => SystemInstruction.decodeInstructionType(ix) === "Transfer" + ); + + let transfers: TransferParams[] = []; + transferInstructions.forEach(ix => { + try { + transfers.push(SystemInstruction.decodeTransfer(ix)); + } catch (err) { + console.log(ix, err); + } + }); + const sig = bs58.encode(signature); - transactions[sig] = transaction; + transactions[sig] = { + transaction, + transfers + }; } }); status = Status.Success;