Make it easy to create test success and failed tx

This commit is contained in:
Justin Starry 2020-04-04 17:24:25 +08:00 committed by Michael Vines
parent 152d7bc774
commit 60981f2031
3 changed files with 44 additions and 10 deletions

View File

@ -102,7 +102,8 @@ function urlAddresses(): Array<string> {
.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 {

View File

@ -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<string> {
.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);
}
}

View File

@ -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;
}