abstract-provider.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * The available providers should suffice for most developers purposes,
  3. * but the [[AbstractProvider]] class has many features which enable
  4. * sub-classing it for specific purposes.
  5. *
  6. * @_section: api/providers/abstract-provider: Subclassing Provider [abstract-provider]
  7. */
  8. import { FetchRequest } from "../utils/index.js";
  9. import { EnsResolver } from "./ens-resolver.js";
  10. import { Network } from "./network.js";
  11. import { Block, FeeData, Log, TransactionReceipt, TransactionResponse } from "./provider.js";
  12. import type { AddressLike } from "../address/index.js";
  13. import type { BigNumberish } from "../utils/index.js";
  14. import type { Listener } from "../utils/index.js";
  15. import type { Networkish } from "./network.js";
  16. import type { BlockParams, LogParams, TransactionReceiptParams, TransactionResponseParams } from "./formatting.js";
  17. import type { BlockTag, EventFilter, Filter, FilterByBlockHash, OrphanFilter, PreparedTransactionRequest, Provider, ProviderEvent, TransactionRequest } from "./provider.js";
  18. /**
  19. * The types of additional event values that can be emitted for the
  20. * ``"debug"`` event.
  21. */
  22. export type DebugEventAbstractProvider = {
  23. action: "sendCcipReadFetchRequest";
  24. request: FetchRequest;
  25. index: number;
  26. urls: Array<string>;
  27. } | {
  28. action: "receiveCcipReadFetchResult";
  29. request: FetchRequest;
  30. result: any;
  31. } | {
  32. action: "receiveCcipReadFetchError";
  33. request: FetchRequest;
  34. result: any;
  35. } | {
  36. action: "sendCcipReadCall";
  37. transaction: {
  38. to: string;
  39. data: string;
  40. };
  41. } | {
  42. action: "receiveCcipReadCallResult";
  43. transaction: {
  44. to: string;
  45. data: string;
  46. };
  47. result: string;
  48. } | {
  49. action: "receiveCcipReadCallError";
  50. transaction: {
  51. to: string;
  52. data: string;
  53. };
  54. error: Error;
  55. };
  56. /**
  57. * The value passed to the [[AbstractProvider-_getSubscriber]] method.
  58. *
  59. * Only developers sub-classing [[AbstractProvider[[ will care about this,
  60. * if they are modifying a low-level feature of how subscriptions operate.
  61. */
  62. export type Subscription = {
  63. type: "block" | "close" | "debug" | "error" | "finalized" | "network" | "pending" | "safe";
  64. tag: string;
  65. } | {
  66. type: "transaction";
  67. tag: string;
  68. hash: string;
  69. } | {
  70. type: "event";
  71. tag: string;
  72. filter: EventFilter;
  73. } | {
  74. type: "orphan";
  75. tag: string;
  76. filter: OrphanFilter;
  77. };
  78. /**
  79. * A **Subscriber** manages a subscription.
  80. *
  81. * Only developers sub-classing [[AbstractProvider[[ will care about this,
  82. * if they are modifying a low-level feature of how subscriptions operate.
  83. */
  84. export interface Subscriber {
  85. /**
  86. * Called initially when a subscriber is added the first time.
  87. */
  88. start(): void;
  89. /**
  90. * Called when there are no more subscribers to the event.
  91. */
  92. stop(): void;
  93. /**
  94. * Called when the subscription should pause.
  95. *
  96. * If %%dropWhilePaused%%, events that occur while paused should not
  97. * be emitted [[resume]].
  98. */
  99. pause(dropWhilePaused?: boolean): void;
  100. /**
  101. * Resume a paused subscriber.
  102. */
  103. resume(): void;
  104. /**
  105. * The frequency (in ms) to poll for events, if polling is used by
  106. * the subscriber.
  107. *
  108. * For non-polling subscribers, this must return ``undefined``.
  109. */
  110. pollingInterval?: number;
  111. }
  112. /**
  113. * An **UnmanagedSubscriber** is useful for events which do not require
  114. * any additional management, such as ``"debug"`` which only requires
  115. * emit in synchronous event loop triggered calls.
  116. */
  117. export declare class UnmanagedSubscriber implements Subscriber {
  118. /**
  119. * The name fof the event.
  120. */
  121. name: string;
  122. /**
  123. * Create a new UnmanagedSubscriber with %%name%%.
  124. */
  125. constructor(name: string);
  126. start(): void;
  127. stop(): void;
  128. pause(dropWhilePaused?: boolean): void;
  129. resume(): void;
  130. }
  131. /**
  132. * An **AbstractPlugin** is used to provide additional internal services
  133. * to an [[AbstractProvider]] without adding backwards-incompatible changes
  134. * to method signatures or other internal and complex logic.
  135. */
  136. export interface AbstractProviderPlugin {
  137. /**
  138. * The reverse domain notation of the plugin.
  139. */
  140. readonly name: string;
  141. /**
  142. * Creates a new instance of the plugin, connected to %%provider%%.
  143. */
  144. connect(provider: AbstractProvider): AbstractProviderPlugin;
  145. }
  146. /**
  147. * A normalized filter used for [[PerformActionRequest]] objects.
  148. */
  149. export type PerformActionFilter = {
  150. address?: string | Array<string>;
  151. topics?: Array<null | string | Array<string>>;
  152. fromBlock?: BlockTag;
  153. toBlock?: BlockTag;
  154. } | {
  155. address?: string | Array<string>;
  156. topics?: Array<null | string | Array<string>>;
  157. blockHash?: string;
  158. };
  159. /**
  160. * A normalized transactions used for [[PerformActionRequest]] objects.
  161. */
  162. export interface PerformActionTransaction extends PreparedTransactionRequest {
  163. /**
  164. * The ``to`` address of the transaction.
  165. */
  166. to?: string;
  167. /**
  168. * The sender of the transaction.
  169. */
  170. from?: string;
  171. }
  172. /**
  173. * The [[AbstractProvider]] methods will normalize all values and pass this
  174. * type to [[AbstractProvider-_perform]].
  175. */
  176. export type PerformActionRequest = {
  177. method: "broadcastTransaction";
  178. signedTransaction: string;
  179. } | {
  180. method: "call";
  181. transaction: PerformActionTransaction;
  182. blockTag: BlockTag;
  183. } | {
  184. method: "chainId";
  185. } | {
  186. method: "estimateGas";
  187. transaction: PerformActionTransaction;
  188. } | {
  189. method: "getBalance";
  190. address: string;
  191. blockTag: BlockTag;
  192. } | {
  193. method: "getBlock";
  194. blockTag: BlockTag;
  195. includeTransactions: boolean;
  196. } | {
  197. method: "getBlock";
  198. blockHash: string;
  199. includeTransactions: boolean;
  200. } | {
  201. method: "getBlockNumber";
  202. } | {
  203. method: "getCode";
  204. address: string;
  205. blockTag: BlockTag;
  206. } | {
  207. method: "getGasPrice";
  208. } | {
  209. method: "getLogs";
  210. filter: PerformActionFilter;
  211. } | {
  212. method: "getPriorityFee";
  213. } | {
  214. method: "getStorage";
  215. address: string;
  216. position: bigint;
  217. blockTag: BlockTag;
  218. } | {
  219. method: "getTransaction";
  220. hash: string;
  221. } | {
  222. method: "getTransactionCount";
  223. address: string;
  224. blockTag: BlockTag;
  225. } | {
  226. method: "getTransactionReceipt";
  227. hash: string;
  228. } | {
  229. method: "getTransactionResult";
  230. hash: string;
  231. };
  232. /**
  233. * Options for configuring some internal aspects of an [[AbstractProvider]].
  234. *
  235. * **``cacheTimeout``** - how long to cache a low-level ``_perform``
  236. * for, based on input parameters. This reduces the number of calls
  237. * to getChainId and getBlockNumber, but may break test chains which
  238. * can perform operations (internally) synchronously. Use ``-1`` to
  239. * disable, ``0`` will only buffer within the same event loop and
  240. * any other value is in ms. (default: ``250``)
  241. */
  242. export type AbstractProviderOptions = {
  243. cacheTimeout?: number;
  244. pollingInterval?: number;
  245. };
  246. /**
  247. * An **AbstractProvider** provides a base class for other sub-classes to
  248. * implement the [[Provider]] API by normalizing input arguments and
  249. * formatting output results as well as tracking events for consistent
  250. * behaviour on an eventually-consistent network.
  251. */
  252. export declare class AbstractProvider implements Provider {
  253. #private;
  254. /**
  255. * Create a new **AbstractProvider** connected to %%network%%, or
  256. * use the various network detection capabilities to discover the
  257. * [[Network]] if necessary.
  258. */
  259. constructor(_network?: "any" | Networkish, options?: AbstractProviderOptions);
  260. get pollingInterval(): number;
  261. /**
  262. * Returns ``this``, to allow an **AbstractProvider** to implement
  263. * the [[ContractRunner]] interface.
  264. */
  265. get provider(): this;
  266. /**
  267. * Returns all the registered plug-ins.
  268. */
  269. get plugins(): Array<AbstractProviderPlugin>;
  270. /**
  271. * Attach a new plug-in.
  272. */
  273. attachPlugin(plugin: AbstractProviderPlugin): this;
  274. /**
  275. * Get a plugin by name.
  276. */
  277. getPlugin<T extends AbstractProviderPlugin = AbstractProviderPlugin>(name: string): null | T;
  278. /**
  279. * Prevent any CCIP-read operation, regardless of whether requested
  280. * in a [[call]] using ``enableCcipRead``.
  281. */
  282. get disableCcipRead(): boolean;
  283. set disableCcipRead(value: boolean);
  284. /**
  285. * Resolves to the data for executing the CCIP-read operations.
  286. */
  287. ccipReadFetch(tx: PerformActionTransaction, calldata: string, urls: Array<string>): Promise<null | string>;
  288. /**
  289. * Provides the opportunity for a sub-class to wrap a block before
  290. * returning it, to add additional properties or an alternate
  291. * sub-class of [[Block]].
  292. */
  293. _wrapBlock(value: BlockParams, network: Network): Block;
  294. /**
  295. * Provides the opportunity for a sub-class to wrap a log before
  296. * returning it, to add additional properties or an alternate
  297. * sub-class of [[Log]].
  298. */
  299. _wrapLog(value: LogParams, network: Network): Log;
  300. /**
  301. * Provides the opportunity for a sub-class to wrap a transaction
  302. * receipt before returning it, to add additional properties or an
  303. * alternate sub-class of [[TransactionReceipt]].
  304. */
  305. _wrapTransactionReceipt(value: TransactionReceiptParams, network: Network): TransactionReceipt;
  306. /**
  307. * Provides the opportunity for a sub-class to wrap a transaction
  308. * response before returning it, to add additional properties or an
  309. * alternate sub-class of [[TransactionResponse]].
  310. */
  311. _wrapTransactionResponse(tx: TransactionResponseParams, network: Network): TransactionResponse;
  312. /**
  313. * Resolves to the Network, forcing a network detection using whatever
  314. * technique the sub-class requires.
  315. *
  316. * Sub-classes **must** override this.
  317. */
  318. _detectNetwork(): Promise<Network>;
  319. /**
  320. * Sub-classes should use this to perform all built-in operations. All
  321. * methods sanitizes and normalizes the values passed into this.
  322. *
  323. * Sub-classes **must** override this.
  324. */
  325. _perform<T = any>(req: PerformActionRequest): Promise<T>;
  326. getBlockNumber(): Promise<number>;
  327. /**
  328. * Returns or resolves to the address for %%address%%, resolving ENS
  329. * names and [[Addressable]] objects and returning if already an
  330. * address.
  331. */
  332. _getAddress(address: AddressLike): string | Promise<string>;
  333. /**
  334. * Returns or resolves to a valid block tag for %%blockTag%%, resolving
  335. * negative values and returning if already a valid block tag.
  336. */
  337. _getBlockTag(blockTag?: BlockTag): string | Promise<string>;
  338. /**
  339. * Returns or resolves to a filter for %%filter%%, resolving any ENS
  340. * names or [[Addressable]] object and returning if already a valid
  341. * filter.
  342. */
  343. _getFilter(filter: Filter | FilterByBlockHash): PerformActionFilter | Promise<PerformActionFilter>;
  344. /**
  345. * Returns or resolves to a transaction for %%request%%, resolving
  346. * any ENS names or [[Addressable]] and returning if already a valid
  347. * transaction.
  348. */
  349. _getTransactionRequest(_request: TransactionRequest): PerformActionTransaction | Promise<PerformActionTransaction>;
  350. getNetwork(): Promise<Network>;
  351. getFeeData(): Promise<FeeData>;
  352. estimateGas(_tx: TransactionRequest): Promise<bigint>;
  353. call(_tx: TransactionRequest): Promise<string>;
  354. getBalance(address: AddressLike, blockTag?: BlockTag): Promise<bigint>;
  355. getTransactionCount(address: AddressLike, blockTag?: BlockTag): Promise<number>;
  356. getCode(address: AddressLike, blockTag?: BlockTag): Promise<string>;
  357. getStorage(address: AddressLike, _position: BigNumberish, blockTag?: BlockTag): Promise<string>;
  358. broadcastTransaction(signedTx: string): Promise<TransactionResponse>;
  359. getBlock(block: BlockTag | string, prefetchTxs?: boolean): Promise<null | Block>;
  360. getTransaction(hash: string): Promise<null | TransactionResponse>;
  361. getTransactionReceipt(hash: string): Promise<null | TransactionReceipt>;
  362. getTransactionResult(hash: string): Promise<null | string>;
  363. getLogs(_filter: Filter | FilterByBlockHash): Promise<Array<Log>>;
  364. _getProvider(chainId: number): AbstractProvider;
  365. getResolver(name: string): Promise<null | EnsResolver>;
  366. getAvatar(name: string): Promise<null | string>;
  367. resolveName(name: string): Promise<null | string>;
  368. lookupAddress(address: string): Promise<null | string>;
  369. waitForTransaction(hash: string, _confirms?: null | number, timeout?: null | number): Promise<null | TransactionReceipt>;
  370. waitForBlock(blockTag?: BlockTag): Promise<Block>;
  371. /**
  372. * Clear a timer created using the [[_setTimeout]] method.
  373. */
  374. _clearTimeout(timerId: number): void;
  375. /**
  376. * Create a timer that will execute %%func%% after at least %%timeout%%
  377. * (in ms). If %%timeout%% is unspecified, then %%func%% will execute
  378. * in the next event loop.
  379. *
  380. * [Pausing](AbstractProvider-paused) the provider will pause any
  381. * associated timers.
  382. */
  383. _setTimeout(_func: () => void, timeout?: number): number;
  384. /**
  385. * Perform %%func%% on each subscriber.
  386. */
  387. _forEachSubscriber(func: (s: Subscriber) => void): void;
  388. /**
  389. * Sub-classes may override this to customize subscription
  390. * implementations.
  391. */
  392. _getSubscriber(sub: Subscription): Subscriber;
  393. /**
  394. * If a [[Subscriber]] fails and needs to replace itself, this
  395. * method may be used.
  396. *
  397. * For example, this is used for providers when using the
  398. * ``eth_getFilterChanges`` method, which can return null if state
  399. * filters are not supported by the backend, allowing the Subscriber
  400. * to swap in a [[PollingEventSubscriber]].
  401. */
  402. _recoverSubscriber(oldSub: Subscriber, newSub: Subscriber): void;
  403. on(event: ProviderEvent, listener: Listener): Promise<this>;
  404. once(event: ProviderEvent, listener: Listener): Promise<this>;
  405. emit(event: ProviderEvent, ...args: Array<any>): Promise<boolean>;
  406. listenerCount(event?: ProviderEvent): Promise<number>;
  407. listeners(event?: ProviderEvent): Promise<Array<Listener>>;
  408. off(event: ProviderEvent, listener?: Listener): Promise<this>;
  409. removeAllListeners(event?: ProviderEvent): Promise<this>;
  410. addListener(event: ProviderEvent, listener: Listener): Promise<this>;
  411. removeListener(event: ProviderEvent, listener: Listener): Promise<this>;
  412. /**
  413. * If this provider has been destroyed using the [[destroy]] method.
  414. *
  415. * Once destroyed, all resources are reclaimed, internal event loops
  416. * and timers are cleaned up and no further requests may be sent to
  417. * the provider.
  418. */
  419. get destroyed(): boolean;
  420. /**
  421. * Sub-classes may use this to shutdown any sockets or release their
  422. * resources and reject any pending requests.
  423. *
  424. * Sub-classes **must** call ``super.destroy()``.
  425. */
  426. destroy(): void;
  427. /**
  428. * Whether the provider is currently paused.
  429. *
  430. * A paused provider will not emit any events, and generally should
  431. * not make any requests to the network, but that is up to sub-classes
  432. * to manage.
  433. *
  434. * Setting ``paused = true`` is identical to calling ``.pause(false)``,
  435. * which will buffer any events that occur while paused until the
  436. * provider is unpaused.
  437. */
  438. get paused(): boolean;
  439. set paused(pause: boolean);
  440. /**
  441. * Pause the provider. If %%dropWhilePaused%%, any events that occur
  442. * while paused are dropped, otherwise all events will be emitted once
  443. * the provider is unpaused.
  444. */
  445. pause(dropWhilePaused?: boolean): void;
  446. /**
  447. * Resume the provider.
  448. */
  449. resume(): void;
  450. }
  451. //# sourceMappingURL=abstract-provider.d.ts.map