UNPKG

@wuwei-labs/srsly

Version:
98 lines 4.21 kB
"use strict"; /** * @purpose Simplified updateContract instruction wrapper * * Thin convenience wrapper around Codama-generated updateContract instruction. * Allows updating any subset of contract fields. Can be called by owner or delegate. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.updateContract = updateContract; 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 amount_1 = require("../params/amount"); const duration_1 = require("../params/duration"); const instructions_2 = require("../utils/instructions"); /** * Update an existing contract's settings. Owners can change any field; * delegates may only adjust the rate. * * @param params - Contract update parameters * @param config - Optional SDK configuration overrides * @returns Kit instruction (or web3.js if PublicKey in config) * * @example * ```typescript * // Update only the rate (works for both owner and delegate) * const ix = await updateContract({ * authority: wallet, * contract: contractAddress, * rate: 150 // Increase rate to 150 ATLAS * }); * * // Update multiple fields (owner only) * const ix2 = await updateContract({ * authority: wallet, * contract: contractAddress, * rate: 100, * durationMax: { days: 60 }, * reservationsDisabled: false * }); * ``` */ async function updateContract(params, config) { // Merge config const finalConfig = (0, config_1.mergeConfig)(config); // Validate required params if (!params.authority) { throw new Error('authority is required'); } if (!params.contract) { throw new Error('contract is required'); } // At least one field must be updated if (params.rate === undefined && params.durationMin === undefined && params.durationMax === undefined && params.cancelDelayMin === undefined && params.reservationsDisabled === undefined && params.toClose === undefined && params.contestedThreshold === undefined) { throw new Error('At least one field must be provided to update'); } // Convert authority to transaction signer const authoritySigner = (0, signer_1.toTransactionSigner)(params.authority); // Convert optional parameters const rateStardust = params.rate !== undefined ? (0, amount_1.convertAmount)(params.rate) : null; const durationMinSeconds = params.durationMin !== undefined ? BigInt((0, duration_1.convertDuration)(params.durationMin)) : null; const durationMaxSeconds = params.durationMax !== undefined ? BigInt((0, duration_1.convertDuration)(params.durationMax)) : null; // Validate duration logic if both are being updated if (durationMinSeconds !== null && durationMaxSeconds !== null) { if (durationMaxSeconds < durationMinSeconds) { throw new Error(`durationMax (${durationMaxSeconds}s) must be >= durationMin (${durationMinSeconds}s)`); } } // Get network-specific addresses const addresses = (0, config_1.getAddresses)(config); const programAddress = (0, kit_1.address)(addresses.srsly); // Convert cancelDelayMin duration to seconds (null means don't update) const cancelDelayMinSeconds = params.cancelDelayMin !== undefined ? BigInt((0, duration_1.convertDuration)(params.cancelDelayMin)) : null; // Call Codama-generated instruction const kitInstruction = await (0, instructions_1.getUpdateContractInstructionAsync)({ authority: authoritySigner, contract: (0, kit_1.address)(params.contract), rate: rateStardust, durationMinSeconds, durationMaxSeconds, cancelDelayMin: cancelDelayMinSeconds, reservationsDisabled: params.reservationsDisabled ?? null, toClose: params.toClose ?? null, contestedThreshold: params.contestedThreshold ?? null, }, { programAddress }); return (0, instructions_2.prepareInstructions)(kitInstruction, { computeUnits: params.computeUnits, PublicKey: finalConfig.PublicKey, }); } //# sourceMappingURL=updateContract.js.map