Files
solisting/sdk/src/listing.ts
2026-06-25 18:53:06 +02:00

55 lines
2.1 KiB
TypeScript

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<SolistingStateListingAccount>
export type GpaRpc = Rpc<GetProgramAccountsApi>
export type GetRpc = Rpc<GetAccountInfoApi>
export async function fetchAllListings(rpc: GpaRpc): Promise<ListingAccountWithPda[]> {
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<ListingAccountWithPda[]> {
const all = await fetchAllListings(rpc)
return all.filter((l) => l.data.seller === seller)
}
export async function fetchListing(
rpc: GetRpc,
addr: Address,
): Promise<ListingAccountWithPda | null> {
return fetchSolistingStateListingAccount(rpc, addr).catch(() => null)
}