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

feat: limit UI upgrade 2 - USD precision #5274

Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TokenWithLogo, USDC } from '@cowprotocol/common-const'
import { USDC } from '@cowprotocol/common-const'
import { getWrappedToken, tryParseCurrencyAmount } from '@cowprotocol/common-utils'
import { SupportedChainId } from '@cowprotocol/cow-sdk'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import { Currency } from '@uniswap/sdk-core'

import { useUsdPrice } from 'modules/usdAmount'

Expand All @@ -15,19 +15,11 @@ export function useConvertUsdToTokenValue(
const usdcToken = USDC[currencyUsdcPrice.currency.chainId as SupportedChainId]
const usdAmount = tryParseCurrencyAmount(typedValue, usdcToken)

const tokenAmount = currencyUsdcPrice.price.invert().quote(hackyAdjustAmountDust(usdAmount))
const tokenAmount = currencyUsdcPrice.price.invert().quote(usdAmount)

return tokenAmount.toExact()
}

return typedValue
}
}

/**
* TODO: this is a hacky way to adjust the amount to avoid dust
* For some reason, when you enter for example $366, price.quote() returns 365,9999999999
*/
function hackyAdjustAmountDust(amount: CurrencyAmount<TokenWithLogo>): typeof amount {
return amount.add(tryParseCurrencyAmount('0.000001', amount.currency))
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ export function CurrencyInputPanel(props: CurrencyInputPanelProps) {
const convertUsdToTokenValue = useConvertUsdToTokenValue(currency)

const onUserInputDispatch = useCallback(
(typedValue: string) => {
(typedValue: string, currencyValue?: string) => {
// Always pass through empty string to allow clearing
if (typedValue === '') {
setTypedValue('')
onUserInput(field, '')
return
}

const value = convertUsdToTokenValue(typedValue, isUsdValuesMode)
setTypedValue(typedValue)
// Avoid converting from USD if currencyValue is already provided
const value = currencyValue || convertUsdToTokenValue(typedValue, isUsdValuesMode)
onUserInput(field, value)
},
[onUserInput, field, convertUsdToTokenValue, isUsdValuesMode],
Expand All @@ -129,7 +130,7 @@ export function CurrencyInputPanel(props: CurrencyInputPanelProps) {
const value = isUsdValuesMode ? maxBalanceUsdAmount : maxBalance

if (value) {
onUserInputDispatch(value.toExact())
onUserInputDispatch(value.toExact(), isUsdValuesMode ? maxBalance.toExact() : undefined)
setMaxSellTokensAnalytics()
}
}, [maxBalance, onUserInputDispatch, isUsdValuesMode, maxBalanceUsdAmount])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum LimitOrderDeadlinePreset {
ONE_DAY = '1 Day',
THREE_DAYS = '3 Days',
ONE_MONTH = '1 Month',
SIX_MONTHS = '6 Months (max)',
SIX_MONTHS = '1 Year (max)',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alfetopito was this intentional?

I thought it was changed already, and also now it doesn't match te const name and the label

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!
I believe @fairlighteth did some refactoring where the label was reverted back to an earlier version.
Great catch that I forgot to update the label 🤦

#5282

}

const DEADLINE_VALUES: Record<LimitOrderDeadlinePreset, number> = {
Expand Down
12 changes: 5 additions & 7 deletions libs/common-utils/src/amountFormat/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


import {
AMOUNT_PRECISION,
FIAT_PRECISION,
INTL_NUMBER_FORMAT,
PERCENTAGE_PRECISION,
ZERO_FRACTION,
INTL_NUMBER_FORMAT,
} from '@cowprotocol/common-const'
import { Currency, CurrencyAmount, Percent, Rounding } from '@uniswap/sdk-core'

Expand Down Expand Up @@ -33,7 +31,7 @@ export function formatPercent(percent: Nullish<Percent>): string {
export function formatAmountWithPrecision(
amount: Nullish<FractionLike>,
precision: number,
numberFormat = INTL_NUMBER_FORMAT
numberFormat = INTL_NUMBER_FORMAT,
): string {
if (!amount) return ''

Expand All @@ -59,7 +57,7 @@ export function formatAmountWithPrecision(
// Apply the language formatting for the amount
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
const formattedQuotient = numberFormat.format(
BigInt(trimTrailingZeros(adjustedQuotient.toString(), decimalsSeparator))
BigInt(trimTrailingZeros(adjustedQuotient.toString(), decimalsSeparator)),
)

// Remove trailing zeros
Expand All @@ -80,7 +78,7 @@ export function formatAmountWithPrecision(
export function formatInputAmount(
amount: Nullish<FractionLike>,
balance: Nullish<CurrencyAmount<Currency>> = null,
isIndependentField = false
isIndependentField = false,
): string {
if (!amount) return ''

Expand All @@ -92,7 +90,7 @@ export function formatInputAmount(
}

const precision = getPrecisionForAmount(amount)
const result = amount.toFixed(precision)
const result = amount.toFixed(precision, undefined, Rounding.ROUND_HALF_UP)

return trimTrailingZeros(+result === 0 ? amount.toSignificant(AMOUNT_PRECISION) : result)
}
Loading