Skip to content

Commit

Permalink
fix non-refreshing voter weight (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xShuk authored Nov 18, 2024
1 parent a57c74d commit 2c42e76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions VoterWeightPlugins/hooks/useCalculatedVoterWeights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,14 +20,9 @@ const argsAreSet = (args: Args): args is Required<Args> =>
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<CalculatedWeight[] | undefined> =>
useAsync(
async () => {
if (!argsAreSet(args)) return undefined;

const voterWeights = args.walletPublicKeys?.map(wallet => {
Expand All @@ -39,6 +34,12 @@ export function useCalculatedVoterWeights(args: Args) {
});
});
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
]
)
4 changes: 2 additions & 2 deletions VoterWeightPlugins/useVoterWeightPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenOwnerRecord>[],
}).data
})

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

0 comments on commit 2c42e76

Please sign in to comment.