essential-eth 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/dist/cjs/classes/Contract.js +310 -71
  2. package/dist/cjs/classes/test/Contract/crv-abi.js +252 -256
  3. package/dist/cjs/classes/test/Contract/crv.test.js +690 -0
  4. package/dist/cjs/classes/test/Contract/ens-abi.js +497 -212
  5. package/dist/cjs/classes/test/Contract/ens.test.js +217 -0
  6. package/dist/cjs/classes/test/Contract/fei-abi.js +605 -284
  7. package/dist/cjs/classes/test/Contract/fei.test.js +224 -0
  8. package/dist/cjs/classes/test/Contract/foo-abi.js +44 -21
  9. package/dist/cjs/classes/test/Contract/foo.test.js +33 -0
  10. package/dist/cjs/classes/test/Contract/uni.test.js +387 -0
  11. package/dist/cjs/classes/test/Contract/uniswap-abi.js +64 -68
  12. package/dist/cjs/classes/utils/clean-block.d.ts +1 -2
  13. package/dist/cjs/classes/utils/clean-block.js +52 -30
  14. package/dist/cjs/classes/utils/clean-log.js +42 -20
  15. package/dist/cjs/classes/utils/clean-transaction-receipt.js +51 -29
  16. package/dist/cjs/classes/utils/clean-transaction.js +52 -29
  17. package/dist/cjs/classes/utils/encode-decode-transaction.d.ts +1 -1
  18. package/dist/cjs/classes/utils/encode-decode-transaction.js +163 -83
  19. package/dist/cjs/classes/utils/fetchers.js +169 -42
  20. package/dist/cjs/classes/utils/hex-to-decimal.js +1 -6
  21. package/dist/cjs/classes/utils/prepare-transaction.js +59 -36
  22. package/dist/cjs/index.js +25 -54
  23. package/dist/cjs/index.umd.js +1 -1
  24. package/dist/cjs/index.umd.js.map +1 -1
  25. package/dist/cjs/logger/logger.js +79 -23
  26. package/dist/cjs/logger/package-version.d.ts +1 -1
  27. package/dist/cjs/logger/package-version.js +1 -4
  28. package/dist/cjs/providers/BaseProvider.js +824 -475
  29. package/dist/cjs/providers/FallthroughProvider.js +118 -37
  30. package/dist/cjs/providers/JsonRpcProvider.js +93 -28
  31. package/dist/cjs/providers/test/fallthrough-provider/get-gas-price.test.js +228 -0
  32. package/dist/cjs/providers/test/get-transaction-count.test.js +276 -0
  33. package/dist/cjs/providers/test/json-rpc-provider/call.test.js +436 -0
  34. package/dist/cjs/providers/test/json-rpc-provider/estimate-gas.test.js +288 -0
  35. package/dist/cjs/providers/test/json-rpc-provider/get-balance.test.js +261 -0
  36. package/dist/cjs/providers/test/json-rpc-provider/get-block-number.test.js +230 -0
  37. package/dist/cjs/providers/test/json-rpc-provider/get-block.test.js +501 -0
  38. package/dist/cjs/providers/test/json-rpc-provider/get-code.test.js +311 -0
  39. package/dist/cjs/providers/test/json-rpc-provider/get-gas-price.test.js +202 -0
  40. package/dist/cjs/providers/test/json-rpc-provider/get-logs.test.js +361 -0
  41. package/dist/cjs/providers/test/json-rpc-provider/get-network.test.js +274 -0
  42. package/dist/cjs/providers/test/json-rpc-provider/get-transaction-receipt.test.js +301 -0
  43. package/dist/cjs/providers/test/json-rpc-provider/get-transaction.test.js +307 -0
  44. package/dist/cjs/providers/test/rpc-urls.js +9 -12
  45. package/dist/cjs/providers/utils/chains-info.js +1 -3
  46. package/dist/cjs/shared/tiny-big/helpers.js +93 -42
  47. package/dist/cjs/shared/tiny-big/helpers.test.js +29 -0
  48. package/dist/cjs/shared/tiny-big/tiny-big.js +161 -72
  49. package/dist/cjs/shared/tiny-big/tiny-big.test.js +34 -0
  50. package/dist/cjs/shared/validate-type.js +7 -7
  51. package/dist/cjs/types/Block.types.js +1 -2
  52. package/dist/cjs/types/Contract.types.js +1 -2
  53. package/dist/cjs/types/Filter.types.js +1 -2
  54. package/dist/cjs/types/Network.types.js +3 -2
  55. package/dist/cjs/types/Transaction.types.js +1 -2
  56. package/dist/cjs/utils/bytes.js +286 -318
  57. package/dist/cjs/utils/compute-address.js +8 -14
  58. package/dist/cjs/utils/compute-public-key.js +5 -10
  59. package/dist/cjs/utils/ether-to-gwei.js +10 -11
  60. package/dist/cjs/utils/ether-to-wei.js +10 -11
  61. package/dist/cjs/utils/gwei-to-ether.js +10 -11
  62. package/dist/cjs/utils/hash-message.d.ts +1 -1
  63. package/dist/cjs/utils/hash-message.js +11 -14
  64. package/dist/cjs/utils/is-address.js +8 -12
  65. package/dist/cjs/utils/keccak256.js +8 -14
  66. package/dist/cjs/utils/solidity-keccak256.js +49 -60
  67. package/dist/cjs/utils/split-signature.js +55 -73
  68. package/dist/cjs/utils/tests/bytes/arrayify.test.js +40 -0
  69. package/dist/cjs/utils/tests/bytes/concat.test.js +15 -0
  70. package/dist/cjs/utils/tests/bytes/hex-concat.test.js +63 -0
  71. package/dist/cjs/utils/tests/bytes/hex-data-length.test.js +49 -0
  72. package/dist/cjs/utils/tests/bytes/hex-data-slice.test.js +52 -0
  73. package/dist/cjs/utils/tests/bytes/hex-strip-zeros.test.js +58 -0
  74. package/dist/cjs/utils/tests/bytes/hex-value.test.js +85 -0
  75. package/dist/cjs/utils/tests/bytes/hex-zero-pad.test.js +62 -0
  76. package/dist/cjs/utils/tests/bytes/hexlify.test.js +62 -0
  77. package/dist/cjs/utils/tests/bytes/is-bytes-like.test.js +69 -0
  78. package/dist/cjs/utils/tests/bytes/is-bytes.test.js +33 -0
  79. package/dist/cjs/utils/tests/bytes/is-hex-string.test.js +88 -0
  80. package/dist/cjs/utils/tests/bytes/strip-zeros.test.js +57 -0
  81. package/dist/cjs/utils/tests/bytes/zero-pad.test.js +80 -0
  82. package/dist/cjs/utils/tests/compute-address.test.js +27 -0
  83. package/dist/cjs/utils/tests/compute-public-key.test.js +15 -0
  84. package/dist/cjs/utils/tests/ether-to-gwei.test.js +26 -0
  85. package/dist/cjs/utils/tests/ether-to-wei.test.js +44 -0
  86. package/dist/cjs/utils/tests/gwei-to-ether.test.js +28 -0
  87. package/dist/cjs/utils/tests/hash-message.test.js +21 -0
  88. package/dist/cjs/utils/tests/is-address.test.js +55 -0
  89. package/dist/cjs/utils/tests/keccak256.test.js +97 -0
  90. package/dist/cjs/utils/tests/solidity-keccak256.test.js +310 -0
  91. package/dist/cjs/utils/tests/split-signature.test.js +25 -0
  92. package/dist/cjs/utils/tests/to-checksum-address.test.js +42 -0
  93. package/dist/cjs/utils/tests/to-utf8-bytes.test.js +14 -0
  94. package/dist/cjs/utils/tests/wei-to-ether.test.js +51 -0
  95. package/dist/cjs/utils/to-checksum-address.js +15 -20
  96. package/dist/cjs/utils/to-utf8-bytes.js +1 -6
  97. package/dist/cjs/utils/wei-to-ether.js +13 -15
  98. package/dist/esm/classes/Contract.js +3 -7
  99. package/dist/esm/classes/utils/clean-block.d.ts +1 -2
  100. package/dist/esm/classes/utils/clean-block.js +1 -1
  101. package/dist/esm/classes/utils/clean-transaction-receipt.js +2 -1
  102. package/dist/esm/classes/utils/clean-transaction.js +2 -1
  103. package/dist/esm/classes/utils/encode-decode-transaction.d.ts +1 -1
  104. package/dist/esm/classes/utils/encode-decode-transaction.js +37 -2
  105. package/dist/esm/logger/package-version.d.ts +1 -1
  106. package/dist/esm/logger/package-version.js +1 -1
  107. package/dist/esm/utils/compute-address.js +2 -1
  108. package/dist/esm/utils/hash-message.d.ts +1 -1
  109. package/dist/esm/utils/hash-message.js +3 -1
  110. package/dist/esm/utils/is-address.js +1 -1
  111. package/package.json +8 -6
  112. package/readme.md +34 -3
@@ -1,50 +1,130 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FallthroughProvider = void 0;
4
- const logger_1 = require("../logger/logger");
5
- const BaseProvider_1 = require("./BaseProvider");
1
+ function _assertThisInitialized(self) {
2
+ if (self === void 0) {
3
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
4
+ }
5
+ return self;
6
+ }
7
+ function _classCallCheck(instance, Constructor) {
8
+ if (!(instance instanceof Constructor)) {
9
+ throw new TypeError("Cannot call a class as a function");
10
+ }
11
+ }
12
+ function _getPrototypeOf(o) {
13
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
14
+ return o.__proto__ || Object.getPrototypeOf(o);
15
+ };
16
+ return _getPrototypeOf(o);
17
+ }
18
+ function _inherits(subClass, superClass) {
19
+ if (typeof superClass !== "function" && superClass !== null) {
20
+ throw new TypeError("Super expression must either be null or a function");
21
+ }
22
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
23
+ constructor: {
24
+ value: subClass,
25
+ writable: true,
26
+ configurable: true
27
+ }
28
+ });
29
+ if (superClass) _setPrototypeOf(subClass, superClass);
30
+ }
31
+ function _possibleConstructorReturn(self, call) {
32
+ if (call && (_typeof(call) === "object" || typeof call === "function")) {
33
+ return call;
34
+ }
35
+ return _assertThisInitialized(self);
36
+ }
37
+ function _setPrototypeOf(o, p) {
38
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
39
+ o.__proto__ = p;
40
+ return o;
41
+ };
42
+ return _setPrototypeOf(o, p);
43
+ }
44
+ var _typeof = function(obj) {
45
+ "@swc/helpers - typeof";
46
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
47
+ };
48
+ function _isNativeReflectConstruct() {
49
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
50
+ if (Reflect.construct.sham) return false;
51
+ if (typeof Proxy === "function") return true;
52
+ try {
53
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
54
+ return true;
55
+ } catch (e) {
56
+ return false;
57
+ }
58
+ }
59
+ function _createSuper(Derived) {
60
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
61
+ return function _createSuperInternal() {
62
+ var Super = _getPrototypeOf(Derived), result;
63
+ if (hasNativeReflectConstruct) {
64
+ var NewTarget = _getPrototypeOf(this).constructor;
65
+ result = Reflect.construct(Super, arguments, NewTarget);
66
+ } else {
67
+ result = Super.apply(this, arguments);
68
+ }
69
+ return _possibleConstructorReturn(this, result);
70
+ };
71
+ }
72
+ import { logger } from "../logger/logger";
73
+ import { BaseProvider } from "./BaseProvider";
6
74
  // https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
7
- const promiseTimeout = (prom, time) => Promise.race([
8
- prom,
9
- new Promise((_r, reject) => setTimeout(() => reject('Promise timed out'), time)),
10
- ]);
11
- const DEFAULT_TIMEOUT_DURATION = 8000;
75
+ var promiseTimeout = function(prom, time) {
76
+ return Promise.race([
77
+ prom,
78
+ new Promise(function(_r, reject) {
79
+ return setTimeout(function() {
80
+ return reject("Promise timed out");
81
+ }, time);
82
+ })
83
+ ]);
84
+ };
85
+ var DEFAULT_TIMEOUT_DURATION = 8000;
12
86
  /**
13
87
  * @beta
14
88
  * A JSON RPC Provider which moves to the next URL when one fails.
15
- */
16
- class FallthroughProvider extends BaseProvider_1.BaseProvider {
17
- constructor(rpcUrls, options = {}) {
89
+ */ export var FallthroughProvider = /*#__PURE__*/ function(BaseProvider) {
90
+ "use strict";
91
+ _inherits(FallthroughProvider, BaseProvider);
92
+ var _super = _createSuper(FallthroughProvider);
93
+ function FallthroughProvider(rpcUrls) {
94
+ var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
95
+ _classCallCheck(this, FallthroughProvider);
96
+ var _this;
18
97
  if (!Array.isArray(rpcUrls)) {
19
- logger_1.logger.throwError('Array required', { rpcUrls });
98
+ logger.throwError("Array required", {
99
+ rpcUrls: rpcUrls
100
+ });
20
101
  }
21
102
  if (rpcUrls.length <= 1) {
22
- logger_1.logger.throwError('More than one rpcUrl is required', { rpcUrls });
103
+ logger.throwError("More than one rpcUrl is required", {
104
+ rpcUrls: rpcUrls
105
+ });
23
106
  }
24
- super(rpcUrls);
107
+ _this = _super.call(this, rpcUrls);
25
108
  // index of current trusted rpc url
26
109
  /**
27
- * @ignore
28
- */
29
- this.rpcUrlCounter = 0;
110
+ * @ignore
111
+ */ _this.rpcUrlCounter = 0;
30
112
  /**
31
- * @ignore
32
- */
33
- this.post = (body) => {
113
+ * @ignore
114
+ */ _this.post = function(body) {
34
115
  // while failing post, add to rpcUrlCounter and post again
35
- const genesisCount = this.rpcUrlCounter;
36
- const recursivePostRetry = () => {
116
+ var genesisCount = _this.rpcUrlCounter;
117
+ var recursivePostRetry = function() {
37
118
  // Times out request
38
- const genesisRpcUrl = this.selectRpcUrl();
39
- const res = promiseTimeout(this._post(body), this.timeoutDuration).catch((e) => {
119
+ var genesisRpcUrl = _this.selectRpcUrl();
120
+ var res = promiseTimeout(_this._post(body), _this.timeoutDuration).catch(function(e) {
40
121
  // A mutex: Only add if no other instance has discovered this url as failing yet
41
- if (genesisRpcUrl === this.selectRpcUrl()) {
122
+ if (genesisRpcUrl === _this.selectRpcUrl()) {
42
123
  // add one and handle array overflow
43
- this.rpcUrlCounter =
44
- (this.rpcUrlCounter + 1) % this._rpcUrls.length;
124
+ _this.rpcUrlCounter = (_this.rpcUrlCounter + 1) % _this._rpcUrls.length;
45
125
  }
46
126
  // we've already tried this rpc, throw for good
47
- if (this.rpcUrlCounter === genesisCount) {
127
+ if (_this.rpcUrlCounter === genesisCount) {
48
128
  throw e;
49
129
  }
50
130
  return recursivePostRetry();
@@ -53,13 +133,14 @@ class FallthroughProvider extends BaseProvider_1.BaseProvider {
53
133
  };
54
134
  return recursivePostRetry();
55
135
  };
56
- this.timeoutDuration = options.timeoutDuration || DEFAULT_TIMEOUT_DURATION;
136
+ _this.timeoutDuration = options.timeoutDuration || DEFAULT_TIMEOUT_DURATION;
137
+ return _this;
57
138
  }
139
+ var _proto = FallthroughProvider.prototype;
58
140
  /**
59
- * @ignore
60
- */
61
- selectRpcUrl() {
141
+ * @ignore
142
+ */ _proto.selectRpcUrl = function selectRpcUrl() {
62
143
  return this._rpcUrls[this.rpcUrlCounter];
63
- }
64
- }
65
- exports.FallthroughProvider = FallthroughProvider;
144
+ };
145
+ return FallthroughProvider;
146
+ }(BaseProvider);
@@ -1,32 +1,99 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.jsonRpcProvider = exports.JsonRpcProvider = void 0;
4
- const BaseProvider_1 = require("./BaseProvider");
5
- class JsonRpcProvider extends BaseProvider_1.BaseProvider {
6
- /**
7
- * @ignore
8
- */
9
- selectRpcUrl() {
10
- return this._rpcUrls[0];
1
+ function _assertThisInitialized(self) {
2
+ if (self === void 0) {
3
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
11
4
  }
12
- /**
13
- * @ignore
14
- */
15
- post(body) {
16
- return this._post(body);
5
+ return self;
6
+ }
7
+ function _classCallCheck(instance, Constructor) {
8
+ if (!(instance instanceof Constructor)) {
9
+ throw new TypeError("Cannot call a class as a function");
17
10
  }
18
- /**
19
- * @param rpcUrl The URL to your Eth node. Consider POKT or Infura
20
- * @example
21
- * `https://free-eth-node.com/api/eth`
22
- * @example
23
- * `https://mainnet.infura.io/v3/YOUR-PROJECT-ID`
24
- */
25
- constructor(rpcUrl = 'https://free-eth-node.com/api/eth') {
26
- super([rpcUrl]);
11
+ }
12
+ function _getPrototypeOf(o) {
13
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
14
+ return o.__proto__ || Object.getPrototypeOf(o);
15
+ };
16
+ return _getPrototypeOf(o);
17
+ }
18
+ function _inherits(subClass, superClass) {
19
+ if (typeof superClass !== "function" && superClass !== null) {
20
+ throw new TypeError("Super expression must either be null or a function");
21
+ }
22
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
23
+ constructor: {
24
+ value: subClass,
25
+ writable: true,
26
+ configurable: true
27
+ }
28
+ });
29
+ if (superClass) _setPrototypeOf(subClass, superClass);
30
+ }
31
+ function _possibleConstructorReturn(self, call) {
32
+ if (call && (_typeof(call) === "object" || typeof call === "function")) {
33
+ return call;
27
34
  }
35
+ return _assertThisInitialized(self);
28
36
  }
29
- exports.JsonRpcProvider = JsonRpcProvider;
37
+ function _setPrototypeOf(o, p) {
38
+ _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
39
+ o.__proto__ = p;
40
+ return o;
41
+ };
42
+ return _setPrototypeOf(o, p);
43
+ }
44
+ var _typeof = function(obj) {
45
+ "@swc/helpers - typeof";
46
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
47
+ };
48
+ function _isNativeReflectConstruct() {
49
+ if (typeof Reflect === "undefined" || !Reflect.construct) return false;
50
+ if (Reflect.construct.sham) return false;
51
+ if (typeof Proxy === "function") return true;
52
+ try {
53
+ Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
54
+ return true;
55
+ } catch (e) {
56
+ return false;
57
+ }
58
+ }
59
+ function _createSuper(Derived) {
60
+ var hasNativeReflectConstruct = _isNativeReflectConstruct();
61
+ return function _createSuperInternal() {
62
+ var Super = _getPrototypeOf(Derived), result;
63
+ if (hasNativeReflectConstruct) {
64
+ var NewTarget = _getPrototypeOf(this).constructor;
65
+ result = Reflect.construct(Super, arguments, NewTarget);
66
+ } else {
67
+ result = Super.apply(this, arguments);
68
+ }
69
+ return _possibleConstructorReturn(this, result);
70
+ };
71
+ }
72
+ import { BaseProvider } from "./BaseProvider";
73
+ export var JsonRpcProvider = /*#__PURE__*/ function(BaseProvider) {
74
+ "use strict";
75
+ _inherits(JsonRpcProvider, BaseProvider);
76
+ var _super = _createSuper(JsonRpcProvider);
77
+ function JsonRpcProvider() {
78
+ var rpcUrl = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "https://free-eth-node.com/api/eth";
79
+ _classCallCheck(this, JsonRpcProvider);
80
+ return _super.call(this, [
81
+ rpcUrl
82
+ ]);
83
+ }
84
+ var _proto = JsonRpcProvider.prototype;
85
+ /**
86
+ * @ignore
87
+ */ _proto.selectRpcUrl = function selectRpcUrl() {
88
+ return this._rpcUrls[0];
89
+ };
90
+ /**
91
+ * @ignore
92
+ */ _proto.post = function post(body) {
93
+ return this._post(body);
94
+ };
95
+ return JsonRpcProvider;
96
+ }(BaseProvider);
30
97
  /**
31
98
  * Helper function to avoid "new"
32
99
  *
@@ -39,8 +106,6 @@ exports.JsonRpcProvider = JsonRpcProvider;
39
106
  * })
40
107
  * // 14530496
41
108
  * ```
42
- */
43
- function jsonRpcProvider(rpcUrl) {
109
+ */ export function jsonRpcProvider(rpcUrl) {
44
110
  return new JsonRpcProvider(rpcUrl);
45
111
  }
46
- exports.jsonRpcProvider = jsonRpcProvider;
@@ -0,0 +1,228 @@
1
+ // performance polyfill required to support node <= 14
2
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
3
+ try {
4
+ var info = gen[key](arg);
5
+ var value = info.value;
6
+ } catch (error) {
7
+ reject(error);
8
+ return;
9
+ }
10
+ if (info.done) {
11
+ resolve(value);
12
+ } else {
13
+ Promise.resolve(value).then(_next, _throw);
14
+ }
15
+ }
16
+ function _asyncToGenerator(fn) {
17
+ return function() {
18
+ var self = this, args = arguments;
19
+ return new Promise(function(resolve, reject) {
20
+ var gen = fn.apply(self, args);
21
+ function _next(value) {
22
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
23
+ }
24
+ function _throw(err) {
25
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
26
+ }
27
+ _next(undefined);
28
+ });
29
+ };
30
+ }
31
+ var __generator = this && this.__generator || function(thisArg, body) {
32
+ var f, y, t, g, _ = {
33
+ label: 0,
34
+ sent: function() {
35
+ if (t[0] & 1) throw t[1];
36
+ return t[1];
37
+ },
38
+ trys: [],
39
+ ops: []
40
+ };
41
+ return g = {
42
+ next: verb(0),
43
+ "throw": verb(1),
44
+ "return": verb(2)
45
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
46
+ return this;
47
+ }), g;
48
+ function verb(n) {
49
+ return function(v) {
50
+ return step([
51
+ n,
52
+ v
53
+ ]);
54
+ };
55
+ }
56
+ function step(op) {
57
+ if (f) throw new TypeError("Generator is already executing.");
58
+ while(_)try {
59
+ 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;
60
+ if (y = 0, t) op = [
61
+ op[0] & 2,
62
+ t.value
63
+ ];
64
+ switch(op[0]){
65
+ case 0:
66
+ case 1:
67
+ t = op;
68
+ break;
69
+ case 4:
70
+ _.label++;
71
+ return {
72
+ value: op[1],
73
+ done: false
74
+ };
75
+ case 5:
76
+ _.label++;
77
+ y = op[1];
78
+ op = [
79
+ 0
80
+ ];
81
+ continue;
82
+ case 7:
83
+ op = _.ops.pop();
84
+ _.trys.pop();
85
+ continue;
86
+ default:
87
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
88
+ _ = 0;
89
+ continue;
90
+ }
91
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
92
+ _.label = op[1];
93
+ break;
94
+ }
95
+ if (op[0] === 6 && _.label < t[1]) {
96
+ _.label = t[1];
97
+ t = op;
98
+ break;
99
+ }
100
+ if (t && _.label < t[2]) {
101
+ _.label = t[2];
102
+ _.ops.push(op);
103
+ break;
104
+ }
105
+ if (t[2]) _.ops.pop();
106
+ _.trys.pop();
107
+ continue;
108
+ }
109
+ op = body.call(thisArg, _);
110
+ } catch (e) {
111
+ op = [
112
+ 6,
113
+ e
114
+ ];
115
+ y = 0;
116
+ } finally{
117
+ f = t = 0;
118
+ }
119
+ if (op[0] & 5) throw op[1];
120
+ return {
121
+ value: op[0] ? op[1] : void 0,
122
+ done: true
123
+ };
124
+ }
125
+ };
126
+ import { performance } from "perf_hooks";
127
+ import { FallthroughProvider } from "../../../index";
128
+ import { rpcUrls } from "../rpc-urls";
129
+ var rpcUrl = rpcUrls.mainnet;
130
+ function timePromise(fn) {
131
+ var onPromiseDone = function() {
132
+ return performance.now() - start;
133
+ };
134
+ var start = performance.now();
135
+ return fn().then(onPromiseDone, onPromiseDone);
136
+ }
137
+ describe("provider.getGasPrice", function() {
138
+ it("should fallthrough on several types of invalid urls", /*#__PURE__*/ _asyncToGenerator(function() {
139
+ var essentialEthProvider, block;
140
+ return __generator(this, function(_state) {
141
+ switch(_state.label){
142
+ case 0:
143
+ essentialEthProvider = new FallthroughProvider([
144
+ "https://bad-123123123123.com",
145
+ "https://bad-523123123123.com",
146
+ rpcUrl
147
+ ]);
148
+ return [
149
+ 4,
150
+ essentialEthProvider.getBlock(14631185)
151
+ ];
152
+ case 1:
153
+ block = _state.sent();
154
+ expect(block.gasUsed.toString()).toBe("2429588");
155
+ return [
156
+ 2
157
+ ];
158
+ }
159
+ });
160
+ }));
161
+ it("should fallthrough after timeout linearly", /*#__PURE__*/ _asyncToGenerator(function() {
162
+ var essentialEthProvider;
163
+ return __generator(this, function(_state) {
164
+ switch(_state.label){
165
+ case 0:
166
+ essentialEthProvider = new FallthroughProvider([
167
+ "https://flash-the-slow-api.herokuapp.com/delay/10000",
168
+ // url with request delayed by 20 seconds
169
+ "https://flash-the-slow-api.herokuapp.com/delay/10000",
170
+ "https://bad-523123123123.com",
171
+ rpcUrl
172
+ ], {
173
+ timeoutDuration: 1000
174
+ });
175
+ return [
176
+ 4,
177
+ timePromise(function() {
178
+ return essentialEthProvider.getBlock(14631000);
179
+ }).then(function(duration) {
180
+ // times out the first two requests in 1200 ms each
181
+ // expect(duration).toBeGreaterThan(1200);
182
+ // finished the last valid request within two seconds
183
+ expect(duration).toBeLessThan(4000);
184
+ })
185
+ ];
186
+ case 1:
187
+ _state.sent();
188
+ return [
189
+ 2
190
+ ];
191
+ }
192
+ });
193
+ }));
194
+ it("should mutex the current rpc selection properly", /*#__PURE__*/ _asyncToGenerator(function() {
195
+ var providers, allBlocks;
196
+ return __generator(this, function(_state) {
197
+ switch(_state.label){
198
+ case 0:
199
+ providers = [
200
+ 1,
201
+ 2,
202
+ 3
203
+ ].map(function() {
204
+ return new FallthroughProvider([
205
+ "https://flash-sgkl.onrender.com/delay/10000",
206
+ rpcUrl
207
+ ], {
208
+ timeoutDuration: 2500
209
+ });
210
+ });
211
+ return [
212
+ 4,
213
+ Promise.all(providers.map(function(provider) {
214
+ return provider.getBlock(123456);
215
+ }))
216
+ ];
217
+ case 1:
218
+ allBlocks = _state.sent();
219
+ allBlocks.forEach(function(blockNumber) {
220
+ expect(blockNumber.difficulty.toString()).toBe("4505282870523");
221
+ });
222
+ return [
223
+ 2
224
+ ];
225
+ }
226
+ });
227
+ }));
228
+ });