UNPKG

@wuwei-labs/srsly

Version:
58 lines 2.6 kB
/** * @purpose Simplified closeContractThread instruction wrapper * * Thin convenience wrapper around Codama-generated closeContractThread instruction. * Closes a per-contract automation thread and reclaims rent. */ import { AccountRole, address } from '@solana/kit'; import { getCloseContractThreadInstructionAsync } from '../generated/codama/instructions'; import { mergeConfig, getAddresses } from '../utils/config'; import { toTransactionSigner } from '../utils/signer'; import { prepareInstructions } from '../utils/instructions'; import { fetchThreadByAddress } from '../accounts/thread'; import { deriveContractThread, deriveFiber } from '../pda/thread'; import { deriveRentalAuthority } from '../pda/srsly'; /** * Close a contract's automation thread and all of its fibers, reclaiming * rent to the owner. * * @param params - Thread closing parameters * @param config - Optional SDK configuration overrides * @returns Kit instruction (or web3.js if PublicKey in config) */ export async function closeContractThread(params, config) { const finalConfig = mergeConfig(config); if (!params.owner) { throw new Error('owner is required'); } if (!params.contract) { throw new Error('contract is required'); } const ownerSigner = toTransactionSigner(params.owner); const addresses = getAddresses(config); const programAddress = address(addresses.srsly); const contractAddress = address(params.contract); // Derive thread PDA const rentalAuthority = await deriveRentalAuthority(); const contractThread = await deriveContractThread(contractAddress, rentalAuthority); // Fetch thread account to read actual fiber_ids const threadAccount = await fetchThreadByAddress(contractThread, finalConfig.rpcUrl); const fiberIds = Array.from(threadAccount.data.fiberIds); // Derive fiber PDAs only for fibers that actually exist on the thread const fiberAccounts = await Promise.all(fiberIds.map(async (id) => ({ address: await deriveFiber(contractThread, id), role: AccountRole.WRITABLE, }))); const kitInstruction = await getCloseContractThreadInstructionAsync({ owner: ownerSigner, contract: contractAddress, thread: contractThread, }, { programAddress }); // Add fiber PDAs as remaining accounts (writable, non-signer) kitInstruction.accounts.push(...fiberAccounts); return prepareInstructions(kitInstruction, { computeUnits: params.computeUnits, PublicKey: finalConfig.PublicKey, }); } //# sourceMappingURL=closeContractThread.js.map