UNPKG

@wuwei-labs/srsly

Version:
108 lines 3.95 kB
"use strict"; /** * @purpose Rental account utilities for SRSLY * * Fetches rental state data from the SRSLY program. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchRental = fetchRental; exports.fetchAllRentals = fetchAllRentals; const kit_1 = require("@solana/kit"); const accounts_1 = require("../generated/codama/accounts"); const srsly_1 = require("../generated/codama/programs/srsly"); const config_1 = require("../utils/config"); const rpc_1 = require("../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); * ``` */ async function fetchRental(rentalAddress, rpcUrl) { try { // Get RPC URL from config or use provided const resolvedRpcUrl = rpcUrl || (0, config_1.getRpcUrl)(); const rpc = (0, rpc_1.createRpc)(resolvedRpcUrl); // Use Codama-generated fetch function const account = await (0, accounts_1.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)); * ``` */ async function fetchAllRentals(rpcUrl) { try { const resolvedRpcUrl = rpcUrl || (0, config_1.getRpcUrl)(); const rpc = (0, rpc_1.createRpc)(resolvedRpcUrl); const discriminatorBase64 = btoa(String.fromCharCode(...accounts_1.RENTAL_STATE_DISCRIMINATOR)); const response = await rpc .getProgramAccounts(srsly_1.SRSLY_PROGRAM_ADDRESS, { encoding: 'base64', filters: [ { memcmp: { offset: 0n, bytes: discriminatorBase64, encoding: 'base64', }, }, ], }) .send(); const base64Encoder = (0, kit_1.getBase64Encoder)(); const decoder = (0, accounts_1.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