Skip to content

Commit 741271a

Browse files
authored
fix(ui-ux): use fetch unspents (#85)
1 parent 2000e3d commit 741271a

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

packages/ord-connect/src/hooks/useBalance.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { useState } from "react";
2-
import { addressNameToType, ordit } from "@sadoprotocol/ordit-sdk";
2+
import {
3+
addressNameToType,
4+
OrditApi,
5+
getAddressesFromPublicKey,
6+
} from "@sadoprotocol/ordit-sdk";
37
import { useOrdContext, Wallet } from "../providers/OrdContext.tsx";
48

59
export function useBalance(): [() => Promise<number>, string | null, boolean] {
@@ -14,17 +18,19 @@ export function useBalance(): [() => Promise<number>, string | null, boolean] {
1418
if (!format || !publicKey) {
1519
throw new Error("No wallet is connected");
1620
}
17-
const walletWithBalances = await ordit.wallet.getWalletWithBalances({
18-
pubKey: publicKey.payments,
21+
const { address } = getAddressesFromPublicKey(
22+
publicKey.payments,
1923
network,
20-
format: addressNameToType[format.payments],
24+
addressNameToType[format.payments],
25+
)[0];
26+
const { spendableUTXOs } = await OrditApi.fetchUnspentUTXOs({
27+
address,
28+
network,
29+
sort: "desc",
30+
type: "spendable",
2131
});
2232

23-
const currentWallet = walletWithBalances.addresses.find(
24-
(w) => w.format === format.payments,
25-
);
26-
27-
const totalCardinalsAvailable = (currentWallet as any).unspents.reduce(
33+
const totalCardinalsAvailable = spendableUTXOs.reduce(
2834
(total: number, spendable: { safeToSpend: boolean; sats: number }) =>
2935
spendable.safeToSpend ? total + spendable.sats : total,
3036
0,

0 commit comments

Comments
 (0)