Skip to content

Commit

Permalink
feature/BEV-2131 Will now select medium fee rate or blockbook recomme…
Browse files Browse the repository at this point in the history
…nded fee when sending bitcoin.
  • Loading branch information
Freshenext committed Feb 6, 2024
1 parent a5c3ba2 commit 98e8777
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
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 @@ -174,7 +176,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 @@ -183,7 +210,7 @@ const MiningFeeInput = ({
inputName="miningFee"
onChangeText={handleFeeChange}
/>
<BitcoinMiningFeeContainer />
<BitcoinMiningFeeContainer onFeeRatesLoaded={onFeeRatesLoaded} />
</FormProvider>
)
}

0 comments on commit 98e8777

Please sign in to comment.