UNPKG

@pod-protocol/sdk

Version:

TypeScript SDK for PoD Protocol - AI agent communication on Solana

221 lines 6.88 kB
import type { Address } from '@solana/kit'; import type { Rpc } from '@solana/kit'; import type { RpcSubscriptions } from '@solana/kit'; import type { Program as ProgramType, BorshCoder, Idl } from "@coral-xyz/anchor"; import type { PodCom } from '../pod_com'; import { AccountCache, AnalyticsCache } from '../utils/cache.js'; import { ACCOUNT_SIZES } from '../utils/account-sizes.js'; type AnchorProgram = ProgramType<PodCom>; type SolanaRpc = Rpc<any>; type SolanaRpcSubscriptions = RpcSubscriptions<any>; type Commitment = 'confirmed' | 'finalized' | 'processed'; interface AnchorProgramAccount { fetch(address: Address): Promise<any>; fetchMultiple(addresses: Address[]): Promise<any[]>; all(filters?: any[]): Promise<any[]>; } interface AnchorProgramMethod { (...args: any[]): { accounts(accounts: Record<string, Address | string>): any; signers(signers: any[]): any; rpc(options?: any): Promise<string>; instruction(): Promise<any>; }; } interface AnchorProgramMethods { registerAgent?: AnchorProgramMethod; sendMessage?: AnchorProgramMethod; createChannel?: AnchorProgramMethod; updateMessageStatus?: AnchorProgramMethod; [methodName: string]: AnchorProgramMethod | undefined; } /** * Configuration object for BaseService constructor */ export interface BaseServiceConfig { rpc: SolanaRpc; rpcSubscriptions: SolanaRpcSubscriptions; programId: Address; commitment: Commitment; program?: AnchorProgram; enableCaching?: boolean; cacheOptions?: { accountTtl?: number; analyticsTtl?: number; maxCacheSize?: number; }; } /** * Connection health monitoring interface */ export interface ConnectionHealth { isHealthy: boolean; latency: number; lastChecked: number; errorCount: number; successCount: number; uptime: number; } /** * Account data structure returned by Web3.js v2.0 */ export interface SolanaAccountInfo { pubkey: string; account: { data: Uint8Array; executable: boolean; lamports: bigint; owner: string; rentEpoch?: bigint; }; } /** * Enhanced base service class with Web3.js v2.0 integration, caching, and error handling */ export declare class BaseService { protected programId: Address; protected commitment: Commitment; protected rpc: SolanaRpc; protected rpcSubscriptions: SolanaRpcSubscriptions; protected coder?: BorshCoder; protected program?: AnchorProgram; protected idl?: Idl; protected accountCache: AccountCache; protected analyticsCache: AnalyticsCache; protected enableCaching: boolean; protected connectionHealth: ConnectionHealth; private healthCheckInterval?; constructor(rpcUrl: string, programId: string, commitment?: Commitment, options?: { enableCaching?: boolean; cacheOptions?: { accountTtl?: number; analyticsTtl?: number; maxCacheSize?: number; }; }); /** * Get the RPC client */ getRpc(): SolanaRpc; /** * Get the RPC subscriptions client */ getRpcSubscriptions(): SolanaRpcSubscriptions; /** * Get the program ID */ getProgramId(): Address; /** * Get commitment level */ getCommitment(): Commitment; /** * Get connection health status */ getConnectionHealth(): ConnectionHealth; /** * Enhanced getProgramAccounts with Web3.js v2.0, caching, and error handling */ getProgramAccounts(accountType: keyof typeof ACCOUNT_SIZES, additionalFilters?: Array<{ memcmp: { offset: number; bytes: string; }; }>, options?: { useCache?: boolean; cacheTtl?: number; encoding?: 'base64' | 'jsonParsed'; limit?: number; }): Promise<SolanaAccountInfo[]>; /** * Get multiple accounts by their addresses with batching and error handling */ getMultipleAccountsInfo(addresses: Address[], options?: { batchSize?: number; useCache?: boolean; cacheTtl?: number; }): Promise<Array<SolanaAccountInfo | null>>; /** * Get single account info with caching and error handling */ getAccountInfo(address: Address, options?: { useCache?: boolean; cacheTtl?: number; }): Promise<SolanaAccountInfo | null>; /** * Get recent blockhash for transactions */ getLatestBlockhash(): Promise<{ blockhash: string; lastValidBlockHeight: bigint; }>; /** * Decode account data using the program's BorshCoder */ protected decodeAccountData<T = any>(accountType: string, data: Uint8Array): T; /** * Process multiple accounts with parallel decoding and error handling */ protected processAccounts<T>(accounts: SolanaAccountInfo[], accountType: string, processor?: (decoded: any, account: SolanaAccountInfo) => T): Promise<T[]>; /** * Helper to create address from string */ protected createAddress(addressString: string): Address; /** * Validate that an address string is valid */ protected validateAddress(addressString: string): boolean; /** * Enhanced cache management */ protected getCachedOrFetch<T>(cacheKey: string, fetcher: () => Promise<T>, ttl?: number, cacheInstance?: AccountCache | AnalyticsCache): Promise<T>; /** * Invalidate related cache entries */ protected invalidateCache(pattern: { accountType?: string; programId?: string; address?: string; }): number; protected ensureInitialized(): AnchorProgram; protected getAccount(accountName: string): AnchorProgramAccount; protected getProgramMethods(): AnchorProgramMethods; setProgram(program: AnchorProgram): void; /** * Remove the program reference to avoid using stale credentials */ clearProgram(): void; /** * Set the IDL for read-only operations */ setIDL(idl: Idl): void; /** * Check if IDL is set for read-only operations */ hasIDL(): boolean; protected ensureIDL(): Idl; /** * Cleanup resources */ destroy(): void; private fetchAccountsBatch; private createAccountsCacheKey; private updateConnectionHealth; private startHealthMonitoring; sendTransaction(transaction: any): Promise<any>; sendRawTransaction(serializedTransaction: Uint8Array): Promise<any>; /** * Get the RPC connection for direct access */ getConnection(): Promise<any>; /** * Get current slot number from blockchain */ getCurrentSlot(): Promise<bigint>; /** * Get recent performance samples */ getRecentPerformanceSamples(limit?: number): Promise<any[]>; } export {}; //# sourceMappingURL=base.d.ts.map