@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
73 lines • 2.72 kB
JavaScript
/**
* @purpose Simplified deleteContract instruction wrapper
*
* Thin convenience wrapper around Codama-generated deleteContract instruction.
* Admin-only instruction to delete contract state for schema migrations.
* Returns lamports to the contract owner.
*/
import { address } from '@solana/kit';
import { getDeleteContractInstructionAsync } from '../generated/codama/instructions';
import { mergeConfig, getAddresses, getRpcUrl } from '../utils/config';
import { toTransactionSigner } from '../utils/signer';
import { prepareInstructions } from '../utils/instructions';
import { fetchContract } from '../accounts/contract';
/**
* Admin escape hatch to delete a corrupted ContractState (used during
* schema migrations). Returns rent to the contract owner.
*
* @param params - Delete contract parameters
* @param config - Optional SDK configuration overrides
* @returns Kit instruction (or web3.js if PublicKey in config)
*
* @example
* ```typescript
* // Admin deletes corrupted contract state
* const ix = await deleteContract({
* admin: adminWallet,
* owner: "OWNER_WALLET_ADDRESS",
* contract: "CONTRACT_STATE_ADDRESS"
* });
* ```
*/
export async function deleteContract(params, config) {
// Merge config
const finalConfig = mergeConfig(config);
// Validate required params
if (!params.admin) {
throw new Error('admin is required');
}
if (!params.contract) {
throw new Error('contract is required');
}
// Auto-fetch owner from contract if not provided
let ownerAddress;
if (params.owner) {
ownerAddress = address(params.owner);
}
else {
try {
const rpcUrl = getRpcUrl();
const contractData = await fetchContract(params.contract, rpcUrl);
ownerAddress = contractData.data.owner;
}
catch (error) {
throw new Error(`Could not fetch owner from contract. If contract state is corrupted, provide --owner manually. Error: ${error.message}`);
}
}
// Convert admin to transaction signer
const adminSigner = toTransactionSigner(params.admin);
// Get network-specific addresses
const addresses = getAddresses(config);
const programAddress = address(addresses.srsly);
// Call Codama-generated instruction (config PDA auto-derived)
const kitInstruction = await getDeleteContractInstructionAsync({
admin: adminSigner,
owner: ownerAddress,
contract: address(params.contract),
}, { programAddress });
return prepareInstructions(kitInstruction, {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
//# sourceMappingURL=deleteContract.js.map