@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
64 lines • 2.21 kB
JavaScript
/**
* @purpose Simplified setDelegate instruction wrapper
*
* Thin convenience wrapper around Codama-generated setDelegate instruction.
* Allows contract owners to set or remove a delegate address that can update the contract rate.
*/
import { address } from '@solana/kit';
import { getSetDelegateInstruction } from '../generated/codama/instructions';
import { mergeConfig, getAddresses } from '../utils/config';
import { toTransactionSigner } from '../utils/signer';
import { prepareInstructions } from '../utils/instructions';
/**
* Set or clear a delegate that may update a contract's rate. Owner-only.
*
* @param params - Delegate parameters
* @param config - Optional SDK configuration overrides
* @returns Kit instruction (or web3.js if PublicKey in config)
*
* @example
* ```typescript
* // Set a delegate
* const ix = await setDelegate({
* owner: wallet,
* contract: contractAddress,
* delegate: delegateAddress
* });
*
* // Remove delegate
* const ix2 = await setDelegate({
* owner: wallet,
* contract: contractAddress,
* delegate: null // or omit delegate entirely
* });
* ```
*/
export async function setDelegate(params, config) {
// Merge config
const finalConfig = mergeConfig(config);
// Validate required params
if (!params.owner) {
throw new Error('owner is required');
}
if (!params.contract) {
throw new Error('contract is required');
}
// Convert owner to transaction signer
const ownerSigner = toTransactionSigner(params.owner);
// Get network-specific addresses
const addresses = getAddresses(config);
const programAddress = address(addresses.srsly);
// Convert delegate to Option<Address>
const delegateOption = params.delegate ? address(params.delegate) : null;
// Call Codama-generated instruction
const kitInstruction = getSetDelegateInstruction({
owner: ownerSigner,
contract: address(params.contract),
delegate: delegateOption,
}, { programAddress });
return prepareInstructions(kitInstruction, {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
//# sourceMappingURL=setDelegate.js.map