2019-05-14 18:48:48 -04:00
|
|
|
"use strict";
|
2019-05-23 19:13:44 -04:00
|
|
|
var __extends = (this && this.__extends) || (function () {
|
|
|
|
var extendStatics = function (d, b) {
|
|
|
|
extendStatics = Object.setPrototypeOf ||
|
|
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
2021-03-07 18:24:04 -05:00
|
|
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
2019-05-23 19:13:44 -04:00
|
|
|
return extendStatics(d, b);
|
|
|
|
};
|
|
|
|
return function (d, b) {
|
2021-03-07 18:24:04 -05:00
|
|
|
if (typeof b !== "function" && b !== null)
|
|
|
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
2019-05-23 19:13:44 -04:00
|
|
|
extendStatics(d, b);
|
|
|
|
function __() { this.constructor = d; }
|
|
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
|
|
};
|
|
|
|
})();
|
2020-04-23 23:35:39 -04:00
|
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
};
|
|
|
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
|
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
|
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
|
|
function step(op) {
|
|
|
|
if (f) throw new TypeError("Generator is already executing.");
|
|
|
|
while (_) try {
|
|
|
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
|
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
|
|
switch (op[0]) {
|
|
|
|
case 0: case 1: t = op; break;
|
|
|
|
case 4: _.label++; return { value: op[1], done: false };
|
|
|
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
|
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
|
|
default:
|
|
|
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
|
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
|
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
|
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
|
|
if (t[2]) _.ops.pop();
|
|
|
|
_.trys.pop(); continue;
|
|
|
|
}
|
|
|
|
op = body.call(thisArg, _);
|
|
|
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
|
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
|
|
}
|
|
|
|
};
|
2021-10-16 02:29:27 -04:00
|
|
|
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
|
|
if (ar || !(i in from)) {
|
|
|
|
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
|
|
ar[i] = from[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return to.concat(ar || Array.prototype.slice.call(from));
|
2019-09-06 17:48:26 -04:00
|
|
|
};
|
2019-05-14 18:48:48 -04:00
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2021-03-30 15:22:45 -04:00
|
|
|
exports.ContractFactory = exports.Contract = exports.BaseContract = void 0;
|
2019-05-14 18:48:48 -04:00
|
|
|
var abi_1 = require("@ethersproject/abi");
|
|
|
|
var abstract_provider_1 = require("@ethersproject/abstract-provider");
|
|
|
|
var abstract_signer_1 = require("@ethersproject/abstract-signer");
|
|
|
|
var address_1 = require("@ethersproject/address");
|
|
|
|
var bignumber_1 = require("@ethersproject/bignumber");
|
|
|
|
var bytes_1 = require("@ethersproject/bytes");
|
|
|
|
var properties_1 = require("@ethersproject/properties");
|
2021-03-30 15:22:45 -04:00
|
|
|
var transactions_1 = require("@ethersproject/transactions");
|
2019-08-02 02:10:58 -04:00
|
|
|
var logger_1 = require("@ethersproject/logger");
|
|
|
|
var _version_1 = require("./_version");
|
|
|
|
var logger = new logger_1.Logger(_version_1.version);
|
2020-06-01 05:04:12 -04:00
|
|
|
;
|
|
|
|
;
|
2019-05-14 18:48:48 -04:00
|
|
|
///////////////////////////////
|
|
|
|
var allowedTransactionKeys = {
|
2021-03-30 15:22:45 -04:00
|
|
|
chainId: true, data: true, from: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true,
|
|
|
|
type: true, accessList: true,
|
2021-10-16 02:29:27 -04:00
|
|
|
maxFeePerGas: true, maxPriorityFeePerGas: true,
|
2022-03-09 02:56:08 -05:00
|
|
|
customData: true,
|
|
|
|
ccipReadEnabled: true
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2020-06-01 05:04:12 -04:00
|
|
|
function resolveName(resolver, nameOrPromise) {
|
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
2020-09-26 03:27:47 -04:00
|
|
|
var name, address;
|
2020-06-01 05:04:12 -04:00
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0: return [4 /*yield*/, nameOrPromise];
|
|
|
|
case 1:
|
|
|
|
name = _a.sent();
|
2021-10-16 02:29:27 -04:00
|
|
|
if (typeof (name) !== "string") {
|
|
|
|
logger.throwArgumentError("invalid address or ENS name", "name", name);
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
// If it is already an address, just use it (after adding checksum)
|
|
|
|
try {
|
2021-10-16 02:29:27 -04:00
|
|
|
return [2 /*return*/, (0, address_1.getAddress)(name)];
|
2020-06-01 05:04:12 -04:00
|
|
|
}
|
|
|
|
catch (error) { }
|
|
|
|
if (!resolver) {
|
|
|
|
logger.throwError("a provider or signer is needed to resolve ENS names", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "resolveName"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return [4 /*yield*/, resolver.resolveName(name)];
|
2020-09-26 03:27:47 -04:00
|
|
|
case 2:
|
|
|
|
address = _a.sent();
|
|
|
|
if (address == null) {
|
|
|
|
logger.throwArgumentError("resolver or addr is not configured for ENS name", "name", name);
|
|
|
|
}
|
|
|
|
return [2 /*return*/, address];
|
2020-06-01 05:04:12 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
// Recursively replaces ENS names with promises to resolve the name and resolves all properties
|
2020-06-01 05:04:12 -04:00
|
|
|
function resolveAddresses(resolver, value, paramType) {
|
2020-09-26 03:27:47 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
|
|
|
if (!Array.isArray(paramType)) return [3 /*break*/, 2];
|
|
|
|
return [4 /*yield*/, Promise.all(paramType.map(function (paramType, index) {
|
|
|
|
return resolveAddresses(resolver, ((Array.isArray(value)) ? value[index] : value[paramType.name]), paramType);
|
|
|
|
}))];
|
|
|
|
case 1: return [2 /*return*/, _a.sent()];
|
|
|
|
case 2:
|
|
|
|
if (!(paramType.type === "address")) return [3 /*break*/, 4];
|
|
|
|
return [4 /*yield*/, resolveName(resolver, value)];
|
|
|
|
case 3: return [2 /*return*/, _a.sent()];
|
|
|
|
case 4:
|
|
|
|
if (!(paramType.type === "tuple")) return [3 /*break*/, 6];
|
|
|
|
return [4 /*yield*/, resolveAddresses(resolver, value, paramType.components)];
|
|
|
|
case 5: return [2 /*return*/, _a.sent()];
|
|
|
|
case 6:
|
|
|
|
if (!(paramType.baseType === "array")) return [3 /*break*/, 8];
|
|
|
|
if (!Array.isArray(value)) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return [2 /*return*/, Promise.reject(logger.makeError("invalid value for array", logger_1.Logger.errors.INVALID_ARGUMENT, {
|
|
|
|
argument: "value",
|
|
|
|
value: value
|
|
|
|
}))];
|
2020-09-26 03:27:47 -04:00
|
|
|
}
|
|
|
|
return [4 /*yield*/, Promise.all(value.map(function (v) { return resolveAddresses(resolver, v, paramType.arrayChildren); }))];
|
|
|
|
case 7: return [2 /*return*/, _a.sent()];
|
|
|
|
case 8: return [2 /*return*/, value];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
function populateTransaction(contract, fragment, args) {
|
2020-05-29 21:27:59 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
2020-09-26 03:27:47 -04:00
|
|
|
var overrides, resolved, data, tx, ro, intrinsic, bytes, i, roValue, leftovers;
|
2020-06-01 05:04:12 -04:00
|
|
|
var _this = this;
|
2020-05-29 21:27:59 -04:00
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
2020-06-01 05:04:12 -04:00
|
|
|
overrides = {};
|
|
|
|
if (args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
|
2021-10-16 02:29:27 -04:00
|
|
|
overrides = (0, properties_1.shallowCopy)(args.pop());
|
2020-06-01 05:04:12 -04:00
|
|
|
}
|
|
|
|
// Make sure the parameter count matches
|
|
|
|
logger.checkArgumentCount(args.length, fragment.inputs.length, "passed to contract");
|
|
|
|
// Populate "from" override (allow promises)
|
|
|
|
if (contract.signer) {
|
|
|
|
if (overrides.from) {
|
|
|
|
// Contracts with a Signer are from the Signer's frame-of-reference;
|
|
|
|
// but we allow overriding "from" if it matches the signer
|
2021-10-16 02:29:27 -04:00
|
|
|
overrides.from = (0, properties_1.resolveProperties)({
|
2020-06-01 05:04:12 -04:00
|
|
|
override: resolveName(contract.signer, overrides.from),
|
|
|
|
signer: contract.signer.getAddress()
|
|
|
|
}).then(function (check) { return __awaiter(_this, void 0, void 0, function () {
|
|
|
|
return __generator(this, function (_a) {
|
2021-10-16 02:29:27 -04:00
|
|
|
if ((0, address_1.getAddress)(check.signer) !== check.override) {
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("Contract with a Signer cannot override from", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.from"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return [2 /*return*/, check.override];
|
|
|
|
});
|
|
|
|
}); });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
overrides.from = contract.signer.getAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (overrides.from) {
|
|
|
|
overrides.from = resolveName(contract.provider, overrides.from);
|
|
|
|
//} else {
|
|
|
|
// Contracts without a signer can override "from", and if
|
|
|
|
// unspecified the zero address is used
|
|
|
|
//overrides.from = AddressZero;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
return [4 /*yield*/, (0, properties_1.resolveProperties)({
|
2020-05-29 21:27:59 -04:00
|
|
|
args: resolveAddresses(contract.signer || contract.provider, args, fragment.inputs),
|
|
|
|
address: contract.resolvedAddress,
|
2021-10-16 02:29:27 -04:00
|
|
|
overrides: ((0, properties_1.resolveProperties)(overrides) || {})
|
2020-05-29 21:27:59 -04:00
|
|
|
})];
|
|
|
|
case 1:
|
|
|
|
resolved = _a.sent();
|
2020-09-26 03:27:47 -04:00
|
|
|
data = contract.interface.encodeFunctionData(fragment, resolved.args);
|
2020-05-29 21:27:59 -04:00
|
|
|
tx = {
|
2020-09-26 03:27:47 -04:00
|
|
|
data: data,
|
2020-05-29 21:27:59 -04:00
|
|
|
to: resolved.address
|
|
|
|
};
|
|
|
|
ro = resolved.overrides;
|
2020-06-01 05:04:12 -04:00
|
|
|
// Populate simple overrides
|
2020-05-29 21:27:59 -04:00
|
|
|
if (ro.nonce != null) {
|
|
|
|
tx.nonce = bignumber_1.BigNumber.from(ro.nonce).toNumber();
|
|
|
|
}
|
|
|
|
if (ro.gasLimit != null) {
|
|
|
|
tx.gasLimit = bignumber_1.BigNumber.from(ro.gasLimit);
|
|
|
|
}
|
|
|
|
if (ro.gasPrice != null) {
|
|
|
|
tx.gasPrice = bignumber_1.BigNumber.from(ro.gasPrice);
|
|
|
|
}
|
2021-06-24 02:13:06 -04:00
|
|
|
if (ro.maxFeePerGas != null) {
|
|
|
|
tx.maxFeePerGas = bignumber_1.BigNumber.from(ro.maxFeePerGas);
|
|
|
|
}
|
|
|
|
if (ro.maxPriorityFeePerGas != null) {
|
|
|
|
tx.maxPriorityFeePerGas = bignumber_1.BigNumber.from(ro.maxPriorityFeePerGas);
|
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
if (ro.from != null) {
|
|
|
|
tx.from = ro.from;
|
|
|
|
}
|
2021-03-30 15:22:45 -04:00
|
|
|
if (ro.type != null) {
|
|
|
|
tx.type = ro.type;
|
|
|
|
}
|
|
|
|
if (ro.accessList != null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
tx.accessList = (0, transactions_1.accessListify)(ro.accessList);
|
2021-03-30 15:22:45 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
// If there was no "gasLimit" override, but the ABI specifies a default, use it
|
2020-05-29 21:27:59 -04:00
|
|
|
if (tx.gasLimit == null && fragment.gas != null) {
|
2020-09-26 03:27:47 -04:00
|
|
|
intrinsic = 21000;
|
2021-10-16 02:29:27 -04:00
|
|
|
bytes = (0, bytes_1.arrayify)(data);
|
2020-09-26 03:27:47 -04:00
|
|
|
for (i = 0; i < bytes.length; i++) {
|
|
|
|
intrinsic += 4;
|
|
|
|
if (bytes[i]) {
|
|
|
|
intrinsic += 64;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.gasLimit = bignumber_1.BigNumber.from(fragment.gas).add(intrinsic);
|
2020-05-29 21:27:59 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
// Populate "value" override
|
2020-05-29 21:27:59 -04:00
|
|
|
if (ro.value) {
|
|
|
|
roValue = bignumber_1.BigNumber.from(ro.value);
|
|
|
|
if (!roValue.isZero() && !fragment.payable) {
|
|
|
|
logger.throwError("non-payable method cannot override value", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.value",
|
2020-06-01 05:04:12 -04:00
|
|
|
value: overrides.value
|
2020-05-29 21:27:59 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
tx.value = roValue;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
if (ro.customData) {
|
|
|
|
tx.customData = (0, properties_1.shallowCopy)(ro.customData);
|
|
|
|
}
|
2022-03-09 02:56:08 -05:00
|
|
|
if (ro.ccipReadEnabled) {
|
|
|
|
tx.ccipReadEnabled = !!ro.ccipReadEnabled;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
// Remove the overrides
|
2020-06-01 05:04:12 -04:00
|
|
|
delete overrides.nonce;
|
|
|
|
delete overrides.gasLimit;
|
|
|
|
delete overrides.gasPrice;
|
2020-05-29 21:27:59 -04:00
|
|
|
delete overrides.from;
|
2020-06-01 05:04:12 -04:00
|
|
|
delete overrides.value;
|
2021-03-30 15:22:45 -04:00
|
|
|
delete overrides.type;
|
|
|
|
delete overrides.accessList;
|
2021-06-24 02:13:06 -04:00
|
|
|
delete overrides.maxFeePerGas;
|
|
|
|
delete overrides.maxPriorityFeePerGas;
|
2021-10-16 02:29:27 -04:00
|
|
|
delete overrides.customData;
|
2022-03-09 02:56:08 -05:00
|
|
|
delete overrides.ccipReadEnabled;
|
2020-06-03 03:47:17 -04:00
|
|
|
leftovers = Object.keys(overrides).filter(function (key) { return (overrides[key] != null); });
|
2020-06-01 05:04:12 -04:00
|
|
|
if (leftovers.length) {
|
|
|
|
logger.throwError("cannot override " + leftovers.map(function (l) { return JSON.stringify(l); }).join(","), logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides",
|
|
|
|
overrides: leftovers
|
|
|
|
});
|
2020-05-29 21:27:59 -04:00
|
|
|
}
|
|
|
|
return [2 /*return*/, tx];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
function buildPopulate(contract, fragment) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return function () {
|
2020-05-29 21:27:59 -04:00
|
|
|
var args = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
2020-05-29 21:27:59 -04:00
|
|
|
args[_i] = arguments[_i];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-09-26 03:27:47 -04:00
|
|
|
return populateTransaction(contract, fragment, args);
|
2020-05-29 21:27:59 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function buildEstimate(contract, fragment) {
|
|
|
|
var signerOrProvider = (contract.signer || contract.provider);
|
|
|
|
return function () {
|
|
|
|
var args = [];
|
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
|
|
args[_i] = arguments[_i];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
2020-06-01 05:04:12 -04:00
|
|
|
var tx;
|
2020-05-29 21:27:59 -04:00
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
|
|
|
if (!signerOrProvider) {
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("estimate require a provider or signer", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "estimateGas"
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
return [4 /*yield*/, populateTransaction(contract, fragment, args)];
|
2020-05-29 21:27:59 -04:00
|
|
|
case 1:
|
|
|
|
tx = _a.sent();
|
|
|
|
return [4 /*yield*/, signerOrProvider.estimateGas(tx)];
|
|
|
|
case 2: return [2 /*return*/, _a.sent()];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2021-10-17 23:03:30 -04:00
|
|
|
function addContractWait(contract, tx) {
|
|
|
|
var wait = tx.wait.bind(tx);
|
|
|
|
tx.wait = function (confirmations) {
|
|
|
|
return wait(confirmations).then(function (receipt) {
|
|
|
|
receipt.events = receipt.logs.map(function (log) {
|
|
|
|
var event = (0, properties_1.deepCopy)(log);
|
|
|
|
var parsed = null;
|
|
|
|
try {
|
|
|
|
parsed = contract.interface.parseLog(log);
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
// Successfully parsed the event log; include it
|
|
|
|
if (parsed) {
|
|
|
|
event.args = parsed.args;
|
|
|
|
event.decode = function (data, topics) {
|
|
|
|
return contract.interface.decodeEventLog(parsed.eventFragment, data, topics);
|
|
|
|
};
|
|
|
|
event.event = parsed.name;
|
|
|
|
event.eventSignature = parsed.signature;
|
|
|
|
}
|
|
|
|
// Useful operations
|
|
|
|
event.removeListener = function () { return contract.provider; };
|
|
|
|
event.getBlock = function () {
|
|
|
|
return contract.provider.getBlock(receipt.blockHash);
|
|
|
|
};
|
|
|
|
event.getTransaction = function () {
|
|
|
|
return contract.provider.getTransaction(receipt.transactionHash);
|
|
|
|
};
|
|
|
|
event.getTransactionReceipt = function () {
|
|
|
|
return Promise.resolve(receipt);
|
|
|
|
};
|
|
|
|
return event;
|
|
|
|
});
|
|
|
|
return receipt;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
function buildCall(contract, fragment, collapseSimple) {
|
|
|
|
var signerOrProvider = (contract.signer || contract.provider);
|
|
|
|
return function () {
|
|
|
|
var args = [];
|
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
|
|
args[_i] = arguments[_i];
|
|
|
|
}
|
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
2020-06-01 05:04:12 -04:00
|
|
|
var blockTag, overrides, tx, result, value;
|
2020-05-29 21:27:59 -04:00
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
|
|
|
blockTag = undefined;
|
2020-06-01 05:04:12 -04:00
|
|
|
if (!(args.length === fragment.inputs.length + 1 && typeof (args[args.length - 1]) === "object")) return [3 /*break*/, 3];
|
2021-10-16 02:29:27 -04:00
|
|
|
overrides = (0, properties_1.shallowCopy)(args.pop());
|
2020-06-03 03:47:17 -04:00
|
|
|
if (!(overrides.blockTag != null)) return [3 /*break*/, 2];
|
2020-05-29 21:27:59 -04:00
|
|
|
return [4 /*yield*/, overrides.blockTag];
|
|
|
|
case 1:
|
|
|
|
blockTag = _a.sent();
|
|
|
|
_a.label = 2;
|
|
|
|
case 2:
|
2020-06-03 03:47:17 -04:00
|
|
|
delete overrides.blockTag;
|
2020-06-01 05:04:12 -04:00
|
|
|
args.push(overrides);
|
|
|
|
_a.label = 3;
|
2020-05-29 21:27:59 -04:00
|
|
|
case 3:
|
2020-06-01 05:04:12 -04:00
|
|
|
if (!(contract.deployTransaction != null)) return [3 /*break*/, 5];
|
|
|
|
return [4 /*yield*/, contract._deployed(blockTag)];
|
|
|
|
case 4:
|
|
|
|
_a.sent();
|
|
|
|
_a.label = 5;
|
|
|
|
case 5: return [4 /*yield*/, populateTransaction(contract, fragment, args)];
|
|
|
|
case 6:
|
2020-05-29 21:27:59 -04:00
|
|
|
tx = _a.sent();
|
|
|
|
return [4 /*yield*/, signerOrProvider.call(tx, blockTag)];
|
2020-06-01 05:04:12 -04:00
|
|
|
case 7:
|
|
|
|
result = _a.sent();
|
2020-05-29 21:27:59 -04:00
|
|
|
try {
|
2020-06-01 05:04:12 -04:00
|
|
|
value = contract.interface.decodeFunctionResult(fragment, result);
|
2020-05-29 21:27:59 -04:00
|
|
|
if (collapseSimple && fragment.outputs.length === 1) {
|
2020-06-01 05:04:12 -04:00
|
|
|
value = value[0];
|
2020-02-26 10:06:48 +00:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
return [2 /*return*/, value];
|
2020-05-29 21:27:59 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
if (error.code === logger_1.Logger.errors.CALL_EXCEPTION) {
|
|
|
|
error.address = contract.address;
|
|
|
|
error.args = args;
|
|
|
|
error.transaction = tx;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return [2 /*return*/];
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
function buildSend(contract, fragment) {
|
|
|
|
return function () {
|
|
|
|
var args = [];
|
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
|
|
args[_i] = arguments[_i];
|
|
|
|
}
|
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
2021-10-17 23:03:30 -04:00
|
|
|
var txRequest, tx;
|
2020-05-29 21:27:59 -04:00
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
|
|
|
if (!contract.signer) {
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("sending a transaction requires a signer", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "sendTransaction"
|
|
|
|
});
|
2020-05-29 21:27:59 -04:00
|
|
|
}
|
2020-06-01 05:04:12 -04:00
|
|
|
if (!(contract.deployTransaction != null)) return [3 /*break*/, 2];
|
|
|
|
return [4 /*yield*/, contract._deployed()];
|
2020-05-29 21:27:59 -04:00
|
|
|
case 1:
|
2020-06-01 05:04:12 -04:00
|
|
|
_a.sent();
|
|
|
|
_a.label = 2;
|
|
|
|
case 2: return [4 /*yield*/, populateTransaction(contract, fragment, args)];
|
|
|
|
case 3:
|
2020-05-29 21:27:59 -04:00
|
|
|
txRequest = _a.sent();
|
|
|
|
return [4 /*yield*/, contract.signer.sendTransaction(txRequest)];
|
2020-06-01 05:04:12 -04:00
|
|
|
case 4:
|
2020-05-29 21:27:59 -04:00
|
|
|
tx = _a.sent();
|
2021-10-17 23:03:30 -04:00
|
|
|
// Tweak the tx.wait so the receipt has extra properties
|
|
|
|
addContractWait(contract, tx);
|
2020-05-29 21:27:59 -04:00
|
|
|
return [2 /*return*/, tx];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function buildDefault(contract, fragment, collapseSimple) {
|
|
|
|
if (fragment.constant) {
|
|
|
|
return buildCall(contract, fragment, collapseSimple);
|
|
|
|
}
|
|
|
|
return buildSend(contract, fragment);
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
function getEventTag(filter) {
|
|
|
|
if (filter.address && (filter.topics == null || filter.topics.length === 0)) {
|
|
|
|
return "*";
|
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
return (filter.address || "*") + "@" + (filter.topics ? filter.topics.map(function (topic) {
|
|
|
|
if (Array.isArray(topic)) {
|
|
|
|
return topic.join("|");
|
|
|
|
}
|
|
|
|
return topic;
|
|
|
|
}).join(":") : "");
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
var RunningEvent = /** @class */ (function () {
|
|
|
|
function RunningEvent(tag, filter) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "tag", tag);
|
|
|
|
(0, properties_1.defineReadOnly)(this, "filter", filter);
|
2019-05-23 19:13:44 -04:00
|
|
|
this._listeners = [];
|
|
|
|
}
|
|
|
|
RunningEvent.prototype.addListener = function (listener, once) {
|
|
|
|
this._listeners.push({ listener: listener, once: once });
|
|
|
|
};
|
|
|
|
RunningEvent.prototype.removeListener = function (listener) {
|
|
|
|
var done = false;
|
|
|
|
this._listeners = this._listeners.filter(function (item) {
|
|
|
|
if (done || item.listener !== listener) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
RunningEvent.prototype.removeAllListeners = function () {
|
|
|
|
this._listeners = [];
|
|
|
|
};
|
|
|
|
RunningEvent.prototype.listeners = function () {
|
|
|
|
return this._listeners.map(function (i) { return i.listener; });
|
|
|
|
};
|
|
|
|
RunningEvent.prototype.listenerCount = function () {
|
|
|
|
return this._listeners.length;
|
|
|
|
};
|
|
|
|
RunningEvent.prototype.run = function (args) {
|
|
|
|
var _this = this;
|
|
|
|
var listenerCount = this.listenerCount();
|
|
|
|
this._listeners = this._listeners.filter(function (item) {
|
|
|
|
var argsCopy = args.slice();
|
|
|
|
// Call the callback in the next event loop
|
|
|
|
setTimeout(function () {
|
|
|
|
item.listener.apply(_this, argsCopy);
|
|
|
|
}, 0);
|
|
|
|
// Reschedule it if it not "once"
|
|
|
|
return !(item.once);
|
|
|
|
});
|
|
|
|
return listenerCount;
|
|
|
|
};
|
|
|
|
RunningEvent.prototype.prepareEvent = function (event) {
|
|
|
|
};
|
2020-04-16 21:59:53 -04:00
|
|
|
// Returns the array that will be applied to an emit
|
|
|
|
RunningEvent.prototype.getEmit = function (event) {
|
|
|
|
return [event];
|
|
|
|
};
|
2019-05-23 19:13:44 -04:00
|
|
|
return RunningEvent;
|
|
|
|
}());
|
|
|
|
var ErrorRunningEvent = /** @class */ (function (_super) {
|
|
|
|
__extends(ErrorRunningEvent, _super);
|
|
|
|
function ErrorRunningEvent() {
|
|
|
|
return _super.call(this, "error", null) || this;
|
|
|
|
}
|
|
|
|
return ErrorRunningEvent;
|
|
|
|
}(RunningEvent));
|
2020-04-16 21:59:53 -04:00
|
|
|
// @TODO Fragment should inherit Wildcard? and just override getEmit?
|
|
|
|
// or have a common abstract super class, with enough constructor
|
|
|
|
// options to configure both.
|
|
|
|
// A Fragment Event will populate all the properties that Wildcard
|
2021-10-16 02:29:27 -04:00
|
|
|
// will, and additionally dereference the arguments when emitting
|
2019-05-23 19:13:44 -04:00
|
|
|
var FragmentRunningEvent = /** @class */ (function (_super) {
|
|
|
|
__extends(FragmentRunningEvent, _super);
|
2019-07-20 21:06:45 -03:00
|
|
|
function FragmentRunningEvent(address, contractInterface, fragment, topics) {
|
2019-05-23 19:13:44 -04:00
|
|
|
var _this = this;
|
|
|
|
var filter = {
|
2019-07-20 21:06:45 -03:00
|
|
|
address: address
|
2019-05-23 19:13:44 -04:00
|
|
|
};
|
2019-07-20 21:06:45 -03:00
|
|
|
var topic = contractInterface.getEventTopic(fragment);
|
|
|
|
if (topics) {
|
|
|
|
if (topic !== topics[0]) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("topic mismatch", "topics", topics);
|
2019-07-20 21:06:45 -03:00
|
|
|
}
|
|
|
|
filter.topics = topics.slice();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
filter.topics = [topic];
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
_this = _super.call(this, getEventTag(filter), filter) || this;
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this, "address", address);
|
|
|
|
(0, properties_1.defineReadOnly)(_this, "interface", contractInterface);
|
|
|
|
(0, properties_1.defineReadOnly)(_this, "fragment", fragment);
|
2019-05-23 19:13:44 -04:00
|
|
|
return _this;
|
|
|
|
}
|
|
|
|
FragmentRunningEvent.prototype.prepareEvent = function (event) {
|
|
|
|
var _this = this;
|
|
|
|
_super.prototype.prepareEvent.call(this, event);
|
|
|
|
event.event = this.fragment.name;
|
|
|
|
event.eventSignature = this.fragment.format();
|
|
|
|
event.decode = function (data, topics) {
|
|
|
|
return _this.interface.decodeEventLog(_this.fragment, data, topics);
|
|
|
|
};
|
2020-04-16 21:59:53 -04:00
|
|
|
try {
|
|
|
|
event.args = this.interface.decodeEventLog(this.fragment, event.data, event.topics);
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
event.args = null;
|
|
|
|
event.decodeError = error;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FragmentRunningEvent.prototype.getEmit = function (event) {
|
2021-10-16 02:29:27 -04:00
|
|
|
var errors = (0, abi_1.checkResultErrors)(event.args);
|
2020-04-25 03:54:54 -04:00
|
|
|
if (errors.length) {
|
|
|
|
throw errors[0].error;
|
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
var args = (event.args || []).slice();
|
|
|
|
args.push(event);
|
|
|
|
return args;
|
2019-05-23 19:13:44 -04:00
|
|
|
};
|
|
|
|
return FragmentRunningEvent;
|
|
|
|
}(RunningEvent));
|
2021-10-16 02:29:27 -04:00
|
|
|
// A Wildcard Event will attempt to populate:
|
2020-04-16 21:59:53 -04:00
|
|
|
// - event The name of the event name
|
|
|
|
// - eventSignature The full signature of the event
|
|
|
|
// - decode A function to decode data and topics
|
|
|
|
// - args The decoded data and topics
|
2019-05-23 19:13:44 -04:00
|
|
|
var WildcardRunningEvent = /** @class */ (function (_super) {
|
|
|
|
__extends(WildcardRunningEvent, _super);
|
|
|
|
function WildcardRunningEvent(address, contractInterface) {
|
|
|
|
var _this = _super.call(this, "*", { address: address }) || this;
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this, "address", address);
|
|
|
|
(0, properties_1.defineReadOnly)(_this, "interface", contractInterface);
|
2019-05-23 19:13:44 -04:00
|
|
|
return _this;
|
|
|
|
}
|
|
|
|
WildcardRunningEvent.prototype.prepareEvent = function (event) {
|
|
|
|
var _this = this;
|
|
|
|
_super.prototype.prepareEvent.call(this, event);
|
2020-04-16 21:59:53 -04:00
|
|
|
try {
|
|
|
|
var parsed_1 = this.interface.parseLog(event);
|
|
|
|
event.event = parsed_1.name;
|
|
|
|
event.eventSignature = parsed_1.signature;
|
2019-05-23 19:13:44 -04:00
|
|
|
event.decode = function (data, topics) {
|
2020-04-16 21:59:53 -04:00
|
|
|
return _this.interface.decodeEventLog(parsed_1.eventFragment, data, topics);
|
2019-05-23 19:13:44 -04:00
|
|
|
};
|
2020-04-16 21:59:53 -04:00
|
|
|
event.args = parsed_1.args;
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// No matching event
|
2019-05-23 19:13:44 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return WildcardRunningEvent;
|
|
|
|
}(RunningEvent));
|
2021-03-30 15:22:45 -04:00
|
|
|
var BaseContract = /** @class */ (function () {
|
|
|
|
function BaseContract(addressOrName, contractInterface, signerOrProvider) {
|
2019-05-14 18:48:48 -04:00
|
|
|
var _newTarget = this.constructor;
|
|
|
|
var _this = this;
|
|
|
|
// @TODO: Maybe still check the addressOrName looks like a valid address or name?
|
|
|
|
//address = getAddress(address);
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "interface", (0, properties_1.getStatic)(_newTarget, "getInterface")(contractInterface));
|
2020-06-01 05:04:12 -04:00
|
|
|
if (signerOrProvider == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "provider", null);
|
|
|
|
(0, properties_1.defineReadOnly)(this, "signer", null);
|
2020-06-01 05:04:12 -04:00
|
|
|
}
|
|
|
|
else if (abstract_signer_1.Signer.isSigner(signerOrProvider)) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "provider", signerOrProvider.provider || null);
|
|
|
|
(0, properties_1.defineReadOnly)(this, "signer", signerOrProvider);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-06-11 17:57:04 -04:00
|
|
|
else if (abstract_provider_1.Provider.isProvider(signerOrProvider)) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "provider", signerOrProvider);
|
|
|
|
(0, properties_1.defineReadOnly)(this, "signer", null);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid signer or provider", "signerOrProvider", signerOrProvider);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "callStatic", {});
|
|
|
|
(0, properties_1.defineReadOnly)(this, "estimateGas", {});
|
|
|
|
(0, properties_1.defineReadOnly)(this, "functions", {});
|
|
|
|
(0, properties_1.defineReadOnly)(this, "populateTransaction", {});
|
|
|
|
(0, properties_1.defineReadOnly)(this, "filters", {});
|
2019-11-20 18:57:38 +09:00
|
|
|
{
|
|
|
|
var uniqueFilters_1 = {};
|
|
|
|
Object.keys(this.interface.events).forEach(function (eventSignature) {
|
|
|
|
var event = _this.interface.events[eventSignature];
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.filters, eventSignature, function () {
|
2019-11-20 18:57:38 +09:00
|
|
|
var args = [];
|
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
|
|
args[_i] = arguments[_i];
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
address: _this.address,
|
|
|
|
topics: _this.interface.encodeFilterTopics(event, args)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
if (!uniqueFilters_1[event.name]) {
|
|
|
|
uniqueFilters_1[event.name] = [];
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-11-20 18:57:38 +09:00
|
|
|
uniqueFilters_1[event.name].push(eventSignature);
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2019-11-20 18:57:38 +09:00
|
|
|
Object.keys(uniqueFilters_1).forEach(function (name) {
|
|
|
|
var filters = uniqueFilters_1[name];
|
|
|
|
if (filters.length === 1) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.filters, name, _this.filters[filters[0]]);
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
logger.warn("Duplicate definition of " + name + " (" + filters.join(", ") + ")");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "_runningEvents", {});
|
|
|
|
(0, properties_1.defineReadOnly)(this, "_wrappedEmits", {});
|
2021-02-12 19:06:01 -05:00
|
|
|
if (addressOrName == null) {
|
|
|
|
logger.throwArgumentError("invalid contract address or ENS name", "addressOrName", addressOrName);
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "address", addressOrName);
|
2019-05-14 18:48:48 -04:00
|
|
|
if (this.provider) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "resolvedAddress", resolveName(this.provider, addressOrName));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "resolvedAddress", Promise.resolve((0, address_1.getAddress)(addressOrName)));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
// Without a provider, we cannot use ENS names
|
2020-06-01 05:04:12 -04:00
|
|
|
logger.throwError("provider is required to use ENS name as contract address", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "new Contract"
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
}
|
2022-03-09 02:56:08 -05:00
|
|
|
// Swallow bad ENS names to prevent Unhandled Exceptions
|
|
|
|
this.resolvedAddress.catch(function (e) { });
|
2020-05-03 17:53:58 -04:00
|
|
|
var uniqueNames = {};
|
|
|
|
var uniqueSignatures = {};
|
|
|
|
Object.keys(this.interface.functions).forEach(function (signature) {
|
|
|
|
var fragment = _this.interface.functions[signature];
|
|
|
|
// Check that the signature is unique; if not the ABI generation has
|
|
|
|
// not been cleaned or may be incorrectly generated
|
|
|
|
if (uniqueSignatures[signature]) {
|
2021-05-31 19:06:24 -04:00
|
|
|
logger.warn("Duplicate ABI entry for " + JSON.stringify(signature));
|
2020-05-03 17:53:58 -04:00
|
|
|
return;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
uniqueSignatures[signature] = true;
|
|
|
|
// Track unique names; we only expose bare named functions if they
|
|
|
|
// are ambiguous
|
|
|
|
{
|
|
|
|
var name_1 = fragment.name;
|
2021-10-16 02:29:27 -04:00
|
|
|
if (!uniqueNames["%" + name_1]) {
|
|
|
|
uniqueNames["%" + name_1] = [];
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
uniqueNames["%" + name_1].push(signature);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
if (_this[signature] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this, signature, buildDefault(_this, fragment, true));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-29 21:27:59 -04:00
|
|
|
// We do not collapse simple calls on this bucket, which allows
|
|
|
|
// frameworks to safely use this without introspection as well as
|
|
|
|
// allows decoding error recovery.
|
2020-05-03 17:53:58 -04:00
|
|
|
if (_this.functions[signature] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.functions, signature, buildDefault(_this, fragment, false));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
if (_this.callStatic[signature] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.callStatic, signature, buildCall(_this, fragment, true));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
if (_this.populateTransaction[signature] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.populateTransaction, signature, buildPopulate(_this, fragment));
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
|
|
|
if (_this.estimateGas[signature] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.estimateGas, signature, buildEstimate(_this, fragment));
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
|
|
|
});
|
2020-05-03 17:53:58 -04:00
|
|
|
Object.keys(uniqueNames).forEach(function (name) {
|
|
|
|
// Ambiguous names to not get attached as bare names
|
|
|
|
var signatures = uniqueNames[name];
|
2019-11-20 18:57:38 +09:00
|
|
|
if (signatures.length > 1) {
|
|
|
|
return;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
// Strip off the leading "%" used for prototype protection
|
|
|
|
name = name.substring(1);
|
2020-05-03 17:53:58 -04:00
|
|
|
var signature = signatures[0];
|
2021-03-30 15:22:45 -04:00
|
|
|
// If overwriting a member property that is null, swallow the error
|
|
|
|
try {
|
|
|
|
if (_this[name] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this, name, _this[signature]);
|
2021-03-30 15:22:45 -04:00
|
|
|
}
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
2021-03-30 15:22:45 -04:00
|
|
|
catch (e) { }
|
2020-05-03 17:53:58 -04:00
|
|
|
if (_this.functions[name] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.functions, name, _this.functions[signature]);
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
|
|
|
if (_this.callStatic[name] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.callStatic, name, _this.callStatic[signature]);
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
|
|
|
if (_this.populateTransaction[name] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.populateTransaction, name, _this.populateTransaction[signature]);
|
2020-05-03 17:53:58 -04:00
|
|
|
}
|
|
|
|
if (_this.estimateGas[name] == null) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(_this.estimateGas, name, _this.estimateGas[signature]);
|
2019-11-20 18:57:38 +09:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
}
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.getContractAddress = function (transaction) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, address_1.getContractAddress)(transaction);
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.getInterface = function (contractInterface) {
|
2019-06-11 17:57:04 -04:00
|
|
|
if (abi_1.Interface.isInterface(contractInterface)) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return contractInterface;
|
|
|
|
}
|
|
|
|
return new abi_1.Interface(contractInterface);
|
|
|
|
};
|
|
|
|
// @TODO: Allow timeout?
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.deployed = function () {
|
2019-05-14 18:48:48 -04:00
|
|
|
return this._deployed();
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype._deployed = function (blockTag) {
|
2019-05-14 18:48:48 -04:00
|
|
|
var _this = this;
|
|
|
|
if (!this._deployedPromise) {
|
|
|
|
// If we were just deployed, we know the transaction we should occur in
|
|
|
|
if (this.deployTransaction) {
|
|
|
|
this._deployedPromise = this.deployTransaction.wait().then(function () {
|
|
|
|
return _this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// @TODO: Once we allow a timeout to be passed in, we will wait
|
|
|
|
// up to that many blocks for getCode
|
|
|
|
// Otherwise, poll for our code to be deployed
|
|
|
|
this._deployedPromise = this.provider.getCode(this.address, blockTag).then(function (code) {
|
|
|
|
if (code === "0x") {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("contract not deployed", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-14 18:48:48 -04:00
|
|
|
contractAddress: _this.address,
|
|
|
|
operation: "getDeployed"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return _this;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._deployedPromise;
|
|
|
|
};
|
|
|
|
// @TODO:
|
|
|
|
// estimateFallback(overrides?: TransactionRequest): Promise<BigNumber>
|
|
|
|
// @TODO:
|
|
|
|
// estimateDeploy(bytecode: string, ...args): Promise<BigNumber>
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.fallback = function (overrides) {
|
2019-05-14 18:48:48 -04:00
|
|
|
var _this = this;
|
|
|
|
if (!this.signer) {
|
2019-09-08 02:46:53 -04:00
|
|
|
logger.throwError("sending a transactions require a signer", logger_1.Logger.errors.UNSUPPORTED_OPERATION, { operation: "sendTransaction(fallback)" });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
var tx = (0, properties_1.shallowCopy)(overrides || {});
|
2019-05-14 18:48:48 -04:00
|
|
|
["from", "to"].forEach(function (key) {
|
|
|
|
if (tx[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("cannot override " + key, logger_1.Logger.errors.UNSUPPORTED_OPERATION, { operation: key });
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2020-03-12 19:14:50 +01:00
|
|
|
tx.to = this.resolvedAddress;
|
2019-05-14 18:48:48 -04:00
|
|
|
return this.deployed().then(function () {
|
|
|
|
return _this.signer.sendTransaction(tx);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
// Reconnect to a different signer or provider
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.connect = function (signerOrProvider) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (signerOrProvider) === "string") {
|
|
|
|
signerOrProvider = new abstract_signer_1.VoidSigner(signerOrProvider, this.provider);
|
|
|
|
}
|
|
|
|
var contract = new (this.constructor)(this.address, this.interface, signerOrProvider);
|
|
|
|
if (this.deployTransaction) {
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(contract, "deployTransaction", this.deployTransaction);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
return contract;
|
|
|
|
};
|
|
|
|
// Re-attach to a different on-chain instance of this contract
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.attach = function (addressOrName) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return new (this.constructor)(addressOrName, this.interface, this.signer || this.provider);
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.isIndexed = function (value) {
|
2019-06-11 17:57:04 -04:00
|
|
|
return abi_1.Indexed.isIndexed(value);
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype._normalizeRunningEvent = function (runningEvent) {
|
2019-05-23 19:13:44 -04:00
|
|
|
// Already have an instance of this event running; we can re-use it
|
|
|
|
if (this._runningEvents[runningEvent.tag]) {
|
|
|
|
return this._runningEvents[runningEvent.tag];
|
|
|
|
}
|
|
|
|
return runningEvent;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype._getRunningEvent = function (eventName) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (typeof (eventName) === "string") {
|
2019-05-23 19:13:44 -04:00
|
|
|
// Listen for "error" events (if your contract has an error event, include
|
|
|
|
// the full signature to bypass this special event keyword)
|
|
|
|
if (eventName === "error") {
|
|
|
|
return this._normalizeRunningEvent(new ErrorRunningEvent());
|
|
|
|
}
|
2020-04-25 03:54:54 -04:00
|
|
|
// Listen for any event that is registered
|
|
|
|
if (eventName === "event") {
|
|
|
|
return this._normalizeRunningEvent(new RunningEvent("event", null));
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// Listen for any event
|
|
|
|
if (eventName === "*") {
|
2019-05-23 19:13:44 -04:00
|
|
|
return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// Get the event Fragment (throws if ambiguous/unknown event)
|
2019-05-23 19:13:44 -04:00
|
|
|
var fragment = this.interface.getEvent(eventName);
|
|
|
|
return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
// We have topics to filter by...
|
|
|
|
if (eventName.topics && eventName.topics.length > 0) {
|
|
|
|
// Is it a known topichash? (throws if no matching topichash)
|
|
|
|
try {
|
2020-05-29 21:27:59 -04:00
|
|
|
var topic = eventName.topics[0];
|
|
|
|
if (typeof (topic) !== "string") {
|
|
|
|
throw new Error("invalid topic"); // @TODO: May happen for anonymous events
|
|
|
|
}
|
|
|
|
var fragment = this.interface.getEvent(topic);
|
2020-04-16 21:59:53 -04:00
|
|
|
return this._normalizeRunningEvent(new FragmentRunningEvent(this.address, this.interface, fragment, eventName.topics));
|
2019-05-23 19:13:44 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
catch (error) { }
|
|
|
|
// Filter by the unknown topichash
|
|
|
|
var filter = {
|
|
|
|
address: this.address,
|
|
|
|
topics: eventName.topics
|
|
|
|
};
|
|
|
|
return this._normalizeRunningEvent(new RunningEvent(getEventTag(filter), filter));
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-04-16 21:59:53 -04:00
|
|
|
return this._normalizeRunningEvent(new WildcardRunningEvent(this.address, this.interface));
|
2019-05-23 19:13:44 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype._checkRunningEvents = function (runningEvent) {
|
2019-05-23 19:13:44 -04:00
|
|
|
if (runningEvent.listenerCount() === 0) {
|
|
|
|
delete this._runningEvents[runningEvent.tag];
|
2020-04-15 18:28:04 -04:00
|
|
|
// If we have a poller for this, remove it
|
|
|
|
var emit = this._wrappedEmits[runningEvent.tag];
|
2021-04-18 19:33:26 -04:00
|
|
|
if (emit && runningEvent.filter) {
|
2020-04-15 18:28:04 -04:00
|
|
|
this.provider.off(runningEvent.filter, emit);
|
|
|
|
delete this._wrappedEmits[runningEvent.tag];
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2020-04-16 21:59:53 -04:00
|
|
|
// Subclasses can override this to gracefully recover
|
|
|
|
// from parse errors if they wish
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype._wrapEvent = function (runningEvent, log, listener) {
|
2019-05-14 18:48:48 -04:00
|
|
|
var _this = this;
|
2021-10-16 02:29:27 -04:00
|
|
|
var event = (0, properties_1.deepCopy)(log);
|
2019-05-14 18:48:48 -04:00
|
|
|
event.removeListener = function () {
|
|
|
|
if (!listener) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
runningEvent.removeListener(listener);
|
|
|
|
_this._checkRunningEvents(runningEvent);
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
event.getBlock = function () { return _this.provider.getBlock(log.blockHash); };
|
|
|
|
event.getTransaction = function () { return _this.provider.getTransaction(log.transactionHash); };
|
|
|
|
event.getTransactionReceipt = function () { return _this.provider.getTransactionReceipt(log.transactionHash); };
|
2020-04-16 21:59:53 -04:00
|
|
|
// This may throw if the topics and data mismatch the signature
|
|
|
|
runningEvent.prepareEvent(event);
|
2019-05-14 18:48:48 -04:00
|
|
|
return event;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype._addEventListener = function (runningEvent, listener, once) {
|
2019-05-14 18:48:48 -04:00
|
|
|
var _this = this;
|
|
|
|
if (!this.provider) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("events require a provider or a signer with a provider", logger_1.Logger.errors.UNSUPPORTED_OPERATION, { operation: "once" });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
runningEvent.addListener(listener, once);
|
2024-02-23 22:30:55 -05:00
|
|
|
// Track this running event and its listeners (may already be there; but no harm in updating)
|
2019-05-23 19:13:44 -04:00
|
|
|
this._runningEvents[runningEvent.tag] = runningEvent;
|
2020-04-16 21:59:53 -04:00
|
|
|
// If we are not polling the provider, start polling
|
2019-05-23 19:13:44 -04:00
|
|
|
if (!this._wrappedEmits[runningEvent.tag]) {
|
|
|
|
var wrappedEmit = function (log) {
|
2020-04-25 03:54:54 -04:00
|
|
|
var event = _this._wrapEvent(runningEvent, log, listener);
|
|
|
|
// Try to emit the result for the parameterized event...
|
|
|
|
if (event.decodeError == null) {
|
|
|
|
try {
|
|
|
|
var args = runningEvent.getEmit(event);
|
2021-10-16 02:29:27 -04:00
|
|
|
_this.emit.apply(_this, __spreadArray([runningEvent.filter], args, false));
|
2020-04-25 03:54:54 -04:00
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
event.decodeError = error.error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Always emit "event" for fragment-base events
|
|
|
|
if (runningEvent.filter != null) {
|
|
|
|
_this.emit("event", event);
|
2020-04-16 21:59:53 -04:00
|
|
|
}
|
2020-04-25 03:54:54 -04:00
|
|
|
// Emit "error" if there was an error
|
|
|
|
if (event.decodeError != null) {
|
|
|
|
_this.emit("error", event.decodeError, event);
|
2020-04-16 21:59:53 -04:00
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
};
|
|
|
|
this._wrappedEmits[runningEvent.tag] = wrappedEmit;
|
|
|
|
// Special events, like "error" do not have a filter
|
|
|
|
if (runningEvent.filter != null) {
|
|
|
|
this.provider.on(runningEvent.filter, wrappedEmit);
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.queryFilter = function (event, fromBlockOrBlockhash, toBlock) {
|
2019-05-24 18:15:42 -04:00
|
|
|
var _this = this;
|
|
|
|
var runningEvent = this._getRunningEvent(event);
|
2021-10-16 02:29:27 -04:00
|
|
|
var filter = (0, properties_1.shallowCopy)(runningEvent.filter);
|
|
|
|
if (typeof (fromBlockOrBlockhash) === "string" && (0, bytes_1.isHexString)(fromBlockOrBlockhash, 32)) {
|
2019-05-24 18:15:42 -04:00
|
|
|
if (toBlock != null) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("cannot specify toBlock with blockhash", "toBlock", toBlock);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2020-05-21 00:07:41 -04:00
|
|
|
filter.blockHash = fromBlockOrBlockhash;
|
2019-05-24 18:15:42 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
filter.fromBlock = ((fromBlockOrBlockhash != null) ? fromBlockOrBlockhash : 0);
|
|
|
|
filter.toBlock = ((toBlock != null) ? toBlock : "latest");
|
|
|
|
}
|
|
|
|
return this.provider.getLogs(filter).then(function (logs) {
|
|
|
|
return logs.map(function (log) { return _this._wrapEvent(runningEvent, log, null); });
|
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.on = function (event, listener) {
|
2019-05-23 19:13:44 -04:00
|
|
|
this._addEventListener(this._getRunningEvent(event), listener, false);
|
2019-05-14 18:48:48 -04:00
|
|
|
return this;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.once = function (event, listener) {
|
2019-05-23 19:13:44 -04:00
|
|
|
this._addEventListener(this._getRunningEvent(event), listener, true);
|
2019-05-14 18:48:48 -04:00
|
|
|
return this;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.emit = function (eventName) {
|
2019-05-14 18:48:48 -04:00
|
|
|
var args = [];
|
|
|
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
|
|
args[_i - 1] = arguments[_i];
|
|
|
|
}
|
|
|
|
if (!this.provider) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
var runningEvent = this._getRunningEvent(eventName);
|
|
|
|
var result = (runningEvent.run(args) > 0);
|
|
|
|
// May have drained all the "once" events; check for living events
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:48:48 -04:00
|
|
|
return result;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.listenerCount = function (eventName) {
|
2021-01-08 03:32:59 -05:00
|
|
|
var _this = this;
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!this.provider) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-08 03:32:59 -05:00
|
|
|
if (eventName == null) {
|
|
|
|
return Object.keys(this._runningEvents).reduce(function (accum, key) {
|
|
|
|
return accum + _this._runningEvents[key].listenerCount();
|
|
|
|
}, 0);
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
return this._getRunningEvent(eventName).listenerCount();
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.listeners = function (eventName) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!this.provider) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (eventName == null) {
|
2019-05-23 19:13:44 -04:00
|
|
|
var result_1 = [];
|
|
|
|
for (var tag in this._runningEvents) {
|
|
|
|
this._runningEvents[tag].listeners().forEach(function (listener) {
|
|
|
|
result_1.push(listener);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result_1;
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
return this._getRunningEvent(eventName).listeners();
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.removeAllListeners = function (eventName) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!this.provider) {
|
|
|
|
return this;
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
if (eventName == null) {
|
|
|
|
for (var tag in this._runningEvents) {
|
|
|
|
var runningEvent_1 = this._runningEvents[tag];
|
|
|
|
runningEvent_1.removeAllListeners();
|
|
|
|
this._checkRunningEvents(runningEvent_1);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// Delete any listeners
|
|
|
|
var runningEvent = this._getRunningEvent(eventName);
|
|
|
|
runningEvent.removeAllListeners();
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:48:48 -04:00
|
|
|
return this;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.off = function (eventName, listener) {
|
2019-05-14 18:48:48 -04:00
|
|
|
if (!this.provider) {
|
|
|
|
return this;
|
|
|
|
}
|
2019-05-23 19:13:44 -04:00
|
|
|
var runningEvent = this._getRunningEvent(eventName);
|
|
|
|
runningEvent.removeListener(listener);
|
|
|
|
this._checkRunningEvents(runningEvent);
|
2019-05-14 18:48:48 -04:00
|
|
|
return this;
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
BaseContract.prototype.removeListener = function (eventName, listener) {
|
2019-05-14 18:48:48 -04:00
|
|
|
return this.off(eventName, listener);
|
|
|
|
};
|
2021-03-30 15:22:45 -04:00
|
|
|
return BaseContract;
|
2019-05-14 18:48:48 -04:00
|
|
|
}());
|
2021-03-30 15:22:45 -04:00
|
|
|
exports.BaseContract = BaseContract;
|
|
|
|
var Contract = /** @class */ (function (_super) {
|
|
|
|
__extends(Contract, _super);
|
|
|
|
function Contract() {
|
|
|
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
|
|
}
|
|
|
|
return Contract;
|
|
|
|
}(BaseContract));
|
2019-05-14 18:48:48 -04:00
|
|
|
exports.Contract = Contract;
|
|
|
|
var ContractFactory = /** @class */ (function () {
|
|
|
|
function ContractFactory(contractInterface, bytecode, signer) {
|
|
|
|
var _newTarget = this.constructor;
|
|
|
|
var bytecodeHex = null;
|
|
|
|
if (typeof (bytecode) === "string") {
|
|
|
|
bytecodeHex = bytecode;
|
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
else if ((0, bytes_1.isBytes)(bytecode)) {
|
|
|
|
bytecodeHex = (0, bytes_1.hexlify)(bytecode);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
else if (bytecode && typeof (bytecode.object) === "string") {
|
|
|
|
// Allow the bytecode object from the Solidity compiler
|
|
|
|
bytecodeHex = bytecode.object;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Crash in the next verification step
|
|
|
|
bytecodeHex = "!";
|
|
|
|
}
|
|
|
|
// Make sure it is 0x prefixed
|
|
|
|
if (bytecodeHex.substring(0, 2) !== "0x") {
|
|
|
|
bytecodeHex = "0x" + bytecodeHex;
|
|
|
|
}
|
|
|
|
// Make sure the final result is valid bytecode
|
2021-10-16 02:29:27 -04:00
|
|
|
if (!(0, bytes_1.isHexString)(bytecodeHex) || (bytecodeHex.length % 2)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid bytecode", "bytecode", bytecode);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
// If we have a signer, make sure it is valid
|
2019-06-11 17:57:04 -04:00
|
|
|
if (signer && !abstract_signer_1.Signer.isSigner(signer)) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwArgumentError("invalid signer", "signer", signer);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(this, "bytecode", bytecodeHex);
|
|
|
|
(0, properties_1.defineReadOnly)(this, "interface", (0, properties_1.getStatic)(_newTarget, "getInterface")(contractInterface));
|
|
|
|
(0, properties_1.defineReadOnly)(this, "signer", signer || null);
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
2021-10-16 02:29:27 -04:00
|
|
|
// @TODO: Future; rename to populateTransaction?
|
2019-05-14 18:48:48 -04:00
|
|
|
ContractFactory.prototype.getDeployTransaction = function () {
|
|
|
|
var args = [];
|
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
|
|
args[_i] = arguments[_i];
|
|
|
|
}
|
|
|
|
var tx = {};
|
|
|
|
// If we have 1 additional argument, we allow transaction overrides
|
2020-04-23 23:35:39 -04:00
|
|
|
if (args.length === this.interface.deploy.inputs.length + 1 && typeof (args[args.length - 1]) === "object") {
|
2021-10-16 02:29:27 -04:00
|
|
|
tx = (0, properties_1.shallowCopy)(args.pop());
|
2019-05-14 18:48:48 -04:00
|
|
|
for (var key in tx) {
|
|
|
|
if (!allowedTransactionKeys[key]) {
|
|
|
|
throw new Error("unknown transaction override " + key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Do not allow these to be overridden in a deployment transaction
|
|
|
|
["data", "from", "to"].forEach(function (key) {
|
|
|
|
if (tx[key] == null) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("cannot override " + key, logger_1.Logger.errors.UNSUPPORTED_OPERATION, { operation: key });
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
2021-07-29 23:30:17 -04:00
|
|
|
if (tx.value) {
|
|
|
|
var value = bignumber_1.BigNumber.from(tx.value);
|
|
|
|
if (!value.isZero() && !this.interface.deploy.payable) {
|
|
|
|
logger.throwError("non-payable constructor cannot override value", logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "overrides.value",
|
|
|
|
value: tx.value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-05-14 18:48:48 -04:00
|
|
|
// Make sure the call matches the constructor signature
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.checkArgumentCount(args.length, this.interface.deploy.inputs.length, " in Contract constructor");
|
2019-05-14 18:48:48 -04:00
|
|
|
// Set the data to the bytecode + the encoded constructor arguments
|
2021-10-16 02:29:27 -04:00
|
|
|
tx.data = (0, bytes_1.hexlify)((0, bytes_1.concat)([
|
2019-05-14 18:48:48 -04:00
|
|
|
this.bytecode,
|
|
|
|
this.interface.encodeDeploy(args)
|
|
|
|
]));
|
|
|
|
return tx;
|
|
|
|
};
|
|
|
|
ContractFactory.prototype.deploy = function () {
|
|
|
|
var args = [];
|
|
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
|
|
args[_i] = arguments[_i];
|
|
|
|
}
|
2020-04-23 23:35:39 -04:00
|
|
|
return __awaiter(this, void 0, void 0, function () {
|
|
|
|
var overrides, params, unsignedTx, tx, address, contract;
|
|
|
|
return __generator(this, function (_a) {
|
|
|
|
switch (_a.label) {
|
|
|
|
case 0:
|
|
|
|
overrides = {};
|
|
|
|
// If 1 extra parameter was passed in, it contains overrides
|
|
|
|
if (args.length === this.interface.deploy.inputs.length + 1) {
|
|
|
|
overrides = args.pop();
|
|
|
|
}
|
|
|
|
// Make sure the call matches the constructor signature
|
|
|
|
logger.checkArgumentCount(args.length, this.interface.deploy.inputs.length, " in Contract constructor");
|
|
|
|
return [4 /*yield*/, resolveAddresses(this.signer, args, this.interface.deploy.inputs)];
|
|
|
|
case 1:
|
|
|
|
params = _a.sent();
|
|
|
|
params.push(overrides);
|
|
|
|
unsignedTx = this.getDeployTransaction.apply(this, params);
|
|
|
|
return [4 /*yield*/, this.signer.sendTransaction(unsignedTx)];
|
|
|
|
case 2:
|
|
|
|
tx = _a.sent();
|
2021-10-16 02:29:27 -04:00
|
|
|
address = (0, properties_1.getStatic)(this.constructor, "getContractAddress")(tx);
|
|
|
|
contract = (0, properties_1.getStatic)(this.constructor, "getContract")(address, this.interface, this.signer);
|
2021-10-17 23:03:30 -04:00
|
|
|
// Add the modified wait that wraps events
|
|
|
|
addContractWait(contract, tx);
|
2021-10-16 02:29:27 -04:00
|
|
|
(0, properties_1.defineReadOnly)(contract, "deployTransaction", tx);
|
2020-04-23 23:35:39 -04:00
|
|
|
return [2 /*return*/, contract];
|
|
|
|
}
|
2019-06-24 21:26:48 -04:00
|
|
|
});
|
2019-05-14 18:48:48 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
ContractFactory.prototype.attach = function (address) {
|
|
|
|
return (this.constructor).getContract(address, this.interface, this.signer);
|
|
|
|
};
|
|
|
|
ContractFactory.prototype.connect = function (signer) {
|
|
|
|
return new (this.constructor)(this.interface, this.bytecode, signer);
|
|
|
|
};
|
|
|
|
ContractFactory.fromSolidity = function (compilerOutput, signer) {
|
|
|
|
if (compilerOutput == null) {
|
2019-08-02 02:10:58 -04:00
|
|
|
logger.throwError("missing compiler output", logger_1.Logger.errors.MISSING_ARGUMENT, { argument: "compilerOutput" });
|
2019-05-14 18:48:48 -04:00
|
|
|
}
|
|
|
|
if (typeof (compilerOutput) === "string") {
|
|
|
|
compilerOutput = JSON.parse(compilerOutput);
|
|
|
|
}
|
|
|
|
var abi = compilerOutput.abi;
|
|
|
|
var bytecode = null;
|
|
|
|
if (compilerOutput.bytecode) {
|
|
|
|
bytecode = compilerOutput.bytecode;
|
|
|
|
}
|
|
|
|
else if (compilerOutput.evm && compilerOutput.evm.bytecode) {
|
|
|
|
bytecode = compilerOutput.evm.bytecode;
|
|
|
|
}
|
|
|
|
return new this(abi, bytecode, signer);
|
|
|
|
};
|
|
|
|
ContractFactory.getInterface = function (contractInterface) {
|
|
|
|
return Contract.getInterface(contractInterface);
|
|
|
|
};
|
|
|
|
ContractFactory.getContractAddress = function (tx) {
|
2021-10-16 02:29:27 -04:00
|
|
|
return (0, address_1.getContractAddress)(tx);
|
2019-05-14 18:48:48 -04:00
|
|
|
};
|
|
|
|
ContractFactory.getContract = function (address, contractInterface, signer) {
|
|
|
|
return new Contract(address, contractInterface, signer);
|
|
|
|
};
|
|
|
|
return ContractFactory;
|
|
|
|
}());
|
|
|
|
exports.ContractFactory = ContractFactory;
|
2020-07-13 08:03:56 -04:00
|
|
|
//# sourceMappingURL=index.js.map
|