2022-09-05 23:57:11 +03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.Contract = exports.BaseContract = exports.resolveArgs = exports.copyOverrides = void 0;
|
2022-09-16 05:58:45 +03:00
|
|
|
const index_js_1 = require("../abi/index.js");
|
|
|
|
const index_js_2 = require("../address/index.js");
|
2022-12-04 01:01:29 +03:00
|
|
|
// import from provider.ts instead of index.ts to prevent circular dep
|
|
|
|
// from EtherscanProvider
|
|
|
|
const provider_js_1 = require("../providers/provider.js");
|
|
|
|
const index_js_3 = require("../utils/index.js");
|
2022-09-05 23:57:11 +03:00
|
|
|
const wrappers_js_1 = require("./wrappers.js");
|
2023-01-31 06:29:09 +03:00
|
|
|
const BN_0 = BigInt(0);
|
2022-09-05 23:57:11 +03:00
|
|
|
function canCall(value) {
|
|
|
|
return (value && typeof (value.call) === "function");
|
|
|
|
}
|
|
|
|
function canEstimate(value) {
|
|
|
|
return (value && typeof (value.estimateGas) === "function");
|
|
|
|
}
|
|
|
|
function canResolve(value) {
|
|
|
|
return (value && typeof (value.resolveName) === "function");
|
|
|
|
}
|
|
|
|
function canSend(value) {
|
|
|
|
return (value && typeof (value.sendTransaction) === "function");
|
|
|
|
}
|
|
|
|
class PreparedTopicFilter {
|
|
|
|
#filter;
|
|
|
|
fragment;
|
|
|
|
constructor(contract, fragment, args) {
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.defineProperties)(this, { fragment });
|
2022-09-05 23:57:11 +03:00
|
|
|
if (fragment.inputs.length < args.length) {
|
|
|
|
throw new Error("too many arguments");
|
|
|
|
}
|
|
|
|
// Recursively descend into args and resolve any addresses
|
|
|
|
const runner = getRunner(contract.runner, "resolveName");
|
|
|
|
const resolver = canResolve(runner) ? runner : null;
|
|
|
|
this.#filter = (async function () {
|
|
|
|
const resolvedArgs = await Promise.all(fragment.inputs.map((param, index) => {
|
2023-01-15 16:03:21 +03:00
|
|
|
const arg = args[index];
|
|
|
|
if (arg == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
return param.walkAsync(args[index], (type, value) => {
|
|
|
|
if (type === "address") {
|
2022-09-16 05:58:45 +03:00
|
|
|
return (0, index_js_2.resolveAddress)(value, resolver);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
return contract.interface.encodeFilterTopics(fragment, resolvedArgs);
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
getTopicFilter() {
|
|
|
|
return this.#filter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// A = Arguments passed in as a tuple
|
|
|
|
// R = The result type of the call (i.e. if only one return type,
|
|
|
|
// the qualified type, otherwise Result)
|
|
|
|
// D = The type the default call will return (i.e. R for view/pure,
|
|
|
|
// TransactionResponse otherwise)
|
|
|
|
//export interface ContractMethod<A extends Array<any> = Array<any>, R = any, D extends R | ContractTransactionResponse = ContractTransactionResponse> {
|
|
|
|
function getRunner(value, feature) {
|
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof (value[feature]) === "function") {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.provider && typeof (value.provider[feature]) === "function") {
|
|
|
|
return value.provider;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
function getProvider(value) {
|
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return value.provider || null;
|
|
|
|
}
|
2023-01-27 07:36:26 +03:00
|
|
|
/**
|
|
|
|
* @_ignore:
|
|
|
|
*/
|
2023-01-31 06:29:09 +03:00
|
|
|
async function copyOverrides(arg, allowed) {
|
2022-09-05 23:57:11 +03:00
|
|
|
// Create a shallow copy (we'll deep-ify anything needed during normalizing)
|
2022-12-04 01:01:29 +03:00
|
|
|
const overrides = (0, provider_js_1.copyRequest)(index_js_1.Typed.dereference(arg, "overrides"));
|
2023-01-31 06:29:09 +03:00
|
|
|
(0, index_js_3.assertArgument)(overrides.to == null || (allowed || []).indexOf("to") >= 0, "cannot override to", "overrides.to", overrides.to);
|
|
|
|
(0, index_js_3.assertArgument)(overrides.data == null || (allowed || []).indexOf("data") >= 0, "cannot override data", "overrides.data", overrides.data);
|
2022-09-05 23:57:11 +03:00
|
|
|
// Resolve any from
|
|
|
|
if (overrides.from) {
|
2022-09-16 05:58:45 +03:00
|
|
|
overrides.from = await (0, index_js_2.resolveAddress)(overrides.from);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
return overrides;
|
|
|
|
}
|
|
|
|
exports.copyOverrides = copyOverrides;
|
2023-01-27 07:36:26 +03:00
|
|
|
/**
|
|
|
|
* @_ignore:
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
async function resolveArgs(_runner, inputs, args) {
|
|
|
|
// Recursively descend into args and resolve any addresses
|
|
|
|
const runner = getRunner(_runner, "resolveName");
|
|
|
|
const resolver = canResolve(runner) ? runner : null;
|
|
|
|
return await Promise.all(inputs.map((param, index) => {
|
|
|
|
return param.walkAsync(args[index], (type, value) => {
|
2022-11-10 06:45:17 +03:00
|
|
|
value = index_js_1.Typed.dereference(value, type);
|
2022-09-05 23:57:11 +03:00
|
|
|
if (type === "address") {
|
2022-09-16 05:58:45 +03:00
|
|
|
return (0, index_js_2.resolveAddress)(value, resolver);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
exports.resolveArgs = resolveArgs;
|
2023-03-20 19:53:37 +03:00
|
|
|
function buildWrappedFallback(contract) {
|
|
|
|
const populateTransaction = async function (overrides) {
|
|
|
|
// If an overrides was passed in, copy it and normalize the values
|
|
|
|
const tx = (await copyOverrides(overrides, ["data"]));
|
|
|
|
tx.to = await contract.getAddress();
|
|
|
|
const iface = contract.interface;
|
|
|
|
// Only allow payable contracts to set non-zero value
|
|
|
|
const payable = iface.receive || (iface.fallback && iface.fallback.payable);
|
|
|
|
(0, index_js_3.assertArgument)(payable || (tx.value || BN_0) === BN_0, "cannot send value to non-payable contract", "overrides.value", tx.value);
|
|
|
|
// Only allow fallback contracts to set non-empty data
|
|
|
|
(0, index_js_3.assertArgument)(iface.fallback || (tx.data || "0x") === "0x", "cannot send data to receive-only contract", "overrides.data", tx.data);
|
|
|
|
return tx;
|
|
|
|
};
|
|
|
|
const staticCall = async function (overrides) {
|
|
|
|
const runner = getRunner(contract.runner, "call");
|
|
|
|
(0, index_js_3.assert)(canCall(runner), "contract runner does not support calling", "UNSUPPORTED_OPERATION", { operation: "call" });
|
|
|
|
const tx = await populateTransaction(overrides);
|
|
|
|
try {
|
|
|
|
return await runner.call(tx);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
if ((0, index_js_3.isCallException)(error) && error.data) {
|
|
|
|
throw contract.interface.makeError(error.data, tx);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const send = async function (overrides) {
|
|
|
|
const runner = contract.runner;
|
|
|
|
(0, index_js_3.assert)(canSend(runner), "contract runner does not support sending transactions", "UNSUPPORTED_OPERATION", { operation: "sendTransaction" });
|
|
|
|
const tx = await runner.sendTransaction(await populateTransaction(overrides));
|
|
|
|
const provider = getProvider(contract.runner);
|
|
|
|
// @TODO: the provider can be null; make a custom dummy provider that will throw a
|
|
|
|
// meaningful error
|
|
|
|
return new wrappers_js_1.ContractTransactionResponse(contract.interface, provider, tx);
|
|
|
|
};
|
|
|
|
const estimateGas = async function (overrides) {
|
|
|
|
const runner = getRunner(contract.runner, "estimateGas");
|
|
|
|
(0, index_js_3.assert)(canEstimate(runner), "contract runner does not support gas estimation", "UNSUPPORTED_OPERATION", { operation: "estimateGas" });
|
|
|
|
return await runner.estimateGas(await populateTransaction(overrides));
|
|
|
|
};
|
|
|
|
const method = async (overrides) => {
|
|
|
|
return await send(overrides);
|
|
|
|
};
|
|
|
|
(0, index_js_3.defineProperties)(method, {
|
|
|
|
_contract: contract,
|
|
|
|
estimateGas,
|
|
|
|
populateTransaction,
|
|
|
|
send, staticCall
|
|
|
|
});
|
|
|
|
return method;
|
|
|
|
}
|
|
|
|
function buildWrappedMethod(contract, key) {
|
|
|
|
const getFragment = function (...args) {
|
|
|
|
const fragment = contract.interface.getFunction(key, args);
|
2023-01-28 01:29:49 +03:00
|
|
|
(0, index_js_3.assert)(fragment, "no matching fragment", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "fragment"
|
|
|
|
});
|
|
|
|
return fragment;
|
2023-03-20 19:53:37 +03:00
|
|
|
};
|
|
|
|
const populateTransaction = async function (...args) {
|
|
|
|
const fragment = getFragment(...args);
|
2022-09-05 23:57:11 +03:00
|
|
|
// If an overrides was passed in, copy it and normalize the values
|
|
|
|
let overrides = {};
|
|
|
|
if (fragment.inputs.length + 1 === args.length) {
|
|
|
|
overrides = await copyOverrides(args.pop());
|
|
|
|
}
|
|
|
|
if (fragment.inputs.length !== args.length) {
|
|
|
|
throw new Error("internal error: fragment inputs doesn't match arguments; should not happen");
|
|
|
|
}
|
2023-03-20 19:53:37 +03:00
|
|
|
const resolvedArgs = await resolveArgs(contract.runner, fragment.inputs, args);
|
2022-12-04 01:01:29 +03:00
|
|
|
return Object.assign({}, overrides, await (0, index_js_3.resolveProperties)({
|
2023-03-20 19:53:37 +03:00
|
|
|
to: contract.getAddress(),
|
|
|
|
data: contract.interface.encodeFunctionData(fragment, resolvedArgs)
|
2022-09-05 23:57:11 +03:00
|
|
|
}));
|
2023-03-20 19:53:37 +03:00
|
|
|
};
|
|
|
|
const staticCall = async function (...args) {
|
|
|
|
const result = await staticCallResult(...args);
|
2022-09-05 23:57:11 +03:00
|
|
|
if (result.length === 1) {
|
|
|
|
return result[0];
|
|
|
|
}
|
|
|
|
return result;
|
2023-03-20 19:53:37 +03:00
|
|
|
};
|
|
|
|
const send = async function (...args) {
|
|
|
|
const runner = contract.runner;
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(canSend(runner), "contract runner does not support sending transactions", "UNSUPPORTED_OPERATION", { operation: "sendTransaction" });
|
2023-03-20 19:53:37 +03:00
|
|
|
const tx = await runner.sendTransaction(await populateTransaction(...args));
|
|
|
|
const provider = getProvider(contract.runner);
|
2022-09-27 10:45:27 +03:00
|
|
|
// @TODO: the provider can be null; make a custom dummy provider that will throw a
|
|
|
|
// meaningful error
|
2023-03-20 19:53:37 +03:00
|
|
|
return new wrappers_js_1.ContractTransactionResponse(contract.interface, provider, tx);
|
|
|
|
};
|
|
|
|
const estimateGas = async function (...args) {
|
|
|
|
const runner = getRunner(contract.runner, "estimateGas");
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(canEstimate(runner), "contract runner does not support gas estimation", "UNSUPPORTED_OPERATION", { operation: "estimateGas" });
|
2023-03-20 19:53:37 +03:00
|
|
|
return await runner.estimateGas(await populateTransaction(...args));
|
|
|
|
};
|
|
|
|
const staticCallResult = async function (...args) {
|
|
|
|
const runner = getRunner(contract.runner, "call");
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(canCall(runner), "contract runner does not support calling", "UNSUPPORTED_OPERATION", { operation: "call" });
|
2023-03-20 19:53:37 +03:00
|
|
|
const tx = await populateTransaction(...args);
|
2022-09-05 23:57:11 +03:00
|
|
|
let result = "0x";
|
|
|
|
try {
|
|
|
|
result = await runner.call(tx);
|
|
|
|
}
|
|
|
|
catch (error) {
|
2022-12-04 01:01:29 +03:00
|
|
|
if ((0, index_js_3.isCallException)(error) && error.data) {
|
2023-03-20 19:53:37 +03:00
|
|
|
throw contract.interface.makeError(error.data, tx);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2023-03-20 19:53:37 +03:00
|
|
|
const fragment = getFragment(...args);
|
|
|
|
return contract.interface.decodeFunctionResult(fragment, result);
|
|
|
|
};
|
|
|
|
const method = async (...args) => {
|
|
|
|
const fragment = getFragment(...args);
|
|
|
|
if (fragment.constant) {
|
|
|
|
return await staticCall(...args);
|
|
|
|
}
|
|
|
|
return await send(...args);
|
|
|
|
};
|
|
|
|
(0, index_js_3.defineProperties)(method, {
|
|
|
|
name: contract.interface.getFunctionName(key),
|
|
|
|
_contract: contract, _key: key,
|
|
|
|
getFragment,
|
|
|
|
estimateGas,
|
|
|
|
populateTransaction,
|
|
|
|
send, staticCall, staticCallResult,
|
|
|
|
});
|
|
|
|
// Only works on non-ambiguous keys (refined fragment is always non-ambiguous)
|
|
|
|
Object.defineProperty(method, "fragment", {
|
|
|
|
configurable: false,
|
2023-03-20 20:33:56 +03:00
|
|
|
enumerable: true,
|
2023-03-20 19:53:37 +03:00
|
|
|
get: () => {
|
|
|
|
const fragment = contract.interface.getFunction(key);
|
|
|
|
(0, index_js_3.assert)(fragment, "no matching fragment", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "fragment"
|
|
|
|
});
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return method;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2023-03-20 19:53:37 +03:00
|
|
|
function buildWrappedEvent(contract, key) {
|
|
|
|
const getFragment = function (...args) {
|
|
|
|
const fragment = contract.interface.getEvent(key, args);
|
2023-01-28 01:29:49 +03:00
|
|
|
(0, index_js_3.assert)(fragment, "no matching fragment", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "fragment"
|
|
|
|
});
|
|
|
|
return fragment;
|
2023-03-20 19:53:37 +03:00
|
|
|
};
|
2023-03-20 20:33:56 +03:00
|
|
|
const method = function (...args) {
|
2023-03-20 19:53:37 +03:00
|
|
|
return new PreparedTopicFilter(contract, getFragment(...args), args);
|
|
|
|
};
|
|
|
|
(0, index_js_3.defineProperties)(method, {
|
|
|
|
name: contract.interface.getEventName(key),
|
|
|
|
_contract: contract, _key: key,
|
|
|
|
getFragment
|
|
|
|
});
|
|
|
|
// Only works on non-ambiguous keys (refined fragment is always non-ambiguous)
|
|
|
|
Object.defineProperty(method, "fragment", {
|
|
|
|
configurable: false,
|
2023-03-20 20:33:56 +03:00
|
|
|
enumerable: true,
|
2023-03-20 19:53:37 +03:00
|
|
|
get: () => {
|
|
|
|
const fragment = contract.interface.getEvent(key);
|
|
|
|
(0, index_js_3.assert)(fragment, "no matching fragment", "UNSUPPORTED_OPERATION", {
|
|
|
|
operation: "fragment"
|
|
|
|
});
|
|
|
|
return fragment;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return method;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
// The combination of TypeScrype, Private Fields and Proxies makes
|
|
|
|
// the world go boom; so we hide variables with some trickery keeping
|
|
|
|
// a symbol attached to each BaseContract which its sub-class (even
|
|
|
|
// via a Proxy) can reach and use to look up its internal values.
|
|
|
|
const internal = Symbol.for("_ethersInternal_contract");
|
|
|
|
const internalValues = new WeakMap();
|
|
|
|
function setInternal(contract, values) {
|
|
|
|
internalValues.set(contract[internal], values);
|
|
|
|
}
|
|
|
|
function getInternal(contract) {
|
|
|
|
return internalValues.get(contract[internal]);
|
|
|
|
}
|
|
|
|
function isDeferred(value) {
|
|
|
|
return (value && typeof (value) === "object" && ("getTopicFilter" in value) &&
|
|
|
|
(typeof (value.getTopicFilter) === "function") && value.fragment);
|
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
async function getSubInfo(contract, event) {
|
2022-09-05 23:57:11 +03:00
|
|
|
let topics;
|
2022-11-09 10:57:02 +03:00
|
|
|
let fragment = null;
|
|
|
|
// Convert named events to topicHash and get the fragment for
|
|
|
|
// events which need deconstructing.
|
2022-09-05 23:57:11 +03:00
|
|
|
if (Array.isArray(event)) {
|
2022-11-09 10:57:02 +03:00
|
|
|
const topicHashify = function (name) {
|
2022-12-04 01:01:29 +03:00
|
|
|
if ((0, index_js_3.isHexString)(name, 32)) {
|
2022-11-09 10:57:02 +03:00
|
|
|
return name;
|
|
|
|
}
|
2023-01-28 01:29:49 +03:00
|
|
|
const fragment = contract.interface.getEvent(name);
|
|
|
|
(0, index_js_3.assertArgument)(fragment, "unknown fragment", "name", name);
|
|
|
|
return fragment.topicHash;
|
2022-11-09 10:57:02 +03:00
|
|
|
};
|
|
|
|
// Array of Topics and Names; e.g. `[ "0x1234...89ab", "Transfer(address)" ]`
|
|
|
|
topics = event.map((e) => {
|
|
|
|
if (e == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (Array.isArray(e)) {
|
|
|
|
return e.map(topicHashify);
|
|
|
|
}
|
|
|
|
return topicHashify(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (event === "*") {
|
|
|
|
topics = [null];
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
else if (typeof (event) === "string") {
|
2022-12-04 01:01:29 +03:00
|
|
|
if ((0, index_js_3.isHexString)(event, 32)) {
|
2022-11-09 10:57:02 +03:00
|
|
|
// Topic Hash
|
|
|
|
topics = [event];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Name or Signature; e.g. `"Transfer", `"Transfer(address)"`
|
|
|
|
fragment = contract.interface.getEvent(event);
|
2023-01-28 01:29:49 +03:00
|
|
|
(0, index_js_3.assertArgument)(fragment, "unknown fragment", "event", event);
|
2022-11-09 10:57:02 +03:00
|
|
|
topics = [fragment.topicHash];
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
else if (isDeferred(event)) {
|
|
|
|
// Deferred Topic Filter; e.g. `contract.filter.Transfer(from)`
|
|
|
|
topics = await event.getTopicFilter();
|
|
|
|
}
|
|
|
|
else if ("fragment" in event) {
|
|
|
|
// ContractEvent; e.g. `contract.filter.Transfer`
|
|
|
|
fragment = event.fragment;
|
2022-09-16 05:58:45 +03:00
|
|
|
topics = [fragment.topicHash];
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
else {
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assertArgument)(false, "unknown event name", "event", event);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
// Normalize topics and sort TopicSets
|
|
|
|
topics = topics.map((t) => {
|
|
|
|
if (t == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (Array.isArray(t)) {
|
2022-11-09 10:57:02 +03:00
|
|
|
const items = Array.from(new Set(t.map((t) => t.toLowerCase())).values());
|
|
|
|
if (items.length === 1) {
|
|
|
|
return items[0];
|
|
|
|
}
|
|
|
|
items.sort();
|
|
|
|
return items;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
return t.toLowerCase();
|
|
|
|
});
|
|
|
|
const tag = topics.map((t) => {
|
|
|
|
if (t == null) {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
if (Array.isArray(t)) {
|
|
|
|
return t.join("|");
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}).join("&");
|
|
|
|
return { fragment, tag, topics };
|
|
|
|
}
|
|
|
|
async function hasSub(contract, event) {
|
|
|
|
const { subs } = getInternal(contract);
|
2022-11-09 10:57:02 +03:00
|
|
|
return subs.get((await getSubInfo(contract, event)).tag) || null;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
async function getSub(contract, operation, event) {
|
2022-09-05 23:57:11 +03:00
|
|
|
// Make sure our runner can actually subscribe to events
|
|
|
|
const provider = getProvider(contract.runner);
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(provider, "contract runner does not support subscribing", "UNSUPPORTED_OPERATION", { operation });
|
2022-11-09 10:57:02 +03:00
|
|
|
const { fragment, tag, topics } = await getSubInfo(contract, event);
|
2022-09-05 23:57:11 +03:00
|
|
|
const { addr, subs } = getInternal(contract);
|
|
|
|
let sub = subs.get(tag);
|
|
|
|
if (!sub) {
|
|
|
|
const address = (addr ? addr : contract);
|
|
|
|
const filter = { address, topics };
|
|
|
|
const listener = (log) => {
|
2022-11-09 10:57:02 +03:00
|
|
|
let foundFragment = fragment;
|
|
|
|
if (foundFragment == null) {
|
|
|
|
try {
|
|
|
|
foundFragment = contract.interface.getEvent(log.topics[0]);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
// If fragment is null, we do not deconstruct the args to emit
|
|
|
|
if (foundFragment) {
|
|
|
|
const _foundFragment = foundFragment;
|
|
|
|
const args = fragment ? contract.interface.decodeEventLog(fragment, log.data, log.topics) : [];
|
|
|
|
emit(contract, event, args, (listener) => {
|
|
|
|
return new wrappers_js_1.ContractEventPayload(contract, listener, event, _foundFragment, log);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit(contract, event, [], (listener) => {
|
|
|
|
return new wrappers_js_1.ContractUnknownEventPayload(contract, listener, event, log);
|
|
|
|
});
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
};
|
2023-01-15 16:03:21 +03:00
|
|
|
let starting = [];
|
2022-09-05 23:57:11 +03:00
|
|
|
const start = () => {
|
2023-01-15 16:03:21 +03:00
|
|
|
if (starting.length) {
|
2022-09-05 23:57:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-01-15 16:03:21 +03:00
|
|
|
starting.push(provider.on(filter, listener));
|
2022-09-05 23:57:11 +03:00
|
|
|
};
|
2023-01-15 16:03:21 +03:00
|
|
|
const stop = async () => {
|
|
|
|
if (starting.length == 0) {
|
2022-09-05 23:57:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2023-01-15 16:03:21 +03:00
|
|
|
let started = starting;
|
|
|
|
starting = [];
|
|
|
|
await Promise.all(started);
|
2022-09-05 23:57:11 +03:00
|
|
|
provider.off(filter, listener);
|
|
|
|
};
|
|
|
|
sub = { tag, listeners: [], start, stop };
|
|
|
|
subs.set(tag, sub);
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
// We use this to ensure one emit resolves before firing the next to
|
|
|
|
// ensure correct ordering (note this cannot throw and just adds the
|
|
|
|
// notice to the event queu using setTimeout).
|
|
|
|
let lastEmit = Promise.resolve();
|
2022-11-09 10:57:02 +03:00
|
|
|
async function _emit(contract, event, args, payloadFunc) {
|
2022-09-05 23:57:11 +03:00
|
|
|
await lastEmit;
|
|
|
|
const sub = await hasSub(contract, event);
|
|
|
|
if (!sub) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const count = sub.listeners.length;
|
|
|
|
sub.listeners = sub.listeners.filter(({ listener, once }) => {
|
2023-02-13 06:14:26 +03:00
|
|
|
const passArgs = Array.from(args);
|
2022-11-09 10:57:02 +03:00
|
|
|
if (payloadFunc) {
|
|
|
|
passArgs.push(payloadFunc(once ? null : listener));
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
listener.call(contract, ...passArgs);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
return !once;
|
|
|
|
});
|
|
|
|
return (count > 0);
|
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
async function emit(contract, event, args, payloadFunc) {
|
2022-09-05 23:57:11 +03:00
|
|
|
try {
|
|
|
|
await lastEmit;
|
|
|
|
}
|
|
|
|
catch (error) { }
|
2022-11-09 10:57:02 +03:00
|
|
|
const resultPromise = _emit(contract, event, args, payloadFunc);
|
2022-09-05 23:57:11 +03:00
|
|
|
lastEmit = resultPromise;
|
|
|
|
return await resultPromise;
|
|
|
|
}
|
|
|
|
const passProperties = ["then"];
|
|
|
|
class BaseContract {
|
|
|
|
target;
|
|
|
|
interface;
|
|
|
|
runner;
|
|
|
|
filters;
|
|
|
|
[internal];
|
2023-01-31 06:29:09 +03:00
|
|
|
fallback;
|
2022-11-30 23:44:23 +03:00
|
|
|
constructor(target, abi, runner, _deployTx) {
|
|
|
|
if (runner == null) {
|
|
|
|
runner = null;
|
|
|
|
}
|
2022-09-16 05:58:45 +03:00
|
|
|
const iface = index_js_1.Interface.from(abi);
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.defineProperties)(this, { target, runner, interface: iface });
|
2022-09-05 23:57:11 +03:00
|
|
|
Object.defineProperty(this, internal, { value: {} });
|
|
|
|
let addrPromise;
|
|
|
|
let addr = null;
|
|
|
|
let deployTx = null;
|
|
|
|
if (_deployTx) {
|
|
|
|
const provider = getProvider(runner);
|
2022-09-27 10:45:27 +03:00
|
|
|
// @TODO: the provider can be null; make a custom dummy provider that will throw a
|
|
|
|
// meaningful error
|
2022-09-05 23:57:11 +03:00
|
|
|
deployTx = new wrappers_js_1.ContractTransactionResponse(this.interface, provider, _deployTx);
|
|
|
|
}
|
|
|
|
let subs = new Map();
|
|
|
|
// Resolve the target as the address
|
|
|
|
if (typeof (target) === "string") {
|
2022-12-04 01:01:29 +03:00
|
|
|
if ((0, index_js_3.isHexString)(target)) {
|
2022-09-05 23:57:11 +03:00
|
|
|
addr = target;
|
|
|
|
addrPromise = Promise.resolve(target);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const resolver = getRunner(runner, "resolveName");
|
|
|
|
if (!canResolve(resolver)) {
|
2022-12-04 01:01:29 +03:00
|
|
|
throw (0, index_js_3.makeError)("contract runner does not support name resolution", "UNSUPPORTED_OPERATION", {
|
2022-09-05 23:57:11 +03:00
|
|
|
operation: "resolveName"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
addrPromise = resolver.resolveName(target).then((addr) => {
|
|
|
|
if (addr == null) {
|
|
|
|
throw new Error("TODO");
|
|
|
|
}
|
|
|
|
getInternal(this).addr = addr;
|
|
|
|
return addr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
addrPromise = target.getAddress().then((addr) => {
|
|
|
|
if (addr == null) {
|
|
|
|
throw new Error("TODO");
|
|
|
|
}
|
|
|
|
getInternal(this).addr = addr;
|
|
|
|
return addr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Set our private values
|
|
|
|
setInternal(this, { addrPromise, addr, deployTx, subs });
|
|
|
|
// Add the event filters
|
|
|
|
const filters = new Proxy({}, {
|
|
|
|
get: (target, _prop, receiver) => {
|
|
|
|
// Pass important checks (like `then` for Promise) through
|
|
|
|
if (passProperties.indexOf(_prop) >= 0) {
|
|
|
|
return Reflect.get(target, _prop, receiver);
|
|
|
|
}
|
|
|
|
const prop = String(_prop);
|
|
|
|
const result = this.getEvent(prop);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
throw new Error(`unknown contract event: ${prop}`);
|
2023-04-06 11:37:10 +03:00
|
|
|
},
|
|
|
|
has: (target, prop) => {
|
|
|
|
// Pass important checks (like `then` for Promise) through
|
|
|
|
if (passProperties.indexOf(prop) >= 0) {
|
|
|
|
return Reflect.has(target, prop);
|
|
|
|
}
|
|
|
|
return Reflect.has(target, prop) || this.interface.hasEvent(String(prop));
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
});
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.defineProperties)(this, { filters });
|
2023-01-31 06:29:09 +03:00
|
|
|
(0, index_js_3.defineProperties)(this, {
|
2023-03-20 19:53:37 +03:00
|
|
|
fallback: ((iface.receive || iface.fallback) ? (buildWrappedFallback(this)) : null)
|
2023-01-31 06:29:09 +03:00
|
|
|
});
|
2022-09-05 23:57:11 +03:00
|
|
|
// Return a Proxy that will respond to functions
|
|
|
|
return new Proxy(this, {
|
|
|
|
get: (target, _prop, receiver) => {
|
|
|
|
if (_prop in target || passProperties.indexOf(_prop) >= 0) {
|
|
|
|
return Reflect.get(target, _prop, receiver);
|
|
|
|
}
|
|
|
|
const prop = String(_prop);
|
|
|
|
const result = target.getFunction(prop);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
throw new Error(`unknown contract method: ${prop}`);
|
2023-04-06 11:37:10 +03:00
|
|
|
},
|
|
|
|
has: (target, prop) => {
|
|
|
|
if (prop in target || passProperties.indexOf(prop) >= 0) {
|
|
|
|
return Reflect.has(target, prop);
|
|
|
|
}
|
|
|
|
return target.interface.hasFunction(String(prop));
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-11-09 10:57:02 +03:00
|
|
|
connect(runner) {
|
|
|
|
return new BaseContract(this.target, this.interface, runner);
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
async getAddress() { return await getInternal(this).addrPromise; }
|
|
|
|
async getDeployedCode() {
|
|
|
|
const provider = getProvider(this.runner);
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(provider, "runner does not support .provider", "UNSUPPORTED_OPERATION", { operation: "getDeployedCode" });
|
2022-09-05 23:57:11 +03:00
|
|
|
const code = await provider.getCode(await this.getAddress());
|
|
|
|
if (code === "0x") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
async waitForDeployment() {
|
|
|
|
// We have the deployement transaction; just use that (throws if deployement fails)
|
|
|
|
const deployTx = this.deploymentTransaction();
|
|
|
|
if (deployTx) {
|
|
|
|
await deployTx.wait();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// Check for code
|
|
|
|
const code = await this.getDeployedCode();
|
|
|
|
if (code != null) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// Make sure we can subscribe to a provider event
|
|
|
|
const provider = getProvider(this.runner);
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(provider != null, "contract runner does not support .provider", "UNSUPPORTED_OPERATION", { operation: "waitForDeployment" });
|
2022-09-05 23:57:11 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const checkCode = async () => {
|
|
|
|
try {
|
|
|
|
const code = await this.getDeployedCode();
|
|
|
|
if (code != null) {
|
|
|
|
return resolve(this);
|
|
|
|
}
|
|
|
|
provider.once("block", checkCode);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
checkCode();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
deploymentTransaction() {
|
|
|
|
return getInternal(this).deployTx;
|
|
|
|
}
|
|
|
|
getFunction(key) {
|
|
|
|
if (typeof (key) !== "string") {
|
|
|
|
key = key.format();
|
|
|
|
}
|
2023-03-20 19:53:37 +03:00
|
|
|
const func = buildWrappedMethod(this, key);
|
|
|
|
return func;
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
getEvent(key) {
|
|
|
|
if (typeof (key) !== "string") {
|
|
|
|
key = key.format();
|
|
|
|
}
|
2023-03-20 19:53:37 +03:00
|
|
|
return buildWrappedEvent(this, key);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async queryTransaction(hash) {
|
|
|
|
// Is this useful?
|
|
|
|
throw new Error("@TODO");
|
|
|
|
}
|
2022-11-30 23:44:23 +03:00
|
|
|
async queryFilter(event, fromBlock, toBlock) {
|
|
|
|
if (fromBlock == null) {
|
|
|
|
fromBlock = 0;
|
|
|
|
}
|
|
|
|
if (toBlock == null) {
|
|
|
|
toBlock = "latest";
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
const { addr, addrPromise } = getInternal(this);
|
|
|
|
const address = (addr ? addr : (await addrPromise));
|
2022-11-09 10:57:02 +03:00
|
|
|
const { fragment, topics } = await getSubInfo(this, event);
|
2022-09-05 23:57:11 +03:00
|
|
|
const filter = { address, topics, fromBlock, toBlock };
|
|
|
|
const provider = getProvider(this.runner);
|
2022-12-04 01:01:29 +03:00
|
|
|
(0, index_js_3.assert)(provider, "contract runner does not have a provider", "UNSUPPORTED_OPERATION", { operation: "queryFilter" });
|
2022-09-05 23:57:11 +03:00
|
|
|
return (await provider.getLogs(filter)).map((log) => {
|
2022-11-09 10:57:02 +03:00
|
|
|
let foundFragment = fragment;
|
|
|
|
if (foundFragment == null) {
|
|
|
|
try {
|
|
|
|
foundFragment = this.interface.getEvent(log.topics[0]);
|
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
}
|
|
|
|
if (foundFragment) {
|
|
|
|
return new wrappers_js_1.EventLog(log, this.interface, foundFragment);
|
|
|
|
}
|
|
|
|
else {
|
2022-12-04 01:01:29 +03:00
|
|
|
return new provider_js_1.Log(log, provider);
|
2022-11-09 10:57:02 +03:00
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
async on(event, listener) {
|
2022-11-09 10:57:02 +03:00
|
|
|
const sub = await getSub(this, "on", event);
|
2022-09-05 23:57:11 +03:00
|
|
|
sub.listeners.push({ listener, once: false });
|
|
|
|
sub.start();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
async once(event, listener) {
|
2022-11-09 10:57:02 +03:00
|
|
|
const sub = await getSub(this, "once", event);
|
2022-09-05 23:57:11 +03:00
|
|
|
sub.listeners.push({ listener, once: true });
|
|
|
|
sub.start();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
async emit(event, ...args) {
|
|
|
|
return await emit(this, event, args, null);
|
|
|
|
}
|
|
|
|
async listenerCount(event) {
|
|
|
|
if (event) {
|
|
|
|
const sub = await hasSub(this, event);
|
|
|
|
if (!sub) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return sub.listeners.length;
|
|
|
|
}
|
|
|
|
const { subs } = getInternal(this);
|
|
|
|
let total = 0;
|
|
|
|
for (const { listeners } of subs.values()) {
|
|
|
|
total += listeners.length;
|
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
async listeners(event) {
|
|
|
|
if (event) {
|
|
|
|
const sub = await hasSub(this, event);
|
|
|
|
if (!sub) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return sub.listeners.map(({ listener }) => listener);
|
|
|
|
}
|
|
|
|
const { subs } = getInternal(this);
|
|
|
|
let result = [];
|
|
|
|
for (const { listeners } of subs.values()) {
|
|
|
|
result = result.concat(listeners.map(({ listener }) => listener));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
async off(event, listener) {
|
|
|
|
const sub = await hasSub(this, event);
|
|
|
|
if (!sub) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
if (listener) {
|
|
|
|
const index = sub.listeners.map(({ listener }) => listener).indexOf(listener);
|
|
|
|
if (index >= 0) {
|
|
|
|
sub.listeners.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (listener == null || sub.listeners.length === 0) {
|
|
|
|
sub.stop();
|
|
|
|
getInternal(this).subs.delete(sub.tag);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
async removeAllListeners(event) {
|
|
|
|
if (event) {
|
|
|
|
const sub = await hasSub(this, event);
|
|
|
|
if (!sub) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
sub.stop();
|
|
|
|
getInternal(this).subs.delete(sub.tag);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const { subs } = getInternal(this);
|
|
|
|
for (const { tag, stop } of subs.values()) {
|
|
|
|
stop();
|
|
|
|
subs.delete(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// Alias for "on"
|
|
|
|
async addListener(event, listener) {
|
|
|
|
return await this.on(event, listener);
|
|
|
|
}
|
|
|
|
// Alias for "off"
|
|
|
|
async removeListener(event, listener) {
|
|
|
|
return await this.off(event, listener);
|
|
|
|
}
|
|
|
|
static buildClass(abi) {
|
|
|
|
class CustomContract extends BaseContract {
|
|
|
|
constructor(address, runner = null) {
|
|
|
|
super(address, abi, runner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CustomContract;
|
|
|
|
}
|
|
|
|
;
|
2022-11-30 23:44:23 +03:00
|
|
|
static from(target, abi, runner) {
|
|
|
|
if (runner == null) {
|
|
|
|
runner = null;
|
|
|
|
}
|
2022-09-05 23:57:11 +03:00
|
|
|
const contract = new this(target, abi, runner);
|
|
|
|
return contract;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.BaseContract = BaseContract;
|
|
|
|
function _ContractBase() {
|
|
|
|
return BaseContract;
|
|
|
|
}
|
|
|
|
class Contract extends _ContractBase() {
|
|
|
|
}
|
|
|
|
exports.Contract = Contract;
|
|
|
|
//# sourceMappingURL=contract.js.map
|