index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Logger = exports.ErrorCode = exports.LogLevel = void 0;
  4. var _permanentCensorErrors = false;
  5. var _censorErrors = false;
  6. var LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
  7. var _logLevel = LogLevels["default"];
  8. var _version_1 = require("./_version");
  9. var _globalLogger = null;
  10. function _checkNormalize() {
  11. try {
  12. var missing_1 = [];
  13. // Make sure all forms of normalization are supported
  14. ["NFD", "NFC", "NFKD", "NFKC"].forEach(function (form) {
  15. try {
  16. if ("test".normalize(form) !== "test") {
  17. throw new Error("bad normalize");
  18. }
  19. ;
  20. }
  21. catch (error) {
  22. missing_1.push(form);
  23. }
  24. });
  25. if (missing_1.length) {
  26. throw new Error("missing " + missing_1.join(", "));
  27. }
  28. if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) {
  29. throw new Error("broken implementation");
  30. }
  31. }
  32. catch (error) {
  33. return error.message;
  34. }
  35. return null;
  36. }
  37. var _normalizeError = _checkNormalize();
  38. var LogLevel;
  39. (function (LogLevel) {
  40. LogLevel["DEBUG"] = "DEBUG";
  41. LogLevel["INFO"] = "INFO";
  42. LogLevel["WARNING"] = "WARNING";
  43. LogLevel["ERROR"] = "ERROR";
  44. LogLevel["OFF"] = "OFF";
  45. })(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
  46. var ErrorCode;
  47. (function (ErrorCode) {
  48. ///////////////////
  49. // Generic Errors
  50. // Unknown Error
  51. ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
  52. // Not Implemented
  53. ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
  54. // Unsupported Operation
  55. // - operation
  56. ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
  57. // Network Error (i.e. Ethereum Network, such as an invalid chain ID)
  58. // - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown)
  59. ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
  60. // Some sort of bad response from the server
  61. ErrorCode["SERVER_ERROR"] = "SERVER_ERROR";
  62. // Timeout
  63. ErrorCode["TIMEOUT"] = "TIMEOUT";
  64. ///////////////////
  65. // Operational Errors
  66. // Buffer Overrun
  67. ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
  68. // Numeric Fault
  69. // - operation: the operation being executed
  70. // - fault: the reason this faulted
  71. ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT";
  72. ///////////////////
  73. // Argument Errors
  74. // Missing new operator to an object
  75. // - name: The name of the class
  76. ErrorCode["MISSING_NEW"] = "MISSING_NEW";
  77. // Invalid argument (e.g. value is incompatible with type) to a function:
  78. // - argument: The argument name that was invalid
  79. // - value: The value of the argument
  80. ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
  81. // Missing argument to a function:
  82. // - count: The number of arguments received
  83. // - expectedCount: The number of arguments expected
  84. ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
  85. // Too many arguments
  86. // - count: The number of arguments received
  87. // - expectedCount: The number of arguments expected
  88. ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
  89. ///////////////////
  90. // Blockchain Errors
  91. // Call exception
  92. // - transaction: the transaction
  93. // - address?: the contract address
  94. // - args?: The arguments passed into the function
  95. // - method?: The Solidity method signature
  96. // - errorSignature?: The EIP848 error signature
  97. // - errorArgs?: The EIP848 error parameters
  98. // - reason: The reason (only for EIP848 "Error(string)")
  99. ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION";
  100. // Insufficient funds (< value + gasLimit * gasPrice)
  101. // - transaction: the transaction attempted
  102. ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
  103. // Nonce has already been used
  104. // - transaction: the transaction attempted
  105. ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED";
  106. // The replacement fee for the transaction is too low
  107. // - transaction: the transaction attempted
  108. ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
  109. // The gas limit could not be estimated
  110. // - transaction: the transaction passed to estimateGas
  111. ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
  112. // The transaction was replaced by one with a higher gas price
  113. // - reason: "cancelled", "replaced" or "repriced"
  114. // - cancelled: true if reason == "cancelled" or reason == "replaced")
  115. // - hash: original transaction hash
  116. // - replacement: the full TransactionsResponse for the replacement
  117. // - receipt: the receipt of the replacement
  118. ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
  119. })(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {}));
  120. ;
  121. var HEX = "0123456789abcdef";
  122. var Logger = /** @class */ (function () {
  123. function Logger(version) {
  124. Object.defineProperty(this, "version", {
  125. enumerable: true,
  126. value: version,
  127. writable: false
  128. });
  129. }
  130. Logger.prototype._log = function (logLevel, args) {
  131. var level = logLevel.toLowerCase();
  132. if (LogLevels[level] == null) {
  133. this.throwArgumentError("invalid log level name", "logLevel", logLevel);
  134. }
  135. if (_logLevel > LogLevels[level]) {
  136. return;
  137. }
  138. console.log.apply(console, args);
  139. };
  140. Logger.prototype.debug = function () {
  141. var args = [];
  142. for (var _i = 0; _i < arguments.length; _i++) {
  143. args[_i] = arguments[_i];
  144. }
  145. this._log(Logger.levels.DEBUG, args);
  146. };
  147. Logger.prototype.info = function () {
  148. var args = [];
  149. for (var _i = 0; _i < arguments.length; _i++) {
  150. args[_i] = arguments[_i];
  151. }
  152. this._log(Logger.levels.INFO, args);
  153. };
  154. Logger.prototype.warn = function () {
  155. var args = [];
  156. for (var _i = 0; _i < arguments.length; _i++) {
  157. args[_i] = arguments[_i];
  158. }
  159. this._log(Logger.levels.WARNING, args);
  160. };
  161. Logger.prototype.makeError = function (message, code, params) {
  162. // Errors are being censored
  163. if (_censorErrors) {
  164. return this.makeError("censored error", code, {});
  165. }
  166. if (!code) {
  167. code = Logger.errors.UNKNOWN_ERROR;
  168. }
  169. if (!params) {
  170. params = {};
  171. }
  172. var messageDetails = [];
  173. Object.keys(params).forEach(function (key) {
  174. var value = params[key];
  175. try {
  176. if (value instanceof Uint8Array) {
  177. var hex = "";
  178. for (var i = 0; i < value.length; i++) {
  179. hex += HEX[value[i] >> 4];
  180. hex += HEX[value[i] & 0x0f];
  181. }
  182. messageDetails.push(key + "=Uint8Array(0x" + hex + ")");
  183. }
  184. else {
  185. messageDetails.push(key + "=" + JSON.stringify(value));
  186. }
  187. }
  188. catch (error) {
  189. messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
  190. }
  191. });
  192. messageDetails.push("code=" + code);
  193. messageDetails.push("version=" + this.version);
  194. var reason = message;
  195. var url = "";
  196. switch (code) {
  197. case ErrorCode.NUMERIC_FAULT: {
  198. url = "NUMERIC_FAULT";
  199. var fault = message;
  200. switch (fault) {
  201. case "overflow":
  202. case "underflow":
  203. case "division-by-zero":
  204. url += "-" + fault;
  205. break;
  206. case "negative-power":
  207. case "negative-width":
  208. url += "-unsupported";
  209. break;
  210. case "unbound-bitwise-result":
  211. url += "-unbound-result";
  212. break;
  213. }
  214. break;
  215. }
  216. case ErrorCode.CALL_EXCEPTION:
  217. case ErrorCode.INSUFFICIENT_FUNDS:
  218. case ErrorCode.MISSING_NEW:
  219. case ErrorCode.NONCE_EXPIRED:
  220. case ErrorCode.REPLACEMENT_UNDERPRICED:
  221. case ErrorCode.TRANSACTION_REPLACED:
  222. case ErrorCode.UNPREDICTABLE_GAS_LIMIT:
  223. url = code;
  224. break;
  225. }
  226. if (url) {
  227. message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]";
  228. }
  229. if (messageDetails.length) {
  230. message += " (" + messageDetails.join(", ") + ")";
  231. }
  232. // @TODO: Any??
  233. var error = new Error(message);
  234. error.reason = reason;
  235. error.code = code;
  236. Object.keys(params).forEach(function (key) {
  237. error[key] = params[key];
  238. });
  239. return error;
  240. };
  241. Logger.prototype.throwError = function (message, code, params) {
  242. throw this.makeError(message, code, params);
  243. };
  244. Logger.prototype.throwArgumentError = function (message, name, value) {
  245. return this.throwError(message, Logger.errors.INVALID_ARGUMENT, {
  246. argument: name,
  247. value: value
  248. });
  249. };
  250. Logger.prototype.assert = function (condition, message, code, params) {
  251. if (!!condition) {
  252. return;
  253. }
  254. this.throwError(message, code, params);
  255. };
  256. Logger.prototype.assertArgument = function (condition, message, name, value) {
  257. if (!!condition) {
  258. return;
  259. }
  260. this.throwArgumentError(message, name, value);
  261. };
  262. Logger.prototype.checkNormalize = function (message) {
  263. if (message == null) {
  264. message = "platform missing String.prototype.normalize";
  265. }
  266. if (_normalizeError) {
  267. this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, {
  268. operation: "String.prototype.normalize", form: _normalizeError
  269. });
  270. }
  271. };
  272. Logger.prototype.checkSafeUint53 = function (value, message) {
  273. if (typeof (value) !== "number") {
  274. return;
  275. }
  276. if (message == null) {
  277. message = "value not safe";
  278. }
  279. if (value < 0 || value >= 0x1fffffffffffff) {
  280. this.throwError(message, Logger.errors.NUMERIC_FAULT, {
  281. operation: "checkSafeInteger",
  282. fault: "out-of-safe-range",
  283. value: value
  284. });
  285. }
  286. if (value % 1) {
  287. this.throwError(message, Logger.errors.NUMERIC_FAULT, {
  288. operation: "checkSafeInteger",
  289. fault: "non-integer",
  290. value: value
  291. });
  292. }
  293. };
  294. Logger.prototype.checkArgumentCount = function (count, expectedCount, message) {
  295. if (message) {
  296. message = ": " + message;
  297. }
  298. else {
  299. message = "";
  300. }
  301. if (count < expectedCount) {
  302. this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, {
  303. count: count,
  304. expectedCount: expectedCount
  305. });
  306. }
  307. if (count > expectedCount) {
  308. this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, {
  309. count: count,
  310. expectedCount: expectedCount
  311. });
  312. }
  313. };
  314. Logger.prototype.checkNew = function (target, kind) {
  315. if (target === Object || target == null) {
  316. this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
  317. }
  318. };
  319. Logger.prototype.checkAbstract = function (target, kind) {
  320. if (target === kind) {
  321. this.throwError("cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class", Logger.errors.UNSUPPORTED_OPERATION, { name: target.name, operation: "new" });
  322. }
  323. else if (target === Object || target == null) {
  324. this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
  325. }
  326. };
  327. Logger.globalLogger = function () {
  328. if (!_globalLogger) {
  329. _globalLogger = new Logger(_version_1.version);
  330. }
  331. return _globalLogger;
  332. };
  333. Logger.setCensorship = function (censorship, permanent) {
  334. if (!censorship && permanent) {
  335. this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, {
  336. operation: "setCensorship"
  337. });
  338. }
  339. if (_permanentCensorErrors) {
  340. if (!censorship) {
  341. return;
  342. }
  343. this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, {
  344. operation: "setCensorship"
  345. });
  346. }
  347. _censorErrors = !!censorship;
  348. _permanentCensorErrors = !!permanent;
  349. };
  350. Logger.setLogLevel = function (logLevel) {
  351. var level = LogLevels[logLevel.toLowerCase()];
  352. if (level == null) {
  353. Logger.globalLogger().warn("invalid log level - " + logLevel);
  354. return;
  355. }
  356. _logLevel = level;
  357. };
  358. Logger.from = function (version) {
  359. return new Logger(version);
  360. };
  361. Logger.errors = ErrorCode;
  362. Logger.levels = LogLevel;
  363. return Logger;
  364. }());
  365. exports.Logger = Logger;
  366. //# sourceMappingURL=index.js.map