feat: add @solisting/sdk helpers and unit tests
Add hand-written SDK helper files (pda.ts, listing.ts, order.ts) with GPA-based account fetching, discriminator filters, PDA derivation helpers, and escrow ID derivation. Add pda.test.ts with 4 unit tests (all passing). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
54
sdk/src/listing.ts
Normal file
54
sdk/src/listing.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
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.js'
|
||||
|
||||
const PROGRAM_ADDRESS = address('DzwUAbpRvqcbA8QsEkRbZeXG4TEho5782cMySodrUHBU')
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user