@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
59 lines • 2.73 kB
JavaScript
/**
* @purpose Simplified deleteContractThread instruction wrapper
*
* Thin convenience wrapper around Codama-generated deleteContractThread instruction.
* Admin-only instruction to delete a contract's thread for schema migrations.
* Unlike closeContractThread, does not require the contract account.
*/
import { AccountRole, address } from '@solana/kit';
import { getDeleteContractThreadInstructionAsync } 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';
/**
* Admin variant of `closeContractThread` that doesn't require the
* contract account on-chain — used when the contract is already deleted
* or unparseable.
*
* @param params - Thread deletion parameters
* @param config - Optional SDK configuration overrides
* @returns Kit instruction (or web3.js if PublicKey in config)
*/
export async function deleteContractThread(params, config) {
const finalConfig = mergeConfig(config);
if (!params.admin) {
throw new Error('admin is required');
}
if (!params.contract) {
throw new Error('contract is required');
}
const adminSigner = toTransactionSigner(params.admin);
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 getDeleteContractThreadInstructionAsync({
admin: adminSigner,
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=deleteContractThread.js.map