import { address, type Account, type Address, type Rpc } from '@solana/kit' import { type GetProgramAccountsApi } from '@solana/rpc-api' import { type GetAccountInfoApi } from '@solana/rpc-api' import type { Base64EncodedBytes } from '@solana/rpc-types' import { decodeSolistingStateListingAccount, fetchSolistingStateListingAccount, SOLISTING_STATE_LISTING_ACCOUNT_DISCRIMINATOR, type SolistingStateListingAccount, } from './generated/solisting/src/generated/index' const PROGRAM_ADDRESS = address('9uF64swnFqo34oFJtv8Qx9JDyXXJz73eDFF4cKRBfQBZ') export type ListingAccountWithPda = Account export type GpaRpc = Rpc export type GetRpc = Rpc export async function fetchAllListings(rpc: GpaRpc): Promise { const disc = SOLISTING_STATE_LISTING_ACCOUNT_DISCRIMINATOR const discBase64 = Buffer.from(disc).toString('base64') as Base64EncodedBytes const results = await rpc .getProgramAccounts(PROGRAM_ADDRESS, { encoding: 'base64', filters: [{ memcmp: { offset: 0n, bytes: discBase64, encoding: 'base64' } }], }) .send() return (results as Array<{ pubkey: Address; account: { executable: boolean; lamports: bigint; owner: Address; space: bigint; data: [string, 'base64'] } }>).map((r) => { const data = new Uint8Array(Buffer.from(r.account.data[0], 'base64')) return decodeSolistingStateListingAccount({ address: r.pubkey, data, executable: r.account.executable, lamports: r.account.lamports as unknown as import('@solana/rpc-types').Lamports, programAddress: r.account.owner, space: r.account.space, exists: true, }) as ListingAccountWithPda }) } export async function fetchListingsBySeller( rpc: GpaRpc, seller: Address, ): Promise { const all = await fetchAllListings(rpc) return all.filter((l) => l.data.seller === seller) } export async function fetchListing( rpc: GetRpc, addr: Address, ): Promise { return fetchSolistingStateListingAccount(rpc, addr).catch(() => null) }