UNPKG

@gear-js/api

Version:

A JavaScript library that provides functionality to connect GEAR Component APIs.

146 lines (145 loc) 5.17 kB
import { AddressOrPair, SignerOptions } from '@polkadot/api/types'; import { ReplyCode } from './utils'; import { GearApi } from './GearApi'; import { HexString } from './types'; interface ICalculateReplyResult { /** * The encoded reply payload. */ payload: HexString; /** * The value associated with the reply. */ value: bigint; /** * The reply code as a ReplyCode object. */ code: ReplyCode; } interface ITxResultBase { /** * The transaction hash */ txHash: HexString; /** * The block hash in which the transaction was included. */ blockHash?: HexString; /** * The block number in which the transaction was included. */ blockNumber?: number; /** * Indicates whether the transaction was succesful */ success: boolean; /** * The error message if the transaction failed */ error?: string; } interface IMessageResponse { /** * The id of the message */ id: HexString; /** * The encoded payload */ payload: HexString; /** * The value associated with the message */ value: bigint; /** * The reply code as a ReplyCode object */ replyCode: ReplyCode; } interface ISendMessageResult extends ITxResultBase { /** * The id of the message */ msgId?: HexString; /** * A function to get the response from the program */ response?: () => Promise<IMessageResponse>; } export declare class Program { private _id; private _api; private _account?; private _signerOptions?; private _isInitialized; readonly waitForInitialization: Promise<void>; private _events; private _storageUnsub; constructor(_id: HexString, _api: GearApi, _account?: AddressOrPair, _signerOptions?: Partial<SignerOptions>); /** * ## Creates a new instance of the Program class and initializes it. * @param programId - The program ID. * @param api - The GearApi instance. * @param account - (optional) The account or address to be used for transactions. * @param signerOptions - (optional) Signer options for transactions. * @returns An initialized Program instance. */ static new(programId: HexString, api: GearApi, account?: AddressOrPair, signerOptions?: Partial<SignerOptions>): Promise<Program>; private throwOnAccountNotSet; private _init; /** * ## Subscribes to a specific event emitted by the program. * @param action - The name of the event to subscribe to (e.g., 'programExited'). * @param cb - The callback function to execute when the event is triggered. Receives the inheritor ID as a parameter. * @returns A function to unsubscribe from the event. */ on(action: 'programExited', callback: (inheritorId: HexString) => void | Promise<void>): Promise<() => void>; private _submitTx; private get _accountAddress(); /** * ## Gets the current program ID. * @returns The program ID as a HexString. */ get id(): HexString; /** * Retrieves the current balance of the program. * @returns The program's balance as a bigint. */ balance(): Promise<bigint>; /** * ## Transfers funds to the program to increase its balance. * @param value - The amount to transfer as a bigint. */ topUp(value: bigint): Promise<Omit<ITxResultBase, 'events'>>; /** * ## Calculates the gas required for the message. * @param payload - The encoded payload to send, as a HexString or Uint8Array. * @param value - The value to send with the payload (default is 0). * @param allowOtherPanics - Whether to allow panics in other programs during gas calculation (default is false). * @returns Gas details. */ calculateGas(payload: HexString | Uint8Array, value?: bigint | number, allowOtherPanics?: boolean): Promise<{ minLimit: bigint; reserved: bigint; burned: bigint; mayBeReturned: bigint; waited: boolean; }>; /** * ## Sends a message to the program. * @param payload - The payload to send, as a HexString or Uint8Array. * @param value - The value to send with the message (default is 0). * @param gasLimit - The gas limit for the message ('max', 'auto', or a specific value). If 'auto', it will be calculated automatically. * @param keepAlive - Whether to use keep-alive for the transaction (default is true). */ sendMessage(payload: HexString | Uint8Array, value?: bigint | number, gasLimit?: bigint | number | 'max' | 'auto', keepAlive?: boolean): Promise<ISendMessageResult>; /** * ## Calculates the reply for a given payload and value. * @param payload - The payload to send, as a HexString or Uint8Array. * @param value - The value to send with the payload (default is 0). * @param gasLimit - The gas limit for the reply ('max' or a specific value). * @returns Reply details. */ calculateReply(payload: HexString | Uint8Array, value?: number, gasLimit?: bigint | number | 'max'): Promise<ICalculateReplyResult>; } export {};