From 74652702043e36a6378507c94a4f21dbc124506a Mon Sep 17 00:00:00 2001 From: Utkarsh <83659045+0xShuk@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:57:54 +0530 Subject: [PATCH] add query for plugin vote calculation --- .../hooks/useCalculatedVoterWeights.ts | 25 +++++++++---------- VoterWeightPlugins/useVoterWeightPlugins.ts | 5 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts b/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts index 3fa2b145d..6b0389ca9 100644 --- a/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts +++ b/VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts @@ -5,8 +5,8 @@ import { VoterWeightPluginInfo, } from '../lib/types' import { calculateVoterWeight } from '../lib/calculateVoterWeights' -import { useAsync, UseAsyncReturn } from 'react-async-hook' import {PublicKey} from "@solana/web3.js"; +import { useQuery } from '@tanstack/react-query'; type Args = UseVoterWeightPluginsArgs & { realmPublicKey?: PublicKey @@ -20,9 +20,14 @@ const argsAreSet = (args: Args): args is Required => args.realmPublicKey !== undefined && args.governanceMintPublicKey !== undefined && args.walletPublicKeys !== undefined && args.plugins !== undefined && args.tokenOwnerRecords !== undefined -export const useCalculatedVoterWeights = (args: Args) : UseAsyncReturn => - useAsync( - async () => { +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 () => { if (!argsAreSet(args)) return undefined; const voterWeights = args.walletPublicKeys?.map(wallet => { @@ -34,12 +39,6 @@ export const useCalculatedVoterWeights = (args: Args) : UseAsyncReturn pubkey.toString()).join(","), - args.tokenOwnerRecords?.map(tor => tor.account.governingTokenDepositAmount).join(","), - args.plugins?.length - ] - ) \ No newline at end of file + } + }) +} \ No newline at end of file diff --git a/VoterWeightPlugins/useVoterWeightPlugins.ts b/VoterWeightPlugins/useVoterWeightPlugins.ts index 98c145e34..382e080a2 100644 --- a/VoterWeightPlugins/useVoterWeightPlugins.ts +++ b/VoterWeightPlugins/useVoterWeightPlugins.ts @@ -68,11 +68,12 @@ export const useVoterWeightPlugins = ( const walletTokenOwnerRecord = useTokenOwnerRecord(args.governanceMintPublicKey); const delegatedTokenOwnerRecords = useTokenOwnerRecordsDelegatedToUser().data; const { data: plugins } = usePlugins(args) - const { result: calculatedVoterWeights} = useCalculatedVoterWeights({ + const calculatedVoterWeights = useCalculatedVoterWeights({ ...args, plugins: plugins?.voterWeight, tokenOwnerRecords: [walletTokenOwnerRecord, ...(delegatedTokenOwnerRecords || [])].filter(Boolean) as ProgramAccount[], - }) + }).data + const { result: calculatedMaxVoterWeight} = useCalculatedMaxVoterWeight({ ...args, plugins: plugins?.maxVoterWeight,