diff --git a/package-lock.json b/package-lock.json index 9fe273e..df0002c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "version": "0.1.0", "license": "MIT", "dependencies": { + "@chainlink/contracts": "^0.2.1", "@craco/craco": "^6.2.0", "@fontsource/fira-code": "^4.5.0", "@fontsource/roboto": "^4.5.0", @@ -1203,6 +1204,11 @@ "version": "0.2.3", "license": "MIT" }, + "node_modules/@chainlink/contracts": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@chainlink/contracts/-/contracts-0.2.1.tgz", + "integrity": "sha512-mAQgPQKiqW3tLMlp31NgcnXpwG3lttgKU0izAqKiirJ9LH7rQ+O0oHIVR5Qp2yuqgmfbLsgfdLo4GcVC8IFz3Q==" + }, "node_modules/@cnakazawa/watch": { "version": "1.0.4", "license": "Apache-2.0", @@ -19913,6 +19919,11 @@ "@bcoe/v8-coverage": { "version": "0.2.3" }, + "@chainlink/contracts": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@chainlink/contracts/-/contracts-0.2.1.tgz", + "integrity": "sha512-mAQgPQKiqW3tLMlp31NgcnXpwG3lttgKU0izAqKiirJ9LH7rQ+O0oHIVR5Qp2yuqgmfbLsgfdLo4GcVC8IFz3Q==" + }, "@cnakazawa/watch": { "version": "1.0.4", "requires": { diff --git a/package.json b/package.json index d76b66f..09b401f 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "private": true, "license": "MIT", "dependencies": { + "@chainlink/contracts": "^0.2.1", "@craco/craco": "^6.2.0", "@fontsource/fira-code": "^4.5.0", "@fontsource/roboto": "^4.5.0", diff --git a/src/PriceBox.tsx b/src/PriceBox.tsx new file mode 100644 index 0000000..af5e461 --- /dev/null +++ b/src/PriceBox.tsx @@ -0,0 +1,109 @@ +import React, { useState, useEffect, useMemo, useContext } from "react"; +import { ethers } from "ethers"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faGasPump } from "@fortawesome/free-solid-svg-icons"; +import AggregatorV3Interface from "@chainlink/contracts/abi/v0.8/AggregatorV3Interface.json"; +import { RuntimeContext } from "./useRuntime"; +import { formatValue } from "./components/formatter"; +import { useLatestBlock } from "./useLatestBlock"; + +const ETH_FEED_DECIMALS = 8; + +const PriceBox: React.FC = () => { + const { provider } = useContext(RuntimeContext); + const latestBlock = useLatestBlock(provider); + + const maybeOutdated: boolean = + latestBlock !== undefined && + Date.now() / 1000 - latestBlock.timestamp > 3600; + 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(); + const [latestGasData, setLatestGasData] = useState(); + 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 [latestPrice, latestPriceTimestamp] = 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, latestGasPriceTimestamp] = useMemo(() => { + if (!latestGasData) { + return [undefined, undefined]; + } + + const formattedGas = formatValue(latestGasData.answer, 9); + const timestamp = new Date(latestGasData.updatedAt * 1000); + return [formattedGas, timestamp]; + }, [latestGasData]); + + return ( + <> + {latestPriceData && ( +
+ + Eth: ${latestPrice} + + {latestGasData && ( + <> + | + + + {latestGasPrice} Gwei + + + )} +
+ )} + + ); +}; + +export default React.memo(PriceBox); diff --git a/src/Title.tsx b/src/Title.tsx index c8aab8f..c64fdbd 100644 --- a/src/Title.tsx +++ b/src/Title.tsx @@ -1,6 +1,7 @@ import React, { useState, useRef } from "react"; import { Link, useHistory } from "react-router-dom"; import useKeyboardShortcut from "use-keyboard-shortcut"; +import PriceBox from "./PriceBox"; const Title: React.FC = () => { const [search, setSearch] = useState(); @@ -41,27 +42,30 @@ const Title: React.FC = () => { Otterscan -
- - -
+ + + + ); }; diff --git a/tailwind.config.js b/tailwind.config.js index 6db5164..3f72cce 100644 --- a/tailwind.config.js +++ b/tailwind.config.js @@ -1,3 +1,5 @@ +const colors = require("tailwindcss/colors"); + module.exports = { purge: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"], darkMode: false, // or 'media' or 'class' @@ -6,6 +8,7 @@ module.exports = { colors: { "link-blue": "#3498db", "link-blue-hover": "#0468ab", + orange: colors.orange, }, fontFamily: { sans: ["Roboto"],