solana/explorer/src/providers/accounts/index.tsx

238 lines
6.2 KiB
TypeScript
Raw Normal View History

2020-03-31 06:58:48 -07:00
import React from "react";
import { StakeAccount as StakeAccountWasm } from "solana-sdk-wasm";
2020-05-14 07:14:28 -07:00
import { PublicKey, Connection, StakeProgram } from "@solana/web3.js";
import { useCluster, Cluster } from "../cluster";
2020-05-14 07:14:28 -07:00
import { HistoryProvider } from "./history";
import { TokensProvider, TOKEN_PROGRAM_ID } from "./tokens";
import { coerce } from "superstruct";
import { ParsedInfo } from "validators";
import { StakeAccount } from "validators/accounts/stake";
import {
TokenAccount,
MintAccountInfo,
TokenAccountInfo,
} from "validators/accounts/token";
import * as Cache from "providers/cache";
import { ActionType, FetchStatus } from "providers/cache";
import { reportError } from "utils/sentry";
2020-05-14 07:14:28 -07:00
export { useAccountHistory } from "./history";
2020-03-31 06:58:48 -07:00
export type StakeProgramData = {
program: "stake";
parsed: StakeAccount | StakeAccountWasm;
};
export type TokenProgramData = {
program: "spl-token";
parsed: TokenAccount;
};
export type ProgramData = StakeProgramData | TokenProgramData;
2020-03-31 06:58:48 -07:00
export interface Details {
executable: boolean;
owner: PublicKey;
space?: number;
data?: ProgramData;
2020-03-31 06:58:48 -07:00
}
export interface Account {
pubkey: PublicKey;
lamports: number;
2020-03-31 06:58:48 -07:00
details?: Details;
}
type State = Cache.State<Account>;
type Dispatch = Cache.Dispatch<Account>;
2020-03-31 06:58:48 -07:00
const StateContext = React.createContext<State | undefined>(undefined);
const DispatchContext = React.createContext<Dispatch | undefined>(undefined);
type AccountsProviderProps = { children: React.ReactNode };
export function AccountsProvider({ children }: AccountsProviderProps) {
const { url } = useCluster();
const [state, dispatch] = Cache.useReducer<Account>(url);
2020-03-31 06:58:48 -07:00
// Clear accounts cache whenever cluster is changed
2020-03-31 06:58:48 -07:00
React.useEffect(() => {
dispatch({ type: ActionType.Clear, url });
}, [dispatch, url]);
2020-03-31 06:58:48 -07:00
return (
<StateContext.Provider value={state}>
<DispatchContext.Provider value={dispatch}>
<TokensProvider>
<HistoryProvider>{children}</HistoryProvider>
</TokensProvider>
2020-03-31 06:58:48 -07:00
</DispatchContext.Provider>
</StateContext.Provider>
);
}
2020-05-12 03:32:14 -07:00
async function fetchAccountInfo(
2020-03-31 06:58:48 -07:00
dispatch: Dispatch,
2020-05-12 03:32:14 -07:00
pubkey: PublicKey,
cluster: Cluster,
url: string
2020-03-31 06:58:48 -07:00
) {
dispatch({
type: ActionType.Update,
key: pubkey.toBase58(),
status: Cache.FetchStatus.Fetching,
url,
2020-03-31 06:58:48 -07:00
});
let data;
2020-05-12 03:32:14 -07:00
let fetchStatus;
2020-03-31 06:58:48 -07:00
try {
const result = (
await new Connection(url, "single").getParsedAccountInfo(pubkey)
).value;
let lamports, details;
2020-04-06 03:54:29 -07:00
if (result === null) {
2020-04-05 01:31:40 -07:00
lamports = 0;
} else {
2020-04-06 03:54:29 -07:00
lamports = result.lamports;
2020-05-14 00:30:33 -07:00
// Only save data in memory if we can decode it
let space;
if (!("parsed" in result.data)) {
space = result.data.length;
}
let data: ProgramData | undefined;
2020-05-14 00:30:33 -07:00
if (result.owner.equals(StakeProgram.programId)) {
2020-05-14 08:20:35 -07:00
try {
let parsed;
if ("parsed" in result.data) {
const info = coerce(result.data.parsed, ParsedInfo);
parsed = coerce(info, StakeAccount);
} else {
const wasm = await import("solana-sdk-wasm");
parsed = wasm.StakeAccount.fromAccountData(result.data);
}
data = {
program: "stake",
parsed,
};
2020-05-14 08:20:35 -07:00
} catch (err) {
reportError(err, { url, address: pubkey.toBase58() });
2020-05-14 08:20:35 -07:00
// TODO store error state in Account info
}
} else if ("parsed" in result.data) {
if (result.owner.equals(TOKEN_PROGRAM_ID)) {
try {
const info = coerce(result.data.parsed, ParsedInfo);
const parsed = coerce(info, TokenAccount);
data = {
program: "spl-token",
parsed,
};
} catch (err) {
reportError(err, { url, address: pubkey.toBase58() });
// TODO store error state in Account info
}
}
2020-05-14 00:30:33 -07:00
}
2020-04-06 03:54:29 -07:00
details = {
space,
2020-04-06 03:54:29 -07:00
executable: result.executable,
2020-05-14 00:30:33 -07:00
owner: result.owner,
2020-06-24 01:07:47 -07:00
data,
2020-04-06 03:54:29 -07:00
};
2020-04-05 01:31:40 -07:00
}
data = { pubkey, lamports, details };
2020-05-14 07:14:28 -07:00
fetchStatus = FetchStatus.Fetched;
2020-04-06 03:54:29 -07:00
} catch (error) {
if (cluster !== Cluster.Custom) {
reportError(error, { url });
}
2020-05-14 07:14:28 -07:00
fetchStatus = FetchStatus.FetchFailed;
2020-03-31 06:58:48 -07:00
}
dispatch({
type: ActionType.Update,
status: fetchStatus,
data,
key: pubkey.toBase58(),
url,
});
2020-04-21 08:30:52 -07:00
}
2020-03-31 06:58:48 -07:00
export function useAccounts() {
const context = React.useContext(StateContext);
if (!context) {
throw new Error(`useAccounts must be used within a AccountsProvider`);
}
return context.entries;
2020-03-31 06:58:48 -07:00
}
export function useAccountInfo(
address: string | undefined
): Cache.CacheEntry<Account> | undefined {
2020-04-21 08:30:52 -07:00
const context = React.useContext(StateContext);
2020-05-12 03:32:14 -07:00
2020-04-21 08:30:52 -07:00
if (!context) {
2020-05-12 03:32:14 -07:00
throw new Error(`useAccountInfo must be used within a AccountsProvider`);
2020-04-21 08:30:52 -07:00
}
if (address === undefined) return;
return context.entries[address];
2020-04-21 08:30:52 -07:00
}
export function useMintAccountInfo(
address: string | undefined
): MintAccountInfo | undefined {
const accountInfo = useAccountInfo(address);
if (address === undefined) return;
try {
const data = accountInfo?.data?.details?.data;
if (!data) return;
if (data.program !== "spl-token" || data.parsed.type !== "mint") {
throw new Error("Expected mint");
}
return coerce(data.parsed.info, MintAccountInfo);
} catch (err) {
reportError(err, { address });
}
}
export function useTokenAccountInfo(
address: string | undefined
): TokenAccountInfo | undefined {
const accountInfo = useAccountInfo(address);
if (address === undefined) return;
try {
const data = accountInfo?.data?.details?.data;
if (!data) return;
if (data.program !== "spl-token" || data.parsed.type !== "account") {
throw new Error("Expected token account");
}
return coerce(data.parsed.info, TokenAccountInfo);
} catch (err) {
reportError(err, { address });
}
}
2020-05-12 03:32:14 -07:00
export function useFetchAccountInfo() {
const dispatch = React.useContext(DispatchContext);
if (!dispatch) {
throw new Error(
`useFetchAccountInfo must be used within a AccountsProvider`
);
}
const { cluster, url } = useCluster();
return React.useCallback(
(pubkey: PublicKey) => {
fetchAccountInfo(dispatch, pubkey, cluster, url);
},
[dispatch, cluster, url]
);
2020-05-12 03:32:14 -07:00
}