diff --git a/src/components/Layout/Page.tsx b/src/components/Layout/Page.tsx index c6a8270..5356c53 100644 --- a/src/components/Layout/Page.tsx +++ b/src/components/Layout/Page.tsx @@ -2,7 +2,7 @@ import { useTranslation } from 'contexts/Localization' import Head from 'next/head' import { useRouter } from 'next/router' import { DEFAULT_META, getCustomMeta } from 'config/constants/meta' -import { useAstraBusdPrice } from 'hooks/useBUSDPrice' +import { useAstraUsdtPrice } from 'hooks/useUSDTPrice' import { Container } from '@astraprotocol/astra-ui' // const StyledPage = styled(Container)` @@ -24,7 +24,7 @@ import { Container } from '@astraprotocol/astra-ui' export const PageMeta: React.FC<{ symbol?: string }> = ({ symbol }) => { const { t } = useTranslation() const { pathname } = useRouter() - const astraPriceUsd = useAstraBusdPrice() + const astraPriceUsd = useAstraUsdtPrice() const asaPriceUsdDisplay = astraPriceUsd ? `$${astraPriceUsd.toFixed(3)}` : '' diff --git a/src/hooks/useBUSDPrice.ts b/src/hooks/useBUSDPrice.ts deleted file mode 100644 index d25d8c7..0000000 --- a/src/hooks/useBUSDPrice.ts +++ /dev/null @@ -1,98 +0,0 @@ -import { Currency, currencyEquals, JSBI, Price } from '@solarswap/sdk' -import tokens from 'config/constants/tokens' -import useActiveWeb3React from 'hooks/useActiveWeb3React' -import { useMemo } from 'react' -import { multiplyPriceByAmount } from 'utils/prices' -import { wrappedCurrency } from '../utils/wrappedCurrency' -import { PairState, usePairs } from './usePairs' - -const { wasa: WBNB, usdt } = tokens - -/** - * Returns the price in USDT of the input currency - * @param currency currency to compute the USDT price of - */ -export default function useBUSDPrice(currency?: Currency): Price | undefined { - const { chainId } = useActiveWeb3React() - const wrapped = wrappedCurrency(currency, chainId) - const tokenPairs: [Currency | undefined, Currency | undefined][] = useMemo( - () => [ - [chainId && wrapped && currencyEquals(WBNB, wrapped) ? undefined : currency, chainId ? WBNB : undefined], - [wrapped?.equals(usdt) ? undefined : wrapped, usdt], - [chainId ? WBNB : undefined, usdt], - ], - [chainId, currency, wrapped], - ) - const [[ethPairState, ethPair], [busdPairState, busdPair], [busdEthPairState, busdEthPair]] = usePairs(tokenPairs) - - return useMemo(() => { - if (!currency || !wrapped || !chainId) { - return undefined - } - // handle weth/eth - if (wrapped.equals(WBNB)) { - if (busdPair) { - const price = busdPair.priceOf(WBNB) - return new Price(currency, usdt, price.denominator, price.numerator) - } - return undefined - } - // handle usdt - if (wrapped.equals(usdt)) { - return new Price(usdt, usdt, '1', '1') - } - - const ethPairETHAmount = ethPair?.reserveOf(WBNB) - const ethPairETHBUSDValue: JSBI = - ethPairETHAmount && busdEthPair ? busdEthPair.priceOf(WBNB).quote(ethPairETHAmount).raw : JSBI.BigInt(0) - - // all other tokens - // first try the usdt pair - if ( - busdPairState === PairState.EXISTS && - busdPair && - busdPair.reserveOf(usdt).greaterThan(ethPairETHBUSDValue) - ) { - const price = busdPair.priceOf(wrapped) - return new Price(currency, usdt, price.denominator, price.numerator) - } - if (ethPairState === PairState.EXISTS && ethPair && busdEthPairState === PairState.EXISTS && busdEthPair) { - if (busdEthPair.reserveOf(usdt).greaterThan('0') && ethPair.reserveOf(WBNB).greaterThan('0')) { - const ethBusdPrice = busdEthPair.priceOf(usdt) - const currencyEthPrice = ethPair.priceOf(WBNB) - const busdPrice = ethBusdPrice.multiply(currencyEthPrice).invert() - return new Price(currency, usdt, busdPrice.denominator, busdPrice.numerator) - } - } - - return undefined - }, [chainId, currency, ethPair, ethPairState, busdEthPair, busdEthPairState, busdPair, busdPairState, wrapped]) -} - -export const useAstraBusdPrice = (): Price | undefined => { - const asaBusdPrice = useBUSDPrice(tokens.wasa) - return asaBusdPrice -} - -export const useBUSDCurrencyAmount = (currency: Currency, amount: number): number | undefined => { - const { chainId } = useActiveWeb3React() - const busdPrice = useBUSDPrice(currency) - const wrapped = wrappedCurrency(currency, chainId) - if (busdPrice) { - return multiplyPriceByAmount(busdPrice, amount, wrapped.decimals) - } - return undefined -} - -export const useBUSDCakeAmount = (amount: number): number | undefined => { - const asaBusdPrice = useAstraBusdPrice() - if (asaBusdPrice) { - return multiplyPriceByAmount(asaBusdPrice, amount) - } - return undefined -} - -export const useBNBBusdPrice = (): Price | undefined => { - const bnbBusdPrice = useBUSDPrice(tokens.wasa) - return bnbBusdPrice -} diff --git a/src/hooks/usePairs.ts b/src/hooks/usePairs.ts index e5ac49b..2c23a2b 100644 --- a/src/hooks/usePairs.ts +++ b/src/hooks/usePairs.ts @@ -60,6 +60,7 @@ export function usePairs(currencies: [Currency | undefined, Currency | undefined if (!reserves) return [PairState.NOT_EXISTS, null] const { reserve0, reserve1 } = reserves const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA] + return [ PairState.EXISTS, new Pair(new TokenAmount(token0, reserve0.toString()), new TokenAmount(token1, reserve1.toString())), diff --git a/src/hooks/useUSDTPrice.ts b/src/hooks/useUSDTPrice.ts new file mode 100644 index 0000000..0300c9f --- /dev/null +++ b/src/hooks/useUSDTPrice.ts @@ -0,0 +1,87 @@ +import { Currency, currencyEquals, JSBI, Price } from '@solarswap/sdk' +import tokens from 'config/constants/tokens' +import useActiveWeb3React from 'hooks/useActiveWeb3React' +import { useMemo } from 'react' +import { multiplyPriceByAmount } from 'utils/prices' +import { wrappedCurrency } from '../utils/wrappedCurrency' +import { PairState, usePairs } from './usePairs' + +const { wasa: WASA, usdt } = tokens + +/** + * Returns the price in USDT of the input currency + * @param currency currency to compute the USDT price of + */ +export default function useUSDTPrice(currency?: Currency): Price | undefined { + const { chainId } = useActiveWeb3React() + const wrapped = wrappedCurrency(currency, chainId) + const tokenPairs: [Currency | undefined, Currency | undefined][] = useMemo( + () => [ + [chainId && wrapped && currencyEquals(WASA, wrapped) ? undefined : currency, chainId ? WASA : undefined], + [wrapped?.equals(usdt) ? undefined : wrapped, usdt], + [chainId ? WASA : undefined, usdt], + ], + [chainId, currency, wrapped], + ) + const [[ethPairState, ethPair], [usdtPairState, usdtPair], [usdtEthPairState, usdtEthPair]] = usePairs(tokenPairs) + + return useMemo(() => { + if (!currency || !wrapped || !chainId) { + return undefined + } + // handle weth/eth + if (wrapped.equals(WASA)) { + if (usdtPair) { + const price = usdtPair.priceOf(WASA) + return new Price(currency, usdt, price.denominator, price.numerator) + } + return undefined + } + // handle usdt + if (wrapped.equals(usdt)) { + return new Price(usdt, usdt, '1', '1') + } + + const ethPairETHAmount = ethPair?.reserveOf(WASA) + const ethPairETHBUSDValue: JSBI = + ethPairETHAmount && usdtEthPair ? usdtEthPair.priceOf(WASA).quote(ethPairETHAmount).raw : JSBI.BigInt(0) + + // all other tokens + // first try the usdt pair + if ( + usdtPairState === PairState.EXISTS && + usdtPair && + usdtPair.reserveOf(usdt).greaterThan(ethPairETHBUSDValue) + ) { + const price = usdtPair.priceOf(wrapped) + return new Price(currency, usdt, price.denominator, price.numerator) + } + if (ethPairState === PairState.EXISTS && ethPair && usdtEthPairState === PairState.EXISTS && usdtEthPair) { + if (usdtEthPair.reserveOf(usdt).greaterThan('0') && ethPair.reserveOf(WASA).greaterThan('0')) { + const ethUsdtPrice = usdtEthPair.priceOf(usdt) + const currencyEthPrice = ethPair.priceOf(WASA) + const usdtPrice = ethUsdtPrice.multiply(currencyEthPrice).invert() + return new Price(currency, usdt, usdtPrice.denominator, usdtPrice.numerator) + } + } + + return undefined + }, [chainId, currency, ethPair, ethPairState, usdtEthPair, usdtEthPairState, usdtPair, usdtPairState, wrapped]) +} + +export const useAstraUsdtPrice = (): Price | undefined => { + const asaUsdtPrice = useUSDTPrice(tokens.wasa) + return asaUsdtPrice +} + +export const useBUSDCurrencyAmount = (currency: Currency, amount: number): number | undefined => { + const { chainId } = useActiveWeb3React() + const busdPrice = useUSDTPrice(currency) + const wrapped = wrappedCurrency(currency, chainId) + if (busdPrice) { + return multiplyPriceByAmount(busdPrice, amount, wrapped.decimals) + } + return undefined +} + +