31 lines
1.2 KiB
TypeScript
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'
|
|
|
|
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
|
|
}
|