ethers.js/lib.commonjs/providers/provider-jsonrpc.d.ts

343 lines
12 KiB
TypeScript
Raw Permalink Normal View History

2023-03-03 18:25:07 -07:00
/**
2023-06-01 17:52:58 -04:00
* One of the most common ways to interact with the blockchain is
* by a node running a JSON-RPC interface which can be connected to,
* based on the transport, using:
*
* - HTTP or HTTPS - [[JsonRpcProvider]]
* - WebSocket - [[WebSocketProvider]]
* - IPC - [[IpcSocketProvider]]
2023-03-03 18:25:07 -07:00
*
* @_section: api/providers/jsonrpc:JSON-RPC Provider [about-jsonrpcProvider]
*/
import { FetchRequest } from "../utils/index.js";
import { AbstractProvider } from "./abstract-provider.js";
import { AbstractSigner } from "./abstract-signer.js";
import { Network } from "./network.js";
import type { TypedDataDomain, TypedDataField } from "../hash/index.js";
import type { TransactionLike } from "../transaction/index.js";
import type { PerformActionRequest, Subscriber, Subscription } from "./abstract-provider.js";
import type { Networkish } from "./network.js";
import type { Provider, TransactionRequest, TransactionResponse } from "./provider.js";
import type { Signer } from "./signer.js";
/**
* A JSON-RPC payload, which are sent to a JSON-RPC server.
*/
export type JsonRpcPayload = {
2023-06-01 17:52:58 -04:00
/**
* The JSON-RPC request ID.
*/
2023-03-03 18:25:07 -07:00
id: number;
2023-06-01 17:52:58 -04:00
/**
* The JSON-RPC request method.
*/
2023-03-03 18:25:07 -07:00
method: string;
2023-06-01 17:52:58 -04:00
/**
* The JSON-RPC request parameters.
*/
2023-03-03 18:25:07 -07:00
params: Array<any> | Record<string, any>;
2023-06-01 17:52:58 -04:00
/**
* A required constant in the JSON-RPC specification.
*/
2023-03-03 18:25:07 -07:00
jsonrpc: "2.0";
};
/**
* A JSON-RPC result, which are returned on success from a JSON-RPC server.
*/
export type JsonRpcResult = {
2023-06-01 17:52:58 -04:00
/**
* The response ID to match it to the relevant request.
*/
2023-03-03 18:25:07 -07:00
id: number;
2023-06-01 17:52:58 -04:00
/**
* The response result.
*/
2023-03-03 18:25:07 -07:00
result: any;
};
/**
* A JSON-RPC error, which are returned on failure from a JSON-RPC server.
*/
export type JsonRpcError = {
2023-06-01 17:52:58 -04:00
/**
* The response ID to match it to the relevant request.
*/
2023-03-03 18:25:07 -07:00
id: number;
2023-06-01 17:52:58 -04:00
/**
* The response error.
*/
2023-03-03 18:25:07 -07:00
error: {
code: number;
message?: string;
data?: any;
};
};
/**
* When subscribing to the ``"debug"`` event, the [[Listener]] will
* receive this object as the first parameter.
*/
export type DebugEventJsonRpcApiProvider = {
action: "sendRpcPayload";
payload: JsonRpcPayload | Array<JsonRpcPayload>;
} | {
action: "receiveRpcResult";
result: Array<JsonRpcResult | JsonRpcError>;
} | {
action: "receiveRpcError";
error: Error;
};
/**
* Options for configuring a [[JsonRpcApiProvider]]. Much of this
* is targetted towards sub-classes, which often will not expose
* any of these options to their consumers.
*
* **``polling``** - use the polling strategy is used immediately
* for events; otherwise, attempt to use filters and fall back onto
* polling (default: ``false``)
*
* **``staticNetwork``** - do not request chain ID on requests to
* validate the underlying chain has not changed (default: ``null``)
*
* This should **ONLY** be used if it is **certain** that the network
* cannot change, such as when using INFURA (since the URL dictates the
* network). If the network is assumed static and it does change, this
* can have tragic consequences. For example, this **CANNOT** be used
* with MetaMask, since the used can select a new network from the
* drop-down at any time.
*
* **``batchStallTime``** - how long (ms) to aggregate requests into a
* single batch. ``0`` indicates batching will only encompass the current
* event loop. If ``batchMaxCount = 1``, this is ignored. (default: ``10``)
*
* **``batchMaxSize``** - target maximum size (bytes) to allow per batch
* request (default: 1Mb)
*
* **``batchMaxCount``** - maximum number of requests to allow in a batch.
* If ``batchMaxCount = 1``, then batching is disabled. (default: ``100``)
*/
export type JsonRpcApiProviderOptions = {
polling?: boolean;
staticNetwork?: null | Network;
batchStallTime?: number;
batchMaxSize?: number;
batchMaxCount?: number;
};
2023-06-01 17:52:58 -04:00
/**
* A **JsonRpcTransactionRequest** is formatted as needed by the JSON-RPC
* Ethereum API specification.
*/
2023-03-03 18:25:07 -07:00
export interface JsonRpcTransactionRequest {
2023-06-01 17:52:58 -04:00
/**
* The sender address to use when signing.
*/
2023-03-03 18:25:07 -07:00
from?: string;
2023-06-01 17:52:58 -04:00
/**
* The target address.
*/
2023-03-03 18:25:07 -07:00
to?: string;
2023-06-01 17:52:58 -04:00
/**
* The transaction data.
*/
2023-03-03 18:25:07 -07:00
data?: string;
2023-06-01 17:52:58 -04:00
/**
* The chain ID the transaction is valid on.
*/
2023-03-03 18:25:07 -07:00
chainId?: string;
2023-06-01 17:52:58 -04:00
/**
* The [[link-eip-2718]] transaction type.
*/
2023-03-03 18:25:07 -07:00
type?: string;
2023-06-01 17:52:58 -04:00
/**
* The maximum amount of gas to allow a transaction to consume.
*
* In most other places in ethers, this is called ``gasLimit`` which
* differs from the JSON-RPC Ethereum API specification.
*/
2023-03-03 18:25:07 -07:00
gas?: string;
2023-06-01 17:52:58 -04:00
/**
* The gas price per wei for transactions prior to [[link-eip-1559]].
*/
2023-03-03 18:25:07 -07:00
gasPrice?: string;
2023-06-01 17:52:58 -04:00
/**
* The maximum fee per gas for [[link-eip-1559]] transactions.
*/
2023-03-03 18:25:07 -07:00
maxFeePerGas?: string;
2023-06-01 17:52:58 -04:00
/**
* The maximum priority fee per gas for [[link-eip-1559]] transactions.
*/
2023-03-03 18:25:07 -07:00
maxPriorityFeePerGas?: string;
2023-06-01 17:52:58 -04:00
/**
* The nonce for the transaction.
*/
2023-03-03 18:25:07 -07:00
nonce?: string;
2023-06-01 17:52:58 -04:00
/**
* The transaction value (in wei).
*/
2023-03-03 18:25:07 -07:00
value?: string;
2023-06-01 17:52:58 -04:00
/**
* The transaction access list.
*/
2023-03-03 18:25:07 -07:00
accessList?: Array<{
address: string;
storageKeys: Array<string>;
}>;
}
export declare class JsonRpcSigner extends AbstractSigner<JsonRpcApiProvider> {
address: string;
constructor(provider: JsonRpcApiProvider, address: string);
connect(provider: null | Provider): Signer;
getAddress(): Promise<string>;
populateTransaction(tx: TransactionRequest): Promise<TransactionLike<string>>;
sendUncheckedTransaction(_tx: TransactionRequest): Promise<string>;
sendTransaction(tx: TransactionRequest): Promise<TransactionResponse>;
signTransaction(_tx: TransactionRequest): Promise<string>;
signMessage(_message: string | Uint8Array): Promise<string>;
signTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, _value: Record<string, any>): Promise<string>;
unlock(password: string): Promise<boolean>;
_legacySignMessage(_message: string | Uint8Array): Promise<string>;
}
/**
* The JsonRpcApiProvider is an abstract class and **MUST** be
* sub-classed.
*
* It provides the base for all JSON-RPC-based Provider interaction.
*
* Sub-classing Notes:
* - a sub-class MUST override _send
* - a sub-class MUST call the `_start()` method once connected
*/
export declare abstract class JsonRpcApiProvider extends AbstractProvider {
#private;
constructor(network?: Networkish, options?: JsonRpcApiProviderOptions);
/**
* Returns the value associated with the option %%key%%.
*
* Sub-classes can use this to inquire about configuration options.
*/
_getOption<K extends keyof JsonRpcApiProviderOptions>(key: K): JsonRpcApiProviderOptions[K];
/**
* Gets the [[Network]] this provider has committed to. On each call, the network
* is detected, and if it has changed, the call will reject.
*/
get _network(): Network;
/**
* Sends a JSON-RPC %%payload%% (or a batch) to the underlying channel.
*
* Sub-classes **MUST** override this.
*/
abstract _send(payload: JsonRpcPayload | Array<JsonRpcPayload>): Promise<Array<JsonRpcResult | JsonRpcError>>;
/**
* Resolves to the non-normalized value by performing %%req%%.
*
* Sub-classes may override this to modify behavior of actions,
* and should generally call ``super._perform`` as a fallback.
*/
_perform(req: PerformActionRequest): Promise<any>;
/**
* Sub-classes may override this; it detects the *actual* network that
* we are **currently** connected to.
*
* Keep in mind that [[send]] may only be used once [[ready]], otherwise the
* _send primitive must be used instead.
*/
_detectNetwork(): Promise<Network>;
/**
* Sub-classes **MUST** call this. Until [[_start]] has been called, no calls
* will be passed to [[_send]] from [[send]]. If it is overridden, then
* ``super._start()`` **MUST** be called.
*
* Calling it multiple times is safe and has no effect.
*/
_start(): void;
/**
* Resolves once the [[_start]] has been called. This can be used in
* sub-classes to defer sending data until the connection has been
* established.
*/
_waitUntilReady(): Promise<void>;
/**
* Return a Subscriber that will manage the %%sub%%.
*
* Sub-classes may override this to modify the behavior of
* subscription management.
*/
_getSubscriber(sub: Subscription): Subscriber;
/**
* Returns true only if the [[_start]] has been called.
*/
get ready(): boolean;
/**
* Returns %%tx%% as a normalized JSON-RPC transaction request,
* which has all values hexlified and any numeric values converted
* to Quantity values.
*/
getRpcTransaction(tx: TransactionRequest): JsonRpcTransactionRequest;
/**
* Returns the request method and arguments required to perform
* %%req%%.
*/
getRpcRequest(req: PerformActionRequest): null | {
method: string;
args: Array<any>;
};
/**
* Returns an ethers-style Error for the given JSON-RPC error
* %%payload%%, coalescing the various strings and error shapes
* that different nodes return, coercing them into a machine-readable
* standardized error.
*/
getRpcError(payload: JsonRpcPayload, _error: JsonRpcError): Error;
/**
* Requests the %%method%% with %%params%% via the JSON-RPC protocol
* over the underlying channel. This can be used to call methods
* on the backend that do not have a high-level API within the Provider
* API.
*
* This method queues requests according to the batch constraints
* in the options, assigns the request a unique ID.
*
* **Do NOT override** this method in sub-classes; instead
* override [[_send]] or force the options values in the
* call to the constructor to modify this method's behavior.
*/
send(method: string, params: Array<any> | Record<string, any>): Promise<any>;
/**
* Resolves to the [[Signer]] account for %%address%% managed by
* the client.
*
* If the %%address%% is a number, it is used as an index in the
* the accounts from [[listAccounts]].
*
* This can only be used on clients which manage accounts (such as
* Geth with imported account or MetaMask).
*
* Throws if the account doesn't exist.
*/
getSigner(address?: number | string): Promise<JsonRpcSigner>;
listAccounts(): Promise<Array<JsonRpcSigner>>;
2023-06-06 22:42:28 -04:00
destroy(): void;
2023-03-03 18:25:07 -07:00
}
export declare abstract class JsonRpcApiPollingProvider extends JsonRpcApiProvider {
#private;
constructor(network?: Networkish, options?: JsonRpcApiProviderOptions);
_getSubscriber(sub: Subscription): Subscriber;
/**
* The polling interval (default: 4000 ms)
*/
get pollingInterval(): number;
set pollingInterval(value: number);
}
/**
* The JsonRpcProvider is one of the most common Providers,
* which performs all operations over HTTP (or HTTPS) requests.
*
* Events are processed by polling the backend for the current block
* number; when it advances, all block-base events are then checked
* for updates.
*/
export declare class JsonRpcProvider extends JsonRpcApiPollingProvider {
#private;
constructor(url?: string | FetchRequest, network?: Networkish, options?: JsonRpcApiProviderOptions);
_getConnection(): FetchRequest;
send(method: string, params: Array<any> | Record<string, any>): Promise<any>;
_send(payload: JsonRpcPayload | Array<JsonRpcPayload>): Promise<Array<JsonRpcResult>>;
}
//# sourceMappingURL=provider-jsonrpc.d.ts.map