dont fetch votrecords if not needed
This commit is contained in:
parent
55c3598088
commit
8b2be1f655
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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 = () => {
|
|||
</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>
|
||||
{`Yes Threshold: ${
|
||||
governances[v.info.governance.toBase58()]?.info.config
|
||||
|
|
|
@ -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<Governance> }
|
||||
proposals: { [proposal: string]: ParsedAccount<Proposal> }
|
||||
tokenRecords: { [owner: string]: ParsedAccount<TokenOwnerRecord> }
|
||||
votes: { [proposal: string]: { yes: number; no: number } }
|
||||
}
|
||||
selectedProposal: any
|
||||
providerUrl: string
|
||||
|
@ -90,7 +87,6 @@ const useWalletStore = create<WalletStore>((set, get) => ({
|
|||
governances: {},
|
||||
proposals: {},
|
||||
tokenRecords: {},
|
||||
votes: {},
|
||||
},
|
||||
selectedProposal: {},
|
||||
providerUrl: DEFAULT_PROVIDER.url,
|
||||
|
@ -218,49 +214,6 @@ const useWalletStore = create<WalletStore>((set, get) => ({
|
|||
set((s) => {
|
||||
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)
|
||||
})
|
||||
},
|
||||
},
|
||||
}))
|
||||
|
|
Loading…
Reference in New Issue