UNPKG

@wuwei-labs/srsly

Version:
98 lines 3.98 kB
/** * @purpose Simplified cancelRental instruction wrapper * * Thin convenience wrapper around Codama-generated cancelRental instruction. * Cancels an active rental with an optional delay. */ import { address } from '@solana/kit'; import { getCancelRentalInstructionAsync } from '../generated/codama/instructions'; import { mergeConfig, getAddresses } from '../utils/config'; import { toTransactionSigner } from '../utils/signer'; import { deriveActiveRental } from '../pda/srsly'; import { deriveAssociatedTokenAccount } from '../pda/token'; import { prepareInstructions } from '../utils/instructions'; import { convertDuration } from '../params/duration'; import { fetchConfig } from '../accounts/config'; import { fetchContract } from '../accounts/contract'; /** * Cancel an active rental and refund the borrower for unused time. * Takes effect immediately or after a delay set by `cancelDelay`. * * @param params - Cancellation parameters (borrower, contract, cancelDelay) * @param config - Optional SDK configuration overrides * @returns Kit instruction (or web3.js if PublicKey in config) * * @example * ```typescript * // Default cancellation (uses contract's cancelDelayMin) * const ix = await cancelRental({ * borrower: borrowerWallet, * contract: contractAddress * }); * * // Instant cancellation * const ix2 = await cancelRental({ * borrower: borrowerWallet, * contract: contractAddress, * cancelDelay: 0 * }); * * // Cancel after 12 hours * const ix3 = await cancelRental({ * borrower: borrowerWallet, * contract: contractAddress, * cancelDelay: { hours: 12 } * }); * ``` */ export async function cancelRental(params, config) { // Merge config const finalConfig = mergeConfig(config); // Validate required params if (!params.borrower) { throw new Error('borrower is required'); } if (!params.contract) { throw new Error('contract is required'); } // Convert borrower to transaction signer const borrowerSigner = toTransactionSigner(params.borrower); // Get atlasMint from on-chain config const configAccount = await fetchConfig(finalConfig.rpcUrl); const atlasMint = configAccount.data.atlasMint; const addresses = getAddresses(config); const programAddress = address(addresses.srsly); // Fetch contract to get fleet const contractAccount = await fetchContract(params.contract, finalConfig.rpcUrl); const fleet = contractAccount.data.fleet; // Derive active rental PDA (no more slot logic) const rentalState = await deriveActiveRental(params.contract); // Derive contract token account (ATA of contract PDA) const contractAddress = address(params.contract); const contractTokenAccount = await deriveAssociatedTokenAccount(contractAddress, atlasMint); // Convert duration to seconds (supports { hours: 1 }, { days: 1 }, etc.) // Default to contract's cancelDelayMin if not specified const cancelDelay = params.cancelDelay !== undefined ? BigInt(convertDuration(params.cancelDelay)) : contractAccount.data.cancelDelayMin; // Resolve contract thread (system_program::ID means no thread configured) const thread = address(contractAccount.data.thread); const hasThread = thread !== '11111111111111111111111111111111'; // Call Codama-generated instruction // Note: Codama auto-derives config, borrowerState, borrowerTokenAccount, rentalAuthority const kitInstruction = await getCancelRentalInstructionAsync({ mint: atlasMint, borrower: borrowerSigner, rentalState, contractTokenAccount, contract: contractAddress, fleet: address(fleet), ...(hasThread && { contractThread: thread }), cancelDelay, }, { programAddress }); return prepareInstructions(kitInstruction, { computeUnits: params.computeUnits, PublicKey: finalConfig.PublicKey, }); } //# sourceMappingURL=cancelRental.js.map