ethers.js/lib.esm/contract/wrappers.js

177 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-12-04 01:01:29 +03:00
// import from provider.ts instead of index.ts to prevent circular dep
// from EtherscanProvider
import { Log, TransactionReceipt, TransactionResponse } from "../providers/provider.js";
2022-09-05 23:57:11 +03:00
import { defineProperties, EventPayload } from "../utils/index.js";
2023-06-02 00:52:58 +03:00
/**
* An **EventLog** contains additional properties parsed from the [[Log]].
*/
2022-09-05 23:57:11 +03:00
export class EventLog extends Log {
2023-06-02 00:52:58 +03:00
/**
* The Contract Interface.
*/
2022-09-05 23:57:11 +03:00
interface;
2023-06-02 00:52:58 +03:00
/**
* The matching event.
*/
2022-09-05 23:57:11 +03:00
fragment;
2023-06-02 00:52:58 +03:00
/**
* The parsed arguments passed to the event by ``emit``.
*/
2022-09-05 23:57:11 +03:00
args;
2023-06-02 00:52:58 +03:00
/**
* @_ignore:
*/
2022-09-05 23:57:11 +03:00
constructor(log, iface, fragment) {
super(log, log.provider);
const args = iface.decodeEventLog(fragment, log.data, log.topics);
defineProperties(this, { args, fragment, interface: iface });
}
2023-06-02 00:52:58 +03:00
/**
* The name of the event.
*/
2022-09-05 23:57:11 +03:00
get eventName() { return this.fragment.name; }
2023-06-02 00:52:58 +03:00
/**
* The signature of the event.
*/
2022-09-05 23:57:11 +03:00
get eventSignature() { return this.fragment.format(); }
}
2023-08-03 06:42:17 +03:00
/**
* An **EventLog** contains additional properties parsed from the [[Log]].
*/
export class UndecodedEventLog extends Log {
/**
* The error encounted when trying to decode the log.
*/
error;
/**
* @_ignore:
*/
constructor(log, error) {
super(log, log.provider);
defineProperties(this, { error });
}
}
2023-06-02 00:52:58 +03:00
/**
* A **ContractTransactionReceipt** includes the parsed logs from a
* [[TransactionReceipt]].
*/
2022-09-05 23:57:11 +03:00
export class ContractTransactionReceipt extends TransactionReceipt {
2023-03-20 19:53:37 +03:00
#iface;
2023-06-02 00:52:58 +03:00
/**
* @_ignore:
*/
2022-09-05 23:57:11 +03:00
constructor(iface, provider, tx) {
super(tx, provider);
2023-03-20 19:53:37 +03:00
this.#iface = iface;
2022-09-05 23:57:11 +03:00
}
2023-06-02 00:52:58 +03:00
/**
* The parsed logs for any [[Log]] which has a matching event in the
* Contract ABI.
*/
2022-09-05 23:57:11 +03:00
get logs() {
return super.logs.map((log) => {
2023-03-20 19:53:37 +03:00
const fragment = log.topics.length ? this.#iface.getEvent(log.topics[0]) : null;
2022-09-05 23:57:11 +03:00
if (fragment) {
2023-07-28 21:50:56 +03:00
try {
return new EventLog(log, this.#iface, fragment);
}
2023-08-03 06:42:17 +03:00
catch (error) {
return new UndecodedEventLog(log, error);
}
2022-09-05 23:57:11 +03:00
}
2023-07-28 21:50:56 +03:00
return log;
2022-09-05 23:57:11 +03:00
});
}
}
2023-06-02 00:52:58 +03:00
/**
* A **ContractTransactionResponse** will return a
* [[ContractTransactionReceipt]] when waited on.
*/
2022-09-05 23:57:11 +03:00
export class ContractTransactionResponse extends TransactionResponse {
2023-03-20 19:53:37 +03:00
#iface;
2023-06-02 00:52:58 +03:00
/**
* @_ignore:
*/
2022-09-05 23:57:11 +03:00
constructor(iface, provider, tx) {
super(tx, provider);
2023-03-20 19:53:37 +03:00
this.#iface = iface;
2022-09-05 23:57:11 +03:00
}
2023-06-02 00:52:58 +03:00
/**
* Resolves once this transaction has been mined and has
* %%confirms%% blocks including it (default: ``1``) with an
* optional %%timeout%%.
*
* This can resolve to ``null`` only if %%confirms%% is ``0``
* and the transaction has not been mined, otherwise this will
* wait until enough confirmations have completed.
*/
2024-02-02 11:25:03 +03:00
async wait(confirms, timeout) {
const receipt = await super.wait(confirms, timeout);
2022-09-05 23:57:11 +03:00
if (receipt == null) {
return null;
}
2023-03-20 19:53:37 +03:00
return new ContractTransactionReceipt(this.#iface, this.provider, receipt);
2022-09-05 23:57:11 +03:00
}
}
2023-06-02 00:52:58 +03:00
/**
* A **ContractUnknownEventPayload** is included as the last parameter to
* Contract Events when the event does not match any events in the ABI.
*/
2022-11-09 10:57:02 +03:00
export class ContractUnknownEventPayload extends EventPayload {
2023-06-02 00:52:58 +03:00
/**
* The log with no matching events.
*/
2022-09-05 23:57:11 +03:00
log;
2023-06-02 00:52:58 +03:00
/**
* @_event:
*/
2022-11-09 10:57:02 +03:00
constructor(contract, listener, filter, log) {
2022-09-05 23:57:11 +03:00
super(contract, listener, filter);
2022-11-09 10:57:02 +03:00
defineProperties(this, { log });
2022-09-05 23:57:11 +03:00
}
2023-06-02 00:52:58 +03:00
/**
* Resolves to the block the event occured in.
*/
2022-09-05 23:57:11 +03:00
async getBlock() {
return await this.log.getBlock();
}
2023-06-02 00:52:58 +03:00
/**
* Resolves to the transaction the event occured in.
*/
2022-09-05 23:57:11 +03:00
async getTransaction() {
return await this.log.getTransaction();
}
2023-06-02 00:52:58 +03:00
/**
* Resolves to the transaction receipt the event occured in.
*/
2022-09-05 23:57:11 +03:00
async getTransactionReceipt() {
return await this.log.getTransactionReceipt();
}
}
2023-06-02 00:52:58 +03:00
/**
* A **ContractEventPayload** is included as the last parameter to
* Contract Events when the event is known.
*/
2022-11-09 10:57:02 +03:00
export class ContractEventPayload extends ContractUnknownEventPayload {
2023-06-02 00:52:58 +03:00
/**
* @_ignore:
*/
2022-11-09 10:57:02 +03:00
constructor(contract, listener, filter, fragment, _log) {
super(contract, listener, filter, new EventLog(_log, contract.interface, fragment));
const args = contract.interface.decodeEventLog(fragment, this.log.data, this.log.topics);
defineProperties(this, { args, fragment });
}
2023-06-02 00:52:58 +03:00
/**
* The event name.
*/
2022-11-09 10:57:02 +03:00
get eventName() {
return this.fragment.name;
}
2023-06-02 00:52:58 +03:00
/**
* The event signature.
*/
2022-11-09 10:57:02 +03:00
get eventSignature() {
return this.fragment.format();
}
}
2022-09-05 23:57:11 +03:00
//# sourceMappingURL=wrappers.js.map