UNPKG

5.06 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var _classCallCheck = _interopDefault(require('@babel/runtime/helpers/classCallCheck'));
8var _createClass = _interopDefault(require('@babel/runtime/helpers/createClass'));
9var isObject = _interopDefault(require('lodash/isObject'));
10var confluxWebProviders = require('conflux-web-providers');
11var confluxWebCoreMethod = require('conflux-web-core-method');
12var confluxWebUtils = require('conflux-web-utils');
13
14var AbstractConfluxWebModule = function () {
15 function AbstractConfluxWebModule(provider) {
16 var _this = this;
17 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
18 var methodFactory = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
19 var nodeNet = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
20 _classCallCheck(this, AbstractConfluxWebModule);
21 this.providerResolver = new confluxWebProviders.ProviderResolver();
22 this.givenProvider = confluxWebProviders.ProviderDetector.detect();
23 this._currentProvider = this.providerResolver.resolve(provider, nodeNet);
24 this._defaultAccount = options.defaultAccount ? confluxWebUtils.toChecksumAddress(options.defaultAccount) : undefined;
25 this._defaultEpoch = options.defaultEpoch || 'latest_state';
26 this._transactionBlockTimeout = options.transactionBlockTimeout || 50;
27 this._transactionConfirmationBlocks = options.transactionConfirmationBlocks || 24;
28 this._transactionPollingTimeout = options.transactionPollingTimeout || 750;
29 this._defaultGasPrice = options.defaultGasPrice;
30 this._defaultGas = options.defaultGas;
31 this.BatchRequest = function () {
32 return new confluxWebProviders.BatchRequest(_this);
33 };
34 if (methodFactory) {
35 return new confluxWebCoreMethod.MethodProxy(this, methodFactory);
36 }
37 }
38 _createClass(AbstractConfluxWebModule, [{
39 key: "setProvider",
40 value: function setProvider(provider, net) {
41 if (!this.isSameProvider(provider)) {
42 var resolvedProvider = this.providerResolver.resolve(provider, net);
43 this.clearSubscriptions();
44 this._currentProvider = resolvedProvider;
45 return true;
46 }
47 return false;
48 }
49 }, {
50 key: "isSameProvider",
51 value: function isSameProvider(provider) {
52 if (isObject(provider)) {
53 if (this.currentProvider.constructor.name === provider.constructor.name) {
54 return this.currentProvider.host === provider.host;
55 }
56 return false;
57 }
58 return this.currentProvider.host === provider;
59 }
60 }, {
61 key: "clearSubscriptions",
62 value: function clearSubscriptions(unsubscribeMethod) {
63 if (typeof this.currentProvider.clearSubscriptions !== 'undefined' && this.currentProvider.subscriptions.length > 0) {
64 return this.currentProvider.clearSubscriptions(unsubscribeMethod);
65 }
66 return Promise.resolve(true);
67 }
68 }, {
69 key: "defaultEpoch",
70 get: function get() {
71 return this._defaultEpoch;
72 }
73 ,
74 set: function set(value) {
75 this._defaultEpoch = value;
76 }
77 }, {
78 key: "transactionBlockTimeout",
79 get: function get() {
80 return this._transactionBlockTimeout;
81 }
82 ,
83 set: function set(value) {
84 this._transactionBlockTimeout = value;
85 }
86 }, {
87 key: "transactionConfirmationBlocks",
88 get: function get() {
89 return this._transactionConfirmationBlocks;
90 }
91 ,
92 set: function set(value) {
93 this._transactionConfirmationBlocks = value;
94 }
95 }, {
96 key: "transactionPollingTimeout",
97 get: function get() {
98 return this._transactionPollingTimeout;
99 }
100 ,
101 set: function set(value) {
102 this._transactionPollingTimeout = value;
103 }
104 }, {
105 key: "defaultGasPrice",
106 get: function get() {
107 return this._defaultGasPrice;
108 }
109 ,
110 set: function set(value) {
111 this._defaultGasPrice = value;
112 }
113 }, {
114 key: "defaultGas",
115 get: function get() {
116 return this._defaultGas;
117 }
118 ,
119 set: function set(value) {
120 this._defaultGas = value;
121 }
122 }, {
123 key: "defaultAccount",
124 get: function get() {
125 return this._defaultAccount;
126 }
127 ,
128 set: function set(value) {
129 this._defaultAccount = confluxWebUtils.toChecksumAddress(value);
130 }
131 }, {
132 key: "currentProvider",
133 get: function get() {
134 return this._currentProvider;
135 }
136 ,
137 set: function set(value) {
138 throw new Error('The property currentProvider is read-only!');
139 }
140 }], [{
141 key: "providers",
142 get: function get() {
143 return {
144 HttpProvider: confluxWebProviders.HttpProvider,
145 WebsocketProvider: confluxWebProviders.WebsocketProvider,
146 IpcProvider: confluxWebProviders.IpcProvider
147 };
148 }
149 }]);
150 return AbstractConfluxWebModule;
151}();
152
153exports.AbstractConfluxWebModule = AbstractConfluxWebModule;