diff --git a/src/components/Display/USDPriceDisplay.tsx b/src/components/Display/USDPriceDisplay.tsx new file mode 100644 index 000000000..e4022acaa --- /dev/null +++ b/src/components/Display/USDPriceDisplay.tsx @@ -0,0 +1,78 @@ +import { useEffect, useState } from "react"; +import { useConfigStore } from "../../hooks/useConfigStore"; +import { chainProperties } from "../../utils/chains"; +import axios from "axios"; +import { ZERO_ADDRESS } from "../../utils/math/constants"; + +function USDPriceDisplay({ token, display }) { + const [chainId, networkName] = useConfigStore((state) => [ + state.chainId, + state.networkName, + ]); + + const [priceFromAPI, setPriceFromAPI] = useState(undefined); + + function fetchUSDPriceFromDefined() { + axios + .post( + "https://graph.defined.fi/graphql", + { + query: `{ + getTokenPrices( + inputs: [ + { address: "${token.address}", networkId: ${chainId}} + ] + ) { + address + networkId + priceUsd + } + }`, + }, + { + headers: { + "Content-Type": "application/json", + Authorization: "944ebfd6779a45fede53b997d2c32b1e2333e316", + }, + } + ) + .then((response) => { + console.log(response.data); + setPriceFromAPI( + response.data.data?.getTokenPrices[0]?.priceUsd ?? undefined + ); + }); + } + + //try to fetch it from the subgraph, if the token doesnt exist in the subgraph, fetch it from defined.fi + + useEffect(() => { + if ( + token.address != ZERO_ADDRESS && + token.USDPrice == "0" && + chainProperties[networkName].sdkSupport.defined + ) { + console.log("timestamp", Date.now() / 1000); + fetchUSDPriceFromDefined(); + } + }, [token.address, chainId]); + + return ( + + ~$ + {!isNaN(token.decimals) && !isNaN(token.USDPrice) && token.USDPrice != "0" + ? ( + (!isNaN(parseFloat(display)) ? parseFloat(display) : 0) * + (token.USDPrice ?? 0) + ).toFixed(2) + : priceFromAPI + ? ( + (!isNaN(parseFloat(display)) ? parseFloat(display) : 0) * + (priceFromAPI ?? 0) + ).toFixed(2) + : "-.--"} + + ); +} + +export default USDPriceDisplay; diff --git a/src/components/Trade/LimitSwap.tsx b/src/components/Trade/LimitSwap.tsx index cd12cfee3..619e11a41 100644 --- a/src/components/Trade/LimitSwap.tsx +++ b/src/components/Trade/LimitSwap.tsx @@ -49,6 +49,7 @@ import JSBI from "jsbi"; import { fetchRangeTokenUSDPrice } from "../../utils/tokens"; import BalanceDisplay from "../Display/BalanceDisplay"; import { getRouterAddress } from "../../utils/config"; +import USDPriceDisplay from "../Display/USDPriceDisplay"; export default function LimitSwap() { const [chainId, networkName, limitSubgraph, setLimitSubgraph, logoMap] = @@ -846,18 +847,10 @@ export default function LimitSwap() {