import { Connection , PublicKey } from '@solana/web3.js' ; import { getGovernanceSchemaForAccount , GovernanceAccountParser , } from './serialisation' ; import { getAccountTypes , getTokenOwnerRecordAddress , Governance , GovernanceAccount , GovernanceAccountClass , Proposal , Realm , TokenOwnerRecord , VoteRecord , } from './accounts' ; import { getBorshProgramAccounts , MemcmpFilter , pubkeyFilter , } from '../core/api' ; import { ProgramAccount } from '../tools/sdk/runtime' ; export async function getRealm ( connection , realm ) { return getGovernanceAccount ( connection , realm , Realm ) ; } export async function getRealms ( connection , programId ) { return getGovernanceAccounts ( connection , programId , Realm ) ; } export async function getVoteRecordsByVoter ( connection , programId , voter ) { return getGovernanceAccounts ( connection , programId , VoteRecord , [ pubkeyFilter ( 33 , voter ) ! , ] ) ; } export async function getTokenOwnerRecordForRealm ( connection , programId , realm , governingTokenMint , governingTokenOwner ) { const tokenOwnerRecordPk = await getTokenOwnerRecordAddress ( programId , realm , governingTokenMint , governingTokenOwner ) ; return getGovernanceAccount ( connection , tokenOwnerRecordPk , TokenOwnerRecord ) ; } param rpcEndpoint param programId param governingTokenOwner returns export async function getTokenOwnerRecordsByOwner ( connection , programId , governingTokenOwner ) { return getGovernanceAccounts ( connection , programId , TokenOwnerRecord , [ pubkeyFilter ( 1 + 32 + 32 , governingTokenOwner ) ! , ] ) ; } export async function getGovernance ( connection , governance ) { return getGovernanceAccount ( connection , governance , Governance ) ; } export async function getProposal ( connection , proposal ) { return getGovernanceAccount ( connection , proposal , Proposal ) ; } export async function getGovernanceAccounts < TAccount extends GovernanceAccount > ( connection , programId , accountClass , filters = [ ] ) { const accountTypes = getAccountTypes ( ( accountClass as any ) as GovernanceAccountClass ) ; if ( accountTypes . length === 1 ) { return getBorshProgramAccounts ( connection , programId , at => getGovernanceSchemaForAccount ( at ) , accountClass , filters , accountTypes [ 0 ] ) ; } const all = await Promise . all ( accountTypes . map ( at => getBorshProgramAccounts < TAccount > ( connection , programId , at => getGovernanceSchemaForAccount ( at ) , accountClass as any , filters , at ) ) ) ; return all . flatMap ( a => a ) ; } export async function getGovernanceAccount < TAccount extends GovernanceAccount > ( connection , accountPk , accountClass ) { const accountInfo = await connection . getAccountInfo ( accountPk ) ; if ( ! accountInfo ) { throw new Error ( `Account ${ accountPk } of type ${ accountClass . name } not found` ) ; } return GovernanceAccountParser ( accountClass as any ) ( accountPk , accountInfo ) as ProgramAccount < TAccount > ; }