Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix non-refreshing voter weight #37

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading