@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
95 lines • 3.99 kB
JavaScript
/**
* @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.
*/
import { address } from '@solana/kit';
import { getUpdateContractInstructionAsync } from '../generated/codama/instructions';
import { mergeConfig, getAddresses } from '../utils/config';
import { toTransactionSigner } from '../utils/signer';
import { convertAmount } from '../params/amount';
import { convertDuration } from '../params/duration';
import { prepareInstructions } from '../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
* });
* ```
*/
export async function updateContract(params, config) {
// Merge config
const finalConfig = 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 = toTransactionSigner(params.authority);
// Convert optional parameters
const rateStardust = params.rate !== undefined ? convertAmount(params.rate) : null;
const durationMinSeconds = params.durationMin !== undefined ? BigInt(convertDuration(params.durationMin)) : null;
const durationMaxSeconds = params.durationMax !== undefined ? BigInt(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 = getAddresses(config);
const programAddress = address(addresses.srsly);
// Convert cancelDelayMin duration to seconds (null means don't update)
const cancelDelayMinSeconds = params.cancelDelayMin !== undefined ? BigInt(convertDuration(params.cancelDelayMin)) : null;
// Call Codama-generated instruction
const kitInstruction = await getUpdateContractInstructionAsync({
authority: authoritySigner,
contract: address(params.contract),
rate: rateStardust,
durationMinSeconds,
durationMaxSeconds,
cancelDelayMin: cancelDelayMinSeconds,
reservationsDisabled: params.reservationsDisabled ?? null,
toClose: params.toClose ?? null,
contestedThreshold: params.contestedThreshold ?? null,
}, { programAddress });
return prepareInstructions(kitInstruction, {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
//# sourceMappingURL=updateContract.js.map