1 | import EventEmitter from 'eventemitter3';
|
2 | import { NewHeadsSubscription } from 'conflux-web-core-subscriptions';
|
3 | import isFunction from 'lodash/isFunction';
|
4 | import isString from 'lodash/isString';
|
5 | import cloneDeep from 'lodash/cloneDeep';
|
6 | import { Observable } from 'rxjs';
|
7 |
|
8 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
9 | try {
|
10 | var info = gen[key](arg);
|
11 | var value = info.value;
|
12 | } catch (error) {
|
13 | reject(error);
|
14 | return;
|
15 | }
|
16 |
|
17 | if (info.done) {
|
18 | resolve(value);
|
19 | } else {
|
20 | Promise.resolve(value).then(_next, _throw);
|
21 | }
|
22 | }
|
23 |
|
24 | function _asyncToGenerator(fn) {
|
25 | return function () {
|
26 | var self = this,
|
27 | args = arguments;
|
28 | return new Promise(function (resolve, reject) {
|
29 | var gen = fn.apply(self, args);
|
30 |
|
31 | function _next(value) {
|
32 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
33 | }
|
34 |
|
35 | function _throw(err) {
|
36 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
37 | }
|
38 |
|
39 | _next(undefined);
|
40 | });
|
41 | };
|
42 | }
|
43 |
|
44 | function _classCallCheck(instance, Constructor) {
|
45 | if (!(instance instanceof Constructor)) {
|
46 | throw new TypeError("Cannot call a class as a function");
|
47 | }
|
48 | }
|
49 |
|
50 | function _defineProperties(target, props) {
|
51 | for (var i = 0; i < props.length; i++) {
|
52 | var descriptor = props[i];
|
53 | descriptor.enumerable = descriptor.enumerable || false;
|
54 | descriptor.configurable = true;
|
55 | if ("value" in descriptor) descriptor.writable = true;
|
56 | Object.defineProperty(target, descriptor.key, descriptor);
|
57 | }
|
58 | }
|
59 |
|
60 | function _createClass(Constructor, protoProps, staticProps) {
|
61 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
62 | if (staticProps) _defineProperties(Constructor, staticProps);
|
63 | return Constructor;
|
64 | }
|
65 |
|
66 | function _inherits(subClass, superClass) {
|
67 | if (typeof superClass !== "function" && superClass !== null) {
|
68 | throw new TypeError("Super expression must either be null or a function");
|
69 | }
|
70 |
|
71 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
72 | constructor: {
|
73 | value: subClass,
|
74 | writable: true,
|
75 | configurable: true
|
76 | }
|
77 | });
|
78 | if (superClass) _setPrototypeOf(subClass, superClass);
|
79 | }
|
80 |
|
81 | function _getPrototypeOf(o) {
|
82 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
83 | return o.__proto__ || Object.getPrototypeOf(o);
|
84 | };
|
85 | return _getPrototypeOf(o);
|
86 | }
|
87 |
|
88 | function _setPrototypeOf(o, p) {
|
89 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
90 | o.__proto__ = p;
|
91 | return o;
|
92 | };
|
93 |
|
94 | return _setPrototypeOf(o, p);
|
95 | }
|
96 |
|
97 | function _isNativeReflectConstruct() {
|
98 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
99 | if (Reflect.construct.sham) return false;
|
100 | if (typeof Proxy === "function") return true;
|
101 |
|
102 | try {
|
103 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
104 | return true;
|
105 | } catch (e) {
|
106 | return false;
|
107 | }
|
108 | }
|
109 |
|
110 | function _assertThisInitialized(self) {
|
111 | if (self === void 0) {
|
112 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
113 | }
|
114 |
|
115 | return self;
|
116 | }
|
117 |
|
118 | function _possibleConstructorReturn(self, call) {
|
119 | if (call && (typeof call === "object" || typeof call === "function")) {
|
120 | return call;
|
121 | }
|
122 |
|
123 | return _assertThisInitialized(self);
|
124 | }
|
125 |
|
126 | function _createSuper(Derived) {
|
127 | return function () {
|
128 | var Super = _getPrototypeOf(Derived),
|
129 | result;
|
130 |
|
131 | if (_isNativeReflectConstruct()) {
|
132 | var NewTarget = _getPrototypeOf(this).constructor;
|
133 |
|
134 | result = Reflect.construct(Super, arguments, NewTarget);
|
135 | } else {
|
136 | result = Super.apply(this, arguments);
|
137 | }
|
138 |
|
139 | return _possibleConstructorReturn(this, result);
|
140 | };
|
141 | }
|
142 |
|
143 | function _superPropBase(object, property) {
|
144 | while (!Object.prototype.hasOwnProperty.call(object, property)) {
|
145 | object = _getPrototypeOf(object);
|
146 | if (object === null) break;
|
147 | }
|
148 |
|
149 | return object;
|
150 | }
|
151 |
|
152 | function _get(target, property, receiver) {
|
153 | if (typeof Reflect !== "undefined" && Reflect.get) {
|
154 | _get = Reflect.get;
|
155 | } else {
|
156 | _get = function _get(target, property, receiver) {
|
157 | var base = _superPropBase(target, property);
|
158 |
|
159 | if (!base) return;
|
160 | var desc = Object.getOwnPropertyDescriptor(base, property);
|
161 |
|
162 | if (desc.get) {
|
163 | return desc.get.call(receiver);
|
164 | }
|
165 |
|
166 | return desc.value;
|
167 | };
|
168 | }
|
169 |
|
170 | return _get(target, property, receiver || target);
|
171 | }
|
172 |
|
173 | function _toConsumableArray(arr) {
|
174 | return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
|
175 | }
|
176 |
|
177 | function _arrayWithoutHoles(arr) {
|
178 | if (Array.isArray(arr)) return _arrayLikeToArray(arr);
|
179 | }
|
180 |
|
181 | function _iterableToArray(iter) {
|
182 | if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
|
183 | }
|
184 |
|
185 | function _unsupportedIterableToArray(o, minLen) {
|
186 | if (!o) return;
|
187 | if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
188 | var n = Object.prototype.toString.call(o).slice(8, -1);
|
189 | if (n === "Object" && o.constructor) n = o.constructor.name;
|
190 | if (n === "Map" || n === "Set") return Array.from(n);
|
191 | if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
192 | }
|
193 |
|
194 | function _arrayLikeToArray(arr, len) {
|
195 | if (len == null || len > arr.length) len = arr.length;
|
196 |
|
197 | for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
198 |
|
199 | return arr2;
|
200 | }
|
201 |
|
202 | function _nonIterableSpread() {
|
203 | throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
204 | }
|
205 |
|
206 | var PromiEvent = function () {
|
207 | function PromiEvent() {
|
208 | var _this = this;
|
209 | _classCallCheck(this, PromiEvent);
|
210 | this.promise = new Promise(function (resolve, reject) {
|
211 | _this.resolve = resolve;
|
212 | _this.reject = reject;
|
213 | });
|
214 | this.eventEmitter = new EventEmitter();
|
215 | return new Proxy(this, {
|
216 | get: this.proxyHandler
|
217 | });
|
218 | }
|
219 | _createClass(PromiEvent, [{
|
220 | key: "proxyHandler",
|
221 | value: function proxyHandler(target, name) {
|
222 | if (name === 'resolve' || name === 'reject') {
|
223 | return target[name];
|
224 | }
|
225 | if (name === 'then') {
|
226 | return target.promise.then.bind(target.promise);
|
227 | }
|
228 | if (name === 'catch') {
|
229 | return target.promise["catch"].bind(target.promise);
|
230 | }
|
231 | if (target.eventEmitter[name]) {
|
232 | return target.eventEmitter[name];
|
233 | }
|
234 | }
|
235 | }]);
|
236 | return PromiEvent;
|
237 | }();
|
238 |
|
239 | var AbstractMethod = function () {
|
240 | function AbstractMethod(rpcMethod, parametersAmount, utils, formatters, moduleInstance) {
|
241 | _classCallCheck(this, AbstractMethod);
|
242 | this.utils = utils;
|
243 | this.formatters = formatters;
|
244 | this.moduleInstance = moduleInstance;
|
245 | this._arguments = {
|
246 | parameters: []
|
247 | };
|
248 | this._rpcMethod = rpcMethod;
|
249 | this._parametersAmount = parametersAmount;
|
250 | }
|
251 | _createClass(AbstractMethod, [{
|
252 | key: "beforeExecution",
|
253 | value: function beforeExecution(moduleInstance) {}
|
254 | }, {
|
255 | key: "afterExecution",
|
256 | value: function afterExecution(response) {
|
257 | return response;
|
258 | }
|
259 | }, {
|
260 | key: "execute",
|
261 | value: function () {
|
262 | var _execute = _asyncToGenerator( regeneratorRuntime.mark(function _callee() {
|
263 | var response;
|
264 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
265 | while (1) {
|
266 | switch (_context.prev = _context.next) {
|
267 | case 0:
|
268 | this.beforeExecution(this.moduleInstance);
|
269 | if (!(this.parameters.length !== this.parametersAmount)) {
|
270 | _context.next = 3;
|
271 | break;
|
272 | }
|
273 | throw new Error("Invalid Arguments length: expected: ".concat(this.parametersAmount, ", given: ").concat(this.parameters.length));
|
274 | case 3:
|
275 | _context.prev = 3;
|
276 | _context.next = 6;
|
277 | return this.moduleInstance.currentProvider.send(this.rpcMethod, this.parameters);
|
278 | case 6:
|
279 | response = _context.sent;
|
280 | if (response) {
|
281 | response = this.afterExecution(response);
|
282 | }
|
283 | if (this.callback) {
|
284 | this.callback(false, response);
|
285 | }
|
286 | return _context.abrupt("return", response);
|
287 | case 12:
|
288 | _context.prev = 12;
|
289 | _context.t0 = _context["catch"](3);
|
290 | if (this.callback) {
|
291 | this.callback(_context.t0, null);
|
292 | }
|
293 | throw _context.t0;
|
294 | case 16:
|
295 | case "end":
|
296 | return _context.stop();
|
297 | }
|
298 | }
|
299 | }, _callee, this, [[3, 12]]);
|
300 | }));
|
301 | function execute() {
|
302 | return _execute.apply(this, arguments);
|
303 | }
|
304 | return execute;
|
305 | }()
|
306 | }, {
|
307 | key: "setArguments",
|
308 | value: function setArguments(methodArguments) {
|
309 | var parameters = cloneDeep(_toConsumableArray(methodArguments));
|
310 | var callback = null;
|
311 | if (parameters.length > this.parametersAmount) {
|
312 | if (!isFunction(parameters[parameters.length - 1])) {
|
313 | throw new TypeError("The latest parameter should be a function otherwise it can't be used as callback");
|
314 | }
|
315 | callback = parameters.pop();
|
316 | }
|
317 | this._arguments = {
|
318 | callback: callback,
|
319 | parameters: parameters
|
320 | };
|
321 | }
|
322 | }, {
|
323 | key: "getArguments",
|
324 | value: function getArguments() {
|
325 | return this._arguments;
|
326 | }
|
327 | }, {
|
328 | key: "isHash",
|
329 | value: function isHash(parameter) {
|
330 | return isString(parameter) && parameter.startsWith('0x');
|
331 | }
|
332 | }, {
|
333 | key: "rpcMethod",
|
334 | set: function set(value) {
|
335 | this._rpcMethod = value;
|
336 | }
|
337 | ,
|
338 | get: function get() {
|
339 | return this._rpcMethod;
|
340 | }
|
341 | }, {
|
342 | key: "parametersAmount",
|
343 | set: function set(value) {
|
344 | this._parametersAmount = value;
|
345 | }
|
346 | ,
|
347 | get: function get() {
|
348 | return this._parametersAmount;
|
349 | }
|
350 | }, {
|
351 | key: "parameters",
|
352 | get: function get() {
|
353 | return this._arguments.parameters;
|
354 | }
|
355 | ,
|
356 | set: function set(value) {
|
357 | this._arguments.parameters = value;
|
358 | }
|
359 | }, {
|
360 | key: "callback",
|
361 | get: function get() {
|
362 | return this._arguments.callback;
|
363 | }
|
364 | ,
|
365 | set: function set(value) {
|
366 | this._arguments.callback = value;
|
367 | }
|
368 | }]);
|
369 | return AbstractMethod;
|
370 | }();
|
371 |
|
372 | var AbstractGetBlockMethod = function (_AbstractMethod) {
|
373 | _inherits(AbstractGetBlockMethod, _AbstractMethod);
|
374 | var _super = _createSuper(AbstractGetBlockMethod);
|
375 | function AbstractGetBlockMethod(rpcMethod, utils, formatters, moduleInstance) {
|
376 | _classCallCheck(this, AbstractGetBlockMethod);
|
377 | return _super.call(this, rpcMethod, 2, utils, formatters, moduleInstance);
|
378 | }
|
379 | _createClass(AbstractGetBlockMethod, [{
|
380 | key: "beforeExecution",
|
381 | value: function beforeExecution(moduleInstance) {
|
382 | this.parameters[0] = this.formatters.inputBlockAddressFormatter(this.parameters[0]);
|
383 | if (isFunction(this.parameters[1])) {
|
384 | this.callback = this.parameters[1];
|
385 | this.parameters[1] = false;
|
386 | } else {
|
387 | this.parameters[1] = !!this.parameters[1];
|
388 | }
|
389 | }
|
390 | }, {
|
391 | key: "afterExecution",
|
392 | value: function afterExecution(response) {
|
393 | return this.formatters.outputBlockFormatter(response);
|
394 | }
|
395 | }]);
|
396 | return AbstractGetBlockMethod;
|
397 | }(AbstractMethod);
|
398 |
|
399 | var GetBlockByNumberMethod = function (_AbstractGetBlockMeth) {
|
400 | _inherits(GetBlockByNumberMethod, _AbstractGetBlockMeth);
|
401 | var _super = _createSuper(GetBlockByNumberMethod);
|
402 | function GetBlockByNumberMethod(utils, formatters, moduleInstance) {
|
403 | _classCallCheck(this, GetBlockByNumberMethod);
|
404 | return _super.call(this, 'cfx_getBlockByEpochNumber', utils, formatters, moduleInstance);
|
405 | }
|
406 | return GetBlockByNumberMethod;
|
407 | }(AbstractGetBlockMethod);
|
408 |
|
409 | var GetTransactionReceiptMethod = function (_AbstractMethod) {
|
410 | _inherits(GetTransactionReceiptMethod, _AbstractMethod);
|
411 | var _super = _createSuper(GetTransactionReceiptMethod);
|
412 | function GetTransactionReceiptMethod(utils, formatters, moduleInstance) {
|
413 | _classCallCheck(this, GetTransactionReceiptMethod);
|
414 | return _super.call(this, 'cfx_getTransactionReceipt', 1, utils, formatters, moduleInstance);
|
415 | }
|
416 | _createClass(GetTransactionReceiptMethod, [{
|
417 | key: "afterExecution",
|
418 | value: function afterExecution(response) {
|
419 | if (response !== null) {
|
420 | return this.formatters.outputTransactionReceiptFormatter(response);
|
421 | }
|
422 | return response;
|
423 | }
|
424 | }]);
|
425 | return GetTransactionReceiptMethod;
|
426 | }(AbstractMethod);
|
427 |
|
428 | var TransactionObserver = function () {
|
429 | function TransactionObserver(provider, timeout, blockConfirmations, getTransactionReceiptMethod, getBlockByNumberMethod, newHeadsSubscription) {
|
430 | _classCallCheck(this, TransactionObserver);
|
431 | this.provider = provider;
|
432 | this.timeout = timeout;
|
433 | this.blockConfirmations = blockConfirmations;
|
434 | this.getTransactionReceiptMethod = getTransactionReceiptMethod;
|
435 | this.getBlockByNumberMethod = getBlockByNumberMethod;
|
436 | this.newHeadsSubscription = newHeadsSubscription;
|
437 | this.blockNumbers = [];
|
438 | this.lastBlock = false;
|
439 | this.confirmations = 0;
|
440 | this.confirmationChecks = 0;
|
441 | this.interval = false;
|
442 | }
|
443 | _createClass(TransactionObserver, [{
|
444 | key: "observe",
|
445 | value: function observe(transactionHash) {
|
446 | var _this = this;
|
447 | return Observable.create(function (observer) {
|
448 | if (_this.provider.supportsSubscriptions()) {
|
449 | _this.startSocketObserver(transactionHash, observer);
|
450 | } else {
|
451 | _this.startHttpObserver(transactionHash, observer);
|
452 | }
|
453 | });
|
454 | }
|
455 | }, {
|
456 | key: "startSocketObserver",
|
457 | value: function startSocketObserver(transactionHash, observer) {
|
458 | var _this2 = this;
|
459 | this.newHeadsSubscription.subscribe( function () {
|
460 | var _ref = _asyncToGenerator( regeneratorRuntime.mark(function _callee(error, newHead) {
|
461 | var receipt;
|
462 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
463 | while (1) {
|
464 | switch (_context.prev = _context.next) {
|
465 | case 0:
|
466 | _context.prev = 0;
|
467 | if (!observer.closed) {
|
468 | _context.next = 5;
|
469 | break;
|
470 | }
|
471 | _context.next = 4;
|
472 | return _this2.newHeadsSubscription.unsubscribe();
|
473 | case 4:
|
474 | return _context.abrupt("return");
|
475 | case 5:
|
476 | if (!error) {
|
477 | _context.next = 7;
|
478 | break;
|
479 | }
|
480 | throw error;
|
481 | case 7:
|
482 | _this2.getTransactionReceiptMethod.parameters = [transactionHash];
|
483 | _context.next = 10;
|
484 | return _this2.getTransactionReceiptMethod.execute();
|
485 | case 10:
|
486 | receipt = _context.sent;
|
487 | if (_this2.blockNumbers.includes(newHead.number)) {
|
488 | _context.next = 25;
|
489 | break;
|
490 | }
|
491 | if (!receipt) {
|
492 | _context.next = 19;
|
493 | break;
|
494 | }
|
495 | _this2.confirmations++;
|
496 | _this2.emitNext(receipt, observer);
|
497 | if (!_this2.isConfirmed()) {
|
498 | _context.next = 19;
|
499 | break;
|
500 | }
|
501 | _context.next = 18;
|
502 | return _this2.newHeadsSubscription.unsubscribe();
|
503 | case 18:
|
504 | observer.complete();
|
505 | case 19:
|
506 | _this2.blockNumbers.push(newHead.number);
|
507 | _this2.confirmationChecks++;
|
508 | if (!_this2.isTimeoutTimeExceeded()) {
|
509 | _context.next = 25;
|
510 | break;
|
511 | }
|
512 | _context.next = 24;
|
513 | return _this2.newHeadsSubscription.unsubscribe();
|
514 | case 24:
|
515 | _this2.emitError(new Error('Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!'), receipt, observer);
|
516 | case 25:
|
517 | _context.next = 30;
|
518 | break;
|
519 | case 27:
|
520 | _context.prev = 27;
|
521 | _context.t0 = _context["catch"](0);
|
522 | _this2.emitError(_context.t0, false, observer);
|
523 | case 30:
|
524 | case "end":
|
525 | return _context.stop();
|
526 | }
|
527 | }
|
528 | }, _callee, null, [[0, 27]]);
|
529 | }));
|
530 | return function (_x, _x2) {
|
531 | return _ref.apply(this, arguments);
|
532 | };
|
533 | }());
|
534 | }
|
535 | }, {
|
536 | key: "startHttpObserver",
|
537 | value: function startHttpObserver(transactionHash, observer) {
|
538 | var _this3 = this;
|
539 | var interval = setInterval( _asyncToGenerator( regeneratorRuntime.mark(function _callee2() {
|
540 | var receipt, block;
|
541 | return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
542 | while (1) {
|
543 | switch (_context2.prev = _context2.next) {
|
544 | case 0:
|
545 | _context2.prev = 0;
|
546 | if (!observer.closed) {
|
547 | _context2.next = 4;
|
548 | break;
|
549 | }
|
550 | clearInterval(interval);
|
551 | return _context2.abrupt("return");
|
552 | case 4:
|
553 | _this3.getTransactionReceiptMethod.parameters = [transactionHash];
|
554 | _context2.next = 7;
|
555 | return _this3.getTransactionReceiptMethod.execute();
|
556 | case 7:
|
557 | receipt = _context2.sent;
|
558 | if (!receipt) {
|
559 | _context2.next = 22;
|
560 | break;
|
561 | }
|
562 | if (!_this3.lastBlock) {
|
563 | _context2.next = 16;
|
564 | break;
|
565 | }
|
566 | _context2.next = 12;
|
567 | return _this3.getBlockByNumber(_this3.increaseBlockNumber(_this3.lastBlock.number));
|
568 | case 12:
|
569 | block = _context2.sent;
|
570 | if (block && _this3.isValidConfirmation(block)) {
|
571 | _this3.lastBlock = block;
|
572 | _this3.confirmations++;
|
573 | _this3.emitNext(receipt, observer);
|
574 | }
|
575 | _context2.next = 21;
|
576 | break;
|
577 | case 16:
|
578 | _context2.next = 18;
|
579 | return _this3.getBlockByNumber(receipt.blockNumber);
|
580 | case 18:
|
581 | _this3.lastBlock = _context2.sent;
|
582 | _this3.confirmations++;
|
583 | _this3.emitNext(receipt, observer);
|
584 | case 21:
|
585 | if (_this3.isConfirmed()) {
|
586 | clearInterval(interval);
|
587 | observer.complete();
|
588 | }
|
589 | case 22:
|
590 | _this3.confirmationChecks++;
|
591 | if (_this3.isTimeoutTimeExceeded()) {
|
592 | clearInterval(interval);
|
593 | _this3.emitError(new Error('Timeout exceeded during the transaction confirmation process. Be aware the transaction could still get confirmed!'), receipt, observer);
|
594 | }
|
595 | _context2.next = 30;
|
596 | break;
|
597 | case 26:
|
598 | _context2.prev = 26;
|
599 | _context2.t0 = _context2["catch"](0);
|
600 | clearInterval(interval);
|
601 | _this3.emitError(_context2.t0, false, observer);
|
602 | case 30:
|
603 | case "end":
|
604 | return _context2.stop();
|
605 | }
|
606 | }
|
607 | }, _callee2, null, [[0, 26]]);
|
608 | })), 1000);
|
609 | }
|
610 | }, {
|
611 | key: "emitNext",
|
612 | value: function emitNext(receipt, observer) {
|
613 | observer.next({
|
614 | receipt: receipt,
|
615 | confirmations: this.confirmations
|
616 | });
|
617 | }
|
618 | }, {
|
619 | key: "emitError",
|
620 | value: function emitError(error, receipt, observer) {
|
621 | observer.error({
|
622 | error: error,
|
623 | receipt: receipt,
|
624 | confirmations: this.confirmations,
|
625 | confirmationChecks: this.confirmationChecks
|
626 | });
|
627 | }
|
628 | }, {
|
629 | key: "getBlockByNumber",
|
630 | value: function getBlockByNumber(blockNumber) {
|
631 | this.getBlockByNumberMethod.parameters = [blockNumber];
|
632 | return this.getBlockByNumberMethod.execute();
|
633 | }
|
634 | }, {
|
635 | key: "isConfirmed",
|
636 | value: function isConfirmed() {
|
637 | return this.confirmations === this.blockConfirmations;
|
638 | }
|
639 | }, {
|
640 | key: "isValidConfirmation",
|
641 | value: function isValidConfirmation(block) {
|
642 | return this.lastBlock.hash === block.parentHash && this.lastBlock.number !== block.number;
|
643 | }
|
644 | }, {
|
645 | key: "isTimeoutTimeExceeded",
|
646 | value: function isTimeoutTimeExceeded() {
|
647 | return this.confirmationChecks === this.timeout;
|
648 | }
|
649 | }, {
|
650 | key: "increaseBlockNumber",
|
651 | value: function increaseBlockNumber(blockNumber) {
|
652 | return '0x' + (parseInt(blockNumber, 16) + 1).toString(16);
|
653 | }
|
654 | }]);
|
655 | return TransactionObserver;
|
656 | }();
|
657 |
|
658 | var GetTransactionCountMethod = function (_AbstractMethod) {
|
659 | _inherits(GetTransactionCountMethod, _AbstractMethod);
|
660 | var _super = _createSuper(GetTransactionCountMethod);
|
661 | function GetTransactionCountMethod(utils, formatters, moduleInstance) {
|
662 | _classCallCheck(this, GetTransactionCountMethod);
|
663 | return _super.call(this, 'cfx_getTransactionCount', 2, utils, formatters, moduleInstance);
|
664 | }
|
665 | _createClass(GetTransactionCountMethod, [{
|
666 | key: "beforeExecution",
|
667 | value: function beforeExecution(moduleInstance) {
|
668 | this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]);
|
669 | if (isFunction(this.parameters[1])) {
|
670 | this.callback = this.parameters[1];
|
671 | this.parameters[1] = moduleInstance.defaultEpoch;
|
672 | }
|
673 | this.parameters[1] = this.formatters.inputDefaultEpochNumberFormatter(this.parameters[1], moduleInstance);
|
674 | }
|
675 | }, {
|
676 | key: "afterExecution",
|
677 | value: function afterExecution(response) {
|
678 | return this.utils.hexToNumber(response);
|
679 | }
|
680 | }]);
|
681 | return GetTransactionCountMethod;
|
682 | }(AbstractMethod);
|
683 |
|
684 | var ChainIdMethod = function (_AbstractMethod) {
|
685 | _inherits(ChainIdMethod, _AbstractMethod);
|
686 | var _super = _createSuper(ChainIdMethod);
|
687 | function ChainIdMethod(utils, formatters, moduleInstance) {
|
688 | _classCallCheck(this, ChainIdMethod);
|
689 | return _super.call(this, 'eth_chainId', 0, utils, formatters, moduleInstance);
|
690 | }
|
691 | _createClass(ChainIdMethod, [{
|
692 | key: "afterExecution",
|
693 | value: function afterExecution(response) {
|
694 | return this.utils.hexToNumber(response);
|
695 | }
|
696 | }]);
|
697 | return ChainIdMethod;
|
698 | }(AbstractMethod);
|
699 |
|
700 | var AbstractMethodFactory = function () {
|
701 | function AbstractMethodFactory(utils, formatters) {
|
702 | _classCallCheck(this, AbstractMethodFactory);
|
703 | this.utils = utils;
|
704 | this.formatters = formatters;
|
705 | this._methods = null;
|
706 | }
|
707 | _createClass(AbstractMethodFactory, [{
|
708 | key: "hasMethod",
|
709 | value: function hasMethod(name) {
|
710 | return typeof this.methods[name] !== 'undefined';
|
711 | }
|
712 | }, {
|
713 | key: "createMethod",
|
714 | value: function createMethod(name, moduleInstance) {
|
715 | var method = this.methods[name];
|
716 | if (method.Type === 'observed-transaction-method') {
|
717 | return new method(this.utils, this.formatters, moduleInstance, this.createTransactionObserver(moduleInstance));
|
718 | }
|
719 | if (method.Type === 'cfx-send-transaction-method') {
|
720 | return new method(this.utils, this.formatters, moduleInstance, this.createTransactionObserver(moduleInstance), new ChainIdMethod(this.utils, this.formatters, moduleInstance), new GetTransactionCountMethod(this.utils, this.formatters, moduleInstance));
|
721 | }
|
722 | return new method(this.utils, this.formatters, moduleInstance);
|
723 | }
|
724 | }, {
|
725 | key: "getTimeout",
|
726 | value: function getTimeout(moduleInstance) {
|
727 | var timeout = moduleInstance.transactionBlockTimeout;
|
728 | if (!moduleInstance.currentProvider.supportsSubscriptions()) {
|
729 | timeout = moduleInstance.transactionPollingTimeout;
|
730 | }
|
731 | return timeout;
|
732 | }
|
733 | }, {
|
734 | key: "createTransactionObserver",
|
735 | value: function createTransactionObserver(moduleInstance) {
|
736 | return new TransactionObserver(moduleInstance.currentProvider, this.getTimeout(moduleInstance), moduleInstance.transactionConfirmationBlocks, new GetTransactionReceiptMethod(this.utils, this.formatters, moduleInstance), new GetBlockByNumberMethod(this.utils, this.formatters, moduleInstance), new NewHeadsSubscription(this.utils, this.formatters, moduleInstance));
|
737 | }
|
738 | }, {
|
739 | key: "methods",
|
740 | get: function get() {
|
741 | if (this._methods) {
|
742 | return this._methods;
|
743 | }
|
744 | throw new Error('No methods defined for MethodFactory!');
|
745 | }
|
746 | ,
|
747 | set: function set(value) {
|
748 | this._methods = value;
|
749 | }
|
750 | }]);
|
751 | return AbstractMethodFactory;
|
752 | }();
|
753 |
|
754 | var MethodProxy =
|
755 | function MethodProxy(target, methodFactory) {
|
756 | _classCallCheck(this, MethodProxy);
|
757 | return new Proxy(target, {
|
758 | get: function get(target, name) {
|
759 | if (methodFactory.hasMethod(name)) {
|
760 | var RpcMethod = function RpcMethod() {
|
761 | method.setArguments(arguments);
|
762 | return method.execute();
|
763 | };
|
764 | if (typeof target[name] !== 'undefined') {
|
765 | throw new TypeError("Duplicated method ".concat(name, ". This method is defined as RPC call and as Object method."));
|
766 | }
|
767 | var method = methodFactory.createMethod(name, target);
|
768 | RpcMethod.method = method;
|
769 | RpcMethod.request = function () {
|
770 | method.setArguments(arguments);
|
771 | return method;
|
772 | };
|
773 | return RpcMethod;
|
774 | }
|
775 | return target[name];
|
776 | }
|
777 | });
|
778 | };
|
779 |
|
780 | var GetProtocolVersionMethod = function (_AbstractMethod) {
|
781 | _inherits(GetProtocolVersionMethod, _AbstractMethod);
|
782 | var _super = _createSuper(GetProtocolVersionMethod);
|
783 | function GetProtocolVersionMethod(utils, formatters, moduleInstance) {
|
784 | _classCallCheck(this, GetProtocolVersionMethod);
|
785 | return _super.call(this, 'eth_protocolVersion', 0, utils, formatters, moduleInstance);
|
786 | }
|
787 | _createClass(GetProtocolVersionMethod, [{
|
788 | key: "afterExecution",
|
789 | value: function afterExecution(response) {
|
790 | return this.utils.hexToNumber(response);
|
791 | }
|
792 | }]);
|
793 | return GetProtocolVersionMethod;
|
794 | }(AbstractMethod);
|
795 |
|
796 | var VersionMethod = function (_AbstractMethod) {
|
797 | _inherits(VersionMethod, _AbstractMethod);
|
798 | var _super = _createSuper(VersionMethod);
|
799 | function VersionMethod(utils, formatters, moduleInstance) {
|
800 | _classCallCheck(this, VersionMethod);
|
801 | return _super.call(this, 'net_version', 0, utils, formatters, moduleInstance);
|
802 | }
|
803 | _createClass(VersionMethod, [{
|
804 | key: "afterExecution",
|
805 | value: function afterExecution(response) {
|
806 | return this.utils.hexToNumber(response);
|
807 | }
|
808 | }]);
|
809 | return VersionMethod;
|
810 | }(AbstractMethod);
|
811 |
|
812 | var ListeningMethod = function (_AbstractMethod) {
|
813 | _inherits(ListeningMethod, _AbstractMethod);
|
814 | var _super = _createSuper(ListeningMethod);
|
815 | function ListeningMethod(utils, formatters, moduleInstance) {
|
816 | _classCallCheck(this, ListeningMethod);
|
817 | return _super.call(this, 'net_listening', 0, utils, formatters, moduleInstance);
|
818 | }
|
819 | return ListeningMethod;
|
820 | }(AbstractMethod);
|
821 |
|
822 | var PeerCountMethod = function (_AbstractMethod) {
|
823 | _inherits(PeerCountMethod, _AbstractMethod);
|
824 | var _super = _createSuper(PeerCountMethod);
|
825 | function PeerCountMethod(utils, formatters, moduleInstance) {
|
826 | _classCallCheck(this, PeerCountMethod);
|
827 | return _super.call(this, 'net_peerCount', 0, utils, formatters, moduleInstance);
|
828 | }
|
829 | _createClass(PeerCountMethod, [{
|
830 | key: "afterExecution",
|
831 | value: function afterExecution(response) {
|
832 | return this.utils.hexToNumber(response);
|
833 | }
|
834 | }]);
|
835 | return PeerCountMethod;
|
836 | }(AbstractMethod);
|
837 |
|
838 | var GetNodeInfoMethod = function (_AbstractMethod) {
|
839 | _inherits(GetNodeInfoMethod, _AbstractMethod);
|
840 | var _super = _createSuper(GetNodeInfoMethod);
|
841 | function GetNodeInfoMethod(utils, formatters, moduleInstance) {
|
842 | _classCallCheck(this, GetNodeInfoMethod);
|
843 | return _super.call(this, 'confluxWeb_clientVersion', 0, utils, formatters, moduleInstance);
|
844 | }
|
845 | return GetNodeInfoMethod;
|
846 | }(AbstractMethod);
|
847 |
|
848 | var GetCoinbaseMethod = function (_AbstractMethod) {
|
849 | _inherits(GetCoinbaseMethod, _AbstractMethod);
|
850 | var _super = _createSuper(GetCoinbaseMethod);
|
851 | function GetCoinbaseMethod(utils, formatters, moduleInstance) {
|
852 | _classCallCheck(this, GetCoinbaseMethod);
|
853 | return _super.call(this, 'eth_coinbase', 0, utils, formatters, moduleInstance);
|
854 | }
|
855 | return GetCoinbaseMethod;
|
856 | }(AbstractMethod);
|
857 |
|
858 | var IsMiningMethod = function (_AbstractMethod) {
|
859 | _inherits(IsMiningMethod, _AbstractMethod);
|
860 | var _super = _createSuper(IsMiningMethod);
|
861 | function IsMiningMethod(utils, formatters, moduleInstance) {
|
862 | _classCallCheck(this, IsMiningMethod);
|
863 | return _super.call(this, 'eth_mining', 0, utils, formatters, moduleInstance);
|
864 | }
|
865 | return IsMiningMethod;
|
866 | }(AbstractMethod);
|
867 |
|
868 | var GetHashrateMethod = function (_AbstractMethod) {
|
869 | _inherits(GetHashrateMethod, _AbstractMethod);
|
870 | var _super = _createSuper(GetHashrateMethod);
|
871 | function GetHashrateMethod(utils, formatters, moduleInstance) {
|
872 | _classCallCheck(this, GetHashrateMethod);
|
873 | return _super.call(this, 'eth_hashrate', 0, utils, formatters, moduleInstance);
|
874 | }
|
875 | _createClass(GetHashrateMethod, [{
|
876 | key: "afterExecution",
|
877 | value: function afterExecution(response) {
|
878 | return this.utils.hexToNumber(response);
|
879 | }
|
880 | }]);
|
881 | return GetHashrateMethod;
|
882 | }(AbstractMethod);
|
883 |
|
884 | var IsSyncingMethod = function (_AbstractMethod) {
|
885 | _inherits(IsSyncingMethod, _AbstractMethod);
|
886 | var _super = _createSuper(IsSyncingMethod);
|
887 | function IsSyncingMethod(utils, formatters, moduleInstance) {
|
888 | _classCallCheck(this, IsSyncingMethod);
|
889 | return _super.call(this, 'eth_syncing', 0, utils, formatters, moduleInstance);
|
890 | }
|
891 | _createClass(IsSyncingMethod, [{
|
892 | key: "afterExecution",
|
893 | value: function afterExecution(response) {
|
894 | if (typeof response !== 'boolean') {
|
895 | return this.formatters.outputSyncingFormatter(response);
|
896 | }
|
897 | return response;
|
898 | }
|
899 | }]);
|
900 | return IsSyncingMethod;
|
901 | }(AbstractMethod);
|
902 |
|
903 | var GetGasPriceMethod = function (_AbstractMethod) {
|
904 | _inherits(GetGasPriceMethod, _AbstractMethod);
|
905 | var _super = _createSuper(GetGasPriceMethod);
|
906 | function GetGasPriceMethod(utils, formatters, moduleInstance) {
|
907 | _classCallCheck(this, GetGasPriceMethod);
|
908 | return _super.call(this, 'cfx_gasPrice', 0, utils, formatters, moduleInstance);
|
909 | }
|
910 | _createClass(GetGasPriceMethod, [{
|
911 | key: "afterExecution",
|
912 | value: function afterExecution(response) {
|
913 | return this.formatters.outputBigNumberFormatter(response);
|
914 | }
|
915 | }]);
|
916 | return GetGasPriceMethod;
|
917 | }(AbstractMethod);
|
918 |
|
919 | var SubmitWorkMethod = function (_AbstractMethod) {
|
920 | _inherits(SubmitWorkMethod, _AbstractMethod);
|
921 | var _super = _createSuper(SubmitWorkMethod);
|
922 | function SubmitWorkMethod(utils, formatters, moduleInstance) {
|
923 | _classCallCheck(this, SubmitWorkMethod);
|
924 | return _super.call(this, 'eth_submitWork', 3, utils, formatters, moduleInstance);
|
925 | }
|
926 | return SubmitWorkMethod;
|
927 | }(AbstractMethod);
|
928 |
|
929 | var GetWorkMethod = function (_AbstractMethod) {
|
930 | _inherits(GetWorkMethod, _AbstractMethod);
|
931 | var _super = _createSuper(GetWorkMethod);
|
932 | function GetWorkMethod(utils, formatters, moduleInstance) {
|
933 | _classCallCheck(this, GetWorkMethod);
|
934 | return _super.call(this, 'eth_getWork', 0, utils, formatters, moduleInstance);
|
935 | }
|
936 | return GetWorkMethod;
|
937 | }(AbstractMethod);
|
938 |
|
939 | var GetAccountsMethod = function (_AbstractMethod) {
|
940 | _inherits(GetAccountsMethod, _AbstractMethod);
|
941 | var _super = _createSuper(GetAccountsMethod);
|
942 | function GetAccountsMethod(utils, formatters, moduleInstance) {
|
943 | _classCallCheck(this, GetAccountsMethod);
|
944 | return _super.call(this, 'cfx_accounts', 0, utils, formatters, moduleInstance);
|
945 | }
|
946 | _createClass(GetAccountsMethod, [{
|
947 | key: "afterExecution",
|
948 | value: function afterExecution(response) {
|
949 | var _this = this;
|
950 | return response.map(function (responseItem) {
|
951 | return _this.utils.toChecksumAddress(responseItem);
|
952 | });
|
953 | }
|
954 | }]);
|
955 | return GetAccountsMethod;
|
956 | }(AbstractMethod);
|
957 |
|
958 | var GetBalanceMethod = function (_AbstractMethod) {
|
959 | _inherits(GetBalanceMethod, _AbstractMethod);
|
960 | var _super = _createSuper(GetBalanceMethod);
|
961 | function GetBalanceMethod(utils, formatters, moduleInstance) {
|
962 | _classCallCheck(this, GetBalanceMethod);
|
963 | return _super.call(this, 'cfx_getBalance', 2, utils, formatters, moduleInstance);
|
964 | }
|
965 | _createClass(GetBalanceMethod, [{
|
966 | key: "beforeExecution",
|
967 | value: function beforeExecution(moduleInstance) {
|
968 | this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]);
|
969 | if (isFunction(this.parameters[1])) {
|
970 | this.callback = this.parameters[1];
|
971 | this.parameters[1] = moduleInstance.defaultEpoch;
|
972 | }
|
973 | this.parameters[1] = this.formatters.inputDefaultEpochNumberFormatter(this.parameters[1], moduleInstance);
|
974 | }
|
975 | }, {
|
976 | key: "afterExecution",
|
977 | value: function afterExecution(response) {
|
978 | return this.formatters.outputBigNumberFormatter(response);
|
979 | }
|
980 | }]);
|
981 | return GetBalanceMethod;
|
982 | }(AbstractMethod);
|
983 |
|
984 | var RequestAccountsMethod = function (_AbstractMethod) {
|
985 | _inherits(RequestAccountsMethod, _AbstractMethod);
|
986 | var _super = _createSuper(RequestAccountsMethod);
|
987 | function RequestAccountsMethod(utils, formatters, moduleInstance) {
|
988 | _classCallCheck(this, RequestAccountsMethod);
|
989 | return _super.call(this, 'eth_requestAccounts', 0, utils, formatters, moduleInstance);
|
990 | }
|
991 | return RequestAccountsMethod;
|
992 | }(AbstractMethod);
|
993 |
|
994 | var AbstractGetBlockTransactionCountMethod = function (_AbstractMethod) {
|
995 | _inherits(AbstractGetBlockTransactionCountMethod, _AbstractMethod);
|
996 | var _super = _createSuper(AbstractGetBlockTransactionCountMethod);
|
997 | function AbstractGetBlockTransactionCountMethod(rpcMethod, utils, formatters, moduleInstance) {
|
998 | _classCallCheck(this, AbstractGetBlockTransactionCountMethod);
|
999 | return _super.call(this, rpcMethod, 1, utils, formatters, moduleInstance);
|
1000 | }
|
1001 | _createClass(AbstractGetBlockTransactionCountMethod, [{
|
1002 | key: "beforeExecution",
|
1003 | value: function beforeExecution(moduleInstance) {
|
1004 | this.parameters[0] = this.formatters.inputBlockAddressFormatter(this.parameters[0]);
|
1005 | }
|
1006 | }, {
|
1007 | key: "afterExecution",
|
1008 | value: function afterExecution(response) {
|
1009 | return this.utils.hexToNumber(response);
|
1010 | }
|
1011 | }]);
|
1012 | return AbstractGetBlockTransactionCountMethod;
|
1013 | }(AbstractMethod);
|
1014 |
|
1015 | var GetBlockByHashMethod = function (_AbstractGetBlockMeth) {
|
1016 | _inherits(GetBlockByHashMethod, _AbstractGetBlockMeth);
|
1017 | var _super = _createSuper(GetBlockByHashMethod);
|
1018 | function GetBlockByHashMethod(utils, formatters, moduleInstance) {
|
1019 | _classCallCheck(this, GetBlockByHashMethod);
|
1020 | return _super.call(this, 'cfx_getBlockByHash', utils, formatters, moduleInstance);
|
1021 | }
|
1022 | return GetBlockByHashMethod;
|
1023 | }(AbstractGetBlockMethod);
|
1024 |
|
1025 | var GetBlockTransactionCountByHashMethod = function (_AbstractGetBlockTran) {
|
1026 | _inherits(GetBlockTransactionCountByHashMethod, _AbstractGetBlockTran);
|
1027 | var _super = _createSuper(GetBlockTransactionCountByHashMethod);
|
1028 | function GetBlockTransactionCountByHashMethod(utils, formatters, moduleInstance) {
|
1029 | _classCallCheck(this, GetBlockTransactionCountByHashMethod);
|
1030 | return _super.call(this, 'cfx_getBlockTransactionCountByHash', utils, formatters, moduleInstance);
|
1031 | }
|
1032 | return GetBlockTransactionCountByHashMethod;
|
1033 | }(AbstractGetBlockTransactionCountMethod);
|
1034 |
|
1035 | var GetBlockTransactionCountByNumberMethod = function (_AbstractGetBlockTran) {
|
1036 | _inherits(GetBlockTransactionCountByNumberMethod, _AbstractGetBlockTran);
|
1037 | var _super = _createSuper(GetBlockTransactionCountByNumberMethod);
|
1038 | function GetBlockTransactionCountByNumberMethod(utils, formatters, moduleInstance) {
|
1039 | _classCallCheck(this, GetBlockTransactionCountByNumberMethod);
|
1040 | return _super.call(this, 'cfx_getBlockTransactionCountByNumber', utils, formatters, moduleInstance);
|
1041 | }
|
1042 | return GetBlockTransactionCountByNumberMethod;
|
1043 | }(AbstractGetBlockTransactionCountMethod);
|
1044 |
|
1045 | var AbstractGetTransactionFromBlockMethod = function (_AbstractMethod) {
|
1046 | _inherits(AbstractGetTransactionFromBlockMethod, _AbstractMethod);
|
1047 | var _super = _createSuper(AbstractGetTransactionFromBlockMethod);
|
1048 | function AbstractGetTransactionFromBlockMethod(rpcMethod, utils, formatters, moduleInstance) {
|
1049 | _classCallCheck(this, AbstractGetTransactionFromBlockMethod);
|
1050 | return _super.call(this, rpcMethod, 2, utils, formatters, moduleInstance);
|
1051 | }
|
1052 | _createClass(AbstractGetTransactionFromBlockMethod, [{
|
1053 | key: "beforeExecution",
|
1054 | value: function beforeExecution(moduleInstance) {
|
1055 | this.parameters[0] = this.formatters.inputBlockAddressFormatter(this.parameters[0]);
|
1056 | this.parameters[1] = this.utils.numberToHex(this.parameters[1]);
|
1057 | }
|
1058 | }, {
|
1059 | key: "afterExecution",
|
1060 | value: function afterExecution(response) {
|
1061 | return this.formatters.outputTransactionFormatter(response);
|
1062 | }
|
1063 | }]);
|
1064 | return AbstractGetTransactionFromBlockMethod;
|
1065 | }(AbstractMethod);
|
1066 |
|
1067 | var AbstractObservedTransactionMethod = function (_AbstractMethod) {
|
1068 | _inherits(AbstractObservedTransactionMethod, _AbstractMethod);
|
1069 | var _super = _createSuper(AbstractObservedTransactionMethod);
|
1070 | function AbstractObservedTransactionMethod(rpcMethod, parametersAmount, utils, formatters, moduleInstance, transactionObserver) {
|
1071 | var _this;
|
1072 | _classCallCheck(this, AbstractObservedTransactionMethod);
|
1073 | _this = _super.call(this, rpcMethod, parametersAmount, utils, formatters, moduleInstance);
|
1074 | _this.transactionObserver = transactionObserver;
|
1075 | _this.promiEvent = new PromiEvent();
|
1076 | return _this;
|
1077 | }
|
1078 | _createClass(AbstractObservedTransactionMethod, [{
|
1079 | key: "execute",
|
1080 | value: function execute() {
|
1081 | var _this2 = this;
|
1082 | this.beforeExecution(this.moduleInstance);
|
1083 | this.moduleInstance.currentProvider.send(this.rpcMethod, this.parameters).then(function (transactionHash) {
|
1084 | if (_this2.callback) {
|
1085 | _this2.promiEvent.resolve(_this2.callback(false, transactionHash));
|
1086 | return;
|
1087 | }
|
1088 | _this2.promiEvent.resolve(transactionHash);
|
1089 | })["catch"](function (error) {
|
1090 | if (_this2.callback) {
|
1091 | _this2.callback(error, null);
|
1092 | return;
|
1093 | }
|
1094 | _this2.handleError(error, false, 0);
|
1095 | });
|
1096 | return this.promiEvent;
|
1097 | }
|
1098 | }, {
|
1099 | key: "handleError",
|
1100 | value: function handleError(error, receipt, confirmations) {
|
1101 | if (this.promiEvent.listenerCount('error') > 0) {
|
1102 | this.promiEvent.emit('error', error, receipt, confirmations);
|
1103 | this.promiEvent.removeAllListeners();
|
1104 | return;
|
1105 | }
|
1106 | this.promiEvent.reject(error);
|
1107 | }
|
1108 | }], [{
|
1109 | key: "Type",
|
1110 | get: function get() {
|
1111 | return 'observed-transaction-method';
|
1112 | }
|
1113 | }]);
|
1114 | return AbstractObservedTransactionMethod;
|
1115 | }(AbstractMethod);
|
1116 |
|
1117 | var SendTransactionMethod = function (_AbstractObservedTran) {
|
1118 | _inherits(SendTransactionMethod, _AbstractObservedTran);
|
1119 | var _super = _createSuper(SendTransactionMethod);
|
1120 | function SendTransactionMethod(utils, formatters, moduleInstance, transactionObserver) {
|
1121 | _classCallCheck(this, SendTransactionMethod);
|
1122 | return _super.call(this, 'cfx_sendTransaction', 1, utils, formatters, moduleInstance, transactionObserver);
|
1123 | }
|
1124 | _createClass(SendTransactionMethod, [{
|
1125 | key: "beforeExecution",
|
1126 | value: function beforeExecution(moduleInstance) {
|
1127 | this.parameters[0] = this.formatters.inputTransactionFormatter(this.parameters[0], moduleInstance);
|
1128 | }
|
1129 | }]);
|
1130 | return SendTransactionMethod;
|
1131 | }(AbstractObservedTransactionMethod);
|
1132 |
|
1133 | var CfxSendTransactionMethod = function (_SendTransactionMetho) {
|
1134 | _inherits(CfxSendTransactionMethod, _SendTransactionMetho);
|
1135 | var _super = _createSuper(CfxSendTransactionMethod);
|
1136 | function CfxSendTransactionMethod(utils, formatters, moduleInstance, transactionObserver, chainIdMethod, getTransactionCountMethod) {
|
1137 | var _this;
|
1138 | _classCallCheck(this, CfxSendTransactionMethod);
|
1139 | _this = _super.call(this, utils, formatters, moduleInstance, transactionObserver);
|
1140 | _this.chainIdMethod = chainIdMethod;
|
1141 | _this.getTransactionCountMethod = getTransactionCountMethod;
|
1142 | return _this;
|
1143 | }
|
1144 | _createClass(CfxSendTransactionMethod, [{
|
1145 | key: "beforeExecution",
|
1146 | value: function beforeExecution(moduleInstance) {
|
1147 | if (this.rpcMethod !== 'cfx_sendRawTransaction') {
|
1148 | _get(_getPrototypeOf(CfxSendTransactionMethod.prototype), "beforeExecution", this).call(this, moduleInstance);
|
1149 | }
|
1150 | }
|
1151 | }, {
|
1152 | key: "execute",
|
1153 | value: function execute() {
|
1154 | var _this2 = this;
|
1155 | if (!this.parameters[0].gas && this.moduleInstance.defaultGas) {
|
1156 | this.parameters[0]['gas'] = this.moduleInstance.defaultGas;
|
1157 | }
|
1158 | if (!this.parameters[0].gasPrice && this.parameters[0].gasPrice !== 0) {
|
1159 | if (!this.moduleInstance.defaultGasPrice) {
|
1160 | this.moduleInstance.currentProvider.send('cfx_gasPrice', []).then(function (gasPrice) {
|
1161 | _this2.parameters[0].gasPrice = gasPrice;
|
1162 | _this2.execute();
|
1163 | })["catch"](function (error) {
|
1164 | _this2.handleError(error, false, 0);
|
1165 | });
|
1166 | return this.promiEvent;
|
1167 | }
|
1168 | this.parameters[0]['gasPrice'] = this.moduleInstance.defaultGasPrice;
|
1169 | }
|
1170 | if (this.hasAccounts() && this.isDefaultSigner()) {
|
1171 | if (this.moduleInstance.accounts.wallet[this.parameters[0].from]) {
|
1172 | this.sendRawTransaction(this.moduleInstance.accounts.wallet[this.parameters[0].from].privateKey)["catch"](function (error) {
|
1173 | _this2.handleError(error, false, 0);
|
1174 | });
|
1175 | return this.promiEvent;
|
1176 | }
|
1177 | }
|
1178 | if (this.hasCustomSigner()) {
|
1179 | this.sendRawTransaction()["catch"](function (error) {
|
1180 | _this2.handleError(error, false, 0);
|
1181 | });
|
1182 | return this.promiEvent;
|
1183 | }
|
1184 | return _get(_getPrototypeOf(CfxSendTransactionMethod.prototype), "execute", this).call(this);
|
1185 | }
|
1186 | }, {
|
1187 | key: "sendRawTransaction",
|
1188 | value: function () {
|
1189 | var _sendRawTransaction = _asyncToGenerator( regeneratorRuntime.mark(function _callee() {
|
1190 | var privateKey,
|
1191 | transaction,
|
1192 | response,
|
1193 | _args = arguments;
|
1194 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
1195 | while (1) {
|
1196 | switch (_context.prev = _context.next) {
|
1197 | case 0:
|
1198 | privateKey = _args.length > 0 && _args[0] !== undefined ? _args[0] : null;
|
1199 | this.beforeExecution(this.moduleInstance);
|
1200 | if (!(!this.parameters[0].nonce && this.parameters[0].nonce !== 0)) {
|
1201 | _context.next = 7;
|
1202 | break;
|
1203 | }
|
1204 | this.getTransactionCountMethod.parameters = [this.parameters[0].from, 'latest_state'];
|
1205 | _context.next = 6;
|
1206 | return this.getTransactionCountMethod.execute();
|
1207 | case 6:
|
1208 | this.parameters[0].nonce = _context.sent;
|
1209 | case 7:
|
1210 | if (!this.parameters[0].storageLimit) {
|
1211 | this.parameters[0]['storageLimit'] = 100000000;
|
1212 | }
|
1213 | if (!this.parameters[0].chainId) {
|
1214 | this.parameters[0]['chainId'] = 0;
|
1215 | }
|
1216 | if (this.parameters[0].epochHeight) {
|
1217 | _context.next = 13;
|
1218 | break;
|
1219 | }
|
1220 | _context.next = 12;
|
1221 | return this.moduleInstance.currentProvider.send('cfx_epochNumber', []);
|
1222 | case 12:
|
1223 | this.parameters[0].epochHeight = _context.sent;
|
1224 | case 13:
|
1225 | transaction = this.parameters[0];
|
1226 | transaction.to = transaction.to || '0x';
|
1227 | transaction.data = transaction.data || '0x';
|
1228 | transaction.value = transaction.value || '0x';
|
1229 | delete transaction.from;
|
1230 | _context.next = 20;
|
1231 | return this.moduleInstance.transactionSigner.sign(transaction, privateKey);
|
1232 | case 20:
|
1233 | response = _context.sent;
|
1234 | this.parameters = [response.rawTransaction];
|
1235 | this.rpcMethod = 'cfx_sendRawTransaction';
|
1236 | return _context.abrupt("return", _get(_getPrototypeOf(CfxSendTransactionMethod.prototype), "execute", this).call(this));
|
1237 | case 24:
|
1238 | case "end":
|
1239 | return _context.stop();
|
1240 | }
|
1241 | }
|
1242 | }, _callee, this);
|
1243 | }));
|
1244 | function sendRawTransaction() {
|
1245 | return _sendRawTransaction.apply(this, arguments);
|
1246 | }
|
1247 | return sendRawTransaction;
|
1248 | }()
|
1249 | }, {
|
1250 | key: "isDefaultSigner",
|
1251 | value: function isDefaultSigner() {
|
1252 | return this.moduleInstance.transactionSigner.type === 'TransactionSigner';
|
1253 | }
|
1254 | }, {
|
1255 | key: "hasAccounts",
|
1256 | value: function hasAccounts() {
|
1257 | return this.moduleInstance.accounts && this.moduleInstance.accounts.wallet.accountsIndex > 0;
|
1258 | }
|
1259 | }, {
|
1260 | key: "hasCustomSigner",
|
1261 | value: function hasCustomSigner() {
|
1262 | return this.moduleInstance.transactionSigner.type !== 'TransactionSigner';
|
1263 | }
|
1264 | }], [{
|
1265 | key: "Type",
|
1266 | get: function get() {
|
1267 | return 'cfx-send-transaction-method';
|
1268 | }
|
1269 | }]);
|
1270 | return CfxSendTransactionMethod;
|
1271 | }(SendTransactionMethod);
|
1272 |
|
1273 | var GetTransactionMethod = function (_AbstractMethod) {
|
1274 | _inherits(GetTransactionMethod, _AbstractMethod);
|
1275 | var _super = _createSuper(GetTransactionMethod);
|
1276 | function GetTransactionMethod(utils, formatters, moduleInstance) {
|
1277 | _classCallCheck(this, GetTransactionMethod);
|
1278 | return _super.call(this, 'cfx_getTransactionByHash', 1, utils, formatters, moduleInstance);
|
1279 | }
|
1280 | _createClass(GetTransactionMethod, [{
|
1281 | key: "afterExecution",
|
1282 | value: function afterExecution(response) {
|
1283 | return this.formatters.outputTransactionFormatter(response);
|
1284 | }
|
1285 | }]);
|
1286 | return GetTransactionMethod;
|
1287 | }(AbstractMethod);
|
1288 |
|
1289 | var GetTransactionByBlockHashAndIndexMethod = function (_AbstractGetTransacti) {
|
1290 | _inherits(GetTransactionByBlockHashAndIndexMethod, _AbstractGetTransacti);
|
1291 | var _super = _createSuper(GetTransactionByBlockHashAndIndexMethod);
|
1292 | function GetTransactionByBlockHashAndIndexMethod(utils, formatters, moduleInstance) {
|
1293 | _classCallCheck(this, GetTransactionByBlockHashAndIndexMethod);
|
1294 | return _super.call(this, 'cfx_getTransactionByBlockHashAndIndex', utils, formatters, moduleInstance);
|
1295 | }
|
1296 | return GetTransactionByBlockHashAndIndexMethod;
|
1297 | }(AbstractGetTransactionFromBlockMethod);
|
1298 |
|
1299 | var GetTransactionByBlockAddressAndIndexMethod = function (_AbstractGetTransacti) {
|
1300 | _inherits(GetTransactionByBlockAddressAndIndexMethod, _AbstractGetTransacti);
|
1301 | var _super = _createSuper(GetTransactionByBlockAddressAndIndexMethod);
|
1302 | function GetTransactionByBlockAddressAndIndexMethod(utils, formatters, moduleInstance) {
|
1303 | _classCallCheck(this, GetTransactionByBlockAddressAndIndexMethod);
|
1304 | return _super.call(this, 'cfx_getTransactionByBlockAddressAndIndex', utils, formatters, moduleInstance);
|
1305 | }
|
1306 | return GetTransactionByBlockAddressAndIndexMethod;
|
1307 | }(AbstractGetTransactionFromBlockMethod);
|
1308 |
|
1309 | var SendRawTransactionMethod = function (_AbstractObservedTran) {
|
1310 | _inherits(SendRawTransactionMethod, _AbstractObservedTran);
|
1311 | var _super = _createSuper(SendRawTransactionMethod);
|
1312 | function SendRawTransactionMethod(utils, formatters, moduleInstance, transactionObserver) {
|
1313 | _classCallCheck(this, SendRawTransactionMethod);
|
1314 | return _super.call(this, 'cfx_sendRawTransaction', 1, utils, formatters, moduleInstance, transactionObserver);
|
1315 | }
|
1316 | return SendRawTransactionMethod;
|
1317 | }(AbstractObservedTransactionMethod);
|
1318 |
|
1319 | var SignTransactionMethod = function (_AbstractMethod) {
|
1320 | _inherits(SignTransactionMethod, _AbstractMethod);
|
1321 | var _super = _createSuper(SignTransactionMethod);
|
1322 | function SignTransactionMethod(utils, formatters, moduleInstance) {
|
1323 | _classCallCheck(this, SignTransactionMethod);
|
1324 | return _super.call(this, 'cfx_signTransaction', 1, utils, formatters, moduleInstance);
|
1325 | }
|
1326 | _createClass(SignTransactionMethod, [{
|
1327 | key: "beforeExecution",
|
1328 | value: function beforeExecution(moduleInstance) {
|
1329 | this.parameters[0] = this.formatters.inputTransactionFormatter(this.parameters[0], moduleInstance);
|
1330 | }
|
1331 | }]);
|
1332 | return SignTransactionMethod;
|
1333 | }(AbstractMethod);
|
1334 |
|
1335 | var GetCodeMethod = function (_AbstractMethod) {
|
1336 | _inherits(GetCodeMethod, _AbstractMethod);
|
1337 | var _super = _createSuper(GetCodeMethod);
|
1338 | function GetCodeMethod(utils, formatters, moduleInstance) {
|
1339 | _classCallCheck(this, GetCodeMethod);
|
1340 | return _super.call(this, 'cfx_getCode', 2, utils, formatters, moduleInstance);
|
1341 | }
|
1342 | _createClass(GetCodeMethod, [{
|
1343 | key: "beforeExecution",
|
1344 | value: function beforeExecution(moduleInstance) {
|
1345 | this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]);
|
1346 | if (isFunction(this.parameters[1])) {
|
1347 | this.callback = this.parameters[1];
|
1348 | this.parameters[1] = moduleInstance.defaultEpoch;
|
1349 | }
|
1350 | this.parameters[1] = this.formatters.inputDefaultEpochNumberFormatter(this.parameters[1], moduleInstance);
|
1351 | }
|
1352 | }]);
|
1353 | return GetCodeMethod;
|
1354 | }(AbstractMethod);
|
1355 |
|
1356 | var SignMethod = function (_AbstractMethod) {
|
1357 | _inherits(SignMethod, _AbstractMethod);
|
1358 | var _super = _createSuper(SignMethod);
|
1359 | function SignMethod(utils, formatters, moduleInstance) {
|
1360 | _classCallCheck(this, SignMethod);
|
1361 | return _super.call(this, 'cfx_sign', 2, utils, formatters, moduleInstance);
|
1362 | }
|
1363 | _createClass(SignMethod, [{
|
1364 | key: "beforeExecution",
|
1365 | value: function beforeExecution(moduleInstance) {
|
1366 | this.parameters[0] = this.formatters.inputSignFormatter(this.parameters[0]);
|
1367 | this.parameters[1] = this.formatters.inputAddressFormatter(this.parameters[1]);
|
1368 | this.parameters.reverse();
|
1369 | }
|
1370 | }]);
|
1371 | return SignMethod;
|
1372 | }(AbstractMethod);
|
1373 |
|
1374 | var CallMethod = function (_AbstractMethod) {
|
1375 | _inherits(CallMethod, _AbstractMethod);
|
1376 | var _super = _createSuper(CallMethod);
|
1377 | function CallMethod(utils, formatters, moduleInstance) {
|
1378 | _classCallCheck(this, CallMethod);
|
1379 | return _super.call(this, 'cfx_call', 2, utils, formatters, moduleInstance);
|
1380 | }
|
1381 | _createClass(CallMethod, [{
|
1382 | key: "beforeExecution",
|
1383 | value: function beforeExecution(moduleInstance) {
|
1384 | this.parameters[0] = this.formatters.inputCallFormatter(this.parameters[0], moduleInstance);
|
1385 | if (isFunction(this.parameters[1])) {
|
1386 | this.callback = this.parameters[1];
|
1387 | this.parameters[1] = moduleInstance.defaultEpoch;
|
1388 | }
|
1389 | this.parameters[1] = this.formatters.inputDefaultEpochNumberFormatter(this.parameters[1], moduleInstance);
|
1390 | }
|
1391 | }]);
|
1392 | return CallMethod;
|
1393 | }(AbstractMethod);
|
1394 |
|
1395 | var GetStorageAtMethod = function (_AbstractMethod) {
|
1396 | _inherits(GetStorageAtMethod, _AbstractMethod);
|
1397 | var _super = _createSuper(GetStorageAtMethod);
|
1398 | function GetStorageAtMethod(utils, formatters, moduleInstance) {
|
1399 | _classCallCheck(this, GetStorageAtMethod);
|
1400 | return _super.call(this, 'eth_getStorageAt', 3, utils, formatters, moduleInstance);
|
1401 | }
|
1402 | _createClass(GetStorageAtMethod, [{
|
1403 | key: "beforeExecution",
|
1404 | value: function beforeExecution(moduleInstance) {
|
1405 | this.parameters[0] = this.formatters.inputAddressFormatter(this.parameters[0]);
|
1406 | this.parameters[1] = this.utils.numberToHex(this.parameters[1]);
|
1407 | if (isFunction(this.parameters[2])) {
|
1408 | this.callback = this.parameters[2];
|
1409 | this.parameters[2] = moduleInstance.defaultEpoch;
|
1410 | }
|
1411 | this.parameters[2] = this.formatters.inputDefaultEpochNumberFormatter(this.parameters[2], moduleInstance);
|
1412 | }
|
1413 | }]);
|
1414 | return GetStorageAtMethod;
|
1415 | }(AbstractMethod);
|
1416 |
|
1417 | var EstimateGasMethod = function (_AbstractMethod) {
|
1418 | _inherits(EstimateGasMethod, _AbstractMethod);
|
1419 | var _super = _createSuper(EstimateGasMethod);
|
1420 | function EstimateGasMethod(utils, formatters, moduleInstance) {
|
1421 | _classCallCheck(this, EstimateGasMethod);
|
1422 | return _super.call(this, 'cfx_estimateGas', 1, utils, formatters, moduleInstance);
|
1423 | }
|
1424 | _createClass(EstimateGasMethod, [{
|
1425 | key: "beforeExecution",
|
1426 | value: function beforeExecution(moduleInstance) {
|
1427 | this.parameters[0] = this.formatters.inputCallFormatter(this.parameters[0], moduleInstance);
|
1428 | }
|
1429 | }, {
|
1430 | key: "afterExecution",
|
1431 | value: function afterExecution(response) {
|
1432 | return this.utils.hexToNumber(response);
|
1433 | }
|
1434 | }]);
|
1435 | return EstimateGasMethod;
|
1436 | }(AbstractMethod);
|
1437 |
|
1438 | var GetPastLogsMethod = function (_AbstractMethod) {
|
1439 | _inherits(GetPastLogsMethod, _AbstractMethod);
|
1440 | var _super = _createSuper(GetPastLogsMethod);
|
1441 | function GetPastLogsMethod(utils, formatters, moduleInstance) {
|
1442 | _classCallCheck(this, GetPastLogsMethod);
|
1443 | return _super.call(this, 'cfx_getLogs', 1, utils, formatters, moduleInstance);
|
1444 | }
|
1445 | _createClass(GetPastLogsMethod, [{
|
1446 | key: "beforeExecution",
|
1447 | value: function beforeExecution(moduleInstance) {
|
1448 | this.parameters[0] = this.formatters.inputLogFormatter(this.parameters[0]);
|
1449 | }
|
1450 | }, {
|
1451 | key: "afterExecution",
|
1452 | value: function afterExecution(response) {
|
1453 | var _this = this;
|
1454 | return response.map(function (responseItem) {
|
1455 | return _this.formatters.outputLogFormatter(responseItem);
|
1456 | });
|
1457 | }
|
1458 | }]);
|
1459 | return GetPastLogsMethod;
|
1460 | }(AbstractMethod);
|
1461 |
|
1462 | var GetEpochNumberMethod = function (_AbstractMethod) {
|
1463 | _inherits(GetEpochNumberMethod, _AbstractMethod);
|
1464 | var _super = _createSuper(GetEpochNumberMethod);
|
1465 | function GetEpochNumberMethod(utils, formatters, moduleInstance) {
|
1466 | _classCallCheck(this, GetEpochNumberMethod);
|
1467 | return _super.call(this, 'cfx_epochNumber', 1, utils, formatters, moduleInstance);
|
1468 | }
|
1469 | _createClass(GetEpochNumberMethod, [{
|
1470 | key: "beforeExecution",
|
1471 | value: function beforeExecution(moduleInstance) {
|
1472 | if (this.parameters[0] === undefined) {
|
1473 | this.parameters[0] = 'latest_mined';
|
1474 | }
|
1475 | if (!this.formatters.isPredefinedEpochNumber(this.parameters[0])) {
|
1476 | throw new Error('The epoch field must be predefined epoch number.');
|
1477 | }
|
1478 | }
|
1479 | }, {
|
1480 | key: "afterExecution",
|
1481 | value: function afterExecution(response) {
|
1482 | return this.utils.hexToNumber(response);
|
1483 | }
|
1484 | }]);
|
1485 | return GetEpochNumberMethod;
|
1486 | }(AbstractMethod);
|
1487 |
|
1488 | export { AbstractGetBlockMethod, AbstractGetBlockTransactionCountMethod, AbstractGetTransactionFromBlockMethod, AbstractMethod, AbstractMethodFactory, AbstractObservedTransactionMethod, CallMethod, CfxSendTransactionMethod, ChainIdMethod, EstimateGasMethod, GetAccountsMethod, GetBalanceMethod, GetBlockByHashMethod, GetBlockByNumberMethod, GetBlockTransactionCountByHashMethod, GetBlockTransactionCountByNumberMethod, GetCodeMethod, GetCoinbaseMethod, GetEpochNumberMethod, GetGasPriceMethod, GetHashrateMethod, GetNodeInfoMethod, GetPastLogsMethod, GetProtocolVersionMethod, GetStorageAtMethod, GetTransactionByBlockAddressAndIndexMethod, GetTransactionByBlockHashAndIndexMethod, GetTransactionCountMethod, GetTransactionMethod, GetTransactionReceiptMethod, GetWorkMethod, IsMiningMethod, IsSyncingMethod, ListeningMethod, MethodProxy, PeerCountMethod, PromiEvent, RequestAccountsMethod, SendRawTransactionMethod, SendTransactionMethod, SignMethod, SignTransactionMethod, SubmitWorkMethod, TransactionObserver, VersionMethod };
|