wrappers.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // import from provider.ts instead of index.ts to prevent circular dep
  2. // from EtherscanProvider
  3. import { Log, TransactionReceipt, TransactionResponse } from "../providers/provider.js";
  4. import { defineProperties, EventPayload } from "../utils/index.js";
  5. /**
  6. * An **EventLog** contains additional properties parsed from the [[Log]].
  7. */
  8. export class EventLog extends Log {
  9. /**
  10. * The Contract Interface.
  11. */
  12. interface;
  13. /**
  14. * The matching event.
  15. */
  16. fragment;
  17. /**
  18. * The parsed arguments passed to the event by ``emit``.
  19. */
  20. args;
  21. /**
  22. * @_ignore:
  23. */
  24. constructor(log, iface, fragment) {
  25. super(log, log.provider);
  26. const args = iface.decodeEventLog(fragment, log.data, log.topics);
  27. defineProperties(this, { args, fragment, interface: iface });
  28. }
  29. /**
  30. * The name of the event.
  31. */
  32. get eventName() { return this.fragment.name; }
  33. /**
  34. * The signature of the event.
  35. */
  36. get eventSignature() { return this.fragment.format(); }
  37. }
  38. /**
  39. * An **EventLog** contains additional properties parsed from the [[Log]].
  40. */
  41. export class UndecodedEventLog extends Log {
  42. /**
  43. * The error encounted when trying to decode the log.
  44. */
  45. error;
  46. /**
  47. * @_ignore:
  48. */
  49. constructor(log, error) {
  50. super(log, log.provider);
  51. defineProperties(this, { error });
  52. }
  53. }
  54. /**
  55. * A **ContractTransactionReceipt** includes the parsed logs from a
  56. * [[TransactionReceipt]].
  57. */
  58. export class ContractTransactionReceipt extends TransactionReceipt {
  59. #iface;
  60. /**
  61. * @_ignore:
  62. */
  63. constructor(iface, provider, tx) {
  64. super(tx, provider);
  65. this.#iface = iface;
  66. }
  67. /**
  68. * The parsed logs for any [[Log]] which has a matching event in the
  69. * Contract ABI.
  70. */
  71. get logs() {
  72. return super.logs.map((log) => {
  73. const fragment = log.topics.length ? this.#iface.getEvent(log.topics[0]) : null;
  74. if (fragment) {
  75. try {
  76. return new EventLog(log, this.#iface, fragment);
  77. }
  78. catch (error) {
  79. return new UndecodedEventLog(log, error);
  80. }
  81. }
  82. return log;
  83. });
  84. }
  85. }
  86. /**
  87. * A **ContractTransactionResponse** will return a
  88. * [[ContractTransactionReceipt]] when waited on.
  89. */
  90. export class ContractTransactionResponse extends TransactionResponse {
  91. #iface;
  92. /**
  93. * @_ignore:
  94. */
  95. constructor(iface, provider, tx) {
  96. super(tx, provider);
  97. this.#iface = iface;
  98. }
  99. /**
  100. * Resolves once this transaction has been mined and has
  101. * %%confirms%% blocks including it (default: ``1``) with an
  102. * optional %%timeout%%.
  103. *
  104. * This can resolve to ``null`` only if %%confirms%% is ``0``
  105. * and the transaction has not been mined, otherwise this will
  106. * wait until enough confirmations have completed.
  107. */
  108. async wait(confirms, timeout) {
  109. const receipt = await super.wait(confirms, timeout);
  110. if (receipt == null) {
  111. return null;
  112. }
  113. return new ContractTransactionReceipt(this.#iface, this.provider, receipt);
  114. }
  115. }
  116. /**
  117. * A **ContractUnknownEventPayload** is included as the last parameter to
  118. * Contract Events when the event does not match any events in the ABI.
  119. */
  120. export class ContractUnknownEventPayload extends EventPayload {
  121. /**
  122. * The log with no matching events.
  123. */
  124. log;
  125. /**
  126. * @_event:
  127. */
  128. constructor(contract, listener, filter, log) {
  129. super(contract, listener, filter);
  130. defineProperties(this, { log });
  131. }
  132. /**
  133. * Resolves to the block the event occured in.
  134. */
  135. async getBlock() {
  136. return await this.log.getBlock();
  137. }
  138. /**
  139. * Resolves to the transaction the event occured in.
  140. */
  141. async getTransaction() {
  142. return await this.log.getTransaction();
  143. }
  144. /**
  145. * Resolves to the transaction receipt the event occured in.
  146. */
  147. async getTransactionReceipt() {
  148. return await this.log.getTransactionReceipt();
  149. }
  150. }
  151. /**
  152. * A **ContractEventPayload** is included as the last parameter to
  153. * Contract Events when the event is known.
  154. */
  155. export class ContractEventPayload extends ContractUnknownEventPayload {
  156. /**
  157. * @_ignore:
  158. */
  159. constructor(contract, listener, filter, fragment, _log) {
  160. super(contract, listener, filter, new EventLog(_log, contract.interface, fragment));
  161. const args = contract.interface.decodeEventLog(fragment, this.log.data, this.log.topics);
  162. defineProperties(this, { args, fragment });
  163. }
  164. /**
  165. * The event name.
  166. */
  167. get eventName() {
  168. return this.fragment.name;
  169. }
  170. /**
  171. * The event signature.
  172. */
  173. get eventSignature() {
  174. return this.fragment.format();
  175. }
  176. }
  177. //# sourceMappingURL=wrappers.js.map