Skip to content

Commit 2e589ee

Browse files
committed
delegator relinquish
1 parent b3d2df6 commit 2e589ee

File tree

6 files changed

+141
-47
lines changed

6 files changed

+141
-47
lines changed

hooks/queries/proposal.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
getAllProposals,
66
getProposal,
77
getProposalsByGovernance,
8+
ProgramAccount,
9+
Proposal,
810
} from '@solana/spl-governance'
911
import { fetchRealmByPubkey, useRealmQuery } from './realm'
1012
import { useRouter } from 'next/router'
@@ -82,18 +84,13 @@ export const useRealmProposalsQuery = () => {
8284
if (!enabled) throw new Error()
8385
console.log('query: fetching realm proposals')
8486

85-
const results = (
86-
await Promise.all(
87-
governances.map((x) =>
88-
// why not just get all proposals for a realm? what was i doing here?
89-
getProposalsByGovernance(connection.current, realm.owner, x.pubkey)
90-
)
91-
)
92-
)
93-
.flat()
94-
// Blacklisted proposals which should not be displayed in the UI
95-
// hidden legacy accounts to declutter UI
96-
.filter((x) => HIDDEN_PROPOSALS.get(x.pubkey.toBase58()) === undefined)
87+
const results: ProgramAccount<Proposal>[] = [];
88+
89+
for (const x of governances) {
90+
// Get proposals synchronously
91+
const proposals = await getProposalsByGovernance(connection.current, realm.owner, x.pubkey);
92+
results.push(...proposals);
93+
}
9794

9895
// TODO instead of using setQueryData, prefetch queries on mouseover ?
9996
results.forEach((x) => {

pages/dao/[symbol]/proposal/components/MyProposalsBtn.tsx

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ import {
5050
} from '@hooks/queries/proposal'
5151
import queryClient from '@hooks/queries/queryClient'
5252
import { getFeeEstimate } from '@tools/feeEstimate'
53-
import { createComputeBudgetIx } from '@blockworks-foundation/mango-v4'
54-
import {useVotingClients} from "@hooks/useVotingClients";
55-
import {useNftClient} from "../../../../../VoterWeightPlugins/useNftClient";
53+
import { useVotingClients } from '@hooks/useVotingClients'
54+
import { useNftClient } from '../../../../../VoterWeightPlugins/useNftClient'
55+
import { useSelectedDelegatorStore } from 'stores/useSelectedDelegatorStore'
5656

5757
const MyProposalsBn = () => {
5858
const [modalIsOpen, setModalIsOpen] = useState(false)
@@ -61,10 +61,16 @@ const MyProposalsBn = () => {
6161
const [isLoading, setIsLoading] = useState(false)
6262
const { governancesArray } = useGovernanceAssets()
6363
const { connection } = useConnection()
64-
const myVoteRecords = useVoteRecordsByOwnerQuery(
65-
wallet?.publicKey ?? undefined
66-
).data
6764

65+
const { communityDelegator } = useSelectedDelegatorStore()
66+
67+
const user = communityDelegator ?? wallet?.publicKey ?? undefined
68+
69+
const myVoteRecords = useVoteRecordsByOwnerQuery(user).data
70+
71+
console.log('myVoteRecords', myVoteRecords)
72+
73+
const realm = useRealmQuery().data?.result
6874
const ownVoteRecordsByProposal = useMemo(() => {
6975
return myVoteRecords !== undefined
7076
? (Object.fromEntries(
@@ -79,14 +85,13 @@ const MyProposalsBn = () => {
7985
const { data: tokenOwnerRecord } = useAddressQuery_CommunityTokenOwner()
8086

8187
const maxVoterWeight = useMaxVoteRecord()?.pubkey || undefined
82-
const realm = useRealmQuery().data?.result
8388
const programId = realm?.owner
8489

8590
const programVersion =
8691
useProgramVersion() ?? DEFAULT_GOVERNANCE_PROGRAM_VERSION
8792

88-
const votingClients = useVotingClients();
89-
const { nftClient } = useNftClient();
93+
const votingClients = useVotingClients()
94+
const { nftClient } = useNftClient()
9095

9196
const [
9297
proposalsWithDepositedTokens,
@@ -97,6 +102,7 @@ const MyProposalsBn = () => {
97102

98103
const { realmInfo, isNftMode } = useRealm()
99104
const { data: proposals } = useRealmProposalsQuery()
105+
100106
const myProposals = useMemo(
101107
() =>
102108
connected
@@ -115,9 +121,11 @@ const MyProposalsBn = () => {
115121
ownCouncilTokenRecord?.pubkey,
116122
]
117123
)
124+
118125
const drafts = myProposals?.filter((x) => {
119126
return x.account.state === ProposalState.Draft
120127
})
128+
121129
const notfinalized = myProposals?.filter((x) => {
122130
const governance = governancesArray?.find(
123131
(gov) => gov.pubkey.toBase58() === x.account.governance.toBase58()
@@ -138,6 +146,7 @@ const MyProposalsBn = () => {
138146
now > timestamp
139147
)
140148
})
149+
141150
const unReleased = proposals?.filter(
142151
(x) =>
143152
(x.account.state === ProposalState.Completed ||
@@ -183,14 +192,16 @@ const MyProposalsBn = () => {
183192
recentBlockhash,
184193
feePayer: wallet.publicKey!,
185194
})
186-
transaction.add(...[createComputeBudgetIx(fee), ...chunk])
187195
transaction.recentBlockhash = recentBlockhash
188196
transaction.setSigners(
189197
// fee payed by the wallet owner
190198
wallet.publicKey!
191199
)
192200
transactions.push(transaction)
193201
}
202+
203+
console.log('transactions', transactions)
204+
194205
const signedTXs = await wallet.signAllTransactions(transactions)
195206
await Promise.all(
196207
signedTXs.map((transaction) =>
@@ -242,10 +253,11 @@ const MyProposalsBn = () => {
242253
realm?.account.communityMint.toBase58()
243254
? ownTokenRecord
244255
: ownCouncilTokenRecord
245-
const role = proposal.account.governingTokenMint.toBase58() ===
256+
const role =
257+
proposal.account.governingTokenMint.toBase58() ===
246258
realm?.account.communityMint.toBase58()
247-
? 'community'
248-
: 'council'
259+
? 'community'
260+
: 'council'
249261
const governanceAuthority = wallet!.publicKey!
250262
const beneficiary = wallet!.publicKey!
251263

@@ -254,27 +266,29 @@ const MyProposalsBn = () => {
254266
proposal.pubkey,
255267
voterTokenRecord!.pubkey
256268
)
257-
269+
258270
let governingTokenMint = proposal.account.governingTokenMint
259271

260272
try {
261273
await getVoteRecord(connection, voteRecordPk)
262274
} catch {
263-
voterTokenRecord = role === "community" ?
264-
ownCouncilTokenRecord :
265-
ownTokenRecord
275+
voterTokenRecord =
276+
role === 'community' ? ownCouncilTokenRecord : ownTokenRecord
266277

267278
voteRecordPk = await getVoteRecordAddress(
268279
realm!.owner,
269280
proposal.pubkey,
270281
voterTokenRecord!.pubkey
271282
)
272283

273-
governingTokenMint = role === "community" && realm?.account.config.councilMint ?
274-
realm.account.config.councilMint :
275-
realm?.account.communityMint!
284+
governingTokenMint =
285+
role === 'community' && realm?.account.config.councilMint
286+
? realm.account.config.councilMint
287+
: realm?.account.communityMint!
276288
}
277-
289+
290+
console.log('THE INSTRUCTIONS', instructions)
291+
278292
const inst = await withRelinquishVote(
279293
instructions,
280294
realm!.owner,
@@ -333,8 +347,15 @@ const MyProposalsBn = () => {
333347

334348
setIsLoading(true)
335349
const instructions: TransactionInstruction[] = []
336-
const { registrar } = nftClient.getRegistrarPDA(realm.pubkey, realm.account.communityMint);
337-
const { voterWeightPk } = await nftClient.getVoterWeightRecordPDA(realm.pubkey, realm.account.communityMint, wallet.publicKey);
350+
const { registrar } = nftClient.getRegistrarPDA(
351+
realm.pubkey,
352+
realm.account.communityMint
353+
)
354+
const { voterWeightPk } = await nftClient.getVoterWeightRecordPDA(
355+
realm.pubkey,
356+
realm.account.communityMint,
357+
wallet.publicKey
358+
)
338359

339360
const nfts = ownNftVoteRecordsFilterd.slice(
340361
0,
@@ -387,7 +408,7 @@ const MyProposalsBn = () => {
387408
}
388409

389410
const getNftsVoteRecord = useCallback(async () => {
390-
if (!nftClient) throw new Error('no nft client');
411+
if (!nftClient) throw new Error('no nft client')
391412
const nftVoteRecords = await nftClient.program.account.nftVoteRecord.all([
392413
{
393414
memcmp: {
@@ -473,13 +494,7 @@ const MyProposalsBn = () => {
473494
if (wallet?.publicKey && isNftMode && nftClient && modalIsOpen) {
474495
getNftsVoteRecord()
475496
}
476-
}, [
477-
nftClient,
478-
getNftsVoteRecord,
479-
isNftMode,
480-
modalIsOpen,
481-
wallet?.publicKey,
482-
])
497+
}, [nftClient, getNftsVoteRecord, isNftMode, modalIsOpen, wallet?.publicKey])
483498

484499
return (
485500
<>

stores/useGovernanceAssetsStore.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,16 @@ const useGovernanceAssetsStore = create<GovernanceAssetsStore>((set, _get) => ({
157157
if (additionalMintAccounts) {
158158
possibleMintAccountPks.push(...additionalMintAccounts)
159159
}
160+
console.log('LOAD 1')
161+
160162
// 1 - Load token accounts behind any type of governance
161163
const governedTokenAccounts = await loadGovernedTokenAccounts(
162164
connection,
163165
realm,
164166
governancesWithNativeTreasuryAddress
165167
)
168+
console.log('LOAD 2')
169+
166170
// 2 - Call to fetch token prices for every token account's mints
167171
await tokenPriceService.fetchTokenPrices(
168172
governedTokenAccounts.reduce((mints, governedTokenAccount) => {
@@ -177,6 +181,8 @@ const useGovernanceAssetsStore = create<GovernanceAssetsStore>((set, _get) => ({
177181
}, [] as string[])
178182
)
179183
accounts.push(...governedTokenAccounts)
184+
185+
console.log('LOAD 3')
180186
const stakeAccounts = await loadStakeAccounts(
181187
connection,
182188
governedTokenAccounts.filter(
@@ -198,6 +204,7 @@ const useGovernanceAssetsStore = create<GovernanceAssetsStore>((set, _get) => ({
198204
s.assetAccounts = accounts.filter(filterOutHiddenAccounts)
199205
})
200206

207+
console.log('LOAD 4')
201208
// 3 - Load accounts related to mint
202209
const mintAccounts = await loadMintGovernanceAccounts(
203210
connection,
@@ -210,6 +217,7 @@ const useGovernanceAssetsStore = create<GovernanceAssetsStore>((set, _get) => ({
210217
s.assetAccounts = accounts.filter(filterOutHiddenAccounts)
211218
})
212219

220+
console.log('LOAD 5')
213221
// 4 - Load accounts related to program governances
214222
const programAccounts = await getProgramAssetAccounts(
215223
connection,
@@ -221,6 +229,7 @@ const useGovernanceAssetsStore = create<GovernanceAssetsStore>((set, _get) => ({
221229
s.assetAccounts = accounts.filter(filterOutHiddenAccounts)
222230
})
223231

232+
console.log('LOAD 6')
224233
// 5 - Create generic asset accounts for governance's governedAccounts that have not been handled yet
225234
// We do this so theses accounts may be selected
226235
const genericGovernances = getGenericAssetAccounts(
@@ -690,6 +699,8 @@ const getTokenAccountsInfo = async (
690699
}
691700

692701
return tokenAccountsInfoJson.reduce((tokenAccountsInfo, { result }) => {
702+
if (!result) return tokenAccountsInfo
703+
693704
result.forEach(
694705
({
695706
account: {
@@ -1046,6 +1057,8 @@ const getProgramAccountInfo = async (
10461057
if (executableAccountInfoJson && executableAccountInfoJson.length) {
10471058
const executableDataPks = executableAccountInfoJson.reduce(
10481059
(executableAccountInfo, { result, id }) => {
1060+
if (!result) return executableAccountInfo
1061+
10491062
result.forEach(({ pubkey }) => {
10501063
const executableDataPk = new PublicKey(pubkey)
10511064
executableAccountInfo.push({

utils/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export async function getFilteredProgramAccounts(
7575
encoding: 'base64',
7676
},
7777
])
78+
7879
if (resp.error) {
7980
throw new Error(resp.error.message)
8081
}

utils/tokens.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export const getTokenAccountsByMint = async (
6868
},
6969
],
7070
})
71+
7172
return results.map((r) => {
7273
const publicKey = r.pubkey
7374
const data = Buffer.from(r.account.data)

0 commit comments

Comments
 (0)