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: Update limit price on refresh #2723

Merged
merged 3 commits into from
Sep 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ export const EditLimitPrice = () => {
const tokenToBuyName = isBuyTouched ? buyTokenInfo.ticker ?? buyTokenInfo.name : '-'

React.useEffect(() => {
const defaultPrice = createOrder.marketPrice

const formattedValue = BigNumber(defaultPrice).decimalPlaces(PRECISION).toFormat(numberLocale)
setText(formattedValue)
setText(BigNumber(createOrder.marketPrice).decimalPlaces(PRECISION).toFormat(numberLocale))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [createOrder.marketPrice])

React.useEffect(() => {
if (createOrder.type === 'limit') return

const defaultPrice = createOrder.marketPrice

const formattedValue = BigNumber(defaultPrice).decimalPlaces(PRECISION).toFormat(numberLocale)
setText(formattedValue)
if (createOrder.type === 'limit') {
setText(
BigNumber(createOrder?.limitPrice ?? 0)
.decimalPlaces(PRECISION)
.toFormat(numberLocale),
)
} else {
setText(BigNumber(createOrder.marketPrice).decimalPlaces(PRECISION).toFormat(numberLocale))
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [createOrder.type])
}, [createOrder.type, createOrder.limitPrice])

const onChange = (text: string) => {
const [formattedPrice, price] = Quantities.parseFromText(text, PRECISION, numberLocale)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {useSwapTouched} from '../../../../common/SwapFormProvider'
export const TopTokenActions = () => {
const strings = useStrings()
const orderTypeLabels = [strings.marketButton, strings.limitButton]
const {createOrder, orderTypeChanged} = useSwap()
const {createOrder, limitPriceChanged, orderTypeChanged} = useSwap()
const {isBuyTouched, isSellTouched} = useSwapTouched()
const isDisabled = !isBuyTouched || !isSellTouched || createOrder.selectedPool === undefined
const orderTypeIndex = createOrder.type === 'market' ? 0 : 1
Expand All @@ -32,6 +32,11 @@ export const TopTokenActions = () => {
}
}

const refresh = () => {
refetch()
limitPriceChanged(createOrder.marketPrice)
}

return (
<View style={styles.buttonsGroup}>
<ButtonGroup
Expand All @@ -40,7 +45,7 @@ export const TopTokenActions = () => {
selected={orderTypeIndex}
/>

<TouchableOpacity onPress={() => refetch()} disabled={isDisabled}>
<TouchableOpacity onPress={refresh} disabled={isDisabled}>
<Icon.Refresh size={24} color={isDisabled ? COLORS.DISABLED : ''} />
</TouchableOpacity>

Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/yoroi-wallets/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const Quantities = {
const invalid = new RegExp(`[^0-9${decimalSeparator}]`, 'g')
const sanitized = text === '' ? '' : text.replaceAll(invalid, '')
if (sanitized === '') return ['', `0`] as [string, Balance.Quantity]
if (sanitized.startsWith(decimalSeparator)) return [`0${decimalSeparator}`, `0`] as [string, Balance.Quantity]
const parts = sanitized.split(decimalSeparator)
const isDec = parts.length >= 2

Expand Down
13 changes: 13 additions & 0 deletions packages/swap/src/utils/quantities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,17 @@ describe('Quantities', () => {
expect(input).toBe('')
expect(quantity).toBe('0')
})

it('should parse "0" when the input text is an decimal separator', () => {
const text = '.'
const precision = 2
const [input, quantity] = Quantities.parseFromText(
text,
precision,
mockNumberLocale,
)

expect(input).toBe('0.')
expect(quantity).toBe('0')
})
})
2 changes: 2 additions & 0 deletions packages/swap/src/utils/quantities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export const Quantities = {
const invalid = new RegExp(`[^0-9${decimalSeparator}]`, 'g')
const sanitized = text === '' ? '' : text.replaceAll(invalid, '')
if (sanitized === '') return ['', `0`] as [string, Balance.Quantity]
if (sanitized.startsWith(decimalSeparator))
return [`0${decimalSeparator}`, `0`] as [string, Balance.Quantity]
const parts = sanitized.split(decimalSeparator)
const isDec = parts.length >= 2

Expand Down