Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Governance: sdk tests (#488)
Browse files Browse the repository at this point in the history
* chore: bump sdk version

* chore: add getTokenOwnerRecordsByOwner test

* fix: change api call result from record to array
  • Loading branch information
SebastianBor authored Jan 14, 2022
1 parent 91f0109 commit 44dfff5
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/governance-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solana/spl-governance",
"version": "0.0.12",
"version": "0.0.13",
"description": "SPL Governance Client API",
"author": "Solana Maintainers <[email protected]>",
"homepage": "https://github.com/solana-labs/oyster/blob/main/packages/governance-sdk/README.md",
Expand Down
6 changes: 3 additions & 3 deletions packages/governance-sdk/src/core/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -102,7 +102,7 @@ export async function getBorshProgramAccounts<
],
});

let accounts: { [pubKey: string]: ProgramAccount<TAccount> } = {};
let accounts: ProgramAccount<TAccount>[] = [];

for (let rawAccount of programAccounts) {
try {
Expand All @@ -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}.`,
Expand Down
5 changes: 1 addition & 4 deletions packages/governance-sdk/src/governance/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ export async function getGovernanceAccounts<TAccount extends GovernanceAccount>(
),
);

return all.reduce((res, r) => ({ ...res, ...r }), {}) as Record<
string,
ProgramAccount<TAccount>
>;
return all.flatMap(a => a);
}

export async function getGovernanceAccount<TAccount extends GovernanceAccount>(
Expand Down
5 changes: 4 additions & 1 deletion packages/governance-sdk/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"target": "es5",
"target": "es6",
"lib": [
"es2019"
],
"module": "commonjs",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/governance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/governance/src/contexts/GovernanceContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ProgramAccount<Realm>>;
Expand Down Expand Up @@ -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({});
Expand Down
3 changes: 2 additions & 1 deletion packages/governance/src/hooks/accountHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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({});
Expand Down
10 changes: 10 additions & 0 deletions packages/governance/src/tools/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ export function getErrorMessage(ex: any) {

return JSON.stringify(ex);
}

export function arrayToRecord<T>(
source: readonly T[],
getKey: (item: T) => string,
) {
return source.reduce((all, a) => ({ ...all, [getKey(a)]: a }), {}) as Record<
string,
T
>;
}
13 changes: 0 additions & 13 deletions packages/governance/src/tools/version.ts

This file was deleted.

1 comment on commit 44dfff5

@vercel
Copy link

@vercel vercel bot commented on 44dfff5 Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.