ethers.js/lib.esm/providers/abstract-provider.d.ts

449 lines
15 KiB
TypeScript
Raw Normal View History

2022-11-30 23:44:23 +03:00
/**
2023-06-02 00:52:58 +03:00
* The available providers should suffice for most developers purposes,
* but the [[AbstractProvider]] class has many features which enable
* sub-classing it for specific purposes.
2022-11-30 23:44:23 +03:00
*
* @_section: api/providers/abstract-provider: Subclassing Provider [abstract-provider]
*/
2022-11-09 10:57:02 +03:00
import { FetchRequest } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import { EnsResolver } from "./ens-resolver.js";
import { Network } from "./network.js";
import { Block, FeeData, Log, TransactionReceipt, TransactionResponse } from "./provider.js";
import type { AddressLike } from "../address/index.js";
import type { BigNumberish } from "../utils/index.js";
2022-09-27 10:45:27 +03:00
import type { Listener } from "../utils/index.js";
2022-09-05 23:57:11 +03:00
import type { Networkish } from "./network.js";
2022-11-30 23:44:23 +03:00
import type { BlockParams, LogParams, TransactionReceiptParams, TransactionResponseParams } from "./formatting.js";
import type { BlockTag, EventFilter, Filter, FilterByBlockHash, OrphanFilter, PreparedTransactionRequest, Provider, ProviderEvent, TransactionRequest } from "./provider.js";
2023-06-02 00:52:58 +03:00
/**
* The types of additional event values that can be emitted for the
* ``"debug"`` event.
*/
2023-02-02 12:05:47 +03:00
export type DebugEventAbstractProvider = {
2022-11-09 10:57:02 +03:00
action: "sendCcipReadFetchRequest";
request: FetchRequest;
index: number;
urls: Array<string>;
} | {
action: "receiveCcipReadFetchResult";
request: FetchRequest;
result: any;
} | {
action: "receiveCcipReadFetchError";
request: FetchRequest;
result: any;
} | {
action: "sendCcipReadCall";
transaction: {
to: string;
data: string;
};
} | {
action: "receiveCcipReadCallResult";
transaction: {
to: string;
data: string;
};
result: string;
} | {
action: "receiveCcipReadCallError";
transaction: {
to: string;
data: string;
};
error: Error;
};
2023-06-02 00:52:58 +03:00
/**
* The value passed to the [[AbstractProvider-_getSubscriber]] method.
*
* Only developers sub-classing [[AbstractProvider[[ will care about this,
* if they are modifying a low-level feature of how subscriptions operate.
*/
2023-02-02 12:05:47 +03:00
export type Subscription = {
2023-04-25 14:04:48 +03:00
type: "block" | "close" | "debug" | "error" | "network" | "pending";
2022-09-05 23:57:11 +03:00
tag: string;
} | {
type: "transaction";
tag: string;
hash: string;
} | {
type: "event";
tag: string;
filter: EventFilter;
} | {
type: "orphan";
tag: string;
filter: OrphanFilter;
};
2023-06-02 00:52:58 +03:00
/**
* A **Subscriber** manages a subscription.
*
* Only developers sub-classing [[AbstractProvider[[ will care about this,
* if they are modifying a low-level feature of how subscriptions operate.
*/
2022-09-05 23:57:11 +03:00
export interface Subscriber {
2023-06-02 00:52:58 +03:00
/**
* Called initially when a subscriber is added the first time.
*/
2022-09-05 23:57:11 +03:00
start(): void;
2023-06-02 00:52:58 +03:00
/**
* Called when there are no more subscribers to the event.
*/
2022-09-05 23:57:11 +03:00
stop(): void;
2023-06-02 00:52:58 +03:00
/**
* Called when the subscription should pause.
*
* If %%dropWhilePaused%%, events that occur while paused should not
* be emitted [[resume]].
*/
2022-09-05 23:57:11 +03:00
pause(dropWhilePaused?: boolean): void;
2023-06-02 00:52:58 +03:00
/**
* Resume a paused subscriber.
*/
2022-09-05 23:57:11 +03:00
resume(): void;
2023-06-02 00:52:58 +03:00
/**
* The frequency (in ms) to poll for events, if polling is used by
* the subscriber.
*
* For non-polling subscribers, this must return ``undefined``.
*/
2022-09-05 23:57:11 +03:00
pollingInterval?: number;
}
2023-06-02 00:52:58 +03:00
/**
* An **UnmanagedSubscriber** is useful for events which do not require
* any additional management, such as ``"debug"`` which only requires
* emit in synchronous event loop triggered calls.
*/
2022-09-05 23:57:11 +03:00
export declare class UnmanagedSubscriber implements Subscriber {
2023-06-02 00:52:58 +03:00
/**
* The name fof the event.
*/
2022-09-05 23:57:11 +03:00
name: string;
2023-06-02 00:52:58 +03:00
/**
* Create a new UnmanagedSubscriber with %%name%%.
*/
2022-09-05 23:57:11 +03:00
constructor(name: string);
start(): void;
stop(): void;
pause(dropWhilePaused?: boolean): void;
resume(): void;
}
2023-06-02 00:52:58 +03:00
/**
* An **AbstractPlugin** is used to provide additional internal services
* to an [[AbstractProvider]] without adding backwards-incompatible changes
* to method signatures or other internal and complex logic.
*/
2022-11-09 10:57:02 +03:00
export interface AbstractProviderPlugin {
2023-06-02 00:52:58 +03:00
/**
* The reverse domain notation of the plugin.
*/
2022-09-05 23:57:11 +03:00
readonly name: string;
2023-06-02 00:52:58 +03:00
/**
* Creates a new instance of the plugin, connected to %%provider%%.
*/
2022-11-09 10:57:02 +03:00
connect(provider: AbstractProvider): AbstractProviderPlugin;
2022-09-05 23:57:11 +03:00
}
2023-06-02 00:52:58 +03:00
/**
* A normalized filter used for [[PerformActionRequest]] objects.
*/
2023-02-02 12:05:47 +03:00
export type PerformActionFilter = {
2022-09-05 23:57:11 +03:00
address?: string | Array<string>;
topics?: Array<null | string | Array<string>>;
fromBlock?: BlockTag;
toBlock?: BlockTag;
} | {
address?: string | Array<string>;
topics?: Array<null | string | Array<string>>;
blockHash?: string;
};
2023-06-02 00:52:58 +03:00
/**
* A normalized transactions used for [[PerformActionRequest]] objects.
*/
2022-09-27 10:45:27 +03:00
export interface PerformActionTransaction extends PreparedTransactionRequest {
2023-06-02 00:52:58 +03:00
/**
* The ``to`` address of the transaction.
*/
2022-09-05 23:57:11 +03:00
to?: string;
2023-06-02 00:52:58 +03:00
/**
* The sender of the transaction.
*/
2022-09-05 23:57:11 +03:00
from?: string;
2022-09-27 10:45:27 +03:00
}
2023-06-02 00:52:58 +03:00
/**
* The [[AbstractProvider]] methods will normalize all values and pass this
* type to [[AbstractProvider-_perform]].
*/
2023-02-02 12:05:47 +03:00
export type PerformActionRequest = {
2022-11-09 10:57:02 +03:00
method: "broadcastTransaction";
signedTransaction: string;
} | {
2022-09-05 23:57:11 +03:00
method: "call";
transaction: PerformActionTransaction;
blockTag: BlockTag;
} | {
method: "chainId";
} | {
method: "estimateGas";
transaction: PerformActionTransaction;
} | {
method: "getBalance";
address: string;
blockTag: BlockTag;
} | {
method: "getBlock";
blockTag: BlockTag;
includeTransactions: boolean;
} | {
method: "getBlock";
blockHash: string;
includeTransactions: boolean;
} | {
method: "getBlockNumber";
} | {
method: "getCode";
address: string;
blockTag: BlockTag;
} | {
method: "getGasPrice";
} | {
method: "getLogs";
filter: PerformActionFilter;
} | {
2022-09-30 05:57:27 +03:00
method: "getStorage";
2022-09-05 23:57:11 +03:00
address: string;
position: bigint;
blockTag: BlockTag;
} | {
method: "getTransaction";
hash: string;
} | {
method: "getTransactionCount";
address: string;
blockTag: BlockTag;
} | {
method: "getTransactionReceipt";
hash: string;
} | {
method: "getTransactionResult";
hash: string;
};
2023-06-14 04:47:44 +03:00
/**
* Options for configuring some internal aspects of an [[AbstractProvider]].
*
* **``cacheTimeout``** - how long to cache a low-level ``_perform``
* for, based on input parameters. This reduces the number of calls
* to getChainId and getBlockNumber, but may break test chains which
* can perform operations (internally) synchronously. Use ``-1`` to
* disable, ``0`` will only buffer within the same event loop and
* any other value is in ms. (default: ``250``)
*/
export type AbstractProviderOptions = {
cacheTimeout?: number;
pollingInterval?: number;
2023-06-14 04:47:44 +03:00
};
2023-06-02 00:52:58 +03:00
/**
* An **AbstractProvider** provides a base class for other sub-classes to
* implement the [[Provider]] API by normalizing input arguments and
* formatting output results as well as tracking events for consistent
* behaviour on an eventually-consistent network.
*/
2022-09-05 23:57:11 +03:00
export declare class AbstractProvider implements Provider {
#private;
2023-06-02 00:52:58 +03:00
/**
* Create a new **AbstractProvider** connected to %%network%%, or
* use the various network detection capabilities to discover the
* [[Network]] if necessary.
*/
2023-06-14 04:47:44 +03:00
constructor(_network?: "any" | Networkish, options?: AbstractProviderOptions);
get pollingInterval(): number;
2023-06-02 00:52:58 +03:00
/**
* Returns ``this``, to allow an **AbstractProvider** to implement
* the [[ContractRunner]] interface.
*/
2022-09-05 23:57:11 +03:00
get provider(): this;
2023-06-02 00:52:58 +03:00
/**
* Returns all the registered plug-ins.
*/
2022-11-09 10:57:02 +03:00
get plugins(): Array<AbstractProviderPlugin>;
2023-06-02 00:52:58 +03:00
/**
* Attach a new plug-in.
*/
2022-11-09 10:57:02 +03:00
attachPlugin(plugin: AbstractProviderPlugin): this;
2023-06-02 00:52:58 +03:00
/**
* Get a plugin by name.
*/
2022-11-09 10:57:02 +03:00
getPlugin<T extends AbstractProviderPlugin = AbstractProviderPlugin>(name: string): null | T;
2023-06-02 00:52:58 +03:00
/**
* Prevent any CCIP-read operation, regardless of whether requested
* in a [[call]] using ``enableCcipRead``.
*/
2022-09-05 23:57:11 +03:00
get disableCcipRead(): boolean;
2022-11-30 23:44:23 +03:00
set disableCcipRead(value: boolean);
2023-06-02 00:52:58 +03:00
/**
* Resolves to the data for executing the CCIP-read operations.
*/
2022-09-05 23:57:11 +03:00
ccipReadFetch(tx: PerformActionTransaction, calldata: string, urls: Array<string>): Promise<null | string>;
2023-06-02 00:52:58 +03:00
/**
* Provides the opportunity for a sub-class to wrap a block before
* returning it, to add additional properties or an alternate
* sub-class of [[Block]].
*/
2022-11-30 23:44:23 +03:00
_wrapBlock(value: BlockParams, network: Network): Block;
2023-06-02 00:52:58 +03:00
/**
* Provides the opportunity for a sub-class to wrap a log before
* returning it, to add additional properties or an alternate
* sub-class of [[Log]].
*/
2022-09-27 10:45:27 +03:00
_wrapLog(value: LogParams, network: Network): Log;
2023-06-02 00:52:58 +03:00
/**
* Provides the opportunity for a sub-class to wrap a transaction
* receipt before returning it, to add additional properties or an
* alternate sub-class of [[TransactionReceipt]].
*/
2022-09-27 10:45:27 +03:00
_wrapTransactionReceipt(value: TransactionReceiptParams, network: Network): TransactionReceipt;
2023-06-02 00:52:58 +03:00
/**
* Provides the opportunity for a sub-class to wrap a transaction
* response before returning it, to add additional properties or an
* alternate sub-class of [[TransactionResponse]].
*/
2022-09-27 10:45:27 +03:00
_wrapTransactionResponse(tx: TransactionResponseParams, network: Network): TransactionResponse;
2023-06-02 00:52:58 +03:00
/**
* Resolves to the Network, forcing a network detection using whatever
* technique the sub-class requires.
*
* Sub-classes **must** override this.
*/
2022-09-27 10:45:27 +03:00
_detectNetwork(): Promise<Network>;
2023-06-02 00:52:58 +03:00
/**
* Sub-classes should use this to perform all built-in operations. All
* methods sanitizes and normalizes the values passed into this.
*
* Sub-classes **must** override this.
*/
2022-09-05 23:57:11 +03:00
_perform<T = any>(req: PerformActionRequest): Promise<T>;
getBlockNumber(): Promise<number>;
2023-06-02 00:52:58 +03:00
/**
* Returns or resolves to the address for %%address%%, resolving ENS
* names and [[Addressable]] objects and returning if already an
* address.
*/
2022-09-05 23:57:11 +03:00
_getAddress(address: AddressLike): string | Promise<string>;
2023-06-02 00:52:58 +03:00
/**
* Returns or resolves to a valid block tag for %%blockTag%%, resolving
* negative values and returning if already a valid block tag.
*/
2022-09-05 23:57:11 +03:00
_getBlockTag(blockTag?: BlockTag): string | Promise<string>;
2023-06-02 00:52:58 +03:00
/**
* Returns or resolves to a filter for %%filter%%, resolving any ENS
* names or [[Addressable]] object and returning if already a valid
* filter.
*/
2022-09-27 10:45:27 +03:00
_getFilter(filter: Filter | FilterByBlockHash): PerformActionFilter | Promise<PerformActionFilter>;
2023-06-02 00:52:58 +03:00
/**
* Returns or resovles to a transaction for %%request%%, resolving
* any ENS names or [[Addressable]] and returning if already a valid
* transaction.
*/
2022-09-27 10:45:27 +03:00
_getTransactionRequest(_request: TransactionRequest): PerformActionTransaction | Promise<PerformActionTransaction>;
getNetwork(): Promise<Network>;
2022-09-05 23:57:11 +03:00
getFeeData(): Promise<FeeData>;
estimateGas(_tx: TransactionRequest): Promise<bigint>;
2022-09-27 10:45:27 +03:00
call(_tx: TransactionRequest): Promise<string>;
2022-09-05 23:57:11 +03:00
getBalance(address: AddressLike, blockTag?: BlockTag): Promise<bigint>;
getTransactionCount(address: AddressLike, blockTag?: BlockTag): Promise<number>;
getCode(address: AddressLike, blockTag?: BlockTag): Promise<string>;
2022-09-30 05:57:27 +03:00
getStorage(address: AddressLike, _position: BigNumberish, blockTag?: BlockTag): Promise<string>;
2022-09-05 23:57:11 +03:00
broadcastTransaction(signedTx: string): Promise<TransactionResponse>;
2022-11-30 23:44:23 +03:00
getBlock(block: BlockTag | string, prefetchTxs?: boolean): Promise<null | Block>;
2022-09-05 23:57:11 +03:00
getTransaction(hash: string): Promise<null | TransactionResponse>;
getTransactionReceipt(hash: string): Promise<null | TransactionReceipt>;
getTransactionResult(hash: string): Promise<null | string>;
getLogs(_filter: Filter | FilterByBlockHash): Promise<Array<Log>>;
_getProvider(chainId: number): AbstractProvider;
getResolver(name: string): Promise<null | EnsResolver>;
getAvatar(name: string): Promise<null | string>;
resolveName(name: string): Promise<null | string>;
lookupAddress(address: string): Promise<null | string>;
2022-12-10 02:24:58 +03:00
waitForTransaction(hash: string, _confirms?: null | number, timeout?: null | number): Promise<null | TransactionReceipt>;
2022-11-30 23:44:23 +03:00
waitForBlock(blockTag?: BlockTag): Promise<Block>;
2023-06-02 00:52:58 +03:00
/**
* Clear a timer created using the [[_setTimeout]] method.
*/
2022-09-05 23:57:11 +03:00
_clearTimeout(timerId: number): void;
2023-06-02 00:52:58 +03:00
/**
* Create a timer that will execute %%func%% after at least %%timeout%%
* (in ms). If %%timeout%% is unspecified, then %%func%% will execute
* in the next event loop.
*
* [Pausing](AbstractProvider-paused) the provider will pause any
* associated timers.
*/
2022-09-05 23:57:11 +03:00
_setTimeout(_func: () => void, timeout?: number): number;
2023-06-02 00:52:58 +03:00
/**
* Perform %%func%% on each subscriber.
*/
2022-09-05 23:57:11 +03:00
_forEachSubscriber(func: (s: Subscriber) => void): void;
2023-06-02 00:52:58 +03:00
/**
* Sub-classes may override this to customize subscription
* implementations.
*/
2022-09-05 23:57:11 +03:00
_getSubscriber(sub: Subscription): Subscriber;
2023-06-02 00:52:58 +03:00
/**
* If a [[Subscriber]] fails and needs to replace itself, this
* method may be used.
*
* For example, this is used for providers when using the
* ``eth_getFilterChanges`` method, which can return null if state
* filters are not supported by the backend, allowing the Subscriber
* to swap in a [[PollingEventSubscriber]].
*/
2022-09-05 23:57:11 +03:00
_recoverSubscriber(oldSub: Subscriber, newSub: Subscriber): void;
on(event: ProviderEvent, listener: Listener): Promise<this>;
once(event: ProviderEvent, listener: Listener): Promise<this>;
emit(event: ProviderEvent, ...args: Array<any>): Promise<boolean>;
listenerCount(event?: ProviderEvent): Promise<number>;
listeners(event?: ProviderEvent): Promise<Array<Listener>>;
off(event: ProviderEvent, listener?: Listener): Promise<this>;
removeAllListeners(event?: ProviderEvent): Promise<this>;
addListener(event: ProviderEvent, listener: Listener): Promise<this>;
removeListener(event: ProviderEvent, listener: Listener): Promise<this>;
2023-06-07 05:42:28 +03:00
/**
* If this provider has been destroyed using the [[destroy]] method.
*
* Once destroyed, all resources are reclaimed, internal event loops
* and timers are cleaned up and no further requests may be sent to
* the provider.
*/
get destroyed(): boolean;
2023-06-02 00:52:58 +03:00
/**
* Sub-classes may use this to shutdown any sockets or release their
2023-06-07 05:42:28 +03:00
* resources and reject any pending requests.
2023-06-02 00:52:58 +03:00
*
* Sub-classes **must** call ``super.destroy()``.
*/
2022-12-31 00:35:04 +03:00
destroy(): void;
2023-06-02 00:52:58 +03:00
/**
* Whether the provider is currently paused.
*
* A paused provider will not emit any events, and generally should
* not make any requests to the network, but that is up to sub-classes
* to manage.
*
* Setting ``paused = true`` is identical to calling ``.pause(false)``,
* which will buffer any events that occur while paused until the
* provider is unpaused.
*/
2022-09-05 23:57:11 +03:00
get paused(): boolean;
set paused(pause: boolean);
2023-06-02 00:52:58 +03:00
/**
* Pause the provider. If %%dropWhilePaused%%, any events that occur
* while paused are dropped, otherwise all events will be emitted once
* the provider is unpaused.
*/
2022-09-05 23:57:11 +03:00
pause(dropWhilePaused?: boolean): void;
2023-06-02 00:52:58 +03:00
/**
* Resume the provider.
*/
2022-09-05 23:57:11 +03:00
resume(): void;
}
//# sourceMappingURL=abstract-provider.d.ts.map