@wuwei-labs/srsly
Version:
TypeScript SDK for SRSLY
44 lines • 1.63 kB
JavaScript
/**
* @purpose Fleet account utilities for SRSLY
*
* Fetches fleet state data from the Star Atlas SAGE program.
*/
import { getBase64Encoder } from '@solana/kit';
import { getFleetDecoder } from '../generated/codama/types/fleet';
import { getRpcUrl } from '../utils/config';
import { createRpc } from '../utils/rpc';
/** Anchor discriminator size (8 bytes) */
const DISCRIMINATOR_SIZE = 8;
/**
* Fetch fleet state from the Star Atlas SAGE program
*
* @param fleetAddress - Fleet account address
* @param rpcUrl - Optional RPC endpoint URL. If not provided, uses RPC from global config
* @returns Decoded fleet data including stats (ShipStats)
* @throws Error if fleet account not found or fetch fails
*
* @example
* ```typescript
* const fleet = await fetchFleet('FleetAddress...');
* console.log(fleet.ownerProfile, fleet.gameId, fleet.stats);
* ```
*/
export async function fetchFleet(fleetAddress, rpcUrl) {
try {
const resolvedRpcUrl = rpcUrl || getRpcUrl();
const rpc = createRpc(resolvedRpcUrl);
const base64Encoder = getBase64Encoder();
const { value } = await rpc
.getAccountInfo(fleetAddress, { encoding: 'base64' })
.send();
if (!value || !value?.data) {
throw new Error(`Fleet account not found: ${fleetAddress}`);
}
const bytes = base64Encoder.encode(value.data[0]);
return getFleetDecoder().decode(bytes.subarray(DISCRIMINATOR_SIZE));
}
catch (error) {
throw new Error(`Failed to fetch fleet state at ${fleetAddress}: ${error.message}`);
}
}
//# sourceMappingURL=fleet.js.map