otterscan/src/useRuntime.ts

31 lines
875 B
TypeScript
Raw Normal View History

2021-07-09 16:54:59 +00:00
import React, { useMemo } from "react";
import { JsonRpcProvider } from "@ethersproject/providers";
2021-07-09 05:07:20 +00:00
import { OtterscanConfig, useConfig } from "./useConfig";
import { useProvider } from "./useProvider";
import { ConnectionStatus } from "./types";
2021-07-09 05:07:20 +00:00
export type OtterscanRuntime = {
config?: OtterscanConfig;
connStatus: ConnectionStatus;
provider?: JsonRpcProvider;
2021-07-09 05:07:20 +00:00
};
export const useRuntime = (): OtterscanRuntime => {
const [configOK, config] = useConfig();
const [connStatus, provider] = useProvider(
configOK ? config?.erigonURL : undefined
);
2021-07-09 16:54:59 +00:00
const runtime = useMemo(
(): OtterscanRuntime => ({ config, connStatus, provider }),
[config, connStatus, provider]
2021-07-09 16:54:59 +00:00
);
2021-07-09 05:07:20 +00:00
if (!configOK) {
return { connStatus: ConnectionStatus.CONNECTING };
2021-07-09 05:07:20 +00:00
}
2021-07-09 16:54:59 +00:00
return runtime;
2021-07-09 05:07:20 +00:00
};
export const RuntimeContext = React.createContext<OtterscanRuntime>(null!);