From cb137c1e8b1fb41bebd955572dc4ea7836f9286d Mon Sep 17 00:00:00 2001 From: "Sebastian.Bor" Date: Wed, 25 Aug 2021 21:03:37 +0200 Subject: [PATCH] feat: add getGovernanceChatMessages api call --- packages/governance/src/models/chat/api.ts | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 packages/governance/src/models/chat/api.ts diff --git a/packages/governance/src/models/chat/api.ts b/packages/governance/src/models/chat/api.ts new file mode 100644 index 0000000..57fbf2e --- /dev/null +++ b/packages/governance/src/models/chat/api.ts @@ -0,0 +1,90 @@ +import { PublicKey } from '@solana/web3.js'; +import bs58 from 'bs58'; +import { MemcmpFilter, pubkeyFilter } from '../core/api'; +import { + ChatMessage, + GovernanceChatAccount, + GovernanceChatAccountClass, + GovernanceChatAccountType, + governanceChatProgramId, +} from './accounts'; +import { deserializeBorsh, ParsedAccount } from '@oyster/common'; +import { GOVERNANCE_CHAT_SCHEMA } from './serialisation'; + +export function getGovernanceChatMessages( + endpoint: string, + proposal: PublicKey, +) { + return getGovernanceChatAccounts( + governanceChatProgramId, + endpoint, + ChatMessage, + GovernanceChatAccountType.ChatMessage, + [pubkeyFilter(1, proposal) as MemcmpFilter], + ); +} + +export async function getGovernanceChatAccounts< + TAccount extends GovernanceChatAccount +>( + programId: PublicKey, + endpoint: string, + accountClass: GovernanceChatAccountClass, + accountType: GovernanceChatAccountType, + filters: MemcmpFilter[] = [], +) { + let getProgramAccounts = await fetch(endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + jsonrpc: '2.0', + id: 1, + method: 'getProgramAccounts', + params: [ + programId.toBase58(), + { + commitment: 'single', + encoding: 'base64', + filters: [ + { + memcmp: { + offset: 0, + bytes: bs58.encode([accountType]), + }, + }, + ...filters.map(f => ({ + memcmp: { offset: f.offset, bytes: bs58.encode(f.bytes) }, + })), + ], + }, + ], + }), + }); + const rawAccounts = (await getProgramAccounts.json())['result']; + let accounts: Record> = {}; + + for (let rawAccount of rawAccounts) { + try { + const account = { + pubkey: new PublicKey(rawAccount.pubkey), + account: { + ...rawAccount.account, + data: [], // There is no need to keep the raw data around once we deserialize it into TAccount + }, + info: deserializeBorsh( + GOVERNANCE_CHAT_SCHEMA, + accountClass, + Buffer.from(rawAccount.account.data[0], 'base64'), + ), + }; + + accounts[account.pubkey.toBase58()] = account; + } catch (ex) { + console.error(`Can't deserialize ${accountClass}`, ex); + } + } + + return accounts; +}