Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove some func + remove busd #42

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/Layout/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand All @@ -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)}` : ''

Expand Down
98 changes: 0 additions & 98 deletions src/hooks/useBUSDPrice.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/hooks/usePairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
Expand Down
87 changes: 87 additions & 0 deletions src/hooks/useUSDTPrice.ts
Original file line number Diff line number Diff line change
@@ -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
}


Loading