solana.js: fix getProgramAccounts

This commit is contained in:
Conner Gallagher 2022-12-07 13:45:36 -07:00
parent 3c6f1d8ced
commit d6adb6dcf1
2 changed files with 66 additions and 26 deletions

View File

@ -1,3 +1,17 @@
import {
AggregatorAccountData,
BufferRelayerAccountData,
CrankAccountData,
JobAccountData,
LeaseAccountData,
OracleAccountData,
OracleQueueAccountData,
PermissionAccountData,
SbState,
SlidingResultAccountData,
VrfAccountData,
} from '../generated';
export * from './account'; export * from './account';
export * from './aggregatorAccount'; export * from './aggregatorAccount';
export * from './aggregatorHistoryBuffer'; export * from './aggregatorHistoryBuffer';
@ -38,3 +52,16 @@ export type SwitchboardAccountTypes =
| 'Queue' | 'Queue'
| 'QueueBuffer' | 'QueueBuffer'
| 'Vrf'; | 'Vrf';
export type SwitchboardAccount =
| AggregatorAccountData
| BufferRelayerAccountData
| CrankAccountData
| JobAccountData
| LeaseAccountData
| OracleAccountData
| PermissionAccountData
| SbState
| OracleQueueAccountData
| SlidingResultAccountData
| VrfAccountData;

View File

@ -455,23 +455,21 @@ export class SwitchboardProgram {
// aggregator - [217, 230, 65, 101, 201, 162, 27, 125] // aggregator - [217, 230, 65, 101, 201, 162, 27, 125]
const discriminatorMap: Map< const discriminatorMap: Map<
Buffer, string,
Array<AccountInfoResponse> Array<AccountInfoResponse>
> = accountInfos.reduce((map, account) => { > = accountInfos.reduce((map, account) => {
const discriminator = account.account.data.slice( const discriminator = account.account.data
0, .slice(0, ACCOUNT_DISCRIMINATOR_SIZE)
ACCOUNT_DISCRIMINATOR_SIZE .toString('utf-8');
);
if (map.has(discriminator)) {
const accounts = map.get(discriminator)!;
map.set(discriminator, [...accounts, account]);
} else {
map.set(discriminator, [account]);
}
return map;
}, new Map<Buffer, Array<AccountInfoResponse>>());
function decodeAccounts<T>( const accounts = map.get(discriminator) ?? [];
accounts.push(account);
map.set(discriminator, accounts);
return map;
}, new Map<string, Array<AccountInfoResponse>>());
function decodeAccounts<T extends sbv2.SwitchboardAccount>(
accounts: Array<AccountInfoResponse>, accounts: Array<AccountInfoResponse>,
decode: (data: Buffer) => T decode: (data: Buffer) => T
): Map<string, T> { ): Map<string, T> {
@ -482,13 +480,15 @@ export class SwitchboardProgram {
} }
const aggregators: Map<string, AggregatorAccountData> = decodeAccounts( const aggregators: Map<string, AggregatorAccountData> = decodeAccounts(
discriminatorMap.get(AggregatorAccountData.discriminator) ?? [], discriminatorMap.get(
AggregatorAccountData.discriminator.toString('utf-8')
) ?? [],
AggregatorAccountData.decode AggregatorAccountData.decode
); );
// TODO: Use aggregator.historyBuffer, crank.dataBuffer, queue.dataBuffer to filter these down and decode // TODO: Use aggregator.historyBuffer, crank.dataBuffer, queue.dataBuffer to filter these down and decode
const buffers: Map<string, Buffer> = ( const buffers: Map<string, Buffer> = (
discriminatorMap.get(sbv2.BUFFER_DISCRIMINATOR) ?? [] discriminatorMap.get(sbv2.BUFFER_DISCRIMINATOR.toString('utf-8')) ?? []
).reduce((map, buffer) => { ).reduce((map, buffer) => {
map.set(buffer.pubkey.toBase58(), buffer.account.data); map.set(buffer.pubkey.toBase58(), buffer.account.data);
return map; return map;
@ -496,52 +496,65 @@ export class SwitchboardProgram {
const bufferRelayers: Map<string, BufferRelayerAccountData> = const bufferRelayers: Map<string, BufferRelayerAccountData> =
decodeAccounts( decodeAccounts(
discriminatorMap.get(BufferRelayerAccountData.discriminator) ?? [], discriminatorMap.get(
BufferRelayerAccountData.discriminator.toString('utf-8')
) ?? [],
BufferRelayerAccountData.decode BufferRelayerAccountData.decode
); );
const cranks: Map<string, CrankAccountData> = decodeAccounts( const cranks: Map<string, CrankAccountData> = decodeAccounts(
discriminatorMap.get(CrankAccountData.discriminator) ?? [], discriminatorMap.get(CrankAccountData.discriminator.toString('utf-8')) ??
[],
CrankAccountData.decode CrankAccountData.decode
); );
const jobs: Map<string, JobAccountData> = decodeAccounts( const jobs: Map<string, JobAccountData> = decodeAccounts(
discriminatorMap.get(JobAccountData.discriminator) ?? [], discriminatorMap.get(JobAccountData.discriminator.toString('utf-8')) ??
[],
JobAccountData.decode JobAccountData.decode
); );
const leases: Map<string, LeaseAccountData> = decodeAccounts( const leases: Map<string, LeaseAccountData> = decodeAccounts(
discriminatorMap.get(LeaseAccountData.discriminator) ?? [], discriminatorMap.get(LeaseAccountData.discriminator.toString('utf-8')) ??
[],
LeaseAccountData.decode LeaseAccountData.decode
); );
const oracles: Map<string, OracleAccountData> = decodeAccounts( const oracles: Map<string, OracleAccountData> = decodeAccounts(
discriminatorMap.get(OracleAccountData.discriminator) ?? [], discriminatorMap.get(OracleAccountData.discriminator.toString('utf-8')) ??
[],
OracleAccountData.decode OracleAccountData.decode
); );
const permissions: Map<string, PermissionAccountData> = decodeAccounts( const permissions: Map<string, PermissionAccountData> = decodeAccounts(
discriminatorMap.get(PermissionAccountData.discriminator) ?? [], discriminatorMap.get(
PermissionAccountData.discriminator.toString('utf-8')
) ?? [],
PermissionAccountData.decode PermissionAccountData.decode
); );
const programState: Map<string, SbState> = decodeAccounts( const programState: Map<string, SbState> = decodeAccounts(
discriminatorMap.get(SbState.discriminator) ?? [], discriminatorMap.get(SbState.discriminator.toString('utf-8')) ?? [],
SbState.decode SbState.decode
); );
const queues: Map<string, OracleQueueAccountData> = decodeAccounts( const queues: Map<string, OracleQueueAccountData> = decodeAccounts(
discriminatorMap.get(OracleQueueAccountData.discriminator) ?? [], discriminatorMap.get(
OracleQueueAccountData.discriminator.toString('utf-8')
) ?? [],
OracleQueueAccountData.decode OracleQueueAccountData.decode
); );
const slidingResult: Map<string, SlidingResultAccountData> = decodeAccounts( const slidingResult: Map<string, SlidingResultAccountData> = decodeAccounts(
discriminatorMap.get(SlidingResultAccountData.discriminator) ?? [], discriminatorMap.get(
SlidingResultAccountData.discriminator.toString('utf-8')
) ?? [],
SlidingResultAccountData.decode SlidingResultAccountData.decode
); );
const vrfs: Map<string, VrfAccountData> = decodeAccounts( const vrfs: Map<string, VrfAccountData> = decodeAccounts(
discriminatorMap.get(VrfAccountData.discriminator) ?? [], discriminatorMap.get(VrfAccountData.discriminator.toString('utf-8')) ??
[],
VrfAccountData.decode VrfAccountData.decode
); );