Files
solisting/sdk/src/pda.ts
thesn10 b6722ee83b 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>
2026-06-24 22:48:24 +02:00

31 lines
1.2 KiB
TypeScript

import { address, getAddressEncoder, type Address, type ProgramDerivedAddress } from '@solana/kit'
import { findListingAccountPda, findOrderAccountPda } from './generated/solisting/src/generated/index.js'
const PROGRAM_ADDRESS = address('DzwUAbpRvqcbA8QsEkRbZeXG4TEho5782cMySodrUHBU')
export async function findListingPda(
seller: Address,
listingId: bigint,
): Promise<ProgramDerivedAddress> {
return findListingAccountPda({ seller, listingId }, { programAddress: PROGRAM_ADDRESS })
}
export async function findOrderPda(
listingAccount: Address,
buyer: Address,
orderId: bigint,
): Promise<ProgramDerivedAddress> {
return findOrderAccountPda({ listingAccount, buyer, orderId }, { programAddress: PROGRAM_ADDRESS })
}
/**
* Replicates the program's escrow_id derivation:
* u64::from_le_bytes(order_pda.to_bytes()[0..8])
* Must match exactly — used when building create_order instructions.
*/
export function deriveEscrowId(orderPda: Address): bigint {
const bytes = getAddressEncoder().encode(orderPda) // 32-byte Uint8Array
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
return view.getBigUint64(0, true) // little-endian, first 8 bytes
}