Skip to content

Commit

Permalink
add query for plugin vote calculation (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShuk authored Nov 8, 2024
1 parent 0d59d86 commit 481d8af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
25 changes: 12 additions & 13 deletions VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,9 +20,14 @@ const argsAreSet = (args: Args): args is Required<Args> =>
args.realmPublicKey !== undefined && args.governanceMintPublicKey !== undefined && args.walletPublicKeys !== undefined &&
args.plugins !== undefined && args.tokenOwnerRecords !== undefined

export const useCalculatedVoterWeights = (args: Args) : UseAsyncReturn<CalculatedWeight[] | undefined> =>
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 => {
Expand All @@ -34,12 +39,6 @@ export const useCalculatedVoterWeights = (args: Args) : UseAsyncReturn<Calculate
});
});
return Promise.all(voterWeights);
},
[
args.realmPublicKey?.toString(),
args.governanceMintPublicKey?.toString(),
args.walletPublicKeys?.map(pubkey => pubkey.toString()).join(","),
args.tokenOwnerRecords?.map(tor => tor.account.governingTokenDepositAmount).join(","),
args.plugins?.length
]
)
}
})
}
5 changes: 3 additions & 2 deletions VoterWeightPlugins/useVoterWeightPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenOwnerRecord>[],
})
}).data

const { result: calculatedMaxVoterWeight} = useCalculatedMaxVoterWeight({
...args,
plugins: plugins?.maxVoterWeight,
Expand Down

0 comments on commit 481d8af

Please sign in to comment.