From 8b2be1f655441f10aac3b20e9a530787638e8af7 Mon Sep 17 00:00:00 2001 From: Maximilian Schneider Date: Tue, 24 Aug 2021 14:16:01 +0200 Subject: [PATCH] dont fetch votrecords if not needed --- hooks/useRealm.tsx | 12 +++------- pages/dao/[symbol].tsx | 15 +++++++++++-- stores/useWalletStore.tsx | 47 --------------------------------------- 3 files changed, 16 insertions(+), 58 deletions(-) diff --git a/hooks/useRealm.tsx b/hooks/useRealm.tsx index 62348a8..8570b89 100644 --- a/hooks/useRealm.tsx +++ b/hooks/useRealm.tsx @@ -16,14 +16,9 @@ export default function useRealm(symbol: string) { const connected = useWalletStore((s) => s.connected) const wallet = useWalletStore((s) => s.current) const tokenAccounts = useWalletStore((s) => s.tokenAccounts) - const { - realm, - mint, - governances, - proposals, - tokenRecords, - votes, - } = useWalletStore((s) => s.selectedRealm) + const { realm, mint, governances, proposals, tokenRecords } = useWalletStore( + (s) => s.selectedRealm + ) const realmInfo = useMemo(() => REALMS.find((r) => r.symbol === symbol), [ symbol, @@ -60,7 +55,6 @@ export default function useRealm(symbol: string) { governances, proposals, tokenRecords, - votes, realmTokenAccount, ownTokenRecord, } diff --git a/pages/dao/[symbol].tsx b/pages/dao/[symbol].tsx index 2604cee..40f85fe 100644 --- a/pages/dao/[symbol].tsx +++ b/pages/dao/[symbol].tsx @@ -3,6 +3,7 @@ import Link from 'next/link' import useWalletStore from '../../stores/useWalletStore' import moment from 'moment' import useRealm from '../../hooks/useRealm' +import BN from 'bn.js' export const ProposalStateLabels = { 0: 'Draft', @@ -22,13 +23,20 @@ const DAO = () => { const wallet = useWalletStore((s) => s.current) const { + mint, governances, proposals, - votes, realmTokenAccount, ownTokenRecord, } = useRealm(symbol as string) + const votePrecision = 10000 + const formatVoteCount = (c: BN) => + `${ + c.mul(new BN(votePrecision)).div(mint.supply).toNumber() * + (100 / votePrecision) + }%` + // DEBUG print remove console.log( 'governance page tokenAccount', @@ -89,7 +97,10 @@ const DAO = () => {

)}

{ProposalStateLabels[v.info.state]}

-

Votes {JSON.stringify(votes[k])}

+

+ Votes Yes: {formatVoteCount(v.info.yesVotesCount)} No:{' '} + {formatVoteCount(v.info.noVotesCount)} +

{`Yes Threshold: ${ governances[v.info.governance.toBase58()]?.info.config diff --git a/stores/useWalletStore.tsx b/stores/useWalletStore.tsx index 46fe8c4..a532171 100644 --- a/stores/useWalletStore.tsx +++ b/stores/useWalletStore.tsx @@ -16,11 +16,9 @@ import { Proposal, Realm, TokenOwnerRecord, - VoteRecord, } from '../models/accounts' import { DEFAULT_PROVIDER } from '../utils/wallet-adapters' import { ParsedAccount } from '../models/serialisation' -import { BN } from '@project-serum/anchor' export const ENDPOINTS: EndpointInfo[] = [ { @@ -52,7 +50,6 @@ interface WalletStore extends State { governances: { [governance: string]: ParsedAccount } proposals: { [proposal: string]: ParsedAccount } tokenRecords: { [owner: string]: ParsedAccount } - votes: { [proposal: string]: { yes: number; no: number } } } selectedProposal: any providerUrl: string @@ -90,7 +87,6 @@ const useWalletStore = create((set, get) => ({ governances: {}, proposals: {}, tokenRecords: {}, - votes: {}, }, selectedProposal: {}, providerUrl: DEFAULT_PROVIDER.url, @@ -218,49 +214,6 @@ const useWalletStore = create((set, get) => ({ set((s) => { s.selectedRealm.proposals = proposals }) - - const votesByProposal = await Promise.all( - mapKeys(proposals, async (p) => { - const voteRecords = await getGovernanceAccounts( - programId, - endpoint, - VoteRecord, - getAccountTypes(VoteRecord), - [pubkeyFilter(1, new PublicKey(p))] - ) - - const precision = Math.pow(10, 6) - const yes = - Object.values(voteRecords) - .map((v) => v.info.voteWeight.yes) - .filter((v) => !!v) - .reduce((acc, cur) => acc.add(cur), new BN(0)) - .mul(new BN(precision)) - .div(realmMint.supply) - .toNumber() * - (100 / precision) - - const no = - Object.values(voteRecords) - .map((v) => v.info.voteWeight.no) - .filter((v) => !!v) - .reduce((acc, cur) => acc.add(cur), new BN(0)) - .mul(new BN(precision)) - .div(realmMint.supply) - .toNumber() * - (100 / precision) - - return [p, { yes, no }] - }) - ) - - // TODO: cache votes of finished proposals permanently in local storage to avoid recounting - - console.log('fetchRealm votes', votesByProposal) - - set((s) => { - s.selectedRealm.votes = Object.fromEntries(votesByProposal) - }) }, }, }))