@@ -5,62 +5,85 @@ import {
55 useQueryClient ,
66} from "@tanstack/react-query" ;
77import { 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" ;
914import { distribute , distributeByToken } from "thirdweb/extensions/split" ;
15+ import { getOwnedTokens } from "thirdweb/insight" ;
1016import { useActiveAccount } from "thirdweb/react" ;
1117import invariant from "tiny-invariant" ;
12- import { getTokenBalancesFromMoralis } from "@/actions/getBalancesFromMoralis " ;
18+ import { parseError } from "../utils/errorParser " ;
1319
14- function getTokenBalancesQuery ( contract : ThirdwebContract ) {
20+ function getTokenBalancesQuery ( params : {
21+ ownerAddress : string ;
22+ client : ThirdwebClient ;
23+ chain : Chain ;
24+ } ) {
1525 return queryOptions ( {
1626 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+ } ,
2034 } ) ;
21-
22- if ( ! res . data ) {
23- throw new Error ( res . error ) ;
24- }
25- return res . data ;
2635 } ,
27- queryKey : [ "split-balances " , contract . chain . id , contract . address ] ,
36+ queryKey : [ "getOwnedTokens " , params . chain . id , params . ownerAddress ] ,
2837 retry : false ,
2938 } ) ;
3039}
3140
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 ) ) ;
3447}
3548
3649export function useSplitDistributeFunds ( contract : ThirdwebContract ) {
3750 const account = useActiveAccount ( ) ;
3851 const queryClient = useQueryClient ( ) ;
3952
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+
4059 return useMutation ( {
4160 mutationFn : async ( ) => {
4261 invariant ( account , "No active account" ) ;
62+
4363 const balances =
4464 // 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 ) ) ) ;
4767
4868 const distributions = balances
49- . filter ( ( token ) => token . display_balance !== "0.0" )
69+ . filter ( ( token ) => token . value !== 0n )
5070 . map ( async ( currency ) => {
5171 const transaction =
5272 currency . name === "Native Token"
5373 ? distribute ( { contract } )
5474 : distributeByToken ( {
5575 contract,
56- tokenAddress : currency . token_address ,
76+ tokenAddress : currency . tokenAddress ,
5777 } ) ;
5878 const promise = sendAndConfirmTransaction ( {
5979 account,
6080 transaction,
6181 } ) ;
6282 toast . promise ( promise , {
63- error : `Error distributing ${ currency . name } ` ,
83+ error : ( err ) => ( {
84+ message : `Error distributing ${ currency . name } ` ,
85+ description : parseError ( err ) ,
86+ } ) ,
6487 loading : `Distributing ${ currency . name } ` ,
6588 success : `Successfully distributed ${ currency . name } ` ,
6689 } ) ;
@@ -69,7 +92,7 @@ export function useSplitDistributeFunds(contract: ThirdwebContract) {
6992 return await Promise . all ( distributions ) ;
7093 } ,
7194 onSettled : ( ) => {
72- queryClient . invalidateQueries ( getTokenBalancesQuery ( contract ) ) ;
95+ queryClient . invalidateQueries ( getTokenBalancesQuery ( params ) ) ;
7396 } ,
7497 } ) ;
7598}
0 commit comments