1 | import { formatters } from 'conflux-web-core-helpers';
|
2 | import * as Utils from 'conflux-web-utils';
|
3 | import { VersionMethod, GetBlockByNumberMethod, ListeningMethod, PeerCountMethod, AbstractMethodFactory } from 'conflux-web-core-method';
|
4 | import { AbstractConfluxWebModule } from 'conflux-web-core';
|
5 | import isFunction from 'lodash/isFunction';
|
6 |
|
7 | function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
8 | try {
|
9 | var info = gen[key](arg);
|
10 | var value = info.value;
|
11 | } catch (error) {
|
12 | reject(error);
|
13 | return;
|
14 | }
|
15 |
|
16 | if (info.done) {
|
17 | resolve(value);
|
18 | } else {
|
19 | Promise.resolve(value).then(_next, _throw);
|
20 | }
|
21 | }
|
22 |
|
23 | function _asyncToGenerator(fn) {
|
24 | return function () {
|
25 | var self = this,
|
26 | args = arguments;
|
27 | return new Promise(function (resolve, reject) {
|
28 | var gen = fn.apply(self, args);
|
29 |
|
30 | function _next(value) {
|
31 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
32 | }
|
33 |
|
34 | function _throw(err) {
|
35 | asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
36 | }
|
37 |
|
38 | _next(undefined);
|
39 | });
|
40 | };
|
41 | }
|
42 |
|
43 | function _classCallCheck(instance, Constructor) {
|
44 | if (!(instance instanceof Constructor)) {
|
45 | throw new TypeError("Cannot call a class as a function");
|
46 | }
|
47 | }
|
48 |
|
49 | function _defineProperties(target, props) {
|
50 | for (var i = 0; i < props.length; i++) {
|
51 | var descriptor = props[i];
|
52 | descriptor.enumerable = descriptor.enumerable || false;
|
53 | descriptor.configurable = true;
|
54 | if ("value" in descriptor) descriptor.writable = true;
|
55 | Object.defineProperty(target, descriptor.key, descriptor);
|
56 | }
|
57 | }
|
58 |
|
59 | function _createClass(Constructor, protoProps, staticProps) {
|
60 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
61 | if (staticProps) _defineProperties(Constructor, staticProps);
|
62 | return Constructor;
|
63 | }
|
64 |
|
65 | function _inherits(subClass, superClass) {
|
66 | if (typeof superClass !== "function" && superClass !== null) {
|
67 | throw new TypeError("Super expression must either be null or a function");
|
68 | }
|
69 |
|
70 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
71 | constructor: {
|
72 | value: subClass,
|
73 | writable: true,
|
74 | configurable: true
|
75 | }
|
76 | });
|
77 | if (superClass) _setPrototypeOf(subClass, superClass);
|
78 | }
|
79 |
|
80 | function _getPrototypeOf(o) {
|
81 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
82 | return o.__proto__ || Object.getPrototypeOf(o);
|
83 | };
|
84 | return _getPrototypeOf(o);
|
85 | }
|
86 |
|
87 | function _setPrototypeOf(o, p) {
|
88 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
89 | o.__proto__ = p;
|
90 | return o;
|
91 | };
|
92 |
|
93 | return _setPrototypeOf(o, p);
|
94 | }
|
95 |
|
96 | function _isNativeReflectConstruct() {
|
97 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
98 | if (Reflect.construct.sham) return false;
|
99 | if (typeof Proxy === "function") return true;
|
100 |
|
101 | try {
|
102 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
103 | return true;
|
104 | } catch (e) {
|
105 | return false;
|
106 | }
|
107 | }
|
108 |
|
109 | function _assertThisInitialized(self) {
|
110 | if (self === void 0) {
|
111 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
112 | }
|
113 |
|
114 | return self;
|
115 | }
|
116 |
|
117 | function _possibleConstructorReturn(self, call) {
|
118 | if (call && (typeof call === "object" || typeof call === "function")) {
|
119 | return call;
|
120 | }
|
121 |
|
122 | return _assertThisInitialized(self);
|
123 | }
|
124 |
|
125 | function _createSuper(Derived) {
|
126 | return function () {
|
127 | var Super = _getPrototypeOf(Derived),
|
128 | result;
|
129 |
|
130 | if (_isNativeReflectConstruct()) {
|
131 | var NewTarget = _getPrototypeOf(this).constructor;
|
132 |
|
133 | result = Reflect.construct(Super, arguments, NewTarget);
|
134 | } else {
|
135 | result = Super.apply(this, arguments);
|
136 | }
|
137 |
|
138 | return _possibleConstructorReturn(this, result);
|
139 | };
|
140 | }
|
141 |
|
142 | var MethodFactory = function (_AbstractMethodFactor) {
|
143 | _inherits(MethodFactory, _AbstractMethodFactor);
|
144 | var _super = _createSuper(MethodFactory);
|
145 | function MethodFactory(utils, formatters) {
|
146 | var _this;
|
147 | _classCallCheck(this, MethodFactory);
|
148 | _this = _super.call(this, utils, formatters);
|
149 | _this.methods = {
|
150 | getId: VersionMethod,
|
151 | getBlockByNumber: GetBlockByNumberMethod,
|
152 | isListening: ListeningMethod,
|
153 | getPeerCount: PeerCountMethod
|
154 | };
|
155 | return _this;
|
156 | }
|
157 | return MethodFactory;
|
158 | }(AbstractMethodFactory);
|
159 |
|
160 | var Network = function (_AbstractConfluxWebMo) {
|
161 | _inherits(Network, _AbstractConfluxWebMo);
|
162 | var _super = _createSuper(Network);
|
163 | function Network(provider, methodFactory, utils, formatters, options, nodeNet) {
|
164 | var _this;
|
165 | _classCallCheck(this, Network);
|
166 | _this = _super.call(this, provider, options, methodFactory, nodeNet);
|
167 | _this.utils = utils;
|
168 | _this.formatters = formatters;
|
169 | return _this;
|
170 | }
|
171 | _createClass(Network, [{
|
172 | key: "getNetworkType",
|
173 | value: function () {
|
174 | var _getNetworkType = _asyncToGenerator( regeneratorRuntime.mark(function _callee(callback) {
|
175 | var id, networkType;
|
176 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
177 | while (1) {
|
178 | switch (_context.prev = _context.next) {
|
179 | case 0:
|
180 | _context.prev = 0;
|
181 | _context.next = 3;
|
182 | return this.getId();
|
183 | case 3:
|
184 | id = _context.sent;
|
185 | networkType = 'private';
|
186 | _context.t0 = id;
|
187 | _context.next = _context.t0 === 1 ? 8 : _context.t0 === 2 ? 10 : _context.t0 === 3 ? 12 : _context.t0 === 4 ? 14 : _context.t0 === 42 ? 16 : 18;
|
188 | break;
|
189 | case 8:
|
190 | networkType = 'main';
|
191 | return _context.abrupt("break", 18);
|
192 | case 10:
|
193 | networkType = 'morden';
|
194 | return _context.abrupt("break", 18);
|
195 | case 12:
|
196 | networkType = 'ropsten';
|
197 | return _context.abrupt("break", 18);
|
198 | case 14:
|
199 | networkType = 'rinkeby';
|
200 | return _context.abrupt("break", 18);
|
201 | case 16:
|
202 | networkType = 'kovan';
|
203 | return _context.abrupt("break", 18);
|
204 | case 18:
|
205 | if (isFunction(callback)) {
|
206 | callback(null, networkType);
|
207 | }
|
208 | return _context.abrupt("return", networkType);
|
209 | case 22:
|
210 | _context.prev = 22;
|
211 | _context.t1 = _context["catch"](0);
|
212 | if (isFunction(callback)) {
|
213 | callback(_context.t1, null);
|
214 | }
|
215 | throw _context.t1;
|
216 | case 26:
|
217 | case "end":
|
218 | return _context.stop();
|
219 | }
|
220 | }
|
221 | }, _callee, this, [[0, 22]]);
|
222 | }));
|
223 | function getNetworkType(_x) {
|
224 | return _getNetworkType.apply(this, arguments);
|
225 | }
|
226 | return getNetworkType;
|
227 | }()
|
228 | }]);
|
229 | return Network;
|
230 | }(AbstractConfluxWebModule);
|
231 |
|
232 | function Network$1(provider) {
|
233 | var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
234 | return new Network(provider, new MethodFactory(Utils, formatters), Utils, formatters, options, null);
|
235 | }
|
236 |
|
237 | export { Network$1 as Network };
|