@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
87 lines • 3.38 kB
JavaScript
;
/**
* @purpose Simplified releaseRental instruction wrapper
*
* Thin convenience wrapper around Codama-generated releaseRental instruction.
* Release a fleet from SRSLY rental control when contract exists but rental is inactive.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.releaseRental = releaseRental;
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 instructions_2 = require("../utils/instructions");
const sage_1 = require("../pda/sage");
const fleet_1 = require("../accounts/fleet");
/**
* Free a fleet from SRSLY's rental hold once the rental has been closed.
* Use `invalidateRental` instead when no contract exists.
*
* @param params - Release rental parameters
* @param config - Optional SDK configuration overrides
* @returns Kit instruction (or web3.js if PublicKey in config)
*
* @example
* ```typescript
* // Release a stuck fleet (contract must exist)
* const ix = await releaseRental({
* fleet: "FLEET_ADDRESS",
* signer: wallet,
* ownerProfile: "OWNER_PROFILE_ADDRESS",
* faction: "mud"
* });
*
* // With compute budget
* const ix2 = await releaseRental({
* fleet: fleetAddress,
* signer: adminWallet,
* ownerProfile: ownerProfileAddress,
* faction: 1,
* computeUnits: 200_000
* });
* ```
*/
async function releaseRental(params, config) {
// Merge config
const finalConfig = (0, config_1.mergeConfig)(config);
// Validate required params
if (!params.fleet) {
throw new Error('fleet is required');
}
if (!params.signer) {
throw new Error('signer is required');
}
// Convert signer to transaction signer
const signerAccount = (0, signer_1.toTransactionSigner)(params.signer);
// Get network-specific addresses (including gameId)
const addresses = (0, config_1.getAddresses)(config);
const gameId = (0, kit_1.address)(addresses.gameId);
const sageProgram = (0, kit_1.address)(addresses.sage);
const programAddress = (0, kit_1.address)(addresses.srsly);
// Auto-fetch ownerProfile and faction from fleet when not provided
let ownerProfile = params.ownerProfile;
let faction = params.faction;
if (!ownerProfile || !faction) {
const fleetState = await (0, fleet_1.fetchFleet)(params.fleet, finalConfig.rpcUrl);
ownerProfile = ownerProfile ?? fleetState.ownerProfile;
faction = faction ?? fleetState.faction;
}
// Derive owner's SAGE accounts (NOT borrower's)
const gameAccounts = await (0, sage_1.deriveGameAccounts)(ownerProfile, faction, gameId);
// Call Codama-generated instruction
// Contract and rentalAuthority are auto-derived by Codama from seeds
const kitInstruction = await (0, instructions_1.getReleaseRentalInstructionAsync)({
signer: signerAccount,
fleet: (0, kit_1.address)(params.fleet),
gameId: gameId,
starbase: gameAccounts.starbase,
starbasePlayer: gameAccounts.starbasePlayer,
sageProgram,
}, { programAddress });
return (0, instructions_2.prepareInstructions)(kitInstruction, {
computeUnits: params.computeUnits,
PublicKey: finalConfig.PublicKey,
});
}
//# sourceMappingURL=releaseRental.js.map