From 60981f2031167570930a994ecdaf2e1a4b251cc6 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Sat, 4 Apr 2020 17:24:25 +0800 Subject: [PATCH] Make it easy to create test success and failed tx --- explorer/src/providers/accounts.tsx | 3 +- explorer/src/providers/transactions.tsx | 43 ++++++++++++++++++++----- explorer/src/utils.ts | 8 ++++- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/explorer/src/providers/accounts.tsx b/explorer/src/providers/accounts.tsx index adecc3c5a3..0f9a36d38a 100644 --- a/explorer/src/providers/accounts.tsx +++ b/explorer/src/providers/accounts.tsx @@ -102,7 +102,8 @@ function urlAddresses(): Array { .concat(findGetParameter("address")?.split(",") || []) .concat(findGetParameter("addresses")?.split(",") || []) .concat(findPathSegment("address")?.split(",") || []) - .concat(findPathSegment("addresses")?.split(",") || []); + .concat(findPathSegment("addresses")?.split(",") || []) + .filter(a => a.length > 0); } function initState(): State { diff --git a/explorer/src/providers/transactions.tsx b/explorer/src/providers/transactions.tsx index 3ca5f24e7b..30e523e39f 100644 --- a/explorer/src/providers/transactions.tsx +++ b/explorer/src/providers/transactions.tsx @@ -1,7 +1,13 @@ import React from "react"; -import { TransactionSignature, Connection, PublicKey } from "@solana/web3.js"; +import { + TransactionSignature, + Connection, + SystemProgram, + Account +} from "@solana/web3.js"; import { findGetParameter, findPathSegment } from "../utils"; import { useCluster, ClusterStatus } from "../providers/cluster"; +import base58 from "bs58"; export enum Status { Checking, @@ -137,7 +143,8 @@ function urlSignatures(): Array { .concat(findPathSegment("tx")?.split(",") || []) .concat(findPathSegment("txn")?.split(",") || []) .concat(findPathSegment("transaction")?.split(",") || []) - .concat(findPathSegment("transactions")?.split(",") || []); + .concat(findPathSegment("transactions")?.split(",") || []) + .filter(s => s.length > 0); } function initState(): State { @@ -174,8 +181,8 @@ export function TransactionsProvider({ children }: TransactionsProviderProps) { if (status !== ClusterStatus.Connected) return; // Create a test transaction - if (findGetParameter("dev")) { - createDevTransaction(dispatch, url); + if (findGetParameter("test") !== null) { + createTestTransaction(dispatch, url); } Object.keys(state.transactions).forEach(signature => { @@ -192,18 +199,38 @@ export function TransactionsProvider({ children }: TransactionsProviderProps) { ); } -async function createDevTransaction(dispatch: Dispatch, url: string) { +async function createTestTransaction(dispatch: Dispatch, url: string) { + const testKey = process.env.REACT_APP_TEST_KEY; + let testAccount = new Account(); + if (testKey) { + testAccount = new Account(base58.decode(testKey)); + } + try { const connection = new Connection(url, "recent"); const signature = await connection.requestAirdrop( - new PublicKey(1), - 1, + testAccount.publicKey, + 100000, "recent" ); dispatch({ type: ActionType.InputSignature, signature }); checkTransactionStatus(dispatch, signature, url); } catch (error) { - console.error("Failed to create dev transaction", error); + console.error("Failed to create test success transaction", error); + } + + try { + const connection = new Connection(url, "recent"); + const tx = SystemProgram.transfer({ + fromPubkey: testAccount.publicKey, + toPubkey: testAccount.publicKey, + lamports: 1 + }); + const signature = await connection.sendTransaction(tx, testAccount); + dispatch({ type: ActionType.InputSignature, signature }); + checkTransactionStatus(dispatch, signature, url); + } catch (error) { + console.error("Failed to create test failure transaction", error); } } diff --git a/explorer/src/utils.ts b/explorer/src/utils.ts index 6d1d5cd8ce..1d8d583a67 100644 --- a/explorer/src/utils.ts +++ b/explorer/src/utils.ts @@ -6,7 +6,13 @@ export function findGetParameter(parameterName: string): string | null { .split("&") .forEach(function(item) { tmp = item.split("="); - if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]); + if (tmp[0] === parameterName) { + if (tmp.length === 2) { + result = decodeURIComponent(tmp[1]); + } else if (tmp.length === 1) { + result = ""; + } + } }); return result; }