2021-07-09 16:54:59 +00:00
|
|
|
import React, { useMemo } from "react";
|
2021-08-08 22:49:45 +00:00
|
|
|
import { JsonRpcProvider } from "@ethersproject/providers";
|
2021-07-09 05:07:20 +00:00
|
|
|
import { OtterscanConfig, useConfig } from "./useConfig";
|
|
|
|
import { useProvider } from "./useProvider";
|
2021-07-12 20:10:16 +00:00
|
|
|
import { ConnectionStatus } from "./types";
|
2021-07-09 05:07:20 +00:00
|
|
|
|
|
|
|
export type OtterscanRuntime = {
|
|
|
|
config?: OtterscanConfig;
|
2021-07-12 20:10:16 +00:00
|
|
|
connStatus: ConnectionStatus;
|
2021-08-08 22:49:45 +00:00
|
|
|
provider?: JsonRpcProvider;
|
2021-07-09 05:07:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useRuntime = (): OtterscanRuntime => {
|
|
|
|
const [configOK, config] = useConfig();
|
2021-07-12 20:10:16 +00:00
|
|
|
const [connStatus, provider] = useProvider(
|
|
|
|
configOK ? config?.erigonURL : undefined
|
|
|
|
);
|
2021-07-09 16:54:59 +00:00
|
|
|
|
|
|
|
const runtime = useMemo(
|
2021-07-12 20:10:16 +00:00
|
|
|
(): 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) {
|
2021-07-12 20:10:16 +00:00
|
|
|
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!);
|