Apply value formatting

This commit is contained in:
Willian Mitsuda 2021-07-13 18:55:32 -03:00
parent 5f04ce8018
commit a36749ee3d

View File

@ -4,6 +4,7 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faGasPump } from "@fortawesome/free-solid-svg-icons"; import { faGasPump } from "@fortawesome/free-solid-svg-icons";
import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json"; import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json";
import { RuntimeContext } from "./useRuntime"; import { RuntimeContext } from "./useRuntime";
import { formatValue } from "./components/formatter";
const ETH_FEED_DECIMALS = 8; const ETH_FEED_DECIMALS = 8;
@ -44,29 +45,43 @@ const PriceBox: React.FC = () => {
readData(); readData();
}, [ethFeed, gasFeed]); }, [ethFeed, gasFeed]);
const [latestPrice, timestamp] = useMemo(() => {
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]);
const latestGasPrice = useMemo(() => {
if (!latestGasData) {
return undefined;
}
return formatValue(latestGasData.answer, 9);
}, [latestGasData]);
return ( return (
<> <>
{latestPriceData && ( {latestPriceData && (
<div className="flex rounded-lg px-2 py-1 space-x-2 bg-gray-100 font-sans text-xs text-gray-800"> <div
className="flex rounded-lg px-2 py-1 space-x-2 bg-gray-100 font-sans text-xs text-gray-800"
title={`Updated at: ${timestamp?.toString()}`}
>
<span> <span>
Eth: $ Eth: $<span className="font-balance">{latestPrice}</span>
<span className="font-balance">
{ethers.utils.commify(
ethers.utils.formatUnits(
latestPriceData.answer,
ETH_FEED_DECIMALS
)
)}
</span>
</span> </span>
{latestGasData && ( {latestGasData && (
<> <>
<span>|</span> <span>|</span>
<span className="text-gray-400"> <span className="text-gray-400">
<FontAwesomeIcon icon={faGasPump} size="1x" /> <FontAwesomeIcon icon={faGasPump} size="1x" />
<span className="ml-1"> <span className="ml-1">{latestGasPrice} Gwei</span>
{ethers.utils.formatUnits(latestGasData.answer, "gwei")} Gwei
</span>
</span> </span>
</> </>
)} )}