otterscan/src/useRuntime.ts

31 lines
865 B
TypeScript
Raw Normal View History

2021-07-09 16:54:59 +00:00
import React, { useMemo } from "react";
2021-07-09 05:07:20 +00:00
import { ethers } from "ethers";
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;
2021-07-09 05:07:20 +00:00
provider?: ethers.providers.JsonRpcProvider;
};
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!);