diff --git a/packages/governance-sdk/package.json b/packages/governance-sdk/package.json index bcf21f3a..c05e8258 100644 --- a/packages/governance-sdk/package.json +++ b/packages/governance-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@solana/spl-governance", - "version": "0.0.12", + "version": "0.0.13", "description": "SPL Governance Client API", "author": "Solana Maintainers ", "homepage": "https://github.com/solana-labs/oyster/blob/main/packages/governance-sdk/README.md", diff --git a/packages/governance-sdk/src/core/api.ts b/packages/governance-sdk/src/core/api.ts index 2656b92c..2bc9ebb2 100644 --- a/packages/governance-sdk/src/core/api.ts +++ b/packages/governance-sdk/src/core/api.ts @@ -87,7 +87,7 @@ export async function getBorshProgramAccounts< ) { accountType = accountType ?? new accountFactory({}).accountType; - let programAccounts = await connection.getProgramAccounts(programId, { + const programAccounts = await connection.getProgramAccounts(programId, { commitment: connection.commitment, filters: [ { @@ -102,7 +102,7 @@ export async function getBorshProgramAccounts< ], }); - let accounts: { [pubKey: string]: ProgramAccount } = {}; + let accounts: ProgramAccount[] = []; for (let rawAccount of programAccounts) { try { @@ -115,7 +115,7 @@ export async function getBorshProgramAccounts< owner: rawAccount.account.owner, }; - accounts[account.pubkey.toBase58()] = account; + accounts.push(account); } catch (ex) { console.info( `Can't deserialize ${accountFactory.name} @ ${rawAccount.pubkey}.`, diff --git a/packages/governance-sdk/src/governance/api.ts b/packages/governance-sdk/src/governance/api.ts index 82148bcc..a22ec5ab 100644 --- a/packages/governance-sdk/src/governance/api.ts +++ b/packages/governance-sdk/src/governance/api.ts @@ -135,10 +135,7 @@ export async function getGovernanceAccounts( ), ); - return all.reduce((res, r) => ({ ...res, ...r }), {}) as Record< - string, - ProgramAccount - >; + return all.flatMap(a => a); } export async function getGovernanceAccount( diff --git a/packages/governance-sdk/tsconfig.json b/packages/governance-sdk/tsconfig.json index 2025a051..60e1a5d4 100644 --- a/packages/governance-sdk/tsconfig.json +++ b/packages/governance-sdk/tsconfig.json @@ -4,7 +4,10 @@ "declarationMap": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, - "target": "es5", + "target": "es6", + "lib": [ + "es2019" + ], "module": "commonjs", "noFallthroughCasesInSwitch": true, "noImplicitAny": true, diff --git a/packages/governance/package.json b/packages/governance/package.json index 5500a807..b07c4f32 100644 --- a/packages/governance/package.json +++ b/packages/governance/package.json @@ -9,7 +9,7 @@ "@oyster/common": "0.0.2", "@project-serum/borsh": "^0.2.2", "@project-serum/serum": "^0.13.11", - "@solana/spl-governance": "0.0.12", + "@solana/spl-governance": "0.0.13", "@solana/spl-token": "0.0.13", "@solana/spl-token-swap": "0.1.0", "@solana/web3.js": "^1.22.0", diff --git a/packages/governance/src/contexts/GovernanceContext.tsx b/packages/governance/src/contexts/GovernanceContext.tsx index c8bf58ad..90c11892 100644 --- a/packages/governance/src/contexts/GovernanceContext.tsx +++ b/packages/governance/src/contexts/GovernanceContext.tsx @@ -17,6 +17,7 @@ import { useLocation } from 'react-router-dom'; import { PROGRAM_VERSION } from '@solana/spl-governance'; import { getGovernanceProgramVersion, ProgramAccount } from '@solana/spl-governance'; +import { arrayToRecord } from '../tools/script'; export interface GovernanceContextState { realms: Record>; @@ -127,7 +128,7 @@ export default function GovernanceProvider({ children = null as any }) { try { const loadedRealms = await getRealms(connection, programPk); - setRealms(loadedRealms); + setRealms(arrayToRecord(loadedRealms, r => r.pubkey.toBase58())); } catch (ex) { console.error("Can't load Realms", ex); setRealms({}); diff --git a/packages/governance/src/hooks/accountHooks.ts b/packages/governance/src/hooks/accountHooks.ts index 73b84dd3..8ced8ea1 100644 --- a/packages/governance/src/hooks/accountHooks.ts +++ b/packages/governance/src/hooks/accountHooks.ts @@ -15,6 +15,7 @@ import { useRpcContext } from './useRpcContext'; import { none, Option, some } from '../tools/option'; import { getGovernanceAccounts } from '@solana/spl-governance'; import { ProgramAccount } from '@solana/spl-governance'; +import { arrayToRecord } from '../tools/script'; // Fetches Governance program account using the given key and subscribes to updates export function useGovernanceAccountByPubkey< @@ -125,7 +126,7 @@ export function useGovernanceAccountsByFilter< (accountClass as any) as new (args: any) => TAccount, queryFilters, ); - setAccounts(loadedAccounts); + setAccounts(arrayToRecord(loadedAccounts, a => a.pubkey.toBase58())); } catch (ex) { console.error(`Can't load ${accountClass.name}`, ex); setAccounts({}); diff --git a/packages/governance/src/tools/script.ts b/packages/governance/src/tools/script.ts index 1e99c2e0..95258aec 100644 --- a/packages/governance/src/tools/script.ts +++ b/packages/governance/src/tools/script.ts @@ -15,3 +15,13 @@ export function getErrorMessage(ex: any) { return JSON.stringify(ex); } + +export function arrayToRecord( + source: readonly T[], + getKey: (item: T) => string, +) { + return source.reduce((all, a) => ({ ...all, [getKey(a)]: a }), {}) as Record< + string, + T + >; +} diff --git a/packages/governance/src/tools/version.ts b/packages/governance/src/tools/version.ts deleted file mode 100644 index 8fa15807..00000000 --- a/packages/governance/src/tools/version.ts +++ /dev/null @@ -1,13 +0,0 @@ -export function parseVersion(version: string) { - var arr = version.split('.'); - - // parse int or default to 0 - var major = parseInt(arr[0]) || 0; - var minor = parseInt(arr[1]) || 0; - var patch = parseInt(arr[2]) || 0; - return { - major, - minor, - patch, - }; -}