Create test account when test url param is present

This commit is contained in:
Justin Starry 2020-04-09 16:09:53 +08:00 committed by Michael Vines
parent 5cf323f9f5
commit 1c1c628b19
3 changed files with 81 additions and 63 deletions

View File

@ -23,77 +23,79 @@ function App() {
const currentTab = useCurrentTab(); const currentTab = useCurrentTab();
return ( return (
<ClusterProvider> <ClusterProvider>
<TransactionsProvider> <AccountsProvider>
<BlocksProvider> <TransactionsProvider>
<ClusterModal <BlocksProvider>
show={showClusterModal} <ClusterModal
onClose={() => setShowClusterModal(false)} show={showClusterModal}
/> onClose={() => setShowClusterModal(false)}
<TransactionModal /> />
<div className="main-content"> <TransactionModal />
<nav className="navbar navbar-expand-xl navbar-light"> <div className="main-content">
<div className="container"> <nav className="navbar navbar-expand-xl navbar-light">
<div className="row align-items-end"> <div className="container">
<div className="col"> <div className="row align-items-end">
<img src={Logo} width="250" alt="Solana Explorer" />
</div>
</div>
</div>
</nav>
<div className="header">
<div className="container">
<div className="header-body">
<div className="row align-items-center d-md-none">
<div className="col-12">
<ClusterStatusButton
expand
onClick={() => setShowClusterModal(true)}
/>
</div>
</div>
<div className="row align-items-center">
<div className="col"> <div className="col">
<ul className="nav nav-tabs nav-overflow header-tabs"> <img src={Logo} width="250" alt="Solana Explorer" />
<li className="nav-item">
<NavLink href="/transactions" tab="Transactions" />
</li>
<li className="nav-item">
<NavLink href="/accounts" tab="Accounts" />
</li>
</ul>
</div> </div>
<div className="col-auto d-none d-md-block"> </div>
<ClusterStatusButton </div>
onClick={() => setShowClusterModal(true)} </nav>
/>
<div className="header">
<div className="container">
<div className="header-body">
<div className="row align-items-center d-md-none">
<div className="col-12">
<ClusterStatusButton
expand
onClick={() => setShowClusterModal(true)}
/>
</div>
</div>
<div className="row align-items-center">
<div className="col">
<ul className="nav nav-tabs nav-overflow header-tabs">
<li className="nav-item">
<NavLink href="/transactions" tab="Transactions" />
</li>
<li className="nav-item">
<NavLink href="/accounts" tab="Accounts" />
</li>
</ul>
</div>
<div className="col-auto d-none d-md-block">
<ClusterStatusButton
onClick={() => setShowClusterModal(true)}
/>
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div>
<div className="container"> <div className="container">
<div className="row"> <div className="row">
<div className="col-12"> <div className="col-12">
{currentTab === "Transactions" ? <TransactionsCard /> : null} {currentTab === "Transactions" ? (
<TransactionsCard />
) : null}
</div>
</div> </div>
</div> <div className="row">
<div className="row"> <div className="col-12">
<div className="col-12">
<AccountsProvider>
{currentTab === "Accounts" ? <AccountsCard /> : null} {currentTab === "Accounts" ? <AccountsCard /> : null}
</AccountsProvider> </div>
</div> </div>
</div> </div>
</div> </div>
</div> <Overlay
<Overlay show={showClusterModal}
show={showClusterModal} onClick={() => setShowClusterModal(false)}
onClick={() => setShowClusterModal(false)} />
/> </BlocksProvider>
</BlocksProvider> </TransactionsProvider>
</TransactionsProvider> </AccountsProvider>
</ClusterProvider> </ClusterProvider>
); );
} }

View File

@ -55,7 +55,7 @@ interface Input {
} }
type Action = Update | Input; type Action = Update | Input;
type Dispatch = (action: Action) => void; export type Dispatch = (action: Action) => void;
function reducer(state: State, action: Action): State { function reducer(state: State, action: Action): State {
switch (action.type) { switch (action.type) {
@ -175,7 +175,7 @@ export async function fetchAccountInfo(
let details; let details;
let lamports; let lamports;
try { try {
const result = await new Connection(url).getAccountInfo( const result = await new Connection(url, "recent").getAccountInfo(
new PublicKey(address) new PublicKey(address)
); );
if (result === null) { if (result === null) {

View File

@ -8,6 +8,12 @@ import {
import { findGetParameter, findPathSegment } from "../utils"; import { findGetParameter, findPathSegment } from "../utils";
import { useCluster, ClusterStatus } from "../providers/cluster"; import { useCluster, ClusterStatus } from "../providers/cluster";
import base58 from "bs58"; import base58 from "bs58";
import {
useAccountsDispatch,
fetchAccountInfo,
Dispatch as AccountsDispatch,
ActionType as AccountsActionType
} from "./accounts";
export enum Status { export enum Status {
Checking, Checking,
@ -181,6 +187,7 @@ export function TransactionsProvider({ children }: TransactionsProviderProps) {
const [state, dispatch] = React.useReducer(reducer, undefined, initState); const [state, dispatch] = React.useReducer(reducer, undefined, initState);
const { status, url } = useCluster(); const { status, url } = useCluster();
const accountsDispatch = useAccountsDispatch();
// Check transaction statuses on startup and whenever cluster updates // Check transaction statuses on startup and whenever cluster updates
React.useEffect(() => { React.useEffect(() => {
@ -188,7 +195,7 @@ export function TransactionsProvider({ children }: TransactionsProviderProps) {
// Create a test transaction // Create a test transaction
if (findGetParameter("test") !== null) { if (findGetParameter("test") !== null) {
createTestTransaction(dispatch, url); createTestTransaction(dispatch, accountsDispatch, url);
} }
Object.keys(state.transactions).forEach(signature => { Object.keys(state.transactions).forEach(signature => {
@ -205,7 +212,11 @@ export function TransactionsProvider({ children }: TransactionsProviderProps) {
); );
} }
async function createTestTransaction(dispatch: Dispatch, url: string) { async function createTestTransaction(
dispatch: Dispatch,
accountsDispatch: AccountsDispatch,
url: string
) {
const testKey = process.env.REACT_APP_TEST_KEY; const testKey = process.env.REACT_APP_TEST_KEY;
let testAccount = new Account(); let testAccount = new Account();
if (testKey) { if (testKey) {
@ -221,6 +232,11 @@ async function createTestTransaction(dispatch: Dispatch, url: string) {
); );
dispatch({ type: ActionType.InputSignature, signature }); dispatch({ type: ActionType.InputSignature, signature });
checkTransactionStatus(dispatch, signature, url); checkTransactionStatus(dispatch, signature, url);
accountsDispatch({
type: AccountsActionType.Input,
pubkey: testAccount.publicKey
});
fetchAccountInfo(accountsDispatch, testAccount.publicKey.toBase58(), url);
} catch (error) { } catch (error) {
console.error("Failed to create test success transaction", error); console.error("Failed to create test success transaction", error);
} }