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

feature/BEV-2131 Will now select medium fee rate or blockbook recomme… #879

Merged
merged 1 commit into from
Feb 12, 2024
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
32 changes: 24 additions & 8 deletions src/ux/requestsModal/BitcoinMiningFeeContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ import { selectBitcoin } from 'store/slices/settingsSlice'
import { Typography } from 'src/components'
import { sharedStyles } from 'shared/styles'

export const BitcoinMiningFeeContainer = () => {
export interface FeeRecord {
feeName: string
feeRate: string
}

interface BitcoinMiningFeeContainerProps {
onFeeRatesLoaded?: (rates: FeeRecord[]) => void
}

export const BitcoinMiningFeeContainer = ({
onFeeRatesLoaded,
}: BitcoinMiningFeeContainerProps) => {
const bitcoinCore = useAppSelector(selectBitcoin)
const [suggestedBitcoinFees, setSuggestedBitcoinFees] = useState<
{ feeName: string; feeRate: string }[]
>([])
const [suggestedBitcoinFees, setSuggestedBitcoinFees] = useState<FeeRecord[]>(
[],
)

useEffect(() => {
// Fetch the bitcoin fees
bitcoinCore?.networksArr[0].bips[0].fetcher
.fetchBitcoinMiningFeeRates('cypher')
.then(cypherResponse => {
setSuggestedBitcoinFees([
const suggestedBitcoinFeesData = [
{
feeName: 'low',
feeRate: cypherResponse.low_fee_per_kb.toString(),
Expand All @@ -30,24 +41,29 @@ export const BitcoinMiningFeeContainer = () => {
feeName: 'high',
feeRate: cypherResponse.high_fee_per_kb.toString(),
},
])
]
setSuggestedBitcoinFees(suggestedBitcoinFeesData)
onFeeRatesLoaded?.(suggestedBitcoinFeesData)
})
.catch(() => {
// Fetch from blockbook if cypher fails

bitcoinCore?.networksArr[0].bips[0].fetcher
.fetchBitcoinMiningFeeRates('blockbook')
.then(blockbookResponse => {
setSuggestedBitcoinFees([
const suggestedBitcoinFeesData = [
{
feeName: 'low',
feeRate: convertBtcToSatoshi(
blockbookResponse.result,
).toString(),
},
])
]
setSuggestedBitcoinFees(suggestedBitcoinFeesData)
onFeeRatesLoaded?.(suggestedBitcoinFeesData)
})
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [bitcoinCore])

return (
Expand Down
37 changes: 32 additions & 5 deletions src/ux/requestsModal/ReviewBitcoinTransactionContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useContext, useMemo, useState } from 'react'
import { useContext, useMemo, useState, useCallback } from 'react'
import {
convertSatoshiToBtcHuman,
SendBitcoinRequest,
} from '@rsksmart/rif-wallet-bitcoin'
import { useTranslation } from 'react-i18next'
import { StyleSheet, View } from 'react-native'
import { useCallback } from 'react'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { FormProvider, useForm } from 'react-hook-form'

Expand All @@ -22,7 +21,10 @@ import { WalletContext } from 'shared/wallet'
import { useAddress } from 'shared/hooks'
import { formatTokenValues } from 'shared/utils'

import { BitcoinMiningFeeContainer } from './BitcoinMiningFeeContainer'
import {
BitcoinMiningFeeContainer,
FeeRecord,
} from './BitcoinMiningFeeContainer'

interface ReviewBitcoinTransactionContainerProps {
request: SendBitcoinRequest
Expand Down Expand Up @@ -179,7 +181,32 @@ const MiningFeeInput = ({
},
})

const handleFeeChange = (text: string) => onChange(text)
const { setValue } = methods

const handleFeeChange = useCallback(
(text: string) => onChange(text),
[onChange],
)

const onFeeRatesLoaded = useCallback(
(feeRates: FeeRecord[]) => {
let newFee
// If length > 1 then it's fetching the fee from cypher
if (feeRates.length > 1) {
newFee = feeRates[1].feeRate
}
// If length === 1 then it's fetching the fee from blockbook
if (feeRates.length === 1) {
newFee = feeRates[0].feeRate
}

if (newFee) {
handleFeeChange(newFee)
setValue('miningFee', Number(newFee))
}
},
[handleFeeChange, setValue],
)

return (
<FormProvider {...methods}>
Expand All @@ -188,7 +215,7 @@ const MiningFeeInput = ({
inputName="miningFee"
onChangeText={handleFeeChange}
/>
<BitcoinMiningFeeContainer />
<BitcoinMiningFeeContainer onFeeRatesLoaded={onFeeRatesLoaded} />
</FormProvider>
)
}
Loading