otterscan/src/useRuntime.ts

27 lines
702 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";
export type OtterscanRuntime = {
config?: OtterscanConfig;
provider?: ethers.providers.JsonRpcProvider;
};
export const useRuntime = (): OtterscanRuntime => {
const [configOK, config] = useConfig();
2021-07-09 16:54:59 +00:00
const provider = useProvider(configOK ? config?.erigonURL : undefined);
const runtime = useMemo(
(): OtterscanRuntime => ({ config, provider }),
[config, provider]
);
2021-07-09 05:07:20 +00:00
if (!configOK) {
return {};
}
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!);