@chorus-one/substrate
Version:
All-in-one toolkit for building staking dApps on Substrate Network SDK blockchains(Polkadot, Kusama, etc.)
179 lines (178 loc) • 6.92 kB
TypeScript
import type { GenericExtrinsic } from '@polkadot/types/extrinsic';
import type { ExtrinsicStatus } from '@polkadot/types/interfaces/author';
import type { AnyTuple } from '@polkadot/types-codec/types';
import type { Signer } from '@chorus-one/signer';
import type { SubstrateFee, SubstrateTxStatus, UnsignedTx, Indexer } from './types';
import { RewardDestination } from './enums';
/**
* This class provides the functionality to stake, nominate, unbond, and withdraw funds for a Substrate-based blockchains.
*
* It also provides the ability to retrieve staking information and rewards for a delegator.
*/
export declare class SubstrateStaker {
private readonly networkConfig;
private api?;
/**
* 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>>;
/**
* This creates a new SubstrateStaker instance.
*
* @param params - Initialization parameters
* @param params.rpcUrl - RPC URL (e.g. wss://rpc.polkadot.io)
* @param params.rewardDestination - Reward destination (e.g., RewardDestination.STASH or RewardDestination.CONTROLLER)
* @param params.denomMultiplier - (Optional) Multiplier to convert the base coin unit to its smallest subunit (e.g., `1000000000000` for 1 DOT = 1000000000000 Planck)
* @param params.fee - (Optional) Transaction fee (e.g. '0.001' for 0.001 DOT)
* @param params.indexer - (Optional) Indexer instance to supplement missing node RPC features
*
* @returns An instance of SusbstrateStaker.
*/
constructor(params: {
rpcUrl: string;
rewardDestination: RewardDestination;
denomMultiplier?: string;
fee?: SubstrateFee;
indexer?: Indexer;
});
/**
* Initializes the SubstrateStaker instance and connects to the blockchain.
*
* @returns A promise which resolves once the Staker instance has been initialized.
*/
init(): Promise<void>;
/**
* Closes the SubstrateStaker instance and disconnects from the blockchain.
*
* @returns A promise which resolves once the Staker instance has been closed.
*/
close(): Promise<void>;
/**
* Builds a staking (delegation) transaction.
*
* @param params - Parameters for building the transaction
* @param params.amount - The amount to stake, specified in base units of the native token (e.g. `DOT` for Polkadot)
*
* @returns Returns a promise that resolves to a Polkadot staking transaction.
*/
buildStakeTx(params: {
amount: string;
}): Promise<{
tx: UnsignedTx;
}>;
/**
* Builds a nomination transaction - allows the user to pick trusted validators to delegate to.
*
* @param params - Parameters for building the transaction
* @param params.validatorAddresses - The list of validator addresses to nominate to
*
* @returns Returns a promise that resolves to a Substrate nomination transaction.
*/
buildNominateTx(params: {
validatorAddresses: string[];
}): Promise<{
tx: UnsignedTx;
}>;
/**
* Builds an unstaking (undelegation) transaction.
*
* @param params - Parameters for building the transaction
* @param params.amount - The amount to unstake, specified in base units of the native token (e.g. `DOT` for Polkadot)
*
* @returns Returns a promise that resolves to a Substrate unstaking transaction.
*/
buildUnstakeTx(params: {
amount: string;
}): Promise<{
tx: UnsignedTx;
}>;
/**
* Builds a transaction to withdraw all unstaked funds from the validator contract.
*
* @returns Returns a promise that resolves to a Substrate withdraw transaction.
*/
buildWithdrawTx(): Promise<{
tx: UnsignedTx;
}>;
/**
* Builds a transaction to delegate more tokens to a validator.
*
* @param params - Parameters for building the transaction
* @param params.amount - The amount to stake, specified in base units of the native token (e.g. `DOT` for Polkadot)
*
* @returns Returns a promise that resolves to a Substrate bond extra transaction.
*/
buildBondExtraTx(params: {
amount: string;
}): Promise<{
tx: UnsignedTx;
}>;
/**
* Retrieves the staking information for a specified delegator.
*
* @param params - Parameters for the request
* @param params.delegatorAddress - The delegator (wallet) address
* @param params.validatorAddress - (Optional) The validator address to assert the delegator is staking with
* @param params.status - (Optional) The status of nomination (default: 'active')
*
* @returns Returns a promise that resolves to the staking information for the specified delegator.
*/
getStake(params: {
delegatorAddress: string;
validatorAddress?: string;
status?: 'active' | 'total';
}): Promise<{
balance: string;
}>;
/**
* Signs a transaction using the provided signer.
*
* @param params - Parameters for the signing process
* @param params.signer - Signer instance
* @param params.signerAddress - The address of the signer
* @param params.tx - The transaction to sign
* @param params.blocks - (Optional) The number of blocks until the transaction expires
*
* @returns A promise that resolves to an object containing the signed transaction.
*/
sign(params: {
signer: Signer;
signerAddress: string;
tx: UnsignedTx;
blocks?: number;
}): Promise<{
signedTx: GenericExtrinsic<AnyTuple>;
}>;
/**
* This method is used to broadcast a signed transaction to the Substrate network.
*
* @param params - Parameters for the broadcast
* @param params.signedTx - The signed transaction to be broadcasted
*
* @returns Returns a promise that resolves to the response of the transaction that was broadcast to the network.
*/
broadcast(params: {
signedTx: GenericExtrinsic;
}): Promise<{
txHash: string;
status: ExtrinsicStatus;
}>;
/**
* 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<SubstrateTxStatus>;
private getApi;
private buildUnsignedTx;
private signRaw;
}