dont fetch votrecords if not needed

This commit is contained in:
Maximilian Schneider 2021-08-24 14:16:01 +02:00
parent 55c3598088
commit 8b2be1f655
3 changed files with 16 additions and 58 deletions

View File

@ -16,14 +16,9 @@ export default function useRealm(symbol: string) {
const connected = useWalletStore((s) => s.connected) const connected = useWalletStore((s) => s.connected)
const wallet = useWalletStore((s) => s.current) const wallet = useWalletStore((s) => s.current)
const tokenAccounts = useWalletStore((s) => s.tokenAccounts) const tokenAccounts = useWalletStore((s) => s.tokenAccounts)
const { const { realm, mint, governances, proposals, tokenRecords } = useWalletStore(
realm, (s) => s.selectedRealm
mint, )
governances,
proposals,
tokenRecords,
votes,
} = useWalletStore((s) => s.selectedRealm)
const realmInfo = useMemo(() => REALMS.find((r) => r.symbol === symbol), [ const realmInfo = useMemo(() => REALMS.find((r) => r.symbol === symbol), [
symbol, symbol,
@ -60,7 +55,6 @@ export default function useRealm(symbol: string) {
governances, governances,
proposals, proposals,
tokenRecords, tokenRecords,
votes,
realmTokenAccount, realmTokenAccount,
ownTokenRecord, ownTokenRecord,
} }

View File

@ -3,6 +3,7 @@ import Link from 'next/link'
import useWalletStore from '../../stores/useWalletStore' import useWalletStore from '../../stores/useWalletStore'
import moment from 'moment' import moment from 'moment'
import useRealm from '../../hooks/useRealm' import useRealm from '../../hooks/useRealm'
import BN from 'bn.js'
export const ProposalStateLabels = { export const ProposalStateLabels = {
0: 'Draft', 0: 'Draft',
@ -22,13 +23,20 @@ const DAO = () => {
const wallet = useWalletStore((s) => s.current) const wallet = useWalletStore((s) => s.current)
const { const {
mint,
governances, governances,
proposals, proposals,
votes,
realmTokenAccount, realmTokenAccount,
ownTokenRecord, ownTokenRecord,
} = useRealm(symbol as string) } = 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 // DEBUG print remove
console.log( console.log(
'governance page tokenAccount', 'governance page tokenAccount',
@ -89,7 +97,10 @@ const DAO = () => {
</p> </p>
)} )}
<p>{ProposalStateLabels[v.info.state]}</p> <p>{ProposalStateLabels[v.info.state]}</p>
<p>Votes {JSON.stringify(votes[k])}</p> <p>
Votes Yes: {formatVoteCount(v.info.yesVotesCount)} No:{' '}
{formatVoteCount(v.info.noVotesCount)}
</p>
<p> <p>
{`Yes Threshold: ${ {`Yes Threshold: ${
governances[v.info.governance.toBase58()]?.info.config governances[v.info.governance.toBase58()]?.info.config

View File

@ -16,11 +16,9 @@ import {
Proposal, Proposal,
Realm, Realm,
TokenOwnerRecord, TokenOwnerRecord,
VoteRecord,
} from '../models/accounts' } from '../models/accounts'
import { DEFAULT_PROVIDER } from '../utils/wallet-adapters' import { DEFAULT_PROVIDER } from '../utils/wallet-adapters'
import { ParsedAccount } from '../models/serialisation' import { ParsedAccount } from '../models/serialisation'
import { BN } from '@project-serum/anchor'
export const ENDPOINTS: EndpointInfo[] = [ export const ENDPOINTS: EndpointInfo[] = [
{ {
@ -52,7 +50,6 @@ interface WalletStore extends State {
governances: { [governance: string]: ParsedAccount<Governance> } governances: { [governance: string]: ParsedAccount<Governance> }
proposals: { [proposal: string]: ParsedAccount<Proposal> } proposals: { [proposal: string]: ParsedAccount<Proposal> }
tokenRecords: { [owner: string]: ParsedAccount<TokenOwnerRecord> } tokenRecords: { [owner: string]: ParsedAccount<TokenOwnerRecord> }
votes: { [proposal: string]: { yes: number; no: number } }
} }
selectedProposal: any selectedProposal: any
providerUrl: string providerUrl: string
@ -90,7 +87,6 @@ const useWalletStore = create<WalletStore>((set, get) => ({
governances: {}, governances: {},
proposals: {}, proposals: {},
tokenRecords: {}, tokenRecords: {},
votes: {},
}, },
selectedProposal: {}, selectedProposal: {},
providerUrl: DEFAULT_PROVIDER.url, providerUrl: DEFAULT_PROVIDER.url,
@ -218,49 +214,6 @@ const useWalletStore = create<WalletStore>((set, get) => ({
set((s) => { set((s) => {
s.selectedRealm.proposals = proposals s.selectedRealm.proposals = proposals
}) })
const votesByProposal = await Promise.all(
mapKeys(proposals, async (p) => {
const voteRecords = await getGovernanceAccounts<VoteRecord>(
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)
})
}, },
}, },
})) }))