Skip to content

Commit

Permalink
fix: bitcoin fees tx size calc
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf authored and pete-watters committed Nov 28, 2023
1 parent cddf09b commit 3f5718c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function determineUtxosForSpend({
sizeInfo = txSizer.calcTxSize({
// Only p2wpkh is supported by the wallet
input_script: 'p2wpkh',
input_count: neededUtxos.length + 1,
input_count: neededUtxos.length,
// From the address of the recipient, we infer the output type
[addressInfo.type + '_output_count']: 2,
});
Expand Down
4 changes: 2 additions & 2 deletions src/app/query/bitcoin/bitcoin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ interface FeeResult {
class FeeEstimatesApi {
constructor(public configuration: Configuration) {}

async getFeeEstimatesFromBlockcypherApi(): Promise<FeeResult> {
async getFeeEstimatesFromBlockcypherApi(network: string): Promise<FeeResult> {
return fetchData({
errorMsg: 'No fee estimates fetched',
url: `https://api.blockcypher.com/v1/btc/main`,
url: `https://api.blockcypher.com/v1/btc/${network}`,
}).then((resp: FeeEstimateEarnApiResponse) => {
const { low_fee_per_kb, medium_fee_per_kb, high_fee_per_kb } = resp;
// These fees are in satoshis per kb
Expand Down
22 changes: 15 additions & 7 deletions src/app/query/bitcoin/fees/fee-estimates.query.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { useQuery } from '@tanstack/react-query';

import { BitcoinNetworkModes } from '@shared/constants';

import { AppUseQueryConfig } from '@app/query/query-config';
import { useBitcoinClient } from '@app/store/common/api-clients.hooks';
import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

import { BitcoinClient } from '../bitcoin-client';

function fetchAllBitcoinFeeEstimates(client: BitcoinClient) {
function fetchAllBitcoinFeeEstimates(client: BitcoinClient, network: BitcoinNetworkModes) {
return async () =>
Promise.allSettled([
client.feeEstimatesApi.getFeeEstimatesFromMempoolSpaceApi(),
client.feeEstimatesApi.getFeeEstimatesFromBlockcypherApi(),
]);
network === 'mainnet'
? Promise.allSettled([
client.feeEstimatesApi.getFeeEstimatesFromMempoolSpaceApi(),
client.feeEstimatesApi.getFeeEstimatesFromBlockcypherApi('main'),
])
: // Using `allSettled` so we can add more testnet apis to the array
Promise.allSettled([client.feeEstimatesApi.getFeeEstimatesFromBlockcypherApi('test3')]);
}

type FetchAllBitcoinFeeEstimatesResp = Awaited<
Expand All @@ -21,9 +27,11 @@ export function useGetAllBitcoinFeeEstimatesQuery<
T extends unknown = FetchAllBitcoinFeeEstimatesResp,
>(options?: AppUseQueryConfig<FetchAllBitcoinFeeEstimatesResp, T>) {
const client = useBitcoinClient();
const network = useCurrentNetwork();

return useQuery({
queryKey: ['average-bitcoin-fee-estimates'],
queryFn: fetchAllBitcoinFeeEstimates(client),
queryKey: ['average-bitcoin-fee-estimates', network.chain.bitcoin.bitcoinNetwork],
queryFn: fetchAllBitcoinFeeEstimates(client, network.chain.bitcoin.bitcoinNetwork),
refetchInterval: 2000 * 60,
...options,
});
Expand Down

0 comments on commit 3f5718c

Please sign in to comment.