@@ -5,62 +5,85 @@ import {
5
5
useQueryClient ,
6
6
} from "@tanstack/react-query" ;
7
7
import { toast } from "sonner" ;
8
- import { sendAndConfirmTransaction , type ThirdwebContract } from "thirdweb" ;
8
+ import {
9
+ type Chain ,
10
+ sendAndConfirmTransaction ,
11
+ type ThirdwebClient ,
12
+ type ThirdwebContract ,
13
+ } from "thirdweb" ;
9
14
import { distribute , distributeByToken } from "thirdweb/extensions/split" ;
15
+ import { getOwnedTokens } from "thirdweb/insight" ;
10
16
import { useActiveAccount } from "thirdweb/react" ;
11
17
import invariant from "tiny-invariant" ;
12
- import { getTokenBalancesFromMoralis } from "@/actions/getBalancesFromMoralis " ;
18
+ import { parseError } from "../utils/errorParser " ;
13
19
14
- function getTokenBalancesQuery ( contract : ThirdwebContract ) {
20
+ function getTokenBalancesQuery ( params : {
21
+ ownerAddress : string ;
22
+ client : ThirdwebClient ;
23
+ chain : Chain ;
24
+ } ) {
15
25
return queryOptions ( {
16
26
queryFn : async ( ) => {
17
- const res = await getTokenBalancesFromMoralis ( {
18
- chainId : contract . chain . id ,
19
- contractAddress : contract . address ,
27
+ return getOwnedTokens ( {
28
+ client : params . client ,
29
+ chains : [ params . chain ] ,
30
+ ownerAddress : params . ownerAddress ,
31
+ queryOptions : {
32
+ include_native : "true" ,
33
+ } ,
20
34
} ) ;
21
-
22
- if ( ! res . data ) {
23
- throw new Error ( res . error ) ;
24
- }
25
- return res . data ;
26
35
} ,
27
- queryKey : [ "split-balances " , contract . chain . id , contract . address ] ,
36
+ queryKey : [ "getOwnedTokens " , params . chain . id , params . ownerAddress ] ,
28
37
retry : false ,
29
38
} ) ;
30
39
}
31
40
32
- export function useSplitBalances ( contract : ThirdwebContract ) {
33
- return useQuery ( getTokenBalancesQuery ( contract ) ) ;
41
+ export function useOwnedTokenBalances ( params : {
42
+ ownerAddress : string ;
43
+ client : ThirdwebClient ;
44
+ chain : Chain ;
45
+ } ) {
46
+ return useQuery ( getTokenBalancesQuery ( params ) ) ;
34
47
}
35
48
36
49
export function useSplitDistributeFunds ( contract : ThirdwebContract ) {
37
50
const account = useActiveAccount ( ) ;
38
51
const queryClient = useQueryClient ( ) ;
39
52
53
+ const params = {
54
+ ownerAddress : contract . address , // because we want to fetch the balance of split contract
55
+ client : contract . client ,
56
+ chain : contract . chain ,
57
+ } ;
58
+
40
59
return useMutation ( {
41
60
mutationFn : async ( ) => {
42
61
invariant ( account , "No active account" ) ;
62
+
43
63
const balances =
44
64
// get the cached data if it exists, otherwise fetch it
45
- queryClient . getQueryData ( getTokenBalancesQuery ( contract ) . queryKey ) ||
46
- ( await queryClient . fetchQuery ( getTokenBalancesQuery ( contract ) ) ) ;
65
+ queryClient . getQueryData ( getTokenBalancesQuery ( params ) . queryKey ) ||
66
+ ( await queryClient . fetchQuery ( getTokenBalancesQuery ( params ) ) ) ;
47
67
48
68
const distributions = balances
49
- . filter ( ( token ) => token . display_balance !== "0.0" )
69
+ . filter ( ( token ) => token . value !== 0n )
50
70
. map ( async ( currency ) => {
51
71
const transaction =
52
72
currency . name === "Native Token"
53
73
? distribute ( { contract } )
54
74
: distributeByToken ( {
55
75
contract,
56
- tokenAddress : currency . token_address ,
76
+ tokenAddress : currency . tokenAddress ,
57
77
} ) ;
58
78
const promise = sendAndConfirmTransaction ( {
59
79
account,
60
80
transaction,
61
81
} ) ;
62
82
toast . promise ( promise , {
63
- error : `Error distributing ${ currency . name } ` ,
83
+ error : ( err ) => ( {
84
+ message : `Error distributing ${ currency . name } ` ,
85
+ description : parseError ( err ) ,
86
+ } ) ,
64
87
loading : `Distributing ${ currency . name } ` ,
65
88
success : `Successfully distributed ${ currency . name } ` ,
66
89
} ) ;
@@ -69,7 +92,7 @@ export function useSplitDistributeFunds(contract: ThirdwebContract) {
69
92
return await Promise . all ( distributions ) ;
70
93
} ,
71
94
onSettled : ( ) => {
72
- queryClient . invalidateQueries ( getTokenBalancesQuery ( contract ) ) ;
95
+ queryClient . invalidateQueries ( getTokenBalancesQuery ( params ) ) ;
73
96
} ,
74
97
} ) ;
75
98
}
0 commit comments