UNPKG

@nosana/kit

Version:

Nosana KIT

163 lines 9.6 kB
import { createSolanaRpc, createSolanaRpcSubscriptions, Address, Signature, Instruction, TransactionMessageWithBlockhashLifetime, sendAndConfirmTransactionFactory, TransactionMessageWithFeePayer, TransactionMessageWithLifetime, TransactionMessage, SendableTransaction, Transaction, BaseTransactionMessage, TransactionSigner, TransactionWithBlockhashLifetime, TransactionPartialSigner } from '@solana/kit'; import { estimateComputeUnitLimitFactory } from '@solana-program/compute-budget'; import { getCreateAssociatedTokenIdempotentInstructionAsync } from '@solana-program/token'; import { getTransferSolInstruction } from '@solana-program/system'; import { Logger } from '../../logger/Logger.js'; import { Wallet } from '../../types.js'; import { SolanaConfig } from '../../config/types.js'; /** * Factory function to create an estimateAndSetComputeUnitLimit function * that estimates compute units and adds the set compute unit limit instruction */ declare function estimateAndSetComputeUnitLimitFactory(...params: Parameters<typeof estimateComputeUnitLimitFactory>): <T extends BaseTransactionMessage & TransactionMessageWithFeePayer>(transactionMessage: T) => Promise<T extends TransactionMessage ? Omit<import("@solana/kit").ExcludeTransactionMessageWithinSizeLimit<T>, "instructions"> & { readonly instructions: readonly [...T["instructions"], import("@solana-program/compute-budget").SetComputeUnitLimitInstruction<Address, []>]; } : never>; /** * Dependencies for SolanaService * @group @nosana/kit */ export interface SolanaServiceDeps { logger: Logger; getWallet: () => Wallet | undefined; } /** * Solana service interface * @group @nosana/kit */ export interface SolanaService { readonly config: SolanaConfig; readonly rpc: ReturnType<typeof createSolanaRpc>; readonly rpcSubscriptions: ReturnType<typeof createSolanaRpcSubscriptions>; readonly sendAndConfirmTransaction: ReturnType<typeof sendAndConfirmTransactionFactory>; readonly estimateAndSetComputeUnitLimit: ReturnType<typeof estimateAndSetComputeUnitLimitFactory>; airdrop(params: { recipient: Address | string; amount: number | bigint; }): Promise<Signature>; /** * Optional fee payer for transactions. If set, will be used as fallback when no feePayer is provided in options. * Set this property directly to configure the fee payer. */ feePayer: TransactionSigner | undefined; pda(seeds: Array<Address | string>, programId: Address): Promise<Address>; /** * Get the SOL balance for a specific address. * * @param addressStr - Optional address to query. If not provided, uses the wallet address. * @returns The SOL balance in lamports as a number * @throws {NosanaError} If neither address nor wallet is provided */ getBalance(addressStr?: string | Address): Promise<number>; /** * Build a transaction message from instructions. * This function creates a transaction message with fee payer, blockhash, and instructions. * * @param instructions Single instruction or array of instructions * @param options Optional configuration * @param options.feePayer Optional custom fee payer. Can be a TransactionSigner (for full signing) * or an Address/string (for partial signing where feepayer signs later). * Takes precedence over service feePayer and wallet. * @param options.estimateComputeUnits If true, estimates and sets the compute unit limit. Default: false. * @returns An unsigned transaction message ready to be signed */ buildTransaction(instructions: Instruction | Instruction[], options?: { feePayer?: TransactionSigner | Address | string; estimateComputeUnits?: boolean; }): Promise<TransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithBlockhashLifetime>; signTransaction(transactionMessage: TransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithBlockhashLifetime): Promise<SendableTransaction & Transaction & TransactionWithBlockhashLifetime>; sendTransaction(transaction: SendableTransaction & Transaction & TransactionWithBlockhashLifetime, options?: { commitment?: 'processed' | 'confirmed' | 'finalized'; }): Promise<Signature>; buildSignAndSend(instructions: Instruction | Instruction[], options?: { feePayer?: TransactionSigner; commitment?: 'processed' | 'confirmed' | 'finalized'; estimateComputeUnits?: boolean; }): Promise<Signature>; /** * Partially sign a transaction message with the signers embedded in the transaction. * The transaction message must already have a fee payer address set (via buildTransaction with an address). * Signers are extracted from instructions in the message (e.g., transfer source signer). * Use this when building transactions where the fee payer will sign later. * * @param transactionMessage The transaction message to sign (must have fee payer address set and signers embedded in instructions) * @returns A partially signed transaction */ partiallySignTransaction(transactionMessage: TransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithBlockhashLifetime): Promise<Transaction & TransactionWithBlockhashLifetime>; /** * Serialize a transaction to a base64 string. * Works with both partially signed and fully signed transactions. * Use this to transmit transactions to other parties (e.g., for fee payer signing). * * @param transaction The transaction to serialize * @returns Base64 encoded wire transaction string */ serializeTransaction(transaction: Transaction): string; /** * Deserialize a base64 string back to a transaction. * Use this to receive transactions from other parties. * * Note: This method automatically restores the `lastValidBlockHeight` metadata * that is lost during serialization by fetching the latest blockhash from the RPC. * * @param base64 The base64 encoded transaction string * @returns The deserialized transaction with restored lifetime metadata */ deserializeTransaction(base64: string): Promise<Transaction & TransactionWithBlockhashLifetime>; /** * Sign a transaction with the provided signers. * Use this when receiving a partially signed transaction that needs additional signatures. * This adds signatures from the provided signers to the transaction. * * @param transaction The transaction to sign (typically partially signed, received from another party) * @param signers Array of TransactionPartialSigners to sign with * @returns The signed transaction with additional signatures */ signTransactionWithSigners(transaction: Transaction & TransactionWithBlockhashLifetime, signers: TransactionPartialSigner[]): Promise<SendableTransaction & Transaction & TransactionWithBlockhashLifetime>; /** * Decompile a transaction back to a transaction message. * Use this to inspect/verify the content of a deserialized transaction before signing. * * Note: Decompilation is lossy - some information like lastValidBlockHeight may not be fully * reconstructed. The returned message is suitable for inspection but may not be suitable * for re-signing without additional context. * * @param transaction The compiled transaction to decompile * @returns The decompiled transaction message (with either blockhash or durable nonce lifetime) */ decompileTransaction(transaction: Transaction): BaseTransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithLifetime; /** * Get an instruction to transfer SOL from one address to another. * * @param params Transfer parameters * @param params.to Recipient address * @param params.amount Amount in lamports (number or bigint) * @param params.from Optional sender TransactionSigner. If not provided, uses wallet from client. * @returns An instruction to transfer SOL */ transfer(params: { to: Address | string; amount: number | bigint; from?: TransactionSigner; }): Promise<ReturnType<typeof getTransferSolInstruction>>; /** * Get an instruction to create an associated token account if it doesn't exist. * Checks if the ATA exists, and if not, returns an instruction to create it. * Uses the idempotent version so it's safe to call even if the account already exists. * * @param ata The associated token account address * @param mint The token mint address * @param owner The owner of the associated token account * @param payer Optional payer for the account creation. Can be a TransactionSigner (for full signing) * or an Address (for deferred signing scenarios where the payer signs later). * If not provided, uses the wallet or service feePayer. * @returns An instruction to create the ATA if it doesn't exist, or null if it already exists */ getCreateATAInstructionIfNeeded(ata: Address, mint: Address, owner: Address, payer?: TransactionSigner | Address): Promise<Awaited<ReturnType<typeof getCreateAssociatedTokenIdempotentInstructionAsync>> | null>; } /** * Creates a Solana service instance. * @group @nosana/kit */ export declare function createSolanaService(deps: SolanaServiceDeps, config: SolanaConfig): SolanaService; export {}; //# sourceMappingURL=SolanaService.d.ts.map