@super-protocol/sdk-js 0.15.0 → 0.15.1

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 (81) hide show
  1. package/build/TIIGenerator.js +129 -239
  2. package/build/connectors/BaseConnector.js +17 -47
  3. package/build/connectors/BlockchainConnector.js +222 -348
  4. package/build/connectors/BlockchainEventsListener.js +38 -92
  5. package/build/contracts/app.json +466 -35
  6. package/build/crypto/Crypto.js +63 -135
  7. package/build/crypto/index.js +1 -1
  8. package/build/crypto/nodejs/AES.js +62 -119
  9. package/build/crypto/nodejs/ARIA.js +61 -118
  10. package/build/crypto/nodejs/ECIES.js +51 -87
  11. package/build/crypto/nodejs/NativeCrypto.js +64 -118
  12. package/build/crypto/nodejs/RSA-Hybrid.js +54 -113
  13. package/build/index.d.ts +5 -0
  14. package/build/index.js +14 -2
  15. package/build/logger.d.ts +3 -1
  16. package/build/logger.js +2 -2
  17. package/build/models/EtlModel.js +65 -114
  18. package/build/models/Offer.js +227 -462
  19. package/build/models/Order.d.ts +4 -0
  20. package/build/models/Order.js +326 -559
  21. package/build/models/Provider.d.ts +2 -0
  22. package/build/models/Provider.js +74 -153
  23. package/build/models/TCB.js +67 -171
  24. package/build/models/TeeOffer.d.ts +1 -0
  25. package/build/models/TeeOffer.js +389 -731
  26. package/build/proto/Compression.js +20 -21
  27. package/build/proto/TRI.js +46 -50
  28. package/build/proto/TeeProperties.js +60 -66
  29. package/build/providers/storage/ChunksDownloadDecorator.js +125 -235
  30. package/build/providers/storage/S3StorageProvider.js +173 -298
  31. package/build/providers/storage/StorageAdapter.d.ts +60 -0
  32. package/build/providers/storage/StorageAdapter.js +317 -0
  33. package/build/providers/storage/StorageContentWriter.d.ts +39 -0
  34. package/build/providers/storage/StorageContentWriter.js +181 -0
  35. package/build/providers/storage/StorageKeyValueAdapter.d.ts +20 -0
  36. package/build/providers/storage/StorageKeyValueAdapter.js +152 -0
  37. package/build/providers/storage/StorageMetadataReader.d.ts +19 -0
  38. package/build/providers/storage/StorageMetadataReader.js +65 -0
  39. package/build/providers/storage/StorjAdapter.d.ts +19 -0
  40. package/build/providers/storage/StorjAdapter.js +62 -0
  41. package/build/providers/storage/StorjStorageProvider.js +138 -315
  42. package/build/providers/storage/getStorageProvider.js +7 -7
  43. package/build/providers/storage/types.d.ts +7 -0
  44. package/build/providers/storage/types.js +2 -0
  45. package/build/staticModels/ActiveOffers.js +32 -100
  46. package/build/staticModels/ActiveOrders.js +42 -120
  47. package/build/staticModels/BaseStaticModel.js +7 -10
  48. package/build/staticModels/Consensus.d.ts +4 -1
  49. package/build/staticModels/Consensus.js +164 -220
  50. package/build/staticModels/Deposits.js +109 -201
  51. package/build/staticModels/Marks.js +21 -82
  52. package/build/staticModels/ModelPackager.js +34 -84
  53. package/build/staticModels/Offers.js +140 -266
  54. package/build/staticModels/Orders.d.ts +1 -0
  55. package/build/staticModels/Orders.js +286 -429
  56. package/build/staticModels/ProviderRegistry.js +114 -225
  57. package/build/staticModels/Superpro.js +17 -63
  58. package/build/staticModels/SuperproToken.js +90 -169
  59. package/build/staticModels/TeeOffers.js +220 -416
  60. package/build/store.js +2 -2
  61. package/build/types/HardwareInfo.js +2 -2
  62. package/build/types/Order.js +4 -4
  63. package/build/types/Provider.js +1 -1
  64. package/build/types/TeeOfferInfo.js +1 -1
  65. package/build/types/TeeOfferOption.js +2 -2
  66. package/build/types/TeeOfferSlot.js +2 -2
  67. package/build/types/ValueOfferSlot.js +3 -3
  68. package/build/utils/Monitoring.js +24 -28
  69. package/build/utils/NonceTracker.js +56 -121
  70. package/build/utils/PubSub.d.ts +9 -0
  71. package/build/utils/PubSub.js +36 -0
  72. package/build/utils/TxManager.js +121 -215
  73. package/build/utils/compressors/GzipCompressor.js +13 -50
  74. package/build/utils/compressors/UncompressedCompressor.js +10 -13
  75. package/build/utils/helpers/tryWithInterval.js +34 -78
  76. package/build/utils/resourceLoaders/BaseResourceLoader.js +26 -78
  77. package/build/utils/resourceLoaders/StorageProviderLoader.js +33 -97
  78. package/build/utils/resourceLoaders/UrlResourceLoader.js +29 -93
  79. package/build/utils/resourceLoaders/getResourceLoader.js +6 -6
  80. package/build/utils.js +60 -105
  81. package/package.json +4 -1
@@ -1,30 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
2
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
29
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
30
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -40,313 +14,227 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
40
14
  step((generator = generator.apply(thisArg, _arguments || [])).next());
41
15
  });
42
16
  };
43
- var __generator = (this && this.__generator) || function (thisArg, body) {
44
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
45
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
46
- function verb(n) { return function (v) { return step([n, v]); }; }
47
- function step(op) {
48
- if (f) throw new TypeError("Generator is already executing.");
49
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
50
- 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;
51
- if (y = 0, t) op = [op[0] & 2, t.value];
52
- switch (op[0]) {
53
- case 0: case 1: t = op; break;
54
- case 4: _.label++; return { value: op[1], done: false };
55
- case 5: _.label++; y = op[1]; op = [0]; continue;
56
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
57
- default:
58
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
59
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
60
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
61
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
62
- if (t[2]) _.ops.pop();
63
- _.trys.pop(); continue;
64
- }
65
- op = body.call(thisArg, _);
66
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
67
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
68
- }
69
- };
70
17
  var __importDefault = (this && this.__importDefault) || function (mod) {
71
18
  return (mod && mod.__esModule) ? mod : { "default": mod };
72
19
  };
73
20
  Object.defineProperty(exports, "__esModule", { value: true });
74
- var BaseConnector_1 = require("./BaseConnector");
75
- var web3_1 = __importDefault(require("web3"));
76
- var web3_core_helpers_1 = require("web3-core-helpers");
77
- var constants_1 = require("../constants");
78
- var utils_1 = require("../utils");
79
- var TxManager_1 = __importDefault(require("../utils/TxManager"));
80
- var app_json_1 = __importDefault(require("../contracts/app.json"));
81
- var ethers_1 = require("ethers");
82
- var Jsonrpc = require("web3-core-requestmanager/src/jsonrpc");
21
+ const BaseConnector_1 = require("./BaseConnector");
22
+ const web3_1 = __importDefault(require("web3"));
23
+ const web3_core_helpers_1 = require("web3-core-helpers");
24
+ const constants_1 = require("../constants");
25
+ const utils_1 = require("../utils");
26
+ const TxManager_1 = __importDefault(require("../utils/TxManager"));
27
+ const app_json_1 = __importDefault(require("../contracts/app.json"));
28
+ const ethers_1 = require("ethers");
29
+ const Jsonrpc = require("web3-core-requestmanager/src/jsonrpc");
83
30
  // TODO: remove this dependencies
84
- var store_1 = __importDefault(require("../store"));
85
- var Superpro_1 = __importDefault(require("../staticModels/Superpro"));
86
- var SuperproToken_1 = __importDefault(require("../staticModels/SuperproToken"));
87
- var Monitoring_1 = require("../utils/Monitoring");
88
- var BlockchainConnector = /** @class */ (function (_super) {
89
- __extends(BlockchainConnector, _super);
90
- function BlockchainConnector() {
91
- return _super.call(this) || this;
31
+ const store_1 = __importDefault(require("../store"));
32
+ const Superpro_1 = __importDefault(require("../staticModels/Superpro"));
33
+ const SuperproToken_1 = __importDefault(require("../staticModels/SuperproToken"));
34
+ const Monitoring_1 = require("../utils/Monitoring");
35
+ class BlockchainConnector extends BaseConnector_1.BaseConnector {
36
+ constructor() {
37
+ super();
92
38
  }
93
- BlockchainConnector.getInstance = function () {
39
+ static getInstance() {
94
40
  if (!BlockchainConnector.instance) {
95
41
  BlockchainConnector.instance = new BlockchainConnector();
96
42
  }
97
43
  return BlockchainConnector.instance;
98
- };
44
+ }
99
45
  // TODO: remove this
100
- BlockchainConnector.prototype.getContract = function (transactionOptions) {
46
+ getContract(transactionOptions) {
101
47
  this.checkIfInitialized();
102
48
  if (transactionOptions === null || transactionOptions === void 0 ? void 0 : transactionOptions.web3) {
103
49
  return new transactionOptions.web3.eth.Contract(app_json_1.default.abi, Superpro_1.default.address);
104
50
  }
105
- return _super.prototype.getContract.call(this);
106
- };
51
+ return super.getContract();
52
+ }
107
53
  /**
108
54
  * Function for connecting to blockchain
109
55
  * Used to setting up settings for blockchain connector
110
56
  * Needs to run this function before using blockchain connector
111
57
  */
112
- BlockchainConnector.prototype.initialize = function (config) {
58
+ initialize(config) {
113
59
  var _a;
114
- return __awaiter(this, void 0, void 0, function () {
115
- var url, _b;
116
- return __generator(this, function (_c) {
117
- switch (_c.label) {
118
- case 0:
119
- this.logger.trace(config, "Initializing");
120
- url = (config === null || config === void 0 ? void 0 : config.blockchainUrl) || constants_1.defaultBlockchainUrl;
121
- this.provider = new web3_1.default.providers.HttpProvider(url);
122
- store_1.default.web3Https = new web3_1.default(this.provider);
123
- store_1.default.gasPrice = (_a = config === null || config === void 0 ? void 0 : config.gasPrice) !== null && _a !== void 0 ? _a : constants_1.defaultGasPrice;
124
- if (config === null || config === void 0 ? void 0 : config.gasLimit)
125
- store_1.default.gasLimit = config.gasLimit;
126
- if (config === null || config === void 0 ? void 0 : config.gasLimitMultiplier)
127
- store_1.default.gasLimitMultiplier = config.gasLimitMultiplier;
128
- if (config === null || config === void 0 ? void 0 : config.gasPriceMultiplier)
129
- store_1.default.gasPriceMultiplier = config.gasPriceMultiplier;
130
- Superpro_1.default.address = config.contractAddress;
131
- this.contract = new store_1.default.web3Https.eth.Contract(app_json_1.default.abi, Superpro_1.default.address);
132
- TxManager_1.default.init(store_1.default.web3Https);
133
- _b = SuperproToken_1.default;
134
- return [4 /*yield*/, Superpro_1.default.getTokenAddress(this.contract)];
135
- case 1:
136
- _b.addressHttps = _c.sent();
137
- Monitoring_1.Monitoring.getInstance().initializeLogging();
138
- this.initialized = true;
139
- this.logger.trace("Initialized");
140
- return [2 /*return*/];
141
- }
142
- });
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ this.logger.trace(config, "Initializing");
62
+ const url = (config === null || config === void 0 ? void 0 : config.blockchainUrl) || constants_1.defaultBlockchainUrl;
63
+ this.provider = new web3_1.default.providers.HttpProvider(url);
64
+ store_1.default.web3Https = new web3_1.default(this.provider);
65
+ store_1.default.gasPrice = (_a = config === null || config === void 0 ? void 0 : config.gasPrice) !== null && _a !== void 0 ? _a : constants_1.defaultGasPrice;
66
+ if (config === null || config === void 0 ? void 0 : config.gasLimit)
67
+ store_1.default.gasLimit = config.gasLimit;
68
+ if (config === null || config === void 0 ? void 0 : config.gasLimitMultiplier)
69
+ store_1.default.gasLimitMultiplier = config.gasLimitMultiplier;
70
+ if (config === null || config === void 0 ? void 0 : config.gasPriceMultiplier)
71
+ store_1.default.gasPriceMultiplier = config.gasPriceMultiplier;
72
+ Superpro_1.default.address = config.contractAddress;
73
+ this.contract = new store_1.default.web3Https.eth.Contract(app_json_1.default.abi, Superpro_1.default.address);
74
+ TxManager_1.default.init(store_1.default.web3Https);
75
+ SuperproToken_1.default.addressHttps = yield Superpro_1.default.getTokenAddress(this.contract);
76
+ Monitoring_1.Monitoring.getInstance().initializeLogging();
77
+ this.initialized = true;
78
+ this.logger.trace("Initialized");
143
79
  });
144
- };
80
+ }
145
81
  /**
146
82
  * Function for connecting provider action account
147
83
  * Needs to run this function before using any set methods in blockchain connector
148
84
  */
149
- BlockchainConnector.prototype.initializeActionAccount = function (actionAccountKey, manageNonce) {
150
- if (manageNonce === void 0) { manageNonce = true; }
151
- return __awaiter(this, void 0, void 0, function () {
152
- var actionAccount;
153
- return __generator(this, function (_a) {
154
- switch (_a.label) {
155
- case 0:
156
- this.checkIfInitialized();
157
- actionAccount = store_1.default.web3Https.eth.accounts.wallet.add(actionAccountKey).address;
158
- if (!store_1.default.actionAccount)
159
- store_1.default.actionAccount = actionAccount;
160
- if (!store_1.default.keys[actionAccount])
161
- store_1.default.keys[actionAccount] = actionAccountKey;
162
- if (!this.defaultActionAccount)
163
- this.defaultActionAccount = actionAccount;
164
- if (!manageNonce) return [3 /*break*/, 2];
165
- return [4 /*yield*/, TxManager_1.default.initAccount(actionAccount)];
166
- case 1:
167
- _a.sent();
168
- _a.label = 2;
169
- case 2: return [2 /*return*/, actionAccount];
170
- }
171
- });
85
+ initializeActionAccount(actionAccountKey, manageNonce = true) {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ this.checkIfInitialized();
88
+ const actionAccount = store_1.default.web3Https.eth.accounts.wallet.add(actionAccountKey).address;
89
+ if (!store_1.default.actionAccount)
90
+ store_1.default.actionAccount = actionAccount;
91
+ if (!store_1.default.keys[actionAccount])
92
+ store_1.default.keys[actionAccount] = actionAccountKey;
93
+ if (!this.defaultActionAccount)
94
+ this.defaultActionAccount = actionAccount;
95
+ if (manageNonce) {
96
+ yield TxManager_1.default.initAccount(actionAccount);
97
+ }
98
+ return actionAccount;
172
99
  });
173
- };
100
+ }
174
101
  /**
175
102
  * Returns balance of blockchain platform tokens in wei
176
103
  */
177
- BlockchainConnector.prototype.getBalance = function (address) {
178
- return __awaiter(this, void 0, void 0, function () {
179
- return __generator(this, function (_a) {
180
- this.checkIfInitialized();
181
- return [2 /*return*/, store_1.default.web3Https.eth.getBalance(address)];
182
- });
104
+ getBalance(address) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ this.checkIfInitialized();
107
+ return store_1.default.web3Https.eth.getBalance(address);
183
108
  });
184
- };
185
- BlockchainConnector.prototype.getTimestamp = function () {
109
+ }
110
+ getTimestamp() {
186
111
  var _a;
187
- return __awaiter(this, void 0, void 0, function () {
188
- var block;
189
- return __generator(this, function (_b) {
190
- switch (_b.label) {
191
- case 0:
192
- this.checkIfInitialized();
193
- return [4 /*yield*/, ((_a = store_1.default.web3Https) === null || _a === void 0 ? void 0 : _a.eth.getBlock("latest"))];
194
- case 1:
195
- block = _b.sent();
196
- return [2 /*return*/, block.timestamp];
197
- }
198
- });
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ this.checkIfInitialized();
114
+ const block = yield ((_a = store_1.default.web3Https) === null || _a === void 0 ? void 0 : _a.eth.getBlock("latest"));
115
+ return block.timestamp;
199
116
  });
200
- };
117
+ }
201
118
  /**
202
119
  * Returns transactions events info
203
120
  * @param txHash - transaction hash
204
121
  * @returns {Promise<EventData[]>} - Transaction events info
205
122
  */
206
- BlockchainConnector.prototype.getTransactionEvents = function (txHash) {
207
- return __awaiter(this, void 0, void 0, function () {
208
- var parseReceiptEvents, receipt, tokenEvents, events, eventData, _i, events_1, event, data, dataValues, i;
209
- return __generator(this, function (_a) {
210
- switch (_a.label) {
211
- case 0:
212
- this.checkIfInitialized();
213
- parseReceiptEvents = require("web3-parse-receipt-events");
214
- return [4 /*yield*/, store_1.default.web3Https.eth.getTransactionReceipt(txHash)];
215
- case 1:
216
- receipt = _a.sent();
217
- tokenEvents = parseReceiptEvents(app_json_1.default.abi, SuperproToken_1.default.addressHttps, receipt);
218
- parseReceiptEvents(app_json_1.default.abi, Superpro_1.default.address, receipt); // don't remove
219
- events = Object.values(tokenEvents.events || {});
220
- eventData = [];
221
- for (_i = 0, events_1 = events; _i < events_1.length; _i++) {
222
- event = events_1[_i];
223
- if (event.address === constants_1.POLYGON_MATIC_EVENT_PATH || !event.event) {
224
- continue;
225
- }
226
- data = event.returnValues;
227
- dataValues = Object.values(data);
228
- if (dataValues.length !== 0) {
229
- for (i = 0; i < dataValues.length / 2; i++) {
230
- delete data[i];
231
- }
232
- }
233
- eventData.push({
234
- contract: event.address,
235
- name: event.event,
236
- data: data,
237
- });
238
- }
239
- return [2 /*return*/, eventData];
123
+ getTransactionEvents(txHash) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ this.checkIfInitialized();
126
+ const parseReceiptEvents = require("web3-parse-receipt-events");
127
+ const receipt = yield store_1.default.web3Https.eth.getTransactionReceipt(txHash);
128
+ const tokenEvents = parseReceiptEvents(app_json_1.default.abi, SuperproToken_1.default.addressHttps, receipt);
129
+ parseReceiptEvents(app_json_1.default.abi, Superpro_1.default.address, receipt); // don't remove
130
+ const events = Object.values(tokenEvents.events || {});
131
+ const eventData = [];
132
+ for (const event of events) {
133
+ if (event.address === constants_1.POLYGON_MATIC_EVENT_PATH || !event.event) {
134
+ continue;
240
135
  }
241
- });
136
+ const data = event.returnValues;
137
+ const dataValues = Object.values(data);
138
+ if (dataValues.length !== 0) {
139
+ for (let i = 0; i < dataValues.length / 2; i++) {
140
+ delete data[i];
141
+ }
142
+ }
143
+ eventData.push({
144
+ contract: event.address,
145
+ name: event.event,
146
+ data,
147
+ });
148
+ }
149
+ return eventData;
242
150
  });
243
- };
151
+ }
244
152
  /**
245
153
  * Function for adding event listeners on TEE offer created event in TEE offers factory contract
246
154
  * @param callback - function for processing created TEE offer
247
155
  * @returns unsubscribe - unsubscribe function from event
248
156
  */
249
- BlockchainConnector.prototype.getLastBlockInfo = function () {
250
- return __awaiter(this, void 0, void 0, function () {
251
- var index, hash;
252
- return __generator(this, function (_a) {
253
- switch (_a.label) {
254
- case 0:
255
- this.checkIfInitialized();
256
- return [4 /*yield*/, store_1.default.web3Https.eth.getBlockNumber()];
257
- case 1:
258
- index = _a.sent();
259
- return [4 /*yield*/, store_1.default.web3Https.eth.getBlock(index)];
260
- case 2:
261
- hash = (_a.sent()).hash;
262
- return [2 /*return*/, {
263
- index: index,
264
- hash: hash,
265
- }];
266
- }
267
- });
157
+ getLastBlockInfo() {
158
+ return __awaiter(this, void 0, void 0, function* () {
159
+ this.checkIfInitialized();
160
+ const index = yield store_1.default.web3Https.eth.getBlockNumber();
161
+ const hash = (yield store_1.default.web3Https.eth.getBlock(index)).hash;
162
+ return {
163
+ index,
164
+ hash,
165
+ };
268
166
  });
269
- };
167
+ }
270
168
  /**
271
169
  * Returns transactions reciept
272
170
  * @param txHash - transaction hash
273
171
  * @returns {Promise<TransactionReceipt>} - Transaction reciept
274
172
  */
275
- BlockchainConnector.prototype.getTransactionReceipt = function (txHash) {
276
- return __awaiter(this, void 0, void 0, function () {
277
- return __generator(this, function (_a) {
278
- this.checkIfInitialized();
279
- return [2 /*return*/, store_1.default.web3Https.eth.getTransactionReceipt(txHash)];
280
- });
173
+ getTransactionReceipt(txHash) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ this.checkIfInitialized();
176
+ return store_1.default.web3Https.eth.getTransactionReceipt(txHash);
281
177
  });
282
- };
178
+ }
283
179
  /**
284
180
  * Returns balance of blockchain platform tokens in wei
285
181
  */
286
- BlockchainConnector.prototype.transfer = function (to, amount, transactionOptions) {
287
- return __awaiter(this, void 0, void 0, function () {
288
- var transaction;
289
- return __generator(this, function (_a) {
290
- this.checkIfInitialized();
291
- (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
292
- transaction = {
293
- to: to,
294
- value: amount,
295
- };
296
- return [2 /*return*/, TxManager_1.default.publishTransaction(transaction, transactionOptions)];
297
- });
182
+ transfer(to, amount, transactionOptions) {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ this.checkIfInitialized();
185
+ (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
186
+ const transaction = {
187
+ to,
188
+ value: amount,
189
+ };
190
+ return TxManager_1.default.publishTransaction(transaction, transactionOptions);
298
191
  });
299
- };
192
+ }
300
193
  /**
301
194
  * Returns transactions count
302
195
  * @param address - wallet address
303
196
  * @returns {Promise<number>} - Transactions count
304
197
  */
305
- BlockchainConnector.prototype.getTransactionCount = function (address, status) {
306
- return __awaiter(this, void 0, void 0, function () {
307
- return __generator(this, function (_a) {
308
- this.checkIfInitialized();
309
- if (status) {
310
- return [2 /*return*/, store_1.default.web3Https.eth.getTransactionCount(address, status)];
311
- }
312
- else {
313
- return [2 /*return*/, store_1.default.web3Https.eth.getTransactionCount(address)];
314
- }
315
- return [2 /*return*/];
316
- });
198
+ getTransactionCount(address, status) {
199
+ return __awaiter(this, void 0, void 0, function* () {
200
+ this.checkIfInitialized();
201
+ if (status) {
202
+ return store_1.default.web3Https.eth.getTransactionCount(address, status);
203
+ }
204
+ else {
205
+ return store_1.default.web3Https.eth.getTransactionCount(address);
206
+ }
317
207
  });
318
- };
319
- BlockchainConnector.prototype.getAddressByKey = function (pk) {
208
+ }
209
+ getAddressByKey(pk) {
320
210
  return new ethers_1.Wallet(pk).address;
321
- };
322
- BlockchainConnector.prototype.executeBatchAsync = function (batch) {
323
- return __awaiter(this, void 0, void 0, function () {
324
- return __generator(this, function (_a) {
325
- return [2 /*return*/, new Promise(function (resolve, reject) {
326
- var requests = batch.requests;
327
- batch.requestManager.sendBatch(requests, function (error, results) {
328
- if (error)
329
- return reject(error);
330
- results = results || [];
331
- var response = requests
332
- .map(function (request, index) {
333
- return results[index] || {};
334
- })
335
- .map(function (result, index) {
336
- if (result && result.error) {
337
- return web3_core_helpers_1.errors.ErrorResponse(result);
338
- }
339
- if (!Jsonrpc.isValidResponse(result)) {
340
- return web3_core_helpers_1.errors.InvalidResponse(result);
341
- }
342
- return requests[index].format ? requests[index].format(result.result) : result.result;
343
- });
344
- resolve(response);
345
- });
346
- })];
211
+ }
212
+ executeBatchAsync(batch) {
213
+ return __awaiter(this, void 0, void 0, function* () {
214
+ return new Promise((resolve, reject) => {
215
+ var requests = batch.requests;
216
+ batch.requestManager.sendBatch(requests, (error, results) => {
217
+ if (error)
218
+ return reject(error);
219
+ results = results || [];
220
+ var response = requests
221
+ .map((request, index) => {
222
+ return results[index] || {};
223
+ })
224
+ .map((result, index) => {
225
+ if (result && result.error) {
226
+ return web3_core_helpers_1.errors.ErrorResponse(result);
227
+ }
228
+ if (!Jsonrpc.isValidResponse(result)) {
229
+ return web3_core_helpers_1.errors.InvalidResponse(result);
230
+ }
231
+ return requests[index].format ? requests[index].format(result.result) : result.result;
232
+ });
233
+ resolve(response);
234
+ });
347
235
  });
348
236
  });
349
- };
237
+ }
350
238
  /**
351
239
  * Fetch transactions for specific addresses starting with specific block until last block
352
240
  * @param addresses - array of addresses to fetch transactions (from these addresses and to these addresses)
@@ -358,88 +246,74 @@ var BlockchainConnector = /** @class */ (function (_super) {
358
246
  * lastBlock, - number of last fetched block (can be used to start fetching from this block next time)
359
247
  * }>}
360
248
  */
361
- BlockchainConnector.prototype.getTransactions = function (addresses, startBlock, lastBlock, batchSize) {
362
- if (batchSize === void 0) { batchSize = constants_1.BLOCK_SIZE_TO_FETCH_TRANSACTION; }
363
- return __awaiter(this, void 0, void 0, function () {
364
- var blockchainLastBlock, transactionsByAddress, validAddresses, batch, getBlock, batchLastBlock, blockNumber, blocks;
365
- return __generator(this, function (_a) {
366
- switch (_a.label) {
367
- case 0:
368
- this.checkIfInitialized();
369
- return [4 /*yield*/, store_1.default.web3Https.eth.getBlockNumber()];
370
- case 1:
371
- blockchainLastBlock = _a.sent();
372
- if (lastBlock) {
373
- lastBlock = Math.min(lastBlock, blockchainLastBlock);
374
- }
375
- else {
376
- lastBlock = blockchainLastBlock;
377
- }
378
- if (!startBlock) {
379
- startBlock = Math.max(lastBlock - 1000, 0);
380
- }
381
- transactionsByAddress = {};
382
- validAddresses = addresses.filter(function (address) { var _a; return (_a = store_1.default.web3Https) === null || _a === void 0 ? void 0 : _a.utils.isAddress(address); });
383
- if (!validAddresses.length) {
384
- return [2 /*return*/, {
385
- transactionsByAddress: transactionsByAddress,
386
- lastBlock: lastBlock,
387
- }];
388
- }
389
- validAddresses.forEach(function (address) { return (transactionsByAddress[address] = []); });
390
- _a.label = 2;
391
- case 2:
392
- if (!(startBlock <= lastBlock)) return [3 /*break*/, 4];
393
- batch = new store_1.default.web3Https.eth.BatchRequest();
394
- getBlock = store_1.default.web3Https.eth.getBlock;
395
- batchLastBlock = Math.min(startBlock + batchSize - 1, lastBlock);
396
- for (blockNumber = startBlock; blockNumber <= batchLastBlock; blockNumber++) {
397
- batch.add(getBlock.request(blockNumber, true));
398
- }
399
- return [4 /*yield*/, this.executeBatchAsync(batch)];
400
- case 3:
401
- blocks = (_a.sent());
402
- blocks.forEach(function (block) {
403
- if (!(block === null || block === void 0 ? void 0 : block.transactions))
404
- return;
405
- block.transactions.forEach(function (transaction) {
406
- var address = null;
407
- if (validAddresses.includes(transaction.from))
408
- address = transaction.from;
409
- else if (transaction.to && validAddresses.includes(transaction.to))
410
- address = transaction.to;
411
- if (address) {
412
- transactionsByAddress[address].push(__assign(__assign({}, transaction), { timestamp: +block.timestamp * 1000, input: transaction.input }));
413
- }
414
- });
415
- });
416
- startBlock = batchLastBlock + 1;
417
- return [3 /*break*/, 2];
418
- case 4: return [2 /*return*/, {
419
- transactionsByAddress: transactionsByAddress,
420
- lastBlock: lastBlock,
421
- }];
249
+ getTransactions(addresses, startBlock, lastBlock, batchSize = constants_1.BLOCK_SIZE_TO_FETCH_TRANSACTION) {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ this.checkIfInitialized();
252
+ const blockchainLastBlock = yield store_1.default.web3Https.eth.getBlockNumber();
253
+ if (lastBlock) {
254
+ lastBlock = Math.min(lastBlock, blockchainLastBlock);
255
+ }
256
+ else {
257
+ lastBlock = blockchainLastBlock;
258
+ }
259
+ if (!startBlock) {
260
+ startBlock = Math.max(lastBlock - 1000, 0);
261
+ }
262
+ const transactionsByAddress = {};
263
+ const validAddresses = addresses.filter((address) => { var _a; return (_a = store_1.default.web3Https) === null || _a === void 0 ? void 0 : _a.utils.isAddress(address); });
264
+ if (!validAddresses.length) {
265
+ return {
266
+ transactionsByAddress,
267
+ lastBlock,
268
+ };
269
+ }
270
+ validAddresses.forEach((address) => (transactionsByAddress[address] = []));
271
+ while (startBlock <= lastBlock) {
272
+ const batch = new store_1.default.web3Https.eth.BatchRequest();
273
+ const getBlock = store_1.default.web3Https.eth.getBlock;
274
+ const batchLastBlock = Math.min(startBlock + batchSize - 1, lastBlock);
275
+ for (let blockNumber = startBlock; blockNumber <= batchLastBlock; blockNumber++) {
276
+ batch.add(getBlock.request(blockNumber, true));
422
277
  }
423
- });
278
+ const blocks = (yield this.executeBatchAsync(batch));
279
+ blocks.forEach((block) => {
280
+ if (!(block === null || block === void 0 ? void 0 : block.transactions))
281
+ return;
282
+ block.transactions.forEach((transaction) => {
283
+ let address = null;
284
+ if (validAddresses.includes(transaction.from))
285
+ address = transaction.from;
286
+ else if (transaction.to && validAddresses.includes(transaction.to))
287
+ address = transaction.to;
288
+ if (address) {
289
+ transactionsByAddress[address].push(Object.assign(Object.assign({}, transaction), { timestamp: +block.timestamp * 1000, input: transaction.input }));
290
+ }
291
+ });
292
+ });
293
+ startBlock = batchLastBlock + 1;
294
+ }
295
+ return {
296
+ transactionsByAddress,
297
+ lastBlock,
298
+ };
424
299
  });
425
- };
426
- BlockchainConnector.prototype.shutdown = function () {
427
- _super.prototype.shutdown.call(this);
300
+ }
301
+ shutdown() {
302
+ super.shutdown();
428
303
  store_1.default.web3Https = undefined;
429
304
  Monitoring_1.Monitoring.getInstance().shutdownLogging();
430
- };
431
- __decorate([
432
- (0, utils_1.incrementMethodCall)()
433
- ], BlockchainConnector.prototype, "getBalance", null);
434
- __decorate([
435
- (0, utils_1.incrementMethodCall)()
436
- ], BlockchainConnector.prototype, "getTransactionEvents", null);
437
- __decorate([
438
- (0, utils_1.incrementMethodCall)()
439
- ], BlockchainConnector.prototype, "transfer", null);
440
- __decorate([
441
- (0, utils_1.incrementMethodCall)()
442
- ], BlockchainConnector.prototype, "getTransactions", null);
443
- return BlockchainConnector;
444
- }(BaseConnector_1.BaseConnector));
305
+ }
306
+ }
307
+ __decorate([
308
+ (0, utils_1.incrementMethodCall)()
309
+ ], BlockchainConnector.prototype, "getBalance", null);
310
+ __decorate([
311
+ (0, utils_1.incrementMethodCall)()
312
+ ], BlockchainConnector.prototype, "getTransactionEvents", null);
313
+ __decorate([
314
+ (0, utils_1.incrementMethodCall)()
315
+ ], BlockchainConnector.prototype, "transfer", null);
316
+ __decorate([
317
+ (0, utils_1.incrementMethodCall)()
318
+ ], BlockchainConnector.prototype, "getTransactions", null);
445
319
  exports.default = BlockchainConnector;