UNPKG

@wuwei-labs/srsly

Version:
104 lines 3.78 kB
/** * @purpose Rental account utilities for SRSLY * * Fetches rental state data from the SRSLY program. */ import { getBase64Encoder } from '@solana/kit'; import { fetchRentalState, RENTAL_STATE_DISCRIMINATOR, getRentalStateDecoder, } from '../generated/codama/accounts'; import { SRSLY_PROGRAM_ADDRESS } from '../generated/codama/programs/srsly'; import { getRpcUrl } from '../utils/config'; import { createRpc } from '../utils/rpc'; /** * Fetch rental state from the SRSLY program * * @param rentalAddress - Rental state account address * @param rpcUrl - Optional RPC endpoint URL. If not provided, uses RPC from global config * @returns Rental state with borrower, borrowerState, contract, etc. * @throws Error if rental account not found or fetch fails * * @example * ```typescript * // Uses RPC URL from global config * const rental = await fetchRental('RentalAddress...'); * console.log(rental.data.borrower, rental.data.borrowerState); * * // Uses specific RPC URL * const rental2 = await fetchRental( * 'RentalAddress...', * 'https://api.devnet.solana.com' * ); * console.log('Rate:', rental2.data.rate); * ``` */ export async function fetchRental(rentalAddress, rpcUrl) { try { // Get RPC URL from config or use provided const resolvedRpcUrl = rpcUrl || getRpcUrl(); const rpc = createRpc(resolvedRpcUrl); // Use Codama-generated fetch function const account = await fetchRentalState(rpc, rentalAddress); return account; } catch (error) { throw new Error(`Failed to fetch rental state at ${rentalAddress}: ${error.message}`); } } /** * Fetch all rental state accounts from the SRSLY program * * Uses getProgramAccounts with a discriminator filter to find all RentalState accounts. * * @param rpcUrl - Optional RPC endpoint URL. If not provided, uses RPC from global config * @returns Array of rental states with their addresses * * @example * ```typescript * const rentals = await fetchAllRentals(); * console.log(`Found ${rentals.length} rentals`); * rentals.forEach(r => console.log(r.address, r.data.borrower)); * ``` */ export async function fetchAllRentals(rpcUrl) { try { const resolvedRpcUrl = rpcUrl || getRpcUrl(); const rpc = createRpc(resolvedRpcUrl); const discriminatorBase64 = btoa(String.fromCharCode(...RENTAL_STATE_DISCRIMINATOR)); const response = await rpc .getProgramAccounts(SRSLY_PROGRAM_ADDRESS, { encoding: 'base64', filters: [ { memcmp: { offset: 0n, bytes: discriminatorBase64, encoding: 'base64', }, }, ], }) .send(); const base64Encoder = getBase64Encoder(); const decoder = getRentalStateDecoder(); const accounts = Array.isArray(response) ? response : response.value; const results = []; for (const account of accounts) { const bytes = base64Encoder.encode(account.account.data[0]); try { const decoded = decoder.decode(bytes); results.push({ data: decoded, address: account.pubkey }); } catch { // Old schema — return address with minimal data so wipe can still delete results.push({ data: { borrower: '11111111111111111111111111111111' }, address: account.pubkey, }); } } return results; } catch (error) { throw new Error(`Failed to fetch all rental states: ${error.message}`); } } //# sourceMappingURL=rental.js.map