2021-07-13 21:12:49 +00:00
|
|
|
import React, { useState, useEffect, useMemo, useContext } from "react";
|
|
|
|
import { ethers } from "ethers";
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2021-08-08 06:51:21 +00:00
|
|
|
import { faGasPump } from "@fortawesome/free-solid-svg-icons/faGasPump";
|
2021-07-13 21:12:49 +00:00
|
|
|
import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json";
|
|
|
|
import { RuntimeContext } from "./useRuntime";
|
2021-07-13 21:55:32 +00:00
|
|
|
import { formatValue } from "./components/formatter";
|
2021-07-13 22:23:39 +00:00
|
|
|
import { useLatestBlock } from "./useLatestBlock";
|
2021-07-13 21:12:49 +00:00
|
|
|
|
|
|
|
const ETH_FEED_DECIMALS = 8;
|
|
|
|
|
|
|
|
const PriceBox: React.FC = () => {
|
|
|
|
const { provider } = useContext(RuntimeContext);
|
2021-07-13 22:23:39 +00:00
|
|
|
const latestBlock = useLatestBlock(provider);
|
|
|
|
|
|
|
|
const maybeOutdated: boolean =
|
|
|
|
latestBlock !== undefined &&
|
|
|
|
Date.now() / 1000 - latestBlock.timestamp > 3600;
|
2021-07-13 21:12:49 +00:00
|
|
|
const ethFeed = useMemo(
|
|
|
|
() =>
|
|
|
|
provider &&
|
|
|
|
new ethers.Contract("eth-usd.data.eth", AggregatorV3Interface, provider),
|
|
|
|
[provider]
|
|
|
|
);
|
|
|
|
const gasFeed = useMemo(
|
|
|
|
() =>
|
|
|
|
provider &&
|
|
|
|
new ethers.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]);
|
|
|
|
|
2021-07-13 22:07:42 +00:00
|
|
|
const [latestPrice, latestPriceTimestamp] = useMemo(() => {
|
2021-07-13 21:55:32 +00:00
|
|
|
if (!latestPriceData) {
|
|
|
|
return [undefined, undefined];
|
|
|
|
}
|
|
|
|
|
|
|
|
const price = latestPriceData.answer.div(10 ** (ETH_FEED_DECIMALS - 2));
|
|
|
|
const formattedPrice = ethers.utils.commify(
|
|
|
|
ethers.utils.formatUnits(price, 2)
|
|
|
|
);
|
|
|
|
|
|
|
|
const timestamp = new Date(latestPriceData.updatedAt * 1000);
|
|
|
|
return [formattedPrice, timestamp];
|
|
|
|
}, [latestPriceData]);
|
|
|
|
|
2021-07-13 22:07:42 +00:00
|
|
|
const [latestGasPrice, latestGasPriceTimestamp] = useMemo(() => {
|
2021-07-13 21:55:32 +00:00
|
|
|
if (!latestGasData) {
|
2021-07-13 22:07:42 +00:00
|
|
|
return [undefined, undefined];
|
2021-07-13 21:55:32 +00:00
|
|
|
}
|
2021-07-13 22:07:42 +00:00
|
|
|
|
|
|
|
const formattedGas = formatValue(latestGasData.answer, 9);
|
|
|
|
const timestamp = new Date(latestGasData.updatedAt * 1000);
|
|
|
|
return [formattedGas, timestamp];
|
2021-07-13 21:55:32 +00:00
|
|
|
}, [latestGasData]);
|
|
|
|
|
2021-07-13 21:12:49 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{latestPriceData && (
|
2021-07-13 22:23:39 +00:00
|
|
|
<div
|
|
|
|
className={`flex rounded-lg px-2 py-1 space-x-2 ${
|
|
|
|
maybeOutdated ? "bg-orange-200" : "bg-gray-100"
|
|
|
|
} font-sans text-xs text-gray-800`}
|
|
|
|
>
|
2021-07-13 22:07:42 +00:00
|
|
|
<span
|
|
|
|
title={`ETH/USD last updated at: ${latestPriceTimestamp?.toString()}`}
|
|
|
|
>
|
2021-07-13 21:55:32 +00:00
|
|
|
Eth: $<span className="font-balance">{latestPrice}</span>
|
2021-07-13 21:12:49 +00:00
|
|
|
</span>
|
|
|
|
{latestGasData && (
|
|
|
|
<>
|
|
|
|
<span>|</span>
|
2021-07-13 22:07:42 +00:00
|
|
|
<span
|
|
|
|
className="text-gray-400"
|
|
|
|
title={`Fast gas price last updated at: ${latestGasPriceTimestamp?.toString()}`}
|
|
|
|
>
|
2021-07-13 21:12:49 +00:00
|
|
|
<FontAwesomeIcon icon={faGasPump} size="1x" />
|
2021-07-13 21:55:32 +00:00
|
|
|
<span className="ml-1">{latestGasPrice} Gwei</span>
|
2021-07-13 21:12:49 +00:00
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(PriceBox);
|