Remove logic duplication
This commit is contained in:
parent
8f3761b8ae
commit
90d25cdd8d
|
@ -1,17 +1,16 @@
|
||||||
import React, { useState, useEffect, useMemo, useContext } from "react";
|
import React, { useMemo, useContext } from "react";
|
||||||
import { Contract } from "@ethersproject/contracts";
|
|
||||||
import { commify, formatUnits } from "@ethersproject/units";
|
import { commify, formatUnits } from "@ethersproject/units";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { faGasPump } from "@fortawesome/free-solid-svg-icons/faGasPump";
|
import { faGasPump } from "@fortawesome/free-solid-svg-icons/faGasPump";
|
||||||
import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json";
|
|
||||||
import { RuntimeContext } from "./useRuntime";
|
import { RuntimeContext } from "./useRuntime";
|
||||||
import { formatValue } from "./components/formatter";
|
import { formatValue } from "./components/formatter";
|
||||||
import { useLatestBlockHeader } from "./useLatestBlock";
|
import { useLatestBlockHeader } from "./useLatestBlock";
|
||||||
import { useChainInfo } from "./useChainInfo";
|
import { useChainInfo } from "./useChainInfo";
|
||||||
|
import { useETHUSDRawOracle, useFastGasRawOracle } from "./usePriceOracle";
|
||||||
|
|
||||||
|
// TODO: encapsulate this magic number
|
||||||
const ETH_FEED_DECIMALS = 8;
|
const ETH_FEED_DECIMALS = 8;
|
||||||
|
|
||||||
// TODO: reduce duplication with useETHUSDOracle
|
|
||||||
const PriceBox: React.FC = () => {
|
const PriceBox: React.FC = () => {
|
||||||
const { provider } = useContext(RuntimeContext);
|
const { provider } = useContext(RuntimeContext);
|
||||||
const {
|
const {
|
||||||
|
@ -22,37 +21,8 @@ const PriceBox: React.FC = () => {
|
||||||
const maybeOutdated: boolean =
|
const maybeOutdated: boolean =
|
||||||
latestBlock !== undefined &&
|
latestBlock !== undefined &&
|
||||||
Date.now() / 1000 - latestBlock.timestamp > 3600;
|
Date.now() / 1000 - latestBlock.timestamp > 3600;
|
||||||
const ethFeed = useMemo(
|
|
||||||
() =>
|
|
||||||
provider &&
|
|
||||||
new Contract("eth-usd.data.eth", AggregatorV3Interface, provider),
|
|
||||||
[provider]
|
|
||||||
);
|
|
||||||
const gasFeed = useMemo(
|
|
||||||
() =>
|
|
||||||
provider &&
|
|
||||||
new Contract("fast-gas-gwei.data.eth", AggregatorV3Interface, provider),
|
|
||||||
[provider]
|
|
||||||
);
|
|
||||||
|
|
||||||
const [latestPriceData, setLatestPriceData] = useState<any>();
|
|
||||||
const [latestGasData, setLatestGasData] = useState<any>();
|
|
||||||
useEffect(() => {
|
|
||||||
if (!ethFeed || !gasFeed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const readData = async () => {
|
|
||||||
const [priceData, gasData] = await Promise.all([
|
|
||||||
ethFeed.latestRoundData(),
|
|
||||||
await gasFeed.latestRoundData(),
|
|
||||||
]);
|
|
||||||
setLatestPriceData(priceData);
|
|
||||||
setLatestGasData(gasData);
|
|
||||||
};
|
|
||||||
readData();
|
|
||||||
}, [ethFeed, gasFeed]);
|
|
||||||
|
|
||||||
|
const latestPriceData = useETHUSDRawOracle(provider, "latest");
|
||||||
const [latestPrice, latestPriceTimestamp] = useMemo(() => {
|
const [latestPrice, latestPriceTimestamp] = useMemo(() => {
|
||||||
if (!latestPriceData) {
|
if (!latestPriceData) {
|
||||||
return [undefined, undefined];
|
return [undefined, undefined];
|
||||||
|
@ -65,6 +35,7 @@ const PriceBox: React.FC = () => {
|
||||||
return [formattedPrice, timestamp];
|
return [formattedPrice, timestamp];
|
||||||
}, [latestPriceData]);
|
}, [latestPriceData]);
|
||||||
|
|
||||||
|
const latestGasData = useFastGasRawOracle(provider, "latest");
|
||||||
const [latestGasPrice, latestGasPriceTimestamp] = useMemo(() => {
|
const [latestGasPrice, latestGasPriceTimestamp] = useMemo(() => {
|
||||||
if (!latestGasData) {
|
if (!latestGasData) {
|
||||||
return [undefined, undefined];
|
return [undefined, undefined];
|
||||||
|
|
|
@ -79,20 +79,32 @@ const ethUSDFetcherKey = (blockTag: BlockTag | undefined) => {
|
||||||
const ethUSDFetcher =
|
const ethUSDFetcher =
|
||||||
(
|
(
|
||||||
provider: JsonRpcProvider | undefined
|
provider: JsonRpcProvider | undefined
|
||||||
): Fetcher<BigNumber | undefined, ["ethusd", BlockTag | undefined]> =>
|
): Fetcher<any | undefined, ["ethusd", BlockTag | undefined]> =>
|
||||||
async (_, blockTag) => {
|
async (_, blockTag) => {
|
||||||
if (provider?.network.chainId !== 1) {
|
if (provider?.network.chainId !== 1) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const c = new Contract("eth-usd.data.eth", AggregatorV3Interface, provider);
|
const c = new Contract("eth-usd.data.eth", AggregatorV3Interface, provider);
|
||||||
const priceData = await c.latestRoundData({ blockTag });
|
const priceData = await c.latestRoundData({ blockTag });
|
||||||
return BigNumber.from(priceData.answer);
|
return priceData;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useETHUSDOracle = (
|
export const useETHUSDOracle = (
|
||||||
provider: JsonRpcProvider | undefined,
|
provider: JsonRpcProvider | undefined,
|
||||||
blockTag: BlockTag | undefined
|
blockTag: BlockTag | undefined
|
||||||
): BigNumber | undefined => {
|
): BigNumber | undefined => {
|
||||||
|
const fetcher = ethUSDFetcher(provider);
|
||||||
|
const { data, error } = useSWRImmutable(ethUSDFetcherKey(blockTag), fetcher);
|
||||||
|
if (error) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return data !== undefined ? BigNumber.from(data.answer) : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useETHUSDRawOracle = (
|
||||||
|
provider: JsonRpcProvider | undefined,
|
||||||
|
blockTag: BlockTag | undefined
|
||||||
|
): any | undefined => {
|
||||||
const fetcher = ethUSDFetcher(provider);
|
const fetcher = ethUSDFetcher(provider);
|
||||||
const { data, error } = useSWRImmutable(ethUSDFetcherKey(blockTag), fetcher);
|
const { data, error } = useSWRImmutable(ethUSDFetcherKey(blockTag), fetcher);
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -100,3 +112,39 @@ export const useETHUSDOracle = (
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fastGasFetcherKey = (blockTag: BlockTag | undefined) => {
|
||||||
|
if (blockTag === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return ["gasgwei", blockTag];
|
||||||
|
};
|
||||||
|
|
||||||
|
const fastGasFetcher =
|
||||||
|
(
|
||||||
|
provider: JsonRpcProvider | undefined
|
||||||
|
): Fetcher<any | undefined, ["gasgwei", BlockTag | undefined]> =>
|
||||||
|
async (_, blockTag) => {
|
||||||
|
if (provider?.network.chainId !== 1) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const c = new Contract(
|
||||||
|
"fast-gas-gwei.data.eth",
|
||||||
|
AggregatorV3Interface,
|
||||||
|
provider
|
||||||
|
);
|
||||||
|
const priceData = await c.latestRoundData({ blockTag });
|
||||||
|
return priceData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useFastGasRawOracle = (
|
||||||
|
provider: JsonRpcProvider | undefined,
|
||||||
|
blockTag: BlockTag | undefined
|
||||||
|
): any | undefined => {
|
||||||
|
const fetcher = fastGasFetcher(provider);
|
||||||
|
const { data, error } = useSWRImmutable(fastGasFetcherKey(blockTag), fetcher);
|
||||||
|
if (error) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in New Issue