@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
70 lines • 2.71 kB
JavaScript
/**
* PDA derivation functions for the Antegen (Thread) program
* @module pda/thread
*/
import { address, getProgramDerivedAddress, getAddressEncoder } from '@solana/kit';
import { getSignerAddress } from '../utils/signer';
import { THREAD_SEED, THREAD_FIBER_SEED, ANTEGEN_THREAD_PROGRAM, ANTEGEN_FIBER_PROGRAM, } from './constants';
/**
* Address encoder for PDA seeds
*/
const addressEncoder = getAddressEncoder();
/**
* Text encoder for string seeds
*/
const textEncoder = new TextEncoder();
/**
* Derive the contract thread PDA
*
* Per-contract thread used by Antegen for automated payment scheduling.
* Thread PDAs use seeds: [SEED_THREAD, authority, id]
* where id = contract address.
*
* @param contract - The contract PDA (used as the thread ID)
* @param authority - The rental authority PDA (thread authority)
* @param programAddress - Optional Antegen program address (defaults to Antegen program)
* @returns The derived contract thread PDA
*/
export async function deriveContractThread(contract, authority, programAddress) {
const contractAddress = getSignerAddress(contract);
const authorityAddr = getSignerAddress(authority);
const program = programAddress
? getSignerAddress(programAddress)
: address(ANTEGEN_THREAD_PROGRAM);
// Encode seeds to bytes
// Antegen ALWAYS uses: [SEED_THREAD, authority, id]
const threadSeedBytes = textEncoder.encode(THREAD_SEED);
const authorityBytes = addressEncoder.encode(authorityAddr);
const contractBytes = addressEncoder.encode(contractAddress);
const seeds = [threadSeedBytes, authorityBytes, contractBytes];
const [pda] = await getProgramDerivedAddress({
programAddress: program,
seeds,
});
return pda;
}
/**
* Derive the fiber PDA for a thread
*
* Fiber PDAs use seeds: ["thread_fiber", thread, fiber_index (u8)]
*
* @param thread - The thread PDA
* @param fiberIndex - The fiber index (1 for close_rental, 2 for activate_rental)
* @param programAddress - Optional Antegen program address
* @returns The derived fiber PDA
*/
export async function deriveFiber(thread, fiberIndex, programAddress) {
const threadAddress = getSignerAddress(thread);
const program = programAddress
? getSignerAddress(programAddress)
: address(ANTEGEN_FIBER_PROGRAM);
const fiberSeedBytes = textEncoder.encode(THREAD_FIBER_SEED);
const threadBytes = addressEncoder.encode(threadAddress);
const indexBytes = new Uint8Array([fiberIndex]);
const [pda] = await getProgramDerivedAddress({
programAddress: program,
seeds: [fiberSeedBytes, threadBytes, indexBytes],
});
return pda;
}
//# sourceMappingURL=thread.js.map