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:
thesn10
2026-06-24 22:48:24 +02:00
parent 825dad30c0
commit b6722ee83b
4 changed files with 185 additions and 0 deletions

65
sdk/src/order.ts Normal file
View File

@@ -0,0 +1,65 @@
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 {
decodeSolistingStateOrderAccount,
fetchSolistingStateOrderAccount,
SOLISTING_STATE_ORDER_ACCOUNT_DISCRIMINATOR,
type SolistingStateOrderAccount,
} from './generated/solisting/src/generated/index.js'
import { deriveEscrowId } from './pda.js'
const PROGRAM_ADDRESS = address('DzwUAbpRvqcbA8QsEkRbZeXG4TEho5782cMySodrUHBU')
export type OrderAccountWithPda = Account<SolistingStateOrderAccount>
type GpaRpc = Rpc<GetProgramAccountsApi>
type GetRpc = Rpc<GetAccountInfoApi>
async function fetchAllOrders(rpc: GpaRpc): Promise<OrderAccountWithPda[]> {
const disc = SOLISTING_STATE_ORDER_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 decodeSolistingStateOrderAccount({
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 OrderAccountWithPda
})
}
export async function fetchOrdersForListing(
rpc: GpaRpc,
listingPk: Address,
): Promise<OrderAccountWithPda[]> {
const all = await fetchAllOrders(rpc)
return all.filter((o) => o.data.listing === listingPk)
}
export async function fetchOrdersByBuyer(
rpc: GpaRpc,
buyer: Address,
): Promise<OrderAccountWithPda[]> {
const all = await fetchAllOrders(rpc)
return all.filter((o) => o.data.buyer === buyer)
}
export async function fetchOrder(
rpc: GetRpc,
addr: Address,
): Promise<OrderAccountWithPda | null> {
return fetchSolistingStateOrderAccount(rpc, addr).catch(() => null)
}
export { deriveEscrowId }