UNPKG

@everstake/wallet-sdk-solana

Version:
363 lines (353 loc) 14.8 kB
import { Address, CompilableTransactionMessage, TransactionMessageWithBlockhashLifetime, Transaction, ClusterUrl, Blockhash, Account } from '@solana/kit'; import { StakeStateAccount, StakeStateV2 } from '@solana-program/stake'; /** * Copyright (c) 2025, Everstake. * Licensed under the BSD-3-Clause License. See LICENSE file for details. */ /** * `WalletSDKError` is a custom error class that extends the built-in `Error` class. * It provides additional properties for error handling within the Wallet SDK. * * @remarks * This class is needed to provide additional context for errors, such as a code * and the original error, if any. * * @public * * @param message - The error message. * @param code - A string representing the error code. * @param originalError - The original error that caused this error, if any. */ declare class WalletSDKError extends Error { code: string; originalError?: Error | undefined; constructor(message: string, code: string, originalError?: Error | undefined); } /** * `Blockchain` is an abstract class that provides a structure for blockchain-specific classes. * It includes methods for error handling and throwing errors. * * @remarks * This class should be extended by classes that implement blockchain-specific functionality. * The extending classes should provide their own `ERROR_MESSAGES` and `ORIGINAL_ERROR_MESSAGES`. * * @property ERROR_MESSAGES - An object that maps error codes to error messages. * @property ORIGINAL_ERROR_MESSAGES - An object that maps original error messages to user-friendly error messages. * * * **/ declare abstract class Blockchain { protected abstract ERROR_MESSAGES: { [key: string]: string; }; protected abstract ORIGINAL_ERROR_MESSAGES: { [key: string]: string; }; /** * Handles errors that occur within the Ethereum class. * * @param {keyof typeof ERROR_MESSAGES} code - The error code associated with the error. * @param {Error | WalletSDKError | unknown} originalError - The original error that was thrown. * * If the original error is an instance of WalletSDKError, it is thrown as is. * If the original error is an instance of the built-in Error class, a new WalletSDKError is thrown with the original error as the cause. * If the original error is not an instance of WalletSDKError or Error, a new WalletSDKError is thrown with a generic message and code. */ handleError(code: keyof typeof this.ERROR_MESSAGES, originalError: Error | WalletSDKError | unknown): void; /** * Throws a WalletSDKError with a specified error code and message. * * @param {keyof typeof ERROR_MESSAGES} code - The error code associated with the error. * @param {...string[]} values - The values to be inserted into the error message. * * The method retrieves the error message template associated with the provided code from the ERROR_MESSAGES object. * It then replaces placeholders in the message template with provided values and throws a WalletSDKError with the final message and the provided code. */ throwError(code: keyof typeof this.ERROR_MESSAGES, ...values: string[]): void; /** * Check if the URL is valid * * @param {string} url - URL * @returns a bool type result. * */ isValidURL(url: string): boolean; } /** * Copyright (c) 2025, Everstake. * Licensed under the BSD-3-Clause License. See LICENSE file for details. */ declare const CHAIN = "solana"; declare const MIN_AMOUNT = 10000000; declare const MAINNET_VALIDATOR_ADDRESS: Address<"9QU2QSxhb24FUX3Tu2FpczXjpK3VYrvRudywSZaM29mF">; declare const DEVNET_VALIDATOR_ADDRESS: Address<"GkqYQysEGmuL6V2AJoNnWZUz2ZBGWhzQXsJiXm2CLKAN">; declare const FILTER_DATA_SIZE = 200n; declare const FILTER_OFFSET = 44n; declare enum Network { Mainnet = "mainnet-beta", Devnet = "devnet" } declare const StakeState: { Inactive: string; Activating: string; Active: string; Deactivating: string; Deactivated: string; }; declare const STAKE_ACCOUNT_V2_SIZE = 200; declare const ADDRESS_DEFAULT: Address<"11111111111111111111111111111111">; declare const STAKE_HISTORY_ACCOUNT: Address<"SysvarStakeHistory1111111111111111111111111">; declare const STAKE_CONFIG_ACCOUNT: Address<"StakeConfig11111111111111111111111111111111">; declare const MAX_CLAIM_ACCOUNTS = 16; declare const MAX_DEACTIVATE_ACCOUNTS = 22; declare const MAX_DEACTIVATE_ACCOUNTS_WITH_SPLIT = 16; /** * Copyright (c) 2025, Everstake. * Licensed under the BSD-3-Clause License. See LICENSE file for details. */ interface ApiResponse<T> { result: T; } type CreateAccountResponse = { transaction: (CompilableTransactionMessage & TransactionMessageWithBlockhashLifetime) | (Transaction & TransactionMessageWithBlockhashLifetime); stakeAccount: Address; }; type StakeResponse = { stakeTx: (CompilableTransactionMessage & TransactionMessageWithBlockhashLifetime) | (Transaction & TransactionMessageWithBlockhashLifetime); stakeAccount: Address; }; type UnstakeResponse = { unstakeTx: CompilableTransactionMessage & TransactionMessageWithBlockhashLifetime; unstakeAmount: bigint; }; type ClaimResponse = { claimTx: CompilableTransactionMessage & TransactionMessageWithBlockhashLifetime; totalClaimAmount: bigint; }; type RpcConfig = { rpc?: ClusterUrl; userAgent?: string; }; type Params = { сomputeUnitPrice?: bigint; computeUnitLimit?: number; epoch?: bigint; finalLatestBlockhash?: { /** a Hash as base-58 encoded string */ blockhash: Blockhash; /** last block height at which the blockhash will be valid */ lastValidBlockHeight: bigint; }; }; type Delegations = Array<Account<StakeStateAccount, Address>>; /** * Copyright (c) 2025, Everstake. * Licensed under the BSD-3-Clause License. See LICENSE file for details. */ /** * The `Solana` class extends the `Blockchain` class and provides methods for interacting with the Solana blockchain. * * @property connection - The connection to the Solana blockchain. * @property ERROR_MESSAGES - The error messages for the Solana class. * @property ORIGINAL_ERROR_MESSAGES - The original error messages for the Solana class. * @throws Throws an error if there's an issue establishing the connection. */ declare class Solana extends Blockchain { private connection; private validator; protected ERROR_MESSAGES: { CONNECTION_ERROR: string; MIN_AMOUNT_ERROR: string; CREATE_ACCOUNT_ERROR: string; DELEGATE_ERROR: string; DEACTIVATE_ERROR: string; WITHDRAW_ERROR: string; GET_DELEGATIONS_ERROR: string; STAKE_ERROR: string; INVALID_RPC_ERROR: string; UNSUPPORTED_NETWORK_ERROR: string; CLAIM_ERROR: string; UNSTAKE_ERROR: string; NOTHING_TO_CLAIM_ERROR: string; NOT_ENOUGH_ACTIVE_STAKE_ERROR: string; }; protected ORIGINAL_ERROR_MESSAGES: {}; constructor(network?: Network, rpcConfig?: RpcConfig); /** * Creates a new stake account. * * @param address - The public key of the account as PublicKey. * @param lamports - The amount to stake in lamports. * @param source - stake source * @param lockup - stake account lockup * * @throws Throws an error if the lamports is less than the minimum amount. * @throws Throws an error if there's an issue creating the stake account. * * @returns Returns a promise that resolves with the versioned transaction of the stake account creation and the public key of the stake account. * */ createAccount(sender: string, amountInLamports: bigint, source: string, params?: Params): Promise<ApiResponse<CreateAccountResponse>>; /** * Delegates a specified amount from a stake account to a validator. * * @param address - The public key of the account. * @param lamports - The amount in lamports to be delegated. * @param stakeAccount - The public key of the stake account. * * @throws Throws an error if the amount is less than the minimum amount, or if there's an issue during the delegation process. * * @returns Returns a promise that resolves with the delegation transaction. * */ delegate(sender: string, lamports: bigint, stakeAccount: string, params?: Params): Promise<ApiResponse<TransactionMessageWithBlockhashLifetime>>; /** * Deactivates a stake account. * * @param address - The public key of the account. * @param stakeAccountPublicKey - The public key of the stake account. * @throws Throws an error if there's an issue during the deactivation process. * @returns Returns a promise that resolves with the deactivation transaction. * */ deactivate(sender: string, stakeAccountPublicKey: string, params?: Params): Promise<ApiResponse<TransactionMessageWithBlockhashLifetime>>; /** * Withdraws a specified amount from a stake account. * * @param address - The public key of the account. * @param stakeAccountPublicKey - The public key of the stake account. * @param stakeBalance - The amount in lamports to be withdrawn from the stake account. * * @throws Throws an error if there's an issue during the withdrawal process. * * @returns Returns a promise that resolves with the withdrawal transaction. * */ withdraw(sender: Address, stakeAccountPublicKey: Address, stakeBalance: bigint, params?: Params): Promise<ApiResponse<TransactionMessageWithBlockhashLifetime>>; /** * Fetches the delegations of a given account. * * @param address - The public key of the account. * * @throws Throws an error if there's an issue fetching the delegations. * * @returns Returns a promise that resolves with the delegations of the account. * */ getDelegations(address: string): Promise<ApiResponse<Delegations>>; /** * Stakes a certain amount of lamports. * * @param sender - The public key of the sender. * @param lamports - The number of lamports to stake. * @param source - stake source * @param lockup - stake account lockup * @returns A promise that resolves to a VersionedTransaction object. */ stake(sender: string, lamports: bigint, source: string, params?: Params): Promise<ApiResponse<StakeResponse>>; /** * Create account Tx, public key and array of keypair. * * @param address - The public key of the account. * @param lamports - The number of lamports to stake. * @param lockup - The stake account lockup * * @throws Throws an error if there's an issue creating an account. * * @returns Returns a promise that resolves with the Transaction, PublicKey and array of Keypair. * */ private createAccountTx; /** * Create account Tx, public key and array of keypair using seed. * * @param authorityPublicKey - The public key of the account. * @param lamports - The number of lamports to stake. * @param source - The stake source * @param lockup - The stake account lockup * * @throws Throws an error if there's an issue creating an account. * * @returns Returns a promise that resolves with the Transaction, PublicKey and array of Keypair. * */ private createAccountWithSeedTx; /** unstake - unstake * @param {string} sender - account blockchain address (staker) * @param {bigint} lamports - lamport amount * @param {string} source - stake source * @returns {Promise<object>} Promise object with Versioned Tx */ unstake(sender: string, lamports: bigint, source: string, params?: Params): Promise<ApiResponse<UnstakeResponse>>; /** * Split existing account to create a new one * * @param authorityPublicKey - The public key of the account. * @param lamports - The number of lamports to stake. * @param oldStakeAccountPubkey -The public key of the old account. * @param source - The stake source * * @throws Throws an error if there's an issue splitting an account. * * @returns Returns a promise that resolves with the Transaction, PublicKey and array of Keypair. * */ private split; /** * Claim makes withdrawal from all sender's deactivated accounts. * * @param sender - The sender solana address. * * @throws Throws an error if there's an issue while claiming a stake. * * @returns Returns a promise that resolves with a Versioned Transaction. * */ claim(sender: string, params?: Params): Promise<ApiResponse<ClaimResponse>>; /** * Merge two accounts into a new one * * @param authorityPublicKey - The public key of the account. * @param stakeAccount1 - The public key of the first account. * @param stakeAccount2 - The public key of the second account. * * @throws Throws an error if there's an issue while merging an account. * * @returns Returns a promise that resolves with the Transaction, PublicKey and array of Keypair. * */ private baseTx; /** * Generate a unique source for crating an account. * * @param source - source ID. * * @returns Returns a unique source for an account. * */ private formatSource; /** * Generate timestamp in seconds. * * @returns Returns a timestamp in seconds. * */ private timestampInSec; } /** * Determins the current state of a stake account given the current epoch * @param currentEpoch * @returns `stakeAccount`'s stake state`string` */ declare function stakeAccountState(account: StakeStateAccount, currentEpoch: bigint): string; /** * Check if lockup is in force * @param currEpoch current epoch. * @param currUnixTimestamp current unix timetamp. * @returns a bool type result. */ declare function isLockupInForce(account: StakeStateAccount, currEpoch: bigint, currUnixTimestamp: bigint): boolean; declare function isStake(state: StakeStateV2): state is Extract<StakeStateV2, { __kind: 'Stake'; }>; export { ADDRESS_DEFAULT, type ApiResponse, Blockchain, CHAIN, type ClaimResponse, type CreateAccountResponse, DEVNET_VALIDATOR_ADDRESS, type Delegations, FILTER_DATA_SIZE, FILTER_OFFSET, MAINNET_VALIDATOR_ADDRESS, MAX_CLAIM_ACCOUNTS, MAX_DEACTIVATE_ACCOUNTS, MAX_DEACTIVATE_ACCOUNTS_WITH_SPLIT, MIN_AMOUNT, Network, type Params, type RpcConfig, STAKE_ACCOUNT_V2_SIZE, STAKE_CONFIG_ACCOUNT, STAKE_HISTORY_ACCOUNT, Solana, type StakeResponse, StakeState, type UnstakeResponse, WalletSDKError, isLockupInForce, isStake, stakeAccountState };