Skip to content

Commit 79bea1e

Browse files
authored
Do not load every realm (#123)
1 parent 969591e commit 79bea1e

1 file changed

Lines changed: 70 additions & 66 deletions

File tree

stores/useWalletStore.tsx

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getGovernanceAccount,
1818
getGovernanceAccounts,
1919
getGovernanceProgramVersion,
20+
getRealm,
2021
Governance,
2122
GovernanceAccountType,
2223
GOVERNANCE_CHAT_PROGRAM_ID,
@@ -47,7 +48,7 @@ import {
4748
} from '@models/api';
4849
import { accountsToPubkeyMap } from '@tools/sdk/accounts';
4950
import { HIDDEN_PROPOSALS } from '@components/instructions/tools';
50-
import { BN } from '@blockworks-foundation/mango-client';
51+
import { BN } from '@project-serum/anchor';
5152

5253
export const EnhancedProposalState = {
5354
...ProposalState,
@@ -66,10 +67,9 @@ export declare type EnhancedProposal = Omit<Proposal, 'state'> & {
6667
interface WalletStore extends State {
6768
connected: boolean;
6869
connection: ConnectionContext;
69-
current: SignerWalletAdapter | undefined;
70+
current?: SignerWalletAdapter;
7071

7172
ownVoteRecordsByProposal: { [proposal: string]: ProgramAccount<VoteRecord> };
72-
realms: { [realm: string]: ProgramAccount<Realm> };
7373
selectedRealm: {
7474
realm?: ProgramAccount<Realm>;
7575
mint?: MintAccount;
@@ -87,11 +87,12 @@ interface WalletStore extends State {
8787
};
8888
mints: { [pubkey: string]: MintAccount };
8989
programVersion: number;
90+
loading: boolean;
9091
};
9192
selectedProposal: {
92-
proposal: ProgramAccount<EnhancedProposal> | undefined;
93-
governance: ProgramAccount<Governance> | undefined;
94-
realm: ProgramAccount<Realm> | undefined;
93+
proposal?: ProgramAccount<EnhancedProposal>;
94+
governance?: ProgramAccount<Governance>;
95+
realm?: ProgramAccount<Realm>;
9596
instructions: {
9697
[instruction: string]: ProgramAccount<ProposalTransaction>;
9798
};
@@ -102,19 +103,15 @@ interface WalletStore extends State {
102103
proposalMint?: MintAccount;
103104
loading: boolean;
104105
tokenType?: GoverningTokenType;
105-
proposalOwner: ProgramAccount<TokenOwnerRecord> | undefined;
106+
proposalOwner?: ProgramAccount<TokenOwnerRecord>;
106107
};
107-
providerUrl: string | undefined;
108+
providerUrl?: string;
108109
tokenAccounts: TokenProgramAccount<TokenAccount>[];
109110
set: (x: any) => void;
110111
actions: any;
111112
}
112113

113-
const INITIAL_REALM_STATE = {
114-
realm: undefined,
115-
mint: undefined,
116-
programId: undefined,
117-
councilMint: undefined,
114+
const INITIAL_REALM_STATE: WalletStore['selectedRealm'] = {
118115
governances: {},
119116
tokenMints: [],
120117
tokenAccounts: [],
@@ -126,21 +123,15 @@ const INITIAL_REALM_STATE = {
126123
programVersion: 1,
127124
};
128125

129-
const INITIAL_PROPOSAL_STATE = {
130-
proposal: undefined,
131-
governance: undefined,
132-
realm: undefined,
126+
const INITIAL_PROPOSAL_STATE: WalletStore['selectedProposal'] = {
133127
instructions: {},
134128
voteRecordsByVoter: {},
135129
signatories: {},
136130
chatMessages: {},
137-
descriptionLink: undefined,
138-
proposalMint: undefined,
139131
loading: true,
140-
proposalOwner: undefined,
141132
};
142133

143-
const TEN_DAYS_IN_MS = 3600 * 24 * 10 * 1000;
134+
const TEN_DAYS_IN_MS = 3_600 * 24 * 10 * 1_000;
144135

145136
function isProposalOutdated(
146137
proposal: ProgramAccount<EnhancedProposal>,
@@ -155,7 +146,7 @@ function isProposalOutdated(
155146

156147
if (
157148
Date.now() -
158-
proposal.account.votingCompletedAt.mul(new BN(1000)).toNumber() <=
149+
proposal.account.votingCompletedAt.mul(new BN(1_000)).toNumber() <=
159150
TEN_DAYS_IN_MS
160151
) {
161152
return false;
@@ -180,55 +171,61 @@ function setOutdatedStateForProposals(
180171
const useWalletStore = create<WalletStore>((set, get) => ({
181172
connected: false,
182173
connection: getConnectionContext('mainnet'),
183-
current: undefined,
184-
realms: {},
185174
ownVoteRecordsByProposal: {},
186175
selectedRealm: INITIAL_REALM_STATE,
187176
selectedProposal: INITIAL_PROPOSAL_STATE,
188-
providerUrl: undefined,
189177
tokenAccounts: [],
190178
set: (fn) => set(produce(fn)),
179+
191180
actions: {
192181
async fetchRealmBySymbol(cluster: string, symbol: string) {
193182
const actions = get().actions;
194183
let connection = get().connection;
195184
const set = get().set;
196185
const newConnection = getConnectionContext(cluster);
186+
187+
// If the cluster is not the same as the one configured in the wallet store, apply a new connection
197188
if (
198189
connection.cluster !== newConnection.cluster ||
199190
connection.endpoint !== newConnection.endpoint
200191
) {
201192
set((s) => {
202193
s.connection = newConnection;
203194
});
195+
204196
connection = get().connection;
205197
}
206198

207199
let programId: PublicKey | undefined;
208200
let realmId = tryParsePublicKey(symbol);
201+
202+
// If no realmId, get it from the symbol
209203
if (!realmId) {
210-
const realmInfo = await getCertifiedRealmInfo(symbol, newConnection);
204+
const realmInfo = getCertifiedRealmInfo(symbol, newConnection);
211205
realmId = realmInfo?.realmId;
212206
programId = realmInfo?.programId;
213207
} else {
214208
try {
215209
const realmAccountInfo = await connection.current.getAccountInfo(
216210
realmId,
217211
);
212+
218213
programId = realmAccountInfo?.owner;
219214
} catch {
220215
//
221216
}
222217
}
218+
223219
if (realmId && programId) {
224220
const programVersion = await getGovernanceProgramVersion(
225221
connection.current,
226222
programId!,
227223
);
224+
228225
set((s) => {
229226
s.selectedRealm.programVersion = programVersion;
230227
});
231-
await actions.fetchAllRealms(programId);
228+
232229
actions.fetchRealm(programId, realmId);
233230
}
234231
},
@@ -238,6 +235,7 @@ const useWalletStore = create<WalletStore>((set, get) => ({
238235
const wallet = get().current;
239236
const walletOwner = wallet?.publicKey;
240237
const set = get().set;
238+
241239
if (connected && walletOwner) {
242240
const ownedTokenAccounts = await getOwnedTokenAccounts(
243241
connection,
@@ -247,11 +245,13 @@ const useWalletStore = create<WalletStore>((set, get) => ({
247245
set((state) => {
248246
state.tokenAccounts = ownedTokenAccounts;
249247
});
250-
} else {
251-
set((state) => {
252-
state.tokenAccounts = [];
253-
});
248+
249+
return;
254250
}
251+
252+
set((state) => {
253+
state.tokenAccounts = [];
254+
});
255255
},
256256
async fetchOwnVoteRecords() {
257257
const connection = get().connection.current;
@@ -271,33 +271,22 @@ const useWalletStore = create<WalletStore>((set, get) => ({
271271
set((state) => {
272272
state.ownVoteRecordsByProposal = ownVoteRecordsByProposal;
273273
});
274-
} else {
275-
set((state) => {
276-
state.ownVoteRecordsByProposal = [];
277-
});
278-
}
279-
},
280-
deselectRealm() {
281-
const set = get().set;
282-
set((s) => {
283-
s.selectedRealm = INITIAL_REALM_STATE;
284-
});
285-
},
286-
async fetchAllRealms(programId: PublicKey) {
287-
const connection = get().connection.current;
288-
const set = get().set;
289274

290-
const realms = await getGovernanceAccounts(connection, programId, Realm);
275+
return;
276+
}
291277

292-
set((s) => {
293-
s.realms = accountsToPubkeyMap(realms);
278+
set((state) => {
279+
state.ownVoteRecordsByProposal = [];
294280
});
295281
},
296282
async fetchRealm(programId: PublicKey, realmId: PublicKey) {
297283
const set = get().set;
298284
const connection = get().connection.current;
299-
const realms = get().realms;
300-
const realm = realms[realmId.toBase58()];
285+
286+
console.log('Fetch Realm');
287+
288+
const realm = await getRealm(connection, realmId);
289+
301290
const mintsArray = (
302291
await Promise.all([
303292
realm?.account.communityMint
@@ -314,12 +303,14 @@ const useWalletStore = create<WalletStore>((set, get) => ({
314303
mintsArray.map((m) => [m!.publicKey.toBase58(), m!.account]),
315304
);
316305
});
306+
317307
const realmMints = get().selectedRealm.mints;
318308
const realmMintPk = realm.account.communityMint;
319309
const realmMint = realmMints[realmMintPk.toBase58()];
320310
const realmCouncilMintPk = realm.account.config.councilMint;
321311
const realmCouncilMint =
322312
realmCouncilMintPk && realmMints[realmCouncilMintPk.toBase58()];
313+
323314
const [
324315
governances,
325316
tokenRecords,
@@ -355,6 +346,7 @@ const useWalletStore = create<WalletStore>((set, get) => ({
355346
s.selectedRealm.tokenRecords = tokenRecords;
356347
s.selectedRealm.councilTokenOwnerRecords = councilTokenOwnerRecords;
357348
});
349+
358350
get().actions.fetchOwnVoteRecords();
359351
get().actions.fetchTokenAccountAndMintsForSelectedRealmGovernances();
360352

@@ -437,22 +429,27 @@ const useWalletStore = create<WalletStore>((set, get) => ({
437429
proposal.account.governance,
438430
Governance,
439431
),
432+
440433
getGovernanceAccounts(connection, programId, ProposalTransaction, [
441434
pubkeyFilter(1, proposalPubKey)!,
442435
]),
436+
443437
getVoteRecordsByProposalMapByVoter(
444438
connection,
445439
programId,
446440
proposalPubKey,
447441
),
442+
448443
getGovernanceAccounts(connection, programId, SignatoryRecord, [
449444
pubkeyFilter(1, proposalPubKey)!,
450445
]),
446+
451447
getGovernanceChatMessages(
452448
connection,
453449
GOVERNANCE_CHAT_PROGRAM_ID,
454450
proposalPubKey,
455451
),
452+
456453
getGovernanceAccount(
457454
connection,
458455
proposal.account.tokenOwnerRecord,
@@ -506,33 +503,40 @@ const useWalletStore = create<WalletStore>((set, get) => ({
506503
fetchTokenAccountsForSelectedRealmGovernances,
507504
fetchMintsForTokenAccounts,
508505
} = get().actions;
506+
509507
await fetchTokenAccountsForSelectedRealmGovernances();
508+
510509
fetchMintsForTokenAccounts(get().selectedRealm.tokenAccounts);
511510
},
512511
async fetchMintsForTokenAccounts(
513512
tokenAccounts: TokenProgramAccount<AccountInfo>[],
514513
) {
515514
const set = get().set;
516515
const connection = get().connection.current;
517-
const tokenMints: TokenProgramAccount<MintInfo>[] = [];
516+
518517
const tokenAccountsMintInfo = await getMultipleAccountInfoChunked(
519518
connection,
520519
tokenAccounts.map((x) => x.account.mint),
521520
);
522-
tokenAccountsMintInfo.forEach((tokenAccountMintInfo, index) => {
523-
const publicKey = tokenAccounts[index].account.mint;
524-
if (!tokenAccountMintInfo) {
525-
throw new Error(
526-
`Missing tokenAccountMintInfo: ${publicKey.toBase58()}`,
527-
);
528-
}
529-
const data = Buffer.from(tokenAccountMintInfo.data);
530-
const parsedMintInfo = parseMintAccountData(data) as MintInfo;
531-
tokenMints.push({
532-
publicKey,
533-
account: parsedMintInfo,
534-
});
535-
});
521+
522+
const tokenMints: TokenProgramAccount<MintInfo>[] = tokenAccountsMintInfo.map(
523+
(tokenAccountMintInfo, index) => {
524+
const publicKey = tokenAccounts[index].account.mint;
525+
if (!tokenAccountMintInfo) {
526+
throw new Error(
527+
`Missing tokenAccountMintInfo: ${publicKey.toBase58()}`,
528+
);
529+
}
530+
const data = Buffer.from(tokenAccountMintInfo.data);
531+
const parsedMintInfo = parseMintAccountData(data) as MintInfo;
532+
533+
return {
534+
publicKey,
535+
account: parsedMintInfo,
536+
};
537+
},
538+
);
539+
536540
set((s) => {
537541
s.selectedRealm.tokenMints = tokenMints;
538542
});

0 commit comments

Comments
 (0)