1
1
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" ;
3
7
import { useOrdContext , Wallet } from "../providers/OrdContext.tsx" ;
4
8
5
9
export function useBalance ( ) : [ ( ) => Promise < number > , string | null , boolean ] {
@@ -14,17 +18,19 @@ export function useBalance(): [() => Promise<number>, string | null, boolean] {
14
18
if ( ! format || ! publicKey ) {
15
19
throw new Error ( "No wallet is connected" ) ;
16
20
}
17
- const walletWithBalances = await ordit . wallet . getWalletWithBalances ( {
18
- pubKey : publicKey . payments ,
21
+ const { address } = getAddressesFromPublicKey (
22
+ publicKey . payments ,
19
23
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" ,
21
31
} ) ;
22
32
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 (
28
34
( total : number , spendable : { safeToSpend : boolean ; sats : number } ) =>
29
35
spendable . safeToSpend ? total + spendable . sats : total ,
30
36
0 ,
0 commit comments