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

fix: Estimate the value of low-denomination tokens #155

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
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/CurrencyInputPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Currency, Pair } from '@apeswapfinance/sdk'
import { Button, ChevronDownIcon, Text } from '@apeswapfinance/uikit'
import styled from 'styled-components'
import { darken } from 'polished'
// import EstimatedConvertDollar from 'components/swap/EstimatedConvertDollar'
import EstimatedConvertDollar from 'components/swap/EstimatedConvertDollar'
import { useCurrencyBalance } from '../../state/wallet/hooks'
import CurrencySearchModal from '../SearchModal/CurrencySearchModal'
import CurrencyLogo from '../CurrencyLogo'
Expand Down Expand Up @@ -183,7 +183,7 @@ export default function CurrencyInputPanel({
</Aligner>
</CurrencySelect>
</InputRow>
{/* <EstimatedConvertDollar currency={currency} typedValue={value}/> */}
<EstimatedConvertDollar currency={currency} typedValue={value}/>
</Container>
{!disableCurrencySelect && onCurrencySelect && (
<CurrencySearchModal
Expand Down
25 changes: 14 additions & 11 deletions src/components/swap/EstimatedConvertDollar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { useEffect, useState } from 'react'
import { tryParseAmount } from 'state/swap/hooks'
import { Field } from 'state/swap/actions'
import styled from 'styled-components'
import { useCurrency } from 'hooks/Tokens'
import { useTradeExactOut } from 'hooks/Trades'
import { useTradeExactIn } from 'hooks/Trades'

const EstimatedPriceDollar = styled.div`
padding: 0rem 0.75rem 0.75rem 1rem;
Expand All @@ -21,27 +20,31 @@ const EstimatedSymbol = styled.span`
`
const CURRENCY_BUSD_ID = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'

export default function EstimatedConvertDollar({ currency, typedValue = null }: { currency: any; typedValue: any }) {
function CalculateRate (currency, typedValue) {

const [totalRate, setTotalRate] = useState(null);
const outputCurrency = useCurrency(CURRENCY_BUSD_ID)
const parsedAmount = tryParseAmount(typedValue, currency)
const parsedAmount2 = tryParseAmount('1', outputCurrency ?? undefined)
const trade = useTradeExactOut(currency, parsedAmount2)

const parsedAmounts = { [Field.INPUT]: trade?.inputAmount || parsedAmount }
const value = 1/Number(parsedAmounts[Field.INPUT]?.toExact())
const inputCurrency = useCurrency(CURRENCY_BUSD_ID)
const parsedAmount = tryParseAmount('1', inputCurrency ?? undefined)
const trade = useTradeExactIn(parsedAmount, currency ?? undefined)
const value = currency?.symbol === 'BUSD' ? 1 : Number(trade?.executionPrice.invert().toSignificant(6))

const decimals = value < 0.1 ? 1000000000 : 100;
const rate: any = Number(typedValue) * (Math.round((value) * decimals) / decimals);
const total = rate > 0.1 ? rate.toFixed(2) : rate;
useEffect(() => {
setTotalRate(total);
}, [total])
return trade ? totalRate : null;
}

export default function EstimatedConvertDollar({ currency, typedValue = null }: { currency: any; typedValue: any }) {

const totalRate = CalculateRate(currency, typedValue);
return (
<EstimatedPriceDollar>
<EstimatedSymbol>~</EstimatedSymbol>
{trade ? <span>${totalRate || 0}</span> : '-'}
{totalRate ? <span>${totalRate || 0}</span> : '-'}
</EstimatedPriceDollar>
)
}