@super-protocol/sdk-js 0.13.0-beta.3 → 0.13.0-beta.5

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.
@@ -361,7 +361,7 @@ var BlockchainConnector = /** @class */ (function (_super) {
361
361
  BlockchainConnector.prototype.getTransactions = function (addresses, startBlock, lastBlock, batchSize) {
362
362
  if (batchSize === void 0) { batchSize = constants_1.BLOCK_SIZE_TO_FETCH_TRANSACTION; }
363
363
  return __awaiter(this, void 0, void 0, function () {
364
- var blockchainLastBlock, transactionsByAddress, batch, getBlock, batchLastBlock, blockNumber, blocks;
364
+ var blockchainLastBlock, transactionsByAddress, validAddresses, batch, getBlock, batchLastBlock, blockNumber, blocks;
365
365
  return __generator(this, function (_a) {
366
366
  switch (_a.label) {
367
367
  case 0:
@@ -379,7 +379,14 @@ var BlockchainConnector = /** @class */ (function (_super) {
379
379
  startBlock = Math.max(lastBlock - 1000, 0);
380
380
  }
381
381
  transactionsByAddress = {};
382
- addresses.forEach(function (address) { return (transactionsByAddress[address] = []); });
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] = []); });
383
390
  _a.label = 2;
384
391
  case 2:
385
392
  if (!(startBlock <= lastBlock)) return [3 /*break*/, 4];
@@ -397,9 +404,9 @@ var BlockchainConnector = /** @class */ (function (_super) {
397
404
  return;
398
405
  block.transactions.forEach(function (transaction) {
399
406
  var address = null;
400
- if (addresses.includes(transaction.from))
407
+ if (validAddresses.includes(transaction.from))
401
408
  address = transaction.from;
402
- else if (transaction.to && addresses.includes(transaction.to))
409
+ else if (transaction.to && validAddresses.includes(transaction.to))
403
410
  address = transaction.to;
404
411
  if (address) {
405
412
  transactionsByAddress[address].push(__assign(__assign({}, transaction), { timestamp: +block.timestamp * 1000, input: transaction.input }));
package/build/index.d.ts CHANGED
@@ -46,6 +46,7 @@ export * from "./types/HardwareInfo";
46
46
  export * from "./types/SlotInfo";
47
47
  export * from "./types/SlotUsage";
48
48
  export * from "./types/OptionInfo";
49
+ export * from "./types/TeeOfferInfo";
49
50
  export * from "./types/TeeOfferOption";
50
51
  export * from "./types/TeeOfferSlot";
51
52
  export * from "./types/Provider";
package/build/index.js CHANGED
@@ -96,6 +96,7 @@ __exportStar(require("./types/HardwareInfo"), exports);
96
96
  __exportStar(require("./types/SlotInfo"), exports);
97
97
  __exportStar(require("./types/SlotUsage"), exports);
98
98
  __exportStar(require("./types/OptionInfo"), exports);
99
+ __exportStar(require("./types/TeeOfferInfo"), exports);
99
100
  __exportStar(require("./types/TeeOfferOption"), exports);
100
101
  __exportStar(require("./types/TeeOfferSlot"), exports);
101
102
  __exportStar(require("./types/Provider"), exports);
@@ -1,5 +1,5 @@
1
1
  import { TransactionOptions } from "../types/Web3";
2
- import { TcbData, TcbStatus, PublicData, TcbVerifiedStatus } from "../types/Consensus";
2
+ import { TcbData, TcbStatus, TcbPublicData, TcbVerifiedStatus } from "../types/Consensus";
3
3
  declare class TCB {
4
4
  tcbId: string;
5
5
  private contract;
@@ -14,7 +14,7 @@ declare class TCB {
14
14
  * @param marks - list of marks
15
15
  * @param transactionOptions - object what contains alternative action account or gas limit (optional)
16
16
  */
17
- addToSupply(pb: PublicData, quote: string, marks: TcbVerifiedStatus[], transactionOptions?: TransactionOptions): Promise<void>;
17
+ addToSupply(pb: TcbPublicData, quote: string, marks: TcbVerifiedStatus[], transactionOptions?: TransactionOptions): Promise<void>;
18
18
  /**
19
19
  * Assign TCB from SuspiciousBlocks table to check
20
20
  * @param transactionOptions - object what contains alternative action account or gas limit (optional)
@@ -1,10 +1,17 @@
1
1
  import { EpochInfo } from "../types/Consensus";
2
2
  import { GetTcbRequest } from "@super-protocol/dto-js";
3
- import { TransactionOptions } from "../types/Web3";
3
+ import { TransactionOptions, BlockInfo } from "../types/Web3";
4
4
  import { ConsensusConstants } from "../types/Consensus";
5
5
  declare class Consensus {
6
+ private static readonly logger;
7
+ private static tcbIds?;
6
8
  static get address(): string;
7
9
  private static initializeTcbAndAssignBlocks;
10
+ /**
11
+ * Function for fetching list of all tcb ids
12
+ * @returns list of tcb ids
13
+ */
14
+ static getAllTcbs(): Promise<string[]>;
8
15
  static initializeTcb(teeOfferId: string, transactionOptions?: TransactionOptions): Promise<void>;
9
16
  static isTcbCreationAvailable(teeOfferId: string): Promise<boolean>;
10
17
  /**
@@ -31,5 +38,15 @@ declare class Consensus {
31
38
  static getLastBlockTable(): Promise<string[]>;
32
39
  static getLastBlockTableSize(): Promise<string[]>;
33
40
  static getConstants(): Promise<ConsensusConstants>;
41
+ static onTcbBanned(callback: onTcbBannedCallback): () => void;
42
+ static onTcbCompleted(callback: onTcbCompletedCallback): () => void;
43
+ static onTcbInitialized(callback: onTcbInitializedCallback): () => void;
44
+ static onTcbBenchmarkChanged(callback: onTcbBenchmarkChangedCallback): () => void;
45
+ static onRewardsClaimed(callback: onRewardsClaimedCallback): () => void;
34
46
  }
47
+ export type onRewardsClaimedCallback = (tcbId: string, amount: string, claimer: string, block?: BlockInfo) => void;
48
+ export type onTcbBenchmarkChangedCallback = (tcbId: string, provider: string, block?: BlockInfo) => void;
49
+ export type onTcbInitializedCallback = (tcbId: string, provider: string, block?: BlockInfo) => void;
50
+ export type onTcbCompletedCallback = (tcbId: string, provider: string, block?: BlockInfo) => void;
51
+ export type onTcbBannedCallback = (tcbId: string, provider: string, block?: BlockInfo) => void;
35
52
  export default Consensus;
@@ -39,10 +39,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
+ var logger_1 = __importDefault(require("../logger"));
42
43
  var TCB_1 = __importDefault(require("../models/TCB"));
43
44
  var utils_1 = require("../utils");
44
45
  var Superpro_1 = __importDefault(require("./Superpro"));
45
46
  var BlockchainConnector_1 = __importDefault(require("../connectors/BlockchainConnector"));
47
+ var BlockchainEventsListener_1 = __importDefault(require("../connectors/BlockchainEventsListener"));
46
48
  var TxManager_1 = __importDefault(require("../utils/TxManager"));
47
49
  var Consensus_1 = require("../types/Consensus");
48
50
  var Consensus = /** @class */ (function () {
@@ -78,6 +80,31 @@ var Consensus = /** @class */ (function () {
78
80
  });
79
81
  });
80
82
  };
83
+ /**
84
+ * Function for fetching list of all tcb ids
85
+ * @returns list of tcb ids
86
+ */
87
+ Consensus.getAllTcbs = function () {
88
+ var _a;
89
+ return __awaiter(this, void 0, void 0, function () {
90
+ var contract, tcbSet, tcbsCount, tcbId;
91
+ return __generator(this, function (_b) {
92
+ switch (_b.label) {
93
+ case 0:
94
+ contract = BlockchainConnector_1.default.getInstance().getContract();
95
+ tcbSet = new Set((_a = this.tcbIds) !== null && _a !== void 0 ? _a : []);
96
+ return [4 /*yield*/, contract.methods.getTcbsCount().call()];
97
+ case 1:
98
+ tcbsCount = _b.sent();
99
+ for (tcbId = tcbSet.size + 1; tcbId <= tcbsCount; tcbId++) {
100
+ tcbSet.add(tcbId.toString());
101
+ }
102
+ this.tcbIds = Array.from(tcbSet);
103
+ return [2 /*return*/, this.tcbIds];
104
+ }
105
+ });
106
+ });
107
+ };
81
108
  Consensus.initializeTcb = function (teeOfferId, transactionOptions) {
82
109
  return __awaiter(this, void 0, void 0, function () {
83
110
  var contract;
@@ -265,6 +292,117 @@ var Consensus = /** @class */ (function () {
265
292
  });
266
293
  });
267
294
  };
295
+ Consensus.onTcbBanned = function (callback) {
296
+ var _this = this;
297
+ var contract = BlockchainEventsListener_1.default.getInstance().getContract();
298
+ var logger = this.logger.child({ method: "onTcbBanned" });
299
+ var subscription = contract.events
300
+ .TcbBanned()
301
+ .on("data", function (event) { return __awaiter(_this, void 0, void 0, function () {
302
+ return __generator(this, function (_a) {
303
+ callback(event.returnValues.tcbId, event.returnValues.provider, {
304
+ index: event.blockNumber,
305
+ hash: event.blockHash,
306
+ });
307
+ return [2 /*return*/];
308
+ });
309
+ }); })
310
+ .on("error", function (error, receipt) {
311
+ if (receipt)
312
+ return; // Used to avoid logging of transaction rejected
313
+ logger.warn(error);
314
+ });
315
+ return function () { return subscription.unsubscribe(); };
316
+ };
317
+ Consensus.onTcbCompleted = function (callback) {
318
+ var _this = this;
319
+ var contract = BlockchainEventsListener_1.default.getInstance().getContract();
320
+ var logger = this.logger.child({ method: "onTcbCompleted" });
321
+ var subscription = contract.events
322
+ .TcbCompleted()
323
+ .on("data", function (event) { return __awaiter(_this, void 0, void 0, function () {
324
+ return __generator(this, function (_a) {
325
+ callback(event.returnValues.tcbId, event.returnValues.provider, {
326
+ index: event.blockNumber,
327
+ hash: event.blockHash,
328
+ });
329
+ return [2 /*return*/];
330
+ });
331
+ }); })
332
+ .on("error", function (error, receipt) {
333
+ if (receipt)
334
+ return; // Used to avoid logging of transaction rejected
335
+ logger.warn(error);
336
+ });
337
+ return function () { return subscription.unsubscribe(); };
338
+ };
339
+ Consensus.onTcbInitialized = function (callback) {
340
+ var _this = this;
341
+ var contract = BlockchainEventsListener_1.default.getInstance().getContract();
342
+ var logger = this.logger.child({ method: "onTcbInitialized" });
343
+ var subscription = contract.events
344
+ .TcbInitialized()
345
+ .on("data", function (event) { return __awaiter(_this, void 0, void 0, function () {
346
+ return __generator(this, function (_a) {
347
+ callback(event.returnValues.tcbId, event.returnValues.provider, {
348
+ index: event.blockNumber,
349
+ hash: event.blockHash,
350
+ });
351
+ return [2 /*return*/];
352
+ });
353
+ }); })
354
+ .on("error", function (error, receipt) {
355
+ if (receipt)
356
+ return; // Used to avoid logging of transaction rejected
357
+ logger.warn(error);
358
+ });
359
+ return function () { return subscription.unsubscribe(); };
360
+ };
361
+ Consensus.onTcbBenchmarkChanged = function (callback) {
362
+ var _this = this;
363
+ var contract = BlockchainEventsListener_1.default.getInstance().getContract();
364
+ var logger = this.logger.child({ method: "onTcbBenchmarkChanged" });
365
+ var subscription = contract.events
366
+ .TcbBenchmarkChanged()
367
+ .on("data", function (event) { return __awaiter(_this, void 0, void 0, function () {
368
+ return __generator(this, function (_a) {
369
+ callback(event.returnValues.tcbId, event.returnValues.provider, {
370
+ index: event.blockNumber,
371
+ hash: event.blockHash,
372
+ });
373
+ return [2 /*return*/];
374
+ });
375
+ }); })
376
+ .on("error", function (error, receipt) {
377
+ if (receipt)
378
+ return; // Used to avoid logging of transaction rejected
379
+ logger.warn(error);
380
+ });
381
+ return function () { return subscription.unsubscribe(); };
382
+ };
383
+ Consensus.onRewardsClaimed = function (callback) {
384
+ var _this = this;
385
+ var contract = BlockchainEventsListener_1.default.getInstance().getContract();
386
+ var logger = this.logger.child({ method: "onRewardsClaimed" });
387
+ var subscription = contract.events
388
+ .RewardsClaimed()
389
+ .on("data", function (event) { return __awaiter(_this, void 0, void 0, function () {
390
+ return __generator(this, function (_a) {
391
+ callback(event.returnValues.tcbId, event.returnValues.amount, event.returnValues.claimer, {
392
+ index: event.blockNumber,
393
+ hash: event.blockHash,
394
+ });
395
+ return [2 /*return*/];
396
+ });
397
+ }); })
398
+ .on("error", function (error, receipt) {
399
+ if (receipt)
400
+ return; // Used to avoid logging of transaction rejected
401
+ logger.warn(error);
402
+ });
403
+ return function () { return subscription.unsubscribe(); };
404
+ };
405
+ Consensus.logger = logger_1.default.child({ className: "Consensus" });
268
406
  return Consensus;
269
407
  }());
270
408
  exports.default = Consensus;
@@ -56,10 +56,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
56
56
  return (mod && mod.__esModule) ? mod : { "default": mod };
57
57
  };
58
58
  Object.defineProperty(exports, "__esModule", { value: true });
59
+ var utils_1 = require("ethers/lib/utils");
59
60
  var logger_1 = __importDefault(require("../logger"));
60
- var utils_1 = require("../utils");
61
+ var utils_2 = require("../utils");
61
62
  var Order_1 = require("../types/Order");
62
- var utils_2 = require("ethers/lib/utils");
63
63
  var Superpro_1 = __importDefault(require("./Superpro"));
64
64
  var TxManager_1 = __importDefault(require("../utils/TxManager"));
65
65
  var BlockchainConnector_1 = __importDefault(require("../connectors/BlockchainConnector"));
@@ -133,9 +133,9 @@ var Orders = /** @class */ (function () {
133
133
  switch (_a.label) {
134
134
  case 0:
135
135
  contract = BlockchainConnector_1.default.getInstance().getContract(transactionOptions);
136
- (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
137
- preparedInfo = __assign(__assign({}, orderInfo), { externalId: (0, utils_2.formatBytes32String)(orderInfo.externalId) });
138
- orderInfoArguments = (0, utils_1.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
136
+ (0, utils_2.checkIfActionAccountInitialized)(transactionOptions);
137
+ preparedInfo = __assign(__assign({}, orderInfo), { externalId: (0, utils_1.formatBytes32String)(orderInfo.externalId) });
138
+ orderInfoArguments = (0, utils_2.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
139
139
  return [4 /*yield*/, TxManager_1.default.execute(contract.methods.createOrder, [orderInfoArguments, deposit, suspended], transactionOptions)];
140
140
  case 1:
141
141
  _a.sent();
@@ -153,7 +153,7 @@ var Orders = /** @class */ (function () {
153
153
  contract = BlockchainConnector_1.default.getInstance().getContract();
154
154
  filter = {
155
155
  consumer: consumer,
156
- externalId: (0, utils_2.formatBytes32String)(externalId),
156
+ externalId: (0, utils_1.formatBytes32String)(externalId),
157
157
  };
158
158
  return [4 /*yield*/, contract.getPastEvents("OrderCreated", { filter: filter })];
159
159
  case 1:
@@ -187,11 +187,11 @@ var Orders = /** @class */ (function () {
187
187
  switch (_a.label) {
188
188
  case 0:
189
189
  contract = BlockchainConnector_1.default.getInstance().getContract(transactionOptions);
190
- (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
191
- preparedInfo = __assign(__assign({}, parentOrderInfo), { externalId: (0, utils_2.formatBytes32String)(parentOrderInfo.externalId) });
192
- parentOrderInfoArgs = (0, utils_1.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
193
- preparedSubOrdersInfo = subOrdersInfo.map(function (o) { return (__assign(__assign({}, o), { externalId: (0, utils_2.formatBytes32String)(o.externalId) })); });
194
- subOrdersInfoArgs = (0, utils_1.objectToTuple)(preparedSubOrdersInfo, Order_1.OrderInfoStructureArray);
190
+ (0, utils_2.checkIfActionAccountInitialized)(transactionOptions);
191
+ preparedInfo = __assign(__assign({}, parentOrderInfo), { externalId: (0, utils_1.formatBytes32String)(parentOrderInfo.externalId) });
192
+ parentOrderInfoArgs = (0, utils_2.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
193
+ preparedSubOrdersInfo = subOrdersInfo.map(function (o) { return (__assign(__assign({}, o), { externalId: (0, utils_1.formatBytes32String)(o.externalId) })); });
194
+ subOrdersInfoArgs = (0, utils_2.objectToTuple)(preparedSubOrdersInfo, Order_1.OrderInfoStructureArray);
195
195
  return [4 /*yield*/, TxManager_1.default.execute(contract.methods.createWorkflow, [parentOrderInfoArgs, workflowDeposit, subOrdersInfoArgs], transactionOptions)];
196
196
  case 1:
197
197
  _a.sent();
@@ -212,7 +212,7 @@ var Orders = /** @class */ (function () {
212
212
  switch (_a.label) {
213
213
  case 0:
214
214
  contract = BlockchainConnector_1.default.getInstance().getContract(transactionOptions);
215
- (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
215
+ (0, utils_2.checkIfActionAccountInitialized)(transactionOptions);
216
216
  return [4 /*yield*/, TxManager_1.default.execute(contract.methods.cancelWorkflow, [perentOrderId], transactionOptions)];
217
217
  case 1:
218
218
  _a.sent();
@@ -233,7 +233,7 @@ var Orders = /** @class */ (function () {
233
233
  switch (_a.label) {
234
234
  case 0:
235
235
  contract = BlockchainConnector_1.default.getInstance().getContract(transactionOptions);
236
- (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
236
+ (0, utils_2.checkIfActionAccountInitialized)(transactionOptions);
237
237
  return [4 /*yield*/, TxManager_1.default.execute(contract.methods.withdrawWorkflowChange, [parentOrderId], transactionOptions)];
238
238
  case 1:
239
239
  _a.sent();
@@ -255,7 +255,7 @@ var Orders = /** @class */ (function () {
255
255
  switch (_a.label) {
256
256
  case 0:
257
257
  contract = BlockchainConnector_1.default.getInstance().getContract(transactionOptions);
258
- (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
258
+ (0, utils_2.checkIfActionAccountInitialized)(transactionOptions);
259
259
  return [4 /*yield*/, TxManager_1.default.execute(contract.methods.refillOrder, [orderId, amount], transactionOptions)];
260
260
  case 1:
261
261
  _a.sent();
@@ -278,7 +278,7 @@ var Orders = /** @class */ (function () {
278
278
  .on("data", function (event) { return __awaiter(_this, void 0, void 0, function () {
279
279
  return __generator(this, function (_a) {
280
280
  //consumer: string, externalId: string, offerId: string, orderId: string
281
- callback(event.returnValues.consumer, (0, utils_2.parseBytes32String)(event.returnValues.externalId), event.returnValues.offerId, event.returnValues.parentOrderId, event.returnValues.orderId, {
281
+ callback(event.returnValues.consumer, (0, utils_1.parseBytes32String)(event.returnValues.externalId), event.returnValues.offerId, event.returnValues.parentOrderId, event.returnValues.orderId, {
282
282
  index: event.blockNumber,
283
283
  hash: event.blockHash,
284
284
  });
@@ -597,13 +597,13 @@ var Orders = /** @class */ (function () {
597
597
  };
598
598
  Orders.logger = logger_1.default.child({ className: "Orders" });
599
599
  __decorate([
600
- (0, utils_1.incrementMethodCall)()
600
+ (0, utils_2.incrementMethodCall)()
601
601
  ], Orders, "createOrder", null);
602
602
  __decorate([
603
- (0, utils_1.incrementMethodCall)()
603
+ (0, utils_2.incrementMethodCall)()
604
604
  ], Orders, "getByExternalId", null);
605
605
  __decorate([
606
- (0, utils_1.incrementMethodCall)()
606
+ (0, utils_2.incrementMethodCall)()
607
607
  ], Orders, "createWorkflow", null);
608
608
  return Orders;
609
609
  }());
@@ -10,7 +10,7 @@ export declare const PublicDataStructure: {
10
10
  benchmark: NumberConstructor;
11
11
  properties: StringConstructor;
12
12
  };
13
- export type PublicData = {
13
+ export type TcbPublicData = {
14
14
  teeOffer?: string;
15
15
  deviceID: string;
16
16
  benchmark: number;
@@ -30,7 +30,7 @@ export declare const UtilityDataStructure: {
30
30
  checked: BooleanConstructor;
31
31
  rewardClaimed: BooleanConstructor;
32
32
  };
33
- export type UtilityData = {
33
+ export type TcbUtilityData = {
34
34
  checkingBlocks: [string];
35
35
  checkingBlockMarks: [TcbVerifiedStatus];
36
36
  lastBlocksTakenAmount: number;
@@ -90,8 +90,8 @@ export type TcbData = {
90
90
  quote: string;
91
91
  timeInitialized: number;
92
92
  timeAdded: number;
93
- publicData: PublicData;
94
- utilData: UtilityData;
93
+ publicData: TcbPublicData;
94
+ utilData: TcbUtilityData;
95
95
  status: TcbStatus;
96
96
  };
97
97
  export type EpochInfo = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super-protocol/sdk-js",
3
- "version": "0.13.0-beta.3",
3
+ "version": "0.13.0-beta.5",
4
4
  "main": "build/index.js",
5
5
  "license": "MIT",
6
6
  "files": [