Skip to content

Commit cb4ce10

Browse files
authored
feature/BEV-2131 Will now select medium fee rate or blockbook recommended fee when sending bitcoin. (#879)
1 parent 166f2c8 commit cb4ce10

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

src/ux/requestsModal/BitcoinMiningFeeContainer.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@ import { selectBitcoin } from 'store/slices/settingsSlice'
66
import { Typography } from 'src/components'
77
import { sharedStyles } from 'shared/styles'
88

9-
export const BitcoinMiningFeeContainer = () => {
9+
export interface FeeRecord {
10+
feeName: string
11+
feeRate: string
12+
}
13+
14+
interface BitcoinMiningFeeContainerProps {
15+
onFeeRatesLoaded?: (rates: FeeRecord[]) => void
16+
}
17+
18+
export const BitcoinMiningFeeContainer = ({
19+
onFeeRatesLoaded,
20+
}: BitcoinMiningFeeContainerProps) => {
1021
const bitcoinCore = useAppSelector(selectBitcoin)
11-
const [suggestedBitcoinFees, setSuggestedBitcoinFees] = useState<
12-
{ feeName: string; feeRate: string }[]
13-
>([])
22+
const [suggestedBitcoinFees, setSuggestedBitcoinFees] = useState<FeeRecord[]>(
23+
[],
24+
)
1425

1526
useEffect(() => {
1627
// Fetch the bitcoin fees
1728
bitcoinCore?.networksArr[0].bips[0].fetcher
1829
.fetchBitcoinMiningFeeRates('cypher')
1930
.then(cypherResponse => {
20-
setSuggestedBitcoinFees([
31+
const suggestedBitcoinFeesData = [
2132
{
2233
feeName: 'low',
2334
feeRate: cypherResponse.low_fee_per_kb.toString(),
@@ -30,24 +41,29 @@ export const BitcoinMiningFeeContainer = () => {
3041
feeName: 'high',
3142
feeRate: cypherResponse.high_fee_per_kb.toString(),
3243
},
33-
])
44+
]
45+
setSuggestedBitcoinFees(suggestedBitcoinFeesData)
46+
onFeeRatesLoaded?.(suggestedBitcoinFeesData)
3447
})
3548
.catch(() => {
3649
// Fetch from blockbook if cypher fails
3750

3851
bitcoinCore?.networksArr[0].bips[0].fetcher
3952
.fetchBitcoinMiningFeeRates('blockbook')
4053
.then(blockbookResponse => {
41-
setSuggestedBitcoinFees([
54+
const suggestedBitcoinFeesData = [
4255
{
4356
feeName: 'low',
4457
feeRate: convertBtcToSatoshi(
4558
blockbookResponse.result,
4659
).toString(),
4760
},
48-
])
61+
]
62+
setSuggestedBitcoinFees(suggestedBitcoinFeesData)
63+
onFeeRatesLoaded?.(suggestedBitcoinFeesData)
4964
})
5065
})
66+
// eslint-disable-next-line react-hooks/exhaustive-deps
5167
}, [bitcoinCore])
5268

5369
return (

src/ux/requestsModal/ReviewBitcoinTransactionContainer.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { useContext, useMemo, useState } from 'react'
1+
import { useContext, useMemo, useState, useCallback } from 'react'
22
import {
33
convertSatoshiToBtcHuman,
44
SendBitcoinRequest,
55
} from '@rsksmart/rif-wallet-bitcoin'
66
import { useTranslation } from 'react-i18next'
77
import { StyleSheet, View } from 'react-native'
8-
import { useCallback } from 'react'
98
import { useSafeAreaInsets } from 'react-native-safe-area-context'
109
import { FormProvider, useForm } from 'react-hook-form'
1110

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

25-
import { BitcoinMiningFeeContainer } from './BitcoinMiningFeeContainer'
24+
import {
25+
BitcoinMiningFeeContainer,
26+
FeeRecord,
27+
} from './BitcoinMiningFeeContainer'
2628

2729
interface ReviewBitcoinTransactionContainerProps {
2830
request: SendBitcoinRequest
@@ -179,7 +181,32 @@ const MiningFeeInput = ({
179181
},
180182
})
181183

182-
const handleFeeChange = (text: string) => onChange(text)
184+
const { setValue } = methods
185+
186+
const handleFeeChange = useCallback(
187+
(text: string) => onChange(text),
188+
[onChange],
189+
)
190+
191+
const onFeeRatesLoaded = useCallback(
192+
(feeRates: FeeRecord[]) => {
193+
let newFee
194+
// If length > 1 then it's fetching the fee from cypher
195+
if (feeRates.length > 1) {
196+
newFee = feeRates[1].feeRate
197+
}
198+
// If length === 1 then it's fetching the fee from blockbook
199+
if (feeRates.length === 1) {
200+
newFee = feeRates[0].feeRate
201+
}
202+
203+
if (newFee) {
204+
handleFeeChange(newFee)
205+
setValue('miningFee', Number(newFee))
206+
}
207+
},
208+
[handleFeeChange, setValue],
209+
)
183210

184211
return (
185212
<FormProvider {...methods}>
@@ -188,7 +215,7 @@ const MiningFeeInput = ({
188215
inputName="miningFee"
189216
onChangeText={handleFeeChange}
190217
/>
191-
<BitcoinMiningFeeContainer />
218+
<BitcoinMiningFeeContainer onFeeRatesLoaded={onFeeRatesLoaded} />
192219
</FormProvider>
193220
)
194221
}

0 commit comments

Comments
 (0)