@super-protocol/sdk-js 0.11.2-beta.0 → 0.11.4-beta.0

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.
@@ -10,7 +10,7 @@ declare class Consensus {
10
10
  * @param transactionOptions - object what contains alternative action account or gas limit (optional)
11
11
  * @returns tcbId and lists of anothers' TCB for their checking
12
12
  */
13
- static getListsForVerification(teeOfferId: string, transactionOptions?: TransactionOptions): Promise<{
13
+ static getListsForVerification(teeOfferId: string, initializeTcbForce?: boolean, transactionOptions?: TransactionOptions): Promise<{
14
14
  tcbId: string;
15
15
  checkingTcbData: CheckingTcbData[];
16
16
  }>;
@@ -55,7 +55,7 @@ var Consensus = /** @class */ (function () {
55
55
  enumerable: false,
56
56
  configurable: true
57
57
  });
58
- Consensus.initializeTcbAndAssignBlocks = function (teeOfferId, transactionOptions) {
58
+ Consensus.initializeTcbAndAssignBlocks = function (teeOfferId, initializeTcbForce, transactionOptions) {
59
59
  return __awaiter(this, void 0, void 0, function () {
60
60
  var tcbId, tcb, timeInitialized, isFirstOffersTcb, isCreatedMoreThenOneDayAgo, _a;
61
61
  return __generator(this, function (_b) {
@@ -72,7 +72,7 @@ var Consensus = /** @class */ (function () {
72
72
  return [4 /*yield*/, (0, utils_1.getTimestamp)()];
73
73
  case 3:
74
74
  isCreatedMoreThenOneDayAgo = _a < +(_b.sent());
75
- if (!(isFirstOffersTcb || isCreatedMoreThenOneDayAgo)) return [3 /*break*/, 8];
75
+ if (!(isFirstOffersTcb || isCreatedMoreThenOneDayAgo || initializeTcbForce)) return [3 /*break*/, 8];
76
76
  return [4 /*yield*/, this.initializeTcb(teeOfferId, transactionOptions)];
77
77
  case 4:
78
78
  _b.sent();
@@ -114,14 +114,15 @@ var Consensus = /** @class */ (function () {
114
114
  * @param transactionOptions - object what contains alternative action account or gas limit (optional)
115
115
  * @returns tcbId and lists of anothers' TCB for their checking
116
116
  */
117
- Consensus.getListsForVerification = function (teeOfferId, transactionOptions) {
117
+ Consensus.getListsForVerification = function (teeOfferId, initializeTcbForce, transactionOptions) {
118
+ if (initializeTcbForce === void 0) { initializeTcbForce = false; }
118
119
  return __awaiter(this, void 0, void 0, function () {
119
120
  var tcb, blocksIds, checkingTcbData, blockIndex, tcb_1, tcbInfo;
120
121
  return __generator(this, function (_a) {
121
122
  switch (_a.label) {
122
123
  case 0:
123
124
  (0, utils_1.checkIfActionAccountInitialized)();
124
- return [4 /*yield*/, this.initializeTcbAndAssignBlocks(teeOfferId, transactionOptions)];
125
+ return [4 /*yield*/, this.initializeTcbAndAssignBlocks(teeOfferId, initializeTcbForce, transactionOptions)];
125
126
  case 1:
126
127
  tcb = _a.sent();
127
128
  return [4 /*yield*/, tcb.getCheckingBlocksMarks()];
@@ -1,14 +1,19 @@
1
1
  import Web3 from "web3";
2
2
  declare class NonceTracker {
3
3
  private web3;
4
- private store;
5
- private static logger;
6
- constructor(web3: Web3);
7
- initAccount(address: string): Promise<void>;
8
- isManaged(address: string): boolean;
9
- reinitialize(): Promise<void>;
10
- private checkAccount;
11
- getNonce(address: string): number;
12
- consumeNonce(address: string): number;
4
+ private address;
5
+ private logger;
6
+ private txCount?;
7
+ private transactionsOnHold;
8
+ private countOfPendingTransactions;
9
+ constructor(web3: Web3, address: string);
10
+ initAccount(): Promise<void>;
11
+ getNonce(): number;
12
+ consumeNonce(): number;
13
+ onTransactionStartPublishing(): Promise<void>;
14
+ onTransactionPublished(): void;
15
+ onTransactionError(): Promise<void>;
16
+ private waitForPendingTransactions;
17
+ private sendHoldTransactions;
13
18
  }
14
19
  export default NonceTracker;
@@ -41,79 +41,115 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  var logger_1 = __importDefault(require("../logger"));
43
43
  var NonceTracker = /** @class */ (function () {
44
- function NonceTracker(web3) {
44
+ function NonceTracker(web3, address) {
45
45
  this.web3 = web3;
46
- this.store = {};
47
- NonceTracker.logger.trace("Created NonceTracker");
46
+ this.address = address;
47
+ this.countOfPendingTransactions = 0;
48
+ this.logger = logger_1.default.child({ className: "NonceTracker", address: address });
49
+ this.logger.trace("Created NonceTracker");
48
50
  }
49
- NonceTracker.prototype.initAccount = function (address) {
51
+ NonceTracker.prototype.initAccount = function () {
50
52
  return __awaiter(this, void 0, void 0, function () {
51
- var txCount;
52
- return __generator(this, function (_a) {
53
- switch (_a.label) {
53
+ var _a;
54
+ return __generator(this, function (_b) {
55
+ switch (_b.label) {
54
56
  case 0:
55
- if (address in this.store) {
56
- return [2 /*return*/];
57
- }
58
- return [4 /*yield*/, this.web3.eth.getTransactionCount(address)];
57
+ _a = this;
58
+ return [4 /*yield*/, this.web3.eth.getTransactionCount(this.address)];
59
59
  case 1:
60
- txCount = _a.sent();
61
- if (address in this.store) {
62
- return [2 /*return*/];
63
- }
64
- NonceTracker.logger.trace("Initialized ".concat(address, " account with nonce: ").concat(txCount));
65
- this.store[address] = txCount;
60
+ _a.txCount = _b.sent();
61
+ this.logger.trace("Initialized ".concat(this.address, " account with nonce: ").concat(this.txCount));
66
62
  return [2 /*return*/];
67
63
  }
68
64
  });
69
65
  });
70
66
  };
71
- NonceTracker.prototype.isManaged = function (address) {
72
- return address in this.store;
67
+ NonceTracker.prototype.getNonce = function () {
68
+ if (this.txCount === undefined)
69
+ throw Error("NonceTracker for address ".concat(this.address, " is not initialized"));
70
+ this.logger.trace("Get nonce: ".concat(this.txCount));
71
+ return this.txCount;
73
72
  };
74
- NonceTracker.prototype.reinitialize = function () {
73
+ NonceTracker.prototype.consumeNonce = function () {
74
+ if (this.txCount === undefined)
75
+ throw Error("NonceTracker for address ".concat(this.address, " is not initialized"));
76
+ this.logger.trace("Consume nonce: ".concat(this.txCount + 1));
77
+ return this.txCount++;
78
+ };
79
+ NonceTracker.prototype.onTransactionStartPublishing = function () {
75
80
  return __awaiter(this, void 0, void 0, function () {
76
- var _this = this;
77
81
  return __generator(this, function (_a) {
78
82
  switch (_a.label) {
79
- case 0: return [4 /*yield*/, Promise.all(Object.keys(this.store).map(function (address) { return __awaiter(_this, void 0, void 0, function () {
80
- var txCount;
81
- return __generator(this, function (_a) {
82
- switch (_a.label) {
83
- case 0: return [4 /*yield*/, this.web3.eth.getTransactionCount(address)];
84
- case 1:
85
- txCount = _a.sent();
86
- NonceTracker.logger.trace("Account ".concat(address, " has been reinitialized with nonce: ").concat(txCount));
87
- this.store[address] = txCount;
88
- return [2 /*return*/];
89
- }
90
- });
91
- }); }))];
83
+ case 0:
84
+ if (!this.transactionsOnHold) return [3 /*break*/, 2];
85
+ return [4 /*yield*/, this.waitForPendingTransactions()];
92
86
  case 1:
93
87
  _a.sent();
94
- NonceTracker.logger.trace("All accounts has been reinitialized");
88
+ _a.label = 2;
89
+ case 2:
90
+ this.countOfPendingTransactions++;
95
91
  return [2 /*return*/];
96
92
  }
97
93
  });
98
94
  });
99
95
  };
100
- NonceTracker.prototype.checkAccount = function (address) {
101
- if (this.isManaged(address)) {
102
- return;
96
+ NonceTracker.prototype.onTransactionPublished = function () {
97
+ this.countOfPendingTransactions--;
98
+ if (this.countOfPendingTransactions === 0) {
99
+ this.sendHoldTransactions();
103
100
  }
104
- throw Error("".concat(address, " account is not initialized. You must call initAccount before using it."));
105
101
  };
106
- NonceTracker.prototype.getNonce = function (address) {
107
- this.checkAccount(address);
108
- NonceTracker.logger.trace("Get nonce: ".concat(this.store[address]));
109
- return this.store[address];
102
+ NonceTracker.prototype.onTransactionError = function () {
103
+ return __awaiter(this, void 0, void 0, function () {
104
+ return __generator(this, function (_a) {
105
+ switch (_a.label) {
106
+ case 0:
107
+ this.countOfPendingTransactions--;
108
+ if (!this.transactionsOnHold)
109
+ this.transactionsOnHold = [];
110
+ if (!(this.countOfPendingTransactions === 0)) return [3 /*break*/, 1];
111
+ this.sendHoldTransactions();
112
+ return [3 /*break*/, 3];
113
+ case 1: return [4 /*yield*/, this.waitForPendingTransactions()];
114
+ case 2:
115
+ _a.sent();
116
+ _a.label = 3;
117
+ case 3: return [2 /*return*/];
118
+ }
119
+ });
120
+ });
121
+ };
122
+ NonceTracker.prototype.waitForPendingTransactions = function () {
123
+ return __awaiter(this, void 0, void 0, function () {
124
+ var _this = this;
125
+ return __generator(this, function (_a) {
126
+ return [2 /*return*/, new Promise(function (resolve) {
127
+ if (!_this.transactionsOnHold)
128
+ return resolve();
129
+ _this.transactionsOnHold.push(function () {
130
+ resolve();
131
+ });
132
+ })];
133
+ });
134
+ });
110
135
  };
111
- NonceTracker.prototype.consumeNonce = function (address) {
112
- this.checkAccount(address);
113
- NonceTracker.logger.trace("Consume nonce: ".concat(this.store[address] + 1));
114
- return this.store[address]++;
136
+ NonceTracker.prototype.sendHoldTransactions = function () {
137
+ return __awaiter(this, void 0, void 0, function () {
138
+ return __generator(this, function (_a) {
139
+ switch (_a.label) {
140
+ case 0:
141
+ if (!this.transactionsOnHold)
142
+ return [2 /*return*/];
143
+ return [4 /*yield*/, this.initAccount()];
144
+ case 1:
145
+ _a.sent();
146
+ this.transactionsOnHold.forEach(function (callback) { return callback(); });
147
+ this.transactionsOnHold = undefined;
148
+ return [2 /*return*/];
149
+ }
150
+ });
151
+ });
115
152
  };
116
- NonceTracker.logger = logger_1.default.child({ className: "NonceTracker" });
117
153
  return NonceTracker;
118
154
  }());
119
155
  exports.default = NonceTracker;
@@ -10,18 +10,12 @@ declare type MethodReturnType = ContractSendMethod & {
10
10
  };
11
11
  declare class TxManager {
12
12
  private static web3;
13
- private static nonceTracker;
14
13
  private static logger;
15
- private static transactionsOnHold;
16
- private static countOfPendingTransactions;
14
+ private static nonceTrackers;
17
15
  static init(web3: Web3): void;
18
16
  private static checkIfInitialized;
19
17
  static initAccount(address: string): Promise<void>;
20
18
  static execute(method: (...args: ArgumentsType) => MethodReturnType, args: ArgumentsType, transactionOptions?: TransactionOptions, to?: string): Promise<TransactionReceipt>;
21
19
  static publishTransaction(txData: Record<string, any>, transactionOptions?: TransactionOptions, transactionCall?: MethodReturnType): Promise<TransactionReceipt>;
22
- private static onStartPublishing;
23
- private static onError;
24
- private static onFinishPublishing;
25
- private static waitForPendingTransactions;
26
20
  }
27
21
  export default TxManager;
@@ -62,7 +62,6 @@ var TxManager = /** @class */ (function () {
62
62
  }
63
63
  TxManager.init = function (web3) {
64
64
  this.web3 = web3;
65
- this.nonceTracker = new NonceTracker_1.default(web3);
66
65
  };
67
66
  TxManager.checkIfInitialized = function () {
68
67
  if (!this.web3) {
@@ -72,7 +71,16 @@ var TxManager = /** @class */ (function () {
72
71
  TxManager.initAccount = function (address) {
73
72
  return __awaiter(this, void 0, void 0, function () {
74
73
  return __generator(this, function (_a) {
75
- return [2 /*return*/, this.nonceTracker.initAccount(address)];
74
+ switch (_a.label) {
75
+ case 0:
76
+ if (this.nonceTrackers[address])
77
+ return [2 /*return*/];
78
+ this.nonceTrackers[address] = new NonceTracker_1.default(this.web3, address);
79
+ return [4 /*yield*/, this.nonceTrackers[address].initAccount()];
80
+ case 1:
81
+ _a.sent();
82
+ return [2 /*return*/];
83
+ }
76
84
  });
77
85
  });
78
86
  };
@@ -92,55 +100,55 @@ var TxManager = /** @class */ (function () {
92
100
  };
93
101
  TxManager.publishTransaction = function (txData, transactionOptions, transactionCall) {
94
102
  return __awaiter(this, void 0, void 0, function () {
95
- var web3, options, estimatedGas, e_1, signingKey, signed, data, data, e_2;
103
+ var web3, options, estimatedGas, e_1, nonceTracker, signingKey, transactionResultData, signed, e_2;
96
104
  return __generator(this, function (_a) {
97
105
  switch (_a.label) {
98
106
  case 0:
99
107
  this.checkIfInitialized();
100
108
  (0, utils_1.checkIfInitialized)();
101
109
  (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
102
- return [4 /*yield*/, this.onStartPublishing()];
103
- case 1:
104
- _a.sent();
105
110
  web3 = (transactionOptions === null || transactionOptions === void 0 ? void 0 : transactionOptions.web3) || this.web3;
106
111
  return [4 /*yield*/, (0, utils_1.createTransactionOptions)(__assign({}, transactionOptions))];
107
- case 2:
112
+ case 1:
108
113
  options = _a.sent();
109
114
  if (!options.from) {
110
115
  throw Error("From account is undefined. You should pass it to transactionOptions or init action account.");
111
116
  }
112
117
  txData = __assign(__assign({}, options), txData);
113
- if (!transactionCall) return [3 /*break*/, 7];
118
+ if (!transactionCall) return [3 /*break*/, 6];
114
119
  estimatedGas = void 0;
115
- _a.label = 3;
116
- case 3:
117
- _a.trys.push([3, 5, , 6]);
120
+ _a.label = 2;
121
+ case 2:
122
+ _a.trys.push([2, 4, , 5]);
118
123
  return [4 /*yield*/, transactionCall.estimateGas(txData)];
119
- case 4:
124
+ case 3:
120
125
  estimatedGas = _a.sent();
121
- return [3 /*break*/, 6];
122
- case 5:
126
+ return [3 /*break*/, 5];
127
+ case 4:
123
128
  e_1 = _a.sent();
124
129
  TxManager.logger.debug({ error: e_1 }, "Fail to calculate estimated gas");
125
130
  estimatedGas = constants_1.defaultGasLimit;
126
- return [3 /*break*/, 6];
127
- case 6:
131
+ return [3 /*break*/, 5];
132
+ case 5:
128
133
  txData.gas = Math.ceil(estimatedGas * store_1.default.gasLimitMultiplier);
129
- _a.label = 7;
134
+ _a.label = 6;
135
+ case 6:
136
+ if (!(!(0, utils_1.checkForUsingExternalTxManager)(transactionOptions) && this.nonceTrackers[options.from])) return [3 /*break*/, 8];
137
+ nonceTracker = this.nonceTrackers[options.from];
138
+ return [4 /*yield*/, nonceTracker.onTransactionStartPublishing()];
130
139
  case 7:
131
- // TODO: Consider a better way to organize different strategies for publishing transactions.
132
- if (!(0, utils_1.checkForUsingExternalTxManager)(transactionOptions)) {
133
- if (this.nonceTracker.isManaged(options.from)) {
134
- txData.nonce = this.nonceTracker.consumeNonce(options.from);
135
- }
136
- }
137
- signingKey = store_1.default.keys[options.from];
140
+ _a.sent();
141
+ txData.nonce = nonceTracker.consumeNonce();
138
142
  _a.label = 8;
139
143
  case 8:
140
- _a.trys.push([8, 14, , 16]);
141
- if (!signingKey) return [3 /*break*/, 11];
142
- return [4 /*yield*/, web3.eth.accounts.signTransaction(txData, signingKey)];
144
+ signingKey = store_1.default.keys[options.from];
145
+ _a.label = 9;
143
146
  case 9:
147
+ _a.trys.push([9, 15, , 18]);
148
+ transactionResultData = void 0;
149
+ if (!signingKey) return [3 /*break*/, 12];
150
+ return [4 /*yield*/, web3.eth.accounts.signTransaction(txData, signingKey)];
151
+ case 10:
144
152
  signed = _a.sent();
145
153
  if (!signed.rawTransaction) {
146
154
  throw new Error("Failed to sign transaction");
@@ -150,97 +158,37 @@ var TxManager = /** @class */ (function () {
150
158
  txData: lodash_1.default.omit(txData, ["data"]),
151
159
  }, "Publishing signed transaction");
152
160
  return [4 /*yield*/, web3.eth.sendSignedTransaction(signed.rawTransaction)];
153
- case 10:
154
- data = _a.sent();
155
- this.onFinishPublishing();
156
- return [2 /*return*/, data];
157
161
  case 11:
162
+ transactionResultData = _a.sent();
163
+ return [3 /*break*/, 14];
164
+ case 12:
158
165
  TxManager.logger.debug({
159
166
  txData: lodash_1.default.omit(txData, ["data"]),
160
167
  }, "Publishing unsigned transaction");
161
168
  return [4 /*yield*/, web3.eth.sendTransaction(txData)];
162
- case 12:
163
- data = _a.sent();
164
- this.onFinishPublishing();
165
- return [2 /*return*/, data];
166
- case 13: return [3 /*break*/, 16];
169
+ case 13:
170
+ transactionResultData = _a.sent();
171
+ _a.label = 14;
167
172
  case 14:
173
+ if (nonceTracker)
174
+ nonceTracker.onTransactionPublished();
175
+ return [2 /*return*/, transactionResultData];
176
+ case 15:
168
177
  e_2 = _a.sent();
169
178
  TxManager.logger.error(e_2, "Error during transaction execution");
170
- return [4 /*yield*/, this.onError()];
171
- case 15:
179
+ if (!nonceTracker) return [3 /*break*/, 17];
180
+ return [4 /*yield*/, nonceTracker.onTransactionError()];
181
+ case 16:
172
182
  _a.sent();
173
- throw e_2;
174
- case 16: return [2 /*return*/];
175
- }
176
- });
177
- });
178
- };
179
- TxManager.onStartPublishing = function () {
180
- return __awaiter(this, void 0, void 0, function () {
181
- return __generator(this, function (_a) {
182
- switch (_a.label) {
183
- case 0:
184
- this.countOfPendingTransactions++;
185
- if (!this.transactionsOnHold)
186
- return [2 /*return*/];
187
- return [4 /*yield*/, this.waitForPendingTransactions()];
188
- case 1:
189
- _a.sent();
190
- return [2 /*return*/];
191
- }
192
- });
193
- });
194
- };
195
- TxManager.onError = function () {
196
- return __awaiter(this, void 0, void 0, function () {
197
- return __generator(this, function (_a) {
198
- switch (_a.label) {
199
- case 0:
200
- this.countOfPendingTransactions--;
201
- if (this.countOfPendingTransactions === 0)
202
- return [2 /*return*/];
203
- this.transactionsOnHold = [];
204
- return [4 /*yield*/, this.waitForPendingTransactions()];
205
- case 1:
206
- _a.sent();
207
- return [2 /*return*/];
208
- }
209
- });
210
- });
211
- };
212
- TxManager.onFinishPublishing = function () {
213
- var _this = this;
214
- this.countOfPendingTransactions--;
215
- if (this.countOfPendingTransactions === 0 && this.transactionsOnHold) {
216
- this.nonceTracker.reinitialize().then(function () {
217
- var _a;
218
- (_a = _this.transactionsOnHold) === null || _a === void 0 ? void 0 : _a.forEach(function (callback) { return callback(); });
219
- _this.transactionsOnHold = undefined;
220
- });
221
- }
222
- };
223
- TxManager.waitForPendingTransactions = function () {
224
- return __awaiter(this, void 0, void 0, function () {
225
- var _this = this;
226
- return __generator(this, function (_a) {
227
- switch (_a.label) {
228
- case 0: return [4 /*yield*/, new Promise(function (resolve) {
229
- if (!_this.transactionsOnHold)
230
- return resolve();
231
- _this.transactionsOnHold.push(function () {
232
- resolve();
233
- });
234
- })];
235
- case 1:
236
- _a.sent();
237
- return [2 /*return*/];
183
+ _a.label = 17;
184
+ case 17: throw e_2;
185
+ case 18: return [2 /*return*/];
238
186
  }
239
187
  });
240
188
  });
241
189
  };
242
190
  TxManager.logger = logger_1.default.child({ className: "TxManager" });
243
- TxManager.countOfPendingTransactions = 0;
191
+ TxManager.nonceTrackers = {};
244
192
  return TxManager;
245
193
  }());
246
194
  exports.default = TxManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super-protocol/sdk-js",
3
- "version": "0.11.2-beta.0",
3
+ "version": "0.11.4-beta.0",
4
4
  "main": "build/index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -53,7 +53,7 @@
53
53
  "typescript": "^4.4.4"
54
54
  },
55
55
  "dependencies": {
56
- "@super-protocol/dto-js": "0.0.13",
56
+ "@super-protocol/dto-js": "0.0.14",
57
57
  "@super-protocol/tee-lib": "^0.1.2",
58
58
  "@super-protocol/uplink-nodejs": "1.2.12",
59
59
  "eccrypto": "^1.1.6",