import { address, getAddressEncoder, type Address, type ProgramDerivedAddress } from '@solana/kit' import { findListingAccountPda, findOrderAccountPda } from './generated/solisting/src/generated/index' const PROGRAM_ADDRESS = address('9uF64swnFqo34oFJtv8Qx9JDyXXJz73eDFF4cKRBfQBZ') export async function findListingPda( seller: Address, listingId: bigint, ): Promise { return findListingAccountPda({ seller, listingId }, { programAddress: PROGRAM_ADDRESS }) } export async function findOrderPda( listingAccount: Address, buyer: Address, orderId: bigint, ): Promise { 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 }