interface.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /**
  2. * The Interface class is a low-level class that accepts an
  3. * ABI and provides all the necessary functionality to encode
  4. * and decode paramaters to and results from methods, events
  5. * and errors.
  6. *
  7. * It also provides several convenience methods to automatically
  8. * search and find matching transactions and events to parse them.
  9. *
  10. * @_subsection api/abi:Interfaces [interfaces]
  11. */
  12. import { AbiCoder } from 'ethers';
  13. import { checkResultErrors, Result } from 'ethers';
  14. import { ConstructorFragment, ErrorFragment, EventFragment, FallbackFragment, Fragment, FunctionFragment, ParamType } from './fragments.js';
  15. import { Typed } from 'ethers';
  16. import type { BigNumberish, BytesLike, CallExceptionError, CallExceptionTransaction } from 'ethers';
  17. import type { JsonFragment } from 'ethers';
  18. export { checkResultErrors, Result };
  19. /**
  20. * When using the [[Interface-parseLog]] to automatically match a Log to its event
  21. * for parsing, a **LogDescription** is returned.
  22. */
  23. export declare class LogDescription {
  24. /**
  25. * The matching fragment for the ``topic0``.
  26. */
  27. readonly fragment: EventFragment;
  28. /**
  29. * The name of the Event.
  30. */
  31. readonly name: string;
  32. /**
  33. * The full Event signature.
  34. */
  35. readonly signature: string;
  36. /**
  37. * The topic hash for the Event.
  38. */
  39. readonly topic: string;
  40. /**
  41. * The arguments passed into the Event with ``emit``.
  42. */
  43. readonly args: Result;
  44. /**
  45. * @_ignore:
  46. */
  47. constructor(fragment: EventFragment, topic: string, args: Result);
  48. }
  49. /**
  50. * When using the [[Interface-parseTransaction]] to automatically match
  51. * a transaction data to its function for parsing,
  52. * a **TransactionDescription** is returned.
  53. */
  54. export declare class TransactionDescription {
  55. /**
  56. * The matching fragment from the transaction ``data``.
  57. */
  58. readonly fragment: FunctionFragment;
  59. /**
  60. * The name of the Function from the transaction ``data``.
  61. */
  62. readonly name: string;
  63. /**
  64. * The arguments passed to the Function from the transaction ``data``.
  65. */
  66. readonly args: Result;
  67. /**
  68. * The full Function signature from the transaction ``data``.
  69. */
  70. readonly signature: string;
  71. /**
  72. * The selector for the Function from the transaction ``data``.
  73. */
  74. readonly selector: string;
  75. /**
  76. * The ``value`` (in wei) from the transaction.
  77. */
  78. readonly value: bigint;
  79. /**
  80. * @_ignore:
  81. */
  82. constructor(fragment: FunctionFragment, selector: string, args: Result, value: bigint);
  83. }
  84. /**
  85. * When using the [[Interface-parseError]] to automatically match an
  86. * error for a call result for parsing, an **ErrorDescription** is returned.
  87. */
  88. export declare class ErrorDescription {
  89. /**
  90. * The matching fragment.
  91. */
  92. readonly fragment: ErrorFragment;
  93. /**
  94. * The name of the Error.
  95. */
  96. readonly name: string;
  97. /**
  98. * The arguments passed to the Error with ``revert``.
  99. */
  100. readonly args: Result;
  101. /**
  102. * The full Error signature.
  103. */
  104. readonly signature: string;
  105. /**
  106. * The selector for the Error.
  107. */
  108. readonly selector: string;
  109. /**
  110. * @_ignore:
  111. */
  112. constructor(fragment: ErrorFragment, selector: string, args: Result);
  113. }
  114. /**
  115. * An **Indexed** is used as a value when a value that does not
  116. * fit within a topic (i.e. not a fixed-length, 32-byte type). It
  117. * is the ``keccak256`` of the value, and used for types such as
  118. * arrays, tuples, bytes and strings.
  119. */
  120. export declare class Indexed {
  121. /**
  122. * The ``keccak256`` of the value logged.
  123. */
  124. readonly hash: null | string;
  125. /**
  126. * @_ignore:
  127. */
  128. readonly _isIndexed: boolean;
  129. /**
  130. * Returns ``true`` if %%value%% is an **Indexed**.
  131. *
  132. * This provides a Type Guard for property access.
  133. */
  134. static isIndexed(value: any): value is Indexed;
  135. /**
  136. * @_ignore:
  137. */
  138. constructor(hash: null | string);
  139. }
  140. /**
  141. * An **InterfaceAbi** may be any supported ABI format.
  142. *
  143. * A string is expected to be a JSON string, which will be parsed
  144. * using ``JSON.parse``. This means that the value **must** be a valid
  145. * JSON string, with no stray commas, etc.
  146. *
  147. * An array may contain any combination of:
  148. * - Human-Readable fragments
  149. * - Parsed JSON fragment
  150. * - [[Fragment]] instances
  151. *
  152. * A **Human-Readable Fragment** is a string which resembles a Solidity
  153. * signature and is introduced in [this blog entry](link-ricmoo-humanreadableabi).
  154. * For example, ``function balanceOf(address) view returns (uint)``.
  155. *
  156. * A **Parsed JSON Fragment** is a JavaScript Object desribed in the
  157. * [Solidity documentation](link-solc-jsonabi).
  158. */
  159. export type InterfaceAbi = string | ReadonlyArray<Fragment | JsonFragment | string>;
  160. /**
  161. * An Interface abstracts many of the low-level details for
  162. * encoding and decoding the data on the blockchain.
  163. *
  164. * An ABI provides information on how to encode data to send to
  165. * a Contract, how to decode the results and events and how to
  166. * interpret revert errors.
  167. *
  168. * The ABI can be specified by [any supported format](InterfaceAbi).
  169. */
  170. export declare class Interface {
  171. #private;
  172. /**
  173. * All the Contract ABI members (i.e. methods, events, errors, etc).
  174. */
  175. readonly fragments: ReadonlyArray<Fragment>;
  176. /**
  177. * The Contract constructor.
  178. */
  179. readonly deploy: ConstructorFragment;
  180. /**
  181. * The Fallback method, if any.
  182. */
  183. readonly fallback: null | FallbackFragment;
  184. /**
  185. * If receiving ether is supported.
  186. */
  187. readonly receive: boolean;
  188. /**
  189. * Create a new Interface for the %%fragments%%.
  190. */
  191. constructor(fragments: InterfaceAbi);
  192. /**
  193. * Returns the entire Human-Readable ABI, as an array of
  194. * signatures, optionally as %%minimal%% strings, which
  195. * removes parameter names and unneceesary spaces.
  196. */
  197. format(minimal?: boolean): Array<string>;
  198. /**
  199. * Return the JSON-encoded ABI. This is the format Solidiy
  200. * returns.
  201. */
  202. formatJson(): string;
  203. /**
  204. * The ABI coder that will be used to encode and decode binary
  205. * data.
  206. */
  207. getAbiCoder(): AbiCoder;
  208. /**
  209. * Get the function name for %%key%%, which may be a function selector,
  210. * function name or function signature that belongs to the ABI.
  211. */
  212. getFunctionName(key: string): string;
  213. /**
  214. * Returns true if %%key%% (a function selector, function name or
  215. * function signature) is present in the ABI.
  216. *
  217. * In the case of a function name, the name may be ambiguous, so
  218. * accessing the [[FunctionFragment]] may require refinement.
  219. */
  220. hasFunction(key: string): boolean;
  221. /**
  222. * Get the [[FunctionFragment]] for %%key%%, which may be a function
  223. * selector, function name or function signature that belongs to the ABI.
  224. *
  225. * If %%values%% is provided, it will use the Typed API to handle
  226. * ambiguous cases where multiple functions match by name.
  227. *
  228. * If the %%key%% and %%values%% do not refine to a single function in
  229. * the ABI, this will throw.
  230. */
  231. getFunction(key: string, values?: Array<any | Typed>): null | FunctionFragment;
  232. /**
  233. * Iterate over all functions, calling %%callback%%, sorted by their name.
  234. */
  235. forEachFunction(callback: (func: FunctionFragment, index: number) => void): void;
  236. /**
  237. * Get the event name for %%key%%, which may be a topic hash,
  238. * event name or event signature that belongs to the ABI.
  239. */
  240. getEventName(key: string): string;
  241. /**
  242. * Returns true if %%key%% (an event topic hash, event name or
  243. * event signature) is present in the ABI.
  244. *
  245. * In the case of an event name, the name may be ambiguous, so
  246. * accessing the [[EventFragment]] may require refinement.
  247. */
  248. hasEvent(key: string): boolean;
  249. /**
  250. * Get the [[EventFragment]] for %%key%%, which may be a topic hash,
  251. * event name or event signature that belongs to the ABI.
  252. *
  253. * If %%values%% is provided, it will use the Typed API to handle
  254. * ambiguous cases where multiple events match by name.
  255. *
  256. * If the %%key%% and %%values%% do not refine to a single event in
  257. * the ABI, this will throw.
  258. */
  259. getEvent(key: string, values?: Array<any | Typed>): null | EventFragment;
  260. /**
  261. * Iterate over all events, calling %%callback%%, sorted by their name.
  262. */
  263. forEachEvent(callback: (func: EventFragment, index: number) => void): void;
  264. /**
  265. * Get the [[ErrorFragment]] for %%key%%, which may be an error
  266. * selector, error name or error signature that belongs to the ABI.
  267. *
  268. * If %%values%% is provided, it will use the Typed API to handle
  269. * ambiguous cases where multiple errors match by name.
  270. *
  271. * If the %%key%% and %%values%% do not refine to a single error in
  272. * the ABI, this will throw.
  273. */
  274. getError(key: string, values?: Array<any | Typed>): null | ErrorFragment;
  275. /**
  276. * Iterate over all errors, calling %%callback%%, sorted by their name.
  277. */
  278. forEachError(callback: (func: ErrorFragment, index: number) => void): void;
  279. _decodeParams(params: ReadonlyArray<ParamType | string>, data: BytesLike): Result;
  280. _encodeParams(params: ReadonlyArray<ParamType>, values: ReadonlyArray<any>): string;
  281. /**
  282. * Encodes a ``tx.data`` object for deploying the Contract with
  283. * the %%values%% as the constructor arguments.
  284. */
  285. encodeDeploy(values?: ReadonlyArray<any>): string;
  286. /**
  287. * Decodes the result %%data%% (e.g. from an ``eth_call``) for the
  288. * specified error (see [[getError]] for valid values for
  289. * %%key%%).
  290. *
  291. * Most developers should prefer the [[parseCallResult]] method instead,
  292. * which will automatically detect a ``CALL_EXCEPTION`` and throw the
  293. * corresponding error.
  294. */
  295. decodeErrorResult(fragment: ErrorFragment | string, data: BytesLike): Result;
  296. /**
  297. * Encodes the transaction revert data for a call result that
  298. * reverted from the the Contract with the sepcified %%error%%
  299. * (see [[getError]] for valid values for %%fragment%%) with the %%values%%.
  300. *
  301. * This is generally not used by most developers, unless trying to mock
  302. * a result from a Contract.
  303. */
  304. encodeErrorResult(fragment: ErrorFragment | string, values?: ReadonlyArray<any>): string;
  305. /**
  306. * Decodes the %%data%% from a transaction ``tx.data`` for
  307. * the function specified (see [[getFunction]] for valid values
  308. * for %%fragment%%).
  309. *
  310. * Most developers should prefer the [[parseTransaction]] method
  311. * instead, which will automatically detect the fragment.
  312. */
  313. decodeFunctionData(fragment: FunctionFragment | string, data: BytesLike): Result;
  314. /**
  315. * Encodes the ``tx.data`` for a transaction that calls the function
  316. * specified (see [[getFunction]] for valid values for %%fragment%%) with
  317. * the %%values%%.
  318. */
  319. encodeFunctionData(fragment: FunctionFragment | string, values?: ReadonlyArray<any>): string;
  320. /**
  321. * Decodes the result %%data%% (e.g. from an ``eth_call``) for the
  322. * specified function (see [[getFunction]] for valid values for
  323. * %%key%%).
  324. *
  325. * Most developers should prefer the [[parseCallResult]] method instead,
  326. * which will automatically detect a ``CALL_EXCEPTION`` and throw the
  327. * corresponding error.
  328. */
  329. decodeFunctionResult(fragment: FunctionFragment | string, data: BytesLike): Result;
  330. makeError(_data: BytesLike, tx: CallExceptionTransaction): CallExceptionError;
  331. /**
  332. * Encodes the result data (e.g. from an ``eth_call``) for the
  333. * specified function (see [[getFunction]] for valid values
  334. * for %%fragment%%) with %%values%%.
  335. *
  336. * This is generally not used by most developers, unless trying to mock
  337. * a result from a Contract.
  338. */
  339. encodeFunctionResult(fragment: FunctionFragment | string, values?: ReadonlyArray<any>): string;
  340. encodeFilterTopics(fragment: EventFragment | string, values: ReadonlyArray<any>): Array<null | string | Array<string>>;
  341. encodeEventLog(fragment: EventFragment | string, values: ReadonlyArray<any>): {
  342. data: string;
  343. topics: Array<string>;
  344. };
  345. decodeEventLog(fragment: EventFragment | string, data: BytesLike, topics?: ReadonlyArray<string>): Result;
  346. /**
  347. * Parses a transaction, finding the matching function and extracts
  348. * the parameter values along with other useful function details.
  349. *
  350. * If the matching function cannot be found, return null.
  351. */
  352. parseTransaction(tx: {
  353. data: string;
  354. value?: BigNumberish;
  355. }): null | TransactionDescription;
  356. parseCallResult(data: BytesLike): Result;
  357. /**
  358. * Parses a receipt log, finding the matching event and extracts
  359. * the parameter values along with other useful event details.
  360. *
  361. * If the matching event cannot be found, returns null.
  362. */
  363. parseLog(log: {
  364. topics: Array<string>;
  365. data: string;
  366. }): null | LogDescription;
  367. /**
  368. * Parses a revert data, finding the matching error and extracts
  369. * the parameter values along with other useful error details.
  370. *
  371. * If the matching event cannot be found, returns null.
  372. */
  373. parseError(data: BytesLike): null | ErrorDescription;
  374. /**
  375. * Creates a new [[Interface]] from the ABI %%value%%.
  376. *
  377. * The %%value%% may be provided as an existing [[Interface]] object,
  378. * a JSON-encoded ABI or any Human-Readable ABI format.
  379. */
  380. static from(value: InterfaceAbi | Interface): Interface;
  381. }