Skip to content

Commit

Permalink
chore: calculate balance for taproot address
Browse files Browse the repository at this point in the history
  • Loading branch information
slavastartsev committed Nov 14, 2024
1 parent 6c17b1e commit 0b663cd
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions packages/sats-wagmi/src/hooks/useBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';

import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { EsploraClient } from '@gobob/bob-sdk';
import { EsploraClient, OrdinalsClient } from '@gobob/bob-sdk';
import { AddressType, getAddressInfo } from 'bitcoin-address-validation';

import { useSatsWagmi } from '../provider';
import { INTERVAL } from '../utils';
Expand Down Expand Up @@ -29,10 +30,31 @@ const useBalance = (props: UseBalanceProps = {}) => {
}

const esploraClient = new EsploraClient(network);

const { confirmed, unconfirmed, total } = await esploraClient.getBalance(address);

return { confirmed: BigInt(confirmed), unconfirmed: BigInt(unconfirmed), total: BigInt(total) };
const ordinalsClient = new OrdinalsClient(network);

const addressInfo = getAddressInfo(address);

if (addressInfo.type === AddressType.p2tr) {
// cardinal = return UTXOs not containing inscriptions or runes
const outputsFromAddress = await ordinalsClient.getOutputsFromAddress(address, 'cardinal');
const taprootBalance = outputsFromAddress.reduce((acc, cur) => {
return acc + cur.value;
}, 0);

return {
confirmed: BigInt(taprootBalance),
unconfirmed: BigInt(0),
total: BigInt(taprootBalance)
};
} else {
const { confirmed, unconfirmed, total } = await esploraClient.getBalance(address);

return {
confirmed: BigInt(confirmed),
unconfirmed: BigInt(unconfirmed),
total: BigInt(total)
};
}
},
...props
});
Expand Down

0 comments on commit 0b663cd

Please sign in to comment.