Skip to content

Commit

Permalink
fix: add refresh btn logic (#2722)
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain authored Sep 27, 2023
2 parents a9da155 + 4e7688b commit 40050da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {useLanguage} from '../../../../../i18n'
import {useSelectedWallet} from '../../../../../SelectedWallet'
import {COLORS} from '../../../../../theme'
import {useTokenInfo} from '../../../../../yoroi-wallets/hooks'
import {Quantities} from '../../../../../yoroi-wallets/utils'
import {asQuantity, Quantities} from '../../../../../yoroi-wallets/utils'
import {useStrings} from '../../../common/strings'
import {useSwapTouched} from '../../../common/SwapFormProvider'

Expand All @@ -18,7 +18,6 @@ const PRECISION = 10
export const EditLimitPrice = () => {
const strings = useStrings()
const {numberLocale} = useLanguage()
const [text, setText] = React.useState('')
const wallet = useSelectedWallet()

const {createOrder, limitPriceChanged} = useSwap()
Expand All @@ -35,7 +34,7 @@ export const EditLimitPrice = () => {
const defaultPrice = createOrder.marketPrice

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

Expand All @@ -45,24 +44,22 @@ export const EditLimitPrice = () => {
const defaultPrice = createOrder.marketPrice

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

const onChange = (text: string) => {
const [formattedPrice, price] = Quantities.parseFromText(text, PRECISION, numberLocale)
const value = Quantities.denominated(price, PRECISION)
const [formattedPrice] = Quantities.parseFromText(text, PRECISION, numberLocale)

setText(formattedPrice)
limitPriceChanged(value)
limitPriceChanged(asQuantity(formattedPrice))
}

return (
<View style={[styles.container, disabled && styles.disabled]}>
<Text style={styles.label}>{disabled ? strings.marketPrice : strings.limitPrice}</Text>

<View style={styles.content}>
<AmountInput onChange={onChange} value={text} editable={!disabled} />
<AmountInput onChange={onChange} value={createOrder.limitPrice} editable={!disabled} />

<View style={[styles.textWrapper, disabled && styles.disabled]}>
<Text style={styles.text}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
import {useSwap, useSwapPoolsByPair} from '@yoroi/swap'
import {Swap} from '@yoroi/types'
import BigNumber from 'bignumber.js'
import React from 'react'
import {StyleSheet, View} from 'react-native'
import {TouchableOpacity} from 'react-native-gesture-handler'

import {Icon} from '../../../../../../components'
import {LoadingOverlay} from '../../../../../../components/LoadingOverlay'
import {useLanguage} from '../../../../../../i18n'
import {COLORS} from '../../../../../../theme'
import {asQuantity} from '../../../../../../yoroi-wallets/utils'
import {ButtonGroup} from '../../../../common/ButtonGroup/ButtonGroup'
import {useStrings} from '../../../../common/strings'
import {useSwapTouched} from '../../../../common/SwapFormProvider'

const PRECISION = 10

export const TopTokenActions = () => {
const strings = useStrings()
const {numberLocale} = useLanguage()
const orderTypeLabels = [strings.marketButton, strings.limitButton]
const {createOrder, orderTypeChanged} = useSwap()
const {createOrder, orderTypeChanged, limitPriceChanged, selectedPoolChanged} = useSwap()
const {isBuyTouched, isSellTouched} = useSwapTouched()
const isDisabled = !isBuyTouched || !isSellTouched || createOrder.selectedPool === undefined
const orderTypeIndex = createOrder.type === 'market' ? 0 : 1

const {refetch, isLoading} = useSwapPoolsByPair({
tokenA: createOrder.amounts.sell.tokenId ?? '',
tokenB: createOrder.amounts.buy.tokenId ?? '',
})
const {refetch, isLoading} = useSwapPoolsByPair(
{
tokenA: createOrder.amounts.sell.tokenId ?? '',
tokenB: createOrder.amounts.buy.tokenId ?? '',
},
{
onSuccess: (poolList: Swap.Pool[]) => {
const bestPool: Swap.Pool | undefined = poolList
.sort((a: Swap.Pool, b: Swap.Pool) => a.price - b.price)
.find(() => true)
if (bestPool !== undefined) {
const defaultPrice = createOrder.marketPrice
const formattedValue = BigNumber(defaultPrice).decimalPlaces(PRECISION).toFormat(numberLocale)
selectedPoolChanged(bestPool)
limitPriceChanged(asQuantity(formattedValue))
}
},
},
)

const handleSelectOrderType = (index: number) => {
if (index === 0) {
Expand Down

0 comments on commit 40050da

Please sign in to comment.