UNPKG

@wuwei-labs/srsly

Version:
61 lines 2.77 kB
"use strict"; /** * @purpose Simplified closeContractThread instruction wrapper * * Thin convenience wrapper around Codama-generated closeContractThread instruction. * Closes a per-contract automation thread and reclaims rent. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.closeContractThread = closeContractThread; const kit_1 = require("@solana/kit"); const instructions_1 = require("../generated/codama/instructions"); const config_1 = require("../utils/config"); const signer_1 = require("../utils/signer"); const instructions_2 = require("../utils/instructions"); const thread_1 = require("../accounts/thread"); const thread_2 = require("../pda/thread"); const srsly_1 = require("../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) */ async function closeContractThread(params, config) { const finalConfig = (0, config_1.mergeConfig)(config); if (!params.owner) { throw new Error('owner is required'); } if (!params.contract) { throw new Error('contract is required'); } const ownerSigner = (0, signer_1.toTransactionSigner)(params.owner); const addresses = (0, config_1.getAddresses)(config); const programAddress = (0, kit_1.address)(addresses.srsly); const contractAddress = (0, kit_1.address)(params.contract); // Derive thread PDA const rentalAuthority = await (0, srsly_1.deriveRentalAuthority)(); const contractThread = await (0, thread_2.deriveContractThread)(contractAddress, rentalAuthority); // Fetch thread account to read actual fiber_ids const threadAccount = await (0, thread_1.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 (0, thread_2.deriveFiber)(contractThread, id), role: kit_1.AccountRole.WRITABLE, }))); const kitInstruction = await (0, instructions_1.getCloseContractThreadInstructionAsync)({ owner: ownerSigner, contract: contractAddress, thread: contractThread, }, { programAddress }); // Add fiber PDAs as remaining accounts (writable, non-signer) kitInstruction.accounts.push(...fiberAccounts); return (0, instructions_2.prepareInstructions)(kitInstruction, { computeUnits: params.computeUnits, PublicKey: finalConfig.PublicKey, }); } //# sourceMappingURL=closeContractThread.js.map