diff --git a/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts b/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts index 6b0389ca..577049f8 100644 --- a/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts +++ b/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts @@ -6,7 +6,7 @@ import { } from '../lib/types' import { calculateVoterWeight } from '../lib/calculateVoterWeights' import {PublicKey} from "@solana/web3.js"; -import { useQuery } from '@tanstack/react-query'; +import { useAsync, UseAsyncReturn } from 'react-async-hook' type Args = UseVoterWeightPluginsArgs & { realmPublicKey?: PublicKey @@ -20,14 +20,9 @@ const argsAreSet = (args: Args): args is Required => args.realmPublicKey !== undefined && args.governanceMintPublicKey !== undefined && args.walletPublicKeys !== undefined && args.plugins !== undefined && args.tokenOwnerRecords !== undefined -export function useCalculatedVoterWeights(args: Args) { - return useQuery({ - queryKey: ['calculate-voter-weight', { - realmPublicKey: args.realmPublicKey, - governanceMintPublicKey: args.governanceMintPublicKey, - walletPublicKeys: args.walletPublicKeys?.map(k => k.toBase58()) - }], - queryFn: async () => { +export const useCalculatedVoterWeights = (args: Args) : UseAsyncReturn => + useAsync( + async () => { if (!argsAreSet(args)) return undefined; const voterWeights = args.walletPublicKeys?.map(wallet => { @@ -39,6 +34,12 @@ export function useCalculatedVoterWeights(args: Args) { }); }); return Promise.all(voterWeights); - } - }) -} \ No newline at end of file + }, + [ + args.realmPublicKey?.toString(), + args.governanceMintPublicKey?.toString(), + args.walletPublicKeys?.map(pubkey => pubkey.toString()).join(","), + args.tokenOwnerRecords?.map(tor => tor.account.governingTokenDepositAmount).join(","), + args.plugins?.length + ] + ) diff --git a/VoterWeightPlugins/useVoterWeightPlugins.ts b/VoterWeightPlugins/useVoterWeightPlugins.ts index 382e080a..afbe64ef 100644 --- a/VoterWeightPlugins/useVoterWeightPlugins.ts +++ b/VoterWeightPlugins/useVoterWeightPlugins.ts @@ -68,11 +68,11 @@ export const useVoterWeightPlugins = ( const walletTokenOwnerRecord = useTokenOwnerRecord(args.governanceMintPublicKey); const delegatedTokenOwnerRecords = useTokenOwnerRecordsDelegatedToUser().data; const { data: plugins } = usePlugins(args) - const calculatedVoterWeights = useCalculatedVoterWeights({ + const { result: calculatedVoterWeights} = useCalculatedVoterWeights({ ...args, plugins: plugins?.voterWeight, tokenOwnerRecords: [walletTokenOwnerRecord, ...(delegatedTokenOwnerRecords || [])].filter(Boolean) as ProgramAccount[], - }).data + }) const { result: calculatedMaxVoterWeight} = useCalculatedMaxVoterWeight({ ...args,