UNPKG

@chorus-one/solana

Version:

All-in-one toolkit for building staking dApps on Solana network

253 lines (252 loc) 10.1 kB
import { Commitment, VersionedTransaction } from '@solana/web3.js'; import type { Signer } from '@chorus-one/signer'; import { SolanaTxStatus, SolanaTransaction, StakeAccount } from './types'; /** * This class provides the functionality to stake, unstake, and withdraw for Solana blockchains. * * It also provides the ability to retrieve staking information and rewards for an account. */ export declare class SolanaStaker { private readonly networkConfig; private commitment; private connection?; /** * This **static** method is used to derive an address from a public key. * * It can be used for signer initialization, e.g. `FireblocksSigner` or `LocalSigner`. * * @returns Returns an array containing the derived address. */ static getAddressDerivationFn: () => (publicKey: Uint8Array, _derivationPath: string) => Promise<Array<string>>; /** * Creates a SolanaStaker instance. * * @param params - Initialization configuration * @param params.rpcUrl - The URL of the SOLANA network RPC endpoint * @param params.commitment - (Optional) The level of commitment desired when querying the blockchain. Default is 'confirmed'. * * @returns An instance of SolanaStaker. */ constructor(params: { rpcUrl: string; commitment?: Commitment; }); /** * Initializes the SolanaStaker instance and connects to the blockchain. * * @returns A promise which resolves once the SolanaStaker instance has been initialized. */ init(): Promise<void>; /** * Builds a new stake account transaction. * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.amount - The amount to stake, specified in `SOL` * * @returns Returns a promise that resolves to new stake account transaction. */ buildCreateStakeAccountTx(params: { ownerAddress: string; amount: string; }): Promise<{ tx: SolanaTransaction; stakeAccountAddress: string; }>; /** * Builds a staking transaction. * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.validatorAddress - The validatiors vote account address to delegate the stake to * @param params.stakeAccountAddress - The stake account address to delegate from. If not provided, a new stake account will be created. * @param params.amount - The amount to stake, specified in `SOL`. If `stakeAccountAddress` is not provided, this parameter is required. * @param params.referrer - (Optional) A custom tracking reference. If not provided, the default tracking reference will be used. * * @returns Returns a promise that resolves to a SOLANA staking transaction. */ buildStakeTx(params: { ownerAddress: string; validatorAddress: string; stakeAccountAddress?: string; amount?: string; referrer?: string; }): Promise<{ tx: SolanaTransaction; stakeAccountAddress: string; }>; /** * Builds an unstaking transaction. * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.stakeAccountAddress - The stake account address to deactivate * @param params.referrer - (Optional) A custom tracking reference. If not provided, the default tracking reference will be used. * * @returns Returns a promise that resolves to a SOLANA unstaking transaction. */ buildUnstakeTx(params: { ownerAddress: string; stakeAccountAddress: string; referrer?: string; }): Promise<{ tx: SolanaTransaction; }>; /** * Builds a partial unstake transaction. * * This method allows for unstaking a specific amount from multiple stake accounts. * It will split the stake accounts if necessary to achieve the desired unstake amount. * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.amount - The amount to unstake, specified in `SOL` * @param params.referrer - (Optional) A custom tracking reference. If not provided, the default tracking reference will be used. * * @returns Returns a promise that resolves to an array of SOLANA transactions for partial unstaking and the affected stake accounts. */ buildPartialUnstakeTx(params: { ownerAddress: string; amount: string; referrer?: string; }): Promise<{ transactions: SolanaTransaction[]; accounts: StakeAccount[]; }>; /** * Builds a withdraw stake transaction. * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.stakeAccountAddress - The stake account address to withdraw funds from * @param params.amount - The amount to withdraw, specified in `SOL`. If not provided, the entire stake amount will be withdrawn. * * @returns Returns a promise that resolves to a SOLANA withdraw stake transaction. */ buildWithdrawStakeTx(params: { ownerAddress: string; stakeAccountAddress: string; amount?: string; }): Promise<{ tx: SolanaTransaction; }>; /** * Builds a merge stake transaction. * * Please note there are conditions for merging stake accounts: * https://docs.solana.com/staking/stake-accounts#merging-stake-accounts * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.sourceAddress - The stake account address to merge funds from * @param params.destinationAddress - The stake account address to merge funds to * * @returns Returns a promise that resolves to a SOLANA merge stake transaction. */ buildMergeStakesTx(params: { ownerAddress: string; sourceAddress: string; destinationAddress: string; }): Promise<{ tx: SolanaTransaction; }>; /** * Builds a split stake transaction. * * @param params - Parameters for building the transaction * @param params.ownerAddress - The stake account owner's address * @param params.stakeAccountAddress - The stake account address to split funds from * @param params.amount - The amount to transfer from stakeAccountAddress to new staking account, specified in `SOL` * * @returns Returns a promise that resolves to a SOLANA split stake transaction. */ buildSplitStakeTx(params: { ownerAddress: string; stakeAccountAddress: string; amount: string; }): Promise<{ tx: SolanaTransaction; stakeAccountAddress: string; }>; /** * Retrieves the staking information for a specified delegator. * * @param params - Parameters for the request * @param params.ownerAddress - The stake account owner's address * @param params.validatorAddress - (Optional) The validator address to gather staking information from * @param params.state - (Optional) The stake account state to filter by (default: 'delegated') * * @returns Returns a promise that resolves to the staking information for the specified delegator. */ getStake(params: { ownerAddress: string; validatorAddress?: string; state?: 'delegated' | 'undelegated' | 'deactivating' | 'all'; }): Promise<{ balance: string; }>; /** * Signs a transaction using the provided signer. * * @param params - Parameters for the signing process * @param params.signer - A signer instance. * @param params.signerAddress - The address of the signer * @param params.tx - The transaction to sign * * @returns A promise that resolves to an object containing the signed transaction. */ sign(params: { signer: Signer; signerAddress: string; tx: SolanaTransaction; }): Promise<{ signedTx: VersionedTransaction; }>; /** * Broadcasts a signed transaction to the network. * * @param params - Parameters for the broadcast process * @param params.signedTx - The signed transaction to broadcast * * @returns A promise that resolves to the final execution outcome of the broadcast transaction. * */ broadcast(params: { signedTx: VersionedTransaction; }): Promise<{ txHash: string; slot: number; error: any; }>; /** * Retrieves the status of a transaction using the transaction hash. * * @param params - Parameters for the transaction status request * @param params.txHash - The transaction hash to query * * @returns A promise that resolves to an object containing the transaction status. */ getTxStatus(params: { txHash: string; }): Promise<SolanaTxStatus>; /** * Retrieves the stake accounts associated with an owner address. * * @param params - Parameters for the broadcast process * @param params.ownerAddress - The stake account owner's address * @param params.validatorAddress - (Optional) The validator address to filter the stake accounts by * @param params.withStates - (Optional) If true, the state of the stake account will be included in the response * @param params.withMacroDenom - (Optional) If true, the stake account balance will be returned in `SOL` denomination * * @returns A promise that resolves to stake account list. */ getStakeAccounts(params: { ownerAddress: string; validatorAddress?: string; withStates?: boolean; withMacroDenom?: boolean; }): Promise<{ accounts: StakeAccount[]; }>; private getConnection; }