2021-07-12 20:10:16 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2021-08-08 22:49:45 +00:00
|
|
|
import { JsonRpcProvider, WebSocketProvider } from "@ethersproject/providers";
|
2021-07-12 20:10:16 +00:00
|
|
|
import { ConnectionStatus } from "./types";
|
2021-07-15 05:40:11 +00:00
|
|
|
import { MIN_API_LEVEL } from "./params";
|
2021-07-08 19:02:42 +00:00
|
|
|
|
|
|
|
export const DEFAULT_ERIGON_URL = "http://127.0.0.1:8545";
|
|
|
|
|
2021-07-09 05:07:20 +00:00
|
|
|
export const useProvider = (
|
2021-07-09 16:54:59 +00:00
|
|
|
erigonURL?: string
|
2021-08-08 22:49:45 +00:00
|
|
|
): [ConnectionStatus, JsonRpcProvider | undefined] => {
|
2021-07-12 20:10:16 +00:00
|
|
|
const [connStatus, setConnStatus] = useState<ConnectionStatus>(
|
|
|
|
ConnectionStatus.CONNECTING
|
2021-07-09 16:54:59 +00:00
|
|
|
);
|
2021-07-12 20:10:16 +00:00
|
|
|
|
|
|
|
if (erigonURL !== undefined) {
|
|
|
|
if (erigonURL === "") {
|
|
|
|
console.info(`Using default erigon URL: ${DEFAULT_ERIGON_URL}`);
|
|
|
|
erigonURL = DEFAULT_ERIGON_URL;
|
|
|
|
} else {
|
|
|
|
console.log(`Using configured erigon URL: ${erigonURL}`);
|
|
|
|
}
|
2021-07-09 16:54:59 +00:00
|
|
|
}
|
2021-07-12 20:10:16 +00:00
|
|
|
|
2021-08-08 22:49:45 +00:00
|
|
|
const [provider, setProvider] = useState<JsonRpcProvider | undefined>();
|
2021-07-12 20:10:16 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (erigonURL === undefined) {
|
|
|
|
setConnStatus(ConnectionStatus.NOT_ETH_NODE);
|
|
|
|
setProvider(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setConnStatus(ConnectionStatus.CONNECTING);
|
|
|
|
|
|
|
|
const tryToConnect = async () => {
|
2021-08-08 22:49:45 +00:00
|
|
|
let provider: JsonRpcProvider;
|
2021-08-01 06:16:11 +00:00
|
|
|
if (erigonURL?.startsWith("ws://") || erigonURL?.startsWith("wss://")) {
|
2021-08-08 22:49:45 +00:00
|
|
|
provider = new WebSocketProvider(erigonURL);
|
2021-08-01 06:16:11 +00:00
|
|
|
} else {
|
2021-08-08 22:49:45 +00:00
|
|
|
provider = new JsonRpcProvider(erigonURL);
|
2021-08-01 06:16:11 +00:00
|
|
|
}
|
2021-07-12 20:10:16 +00:00
|
|
|
|
|
|
|
// Check if it is at least a regular ETH node
|
|
|
|
let blockNumber: number = 0;
|
|
|
|
try {
|
|
|
|
blockNumber = await provider.getBlockNumber();
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
setConnStatus(ConnectionStatus.NOT_ETH_NODE);
|
|
|
|
setProvider(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it is an Erigon node by probing a lightweight method
|
|
|
|
try {
|
|
|
|
await provider.send("erigon_getHeaderByNumber", [blockNumber]);
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
setConnStatus(ConnectionStatus.NOT_ERIGON);
|
|
|
|
setProvider(undefined);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it has Otterscan patches by probing a lightweight method
|
|
|
|
try {
|
2021-07-15 05:40:11 +00:00
|
|
|
const level = await provider.send("ots_getApiLevel", []);
|
|
|
|
if (level < MIN_API_LEVEL) {
|
|
|
|
setConnStatus(ConnectionStatus.NOT_OTTERSCAN_PATCHED);
|
|
|
|
setProvider(undefined);
|
|
|
|
} else {
|
|
|
|
setConnStatus(ConnectionStatus.CONNECTED);
|
|
|
|
setProvider(provider);
|
|
|
|
}
|
2021-07-12 20:10:16 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
setConnStatus(ConnectionStatus.NOT_OTTERSCAN_PATCHED);
|
|
|
|
setProvider(undefined);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
tryToConnect();
|
|
|
|
}, [erigonURL]);
|
|
|
|
|
|
|
|
return [connStatus, provider];
|
2021-07-08 19:02:42 +00:00
|
|
|
};
|