@super-protocol/sdk-js 0.10.1-beta.2 → 0.10.2-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.
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -411,13 +422,14 @@ var Order = /** @class */ (function () {
411
422
  Order.prototype.createSubOrder = function (subOrderInfo, blocking, holdSum, transactionOptions) {
412
423
  if (holdSum === void 0) { holdSum = '0'; }
413
424
  return __awaiter(this, void 0, void 0, function () {
414
- var tupleSubOrder, params;
425
+ var preparedInfo, tupleSubOrder, params;
415
426
  return __generator(this, function (_a) {
416
427
  switch (_a.label) {
417
428
  case 0:
418
429
  transactionOptions !== null && transactionOptions !== void 0 ? transactionOptions : this.checkInitOrder(transactionOptions);
419
430
  (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
420
- tupleSubOrder = (0, utils_1.objectToTuple)(subOrderInfo, Order_1.OrderInfoStructure);
431
+ preparedInfo = __assign(__assign({}, subOrderInfo), { externalId: (0, utils_2.formatBytes32String)(subOrderInfo.externalId) });
432
+ tupleSubOrder = (0, utils_1.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
421
433
  params = {
422
434
  blockParentOrder: blocking,
423
435
  holdSum: holdSum,
@@ -448,7 +460,8 @@ var Order = /** @class */ (function () {
448
460
  batch = new transactionOptions.web3.BatchRequest();
449
461
  promises = subOrdersInfo.map(function (subOrderInfo) {
450
462
  return new Promise(function (res, rej) {
451
- var tupleSubOrder = (0, utils_1.objectToTuple)(subOrderInfo, Order_1.OrderInfoStructure);
463
+ var preparedInfo = __assign(__assign({}, subOrderInfo), { externalId: (0, utils_2.formatBytes32String)(subOrderInfo.externalId) });
464
+ var tupleSubOrder = (0, utils_1.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
452
465
  var params = {
453
466
  blockParentOrder: subOrderInfo.blocking,
454
467
  holdSum: subOrderInfo.holdSum,
@@ -36,7 +36,19 @@ declare class OrdersFactory {
36
36
  * @param transactionOptions - object what contains alternative action account or gas limit (optional)
37
37
  * @returns {Promise<void>} - Does not return id of created order!
38
38
  */
39
- static createWorkflow(perentOrderInfo: OrderInfo, subOrdersInfo: OrderInfo[], holdDeposit?: string, transactionOptions?: TransactionOptions): Promise<void>;
39
+ static createWorkflow(parentOrderInfo: OrderInfo, subOrdersInfo: OrderInfo[], holdDeposit?: string, transactionOptions?: TransactionOptions): Promise<void>;
40
+ /**
41
+ * Function for cancel workflow
42
+ * @param parentOrderId - Parent order id
43
+ * @returns {Promise<void>} - Does not return id of created order!
44
+ */
45
+ static cancelWorkflow(perentOrderId: string, transactionOptions?: TransactionOptions): Promise<void>;
46
+ /**
47
+ * Function for withdraw workflow change
48
+ * @param parentOrderId - Parent order id
49
+ * @returns {Promise<void>} - Does not return id of created order!
50
+ */
51
+ static withdrawWorkflowChange(parentOrderId: string, transactionOptions?: TransactionOptions): Promise<void>;
40
52
  /**
41
53
  * Function for refilling order deposit
42
54
  * @param orderId - order id
@@ -1,4 +1,15 @@
1
1
  "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
2
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -127,13 +138,14 @@ var OrdersFactory = /** @class */ (function () {
127
138
  if (holdDeposit === void 0) { holdDeposit = "0"; }
128
139
  if (suspended === void 0) { suspended = false; }
129
140
  return __awaiter(this, void 0, void 0, function () {
130
- var contract, orderInfoArguments;
141
+ var contract, preparedInfo, orderInfoArguments;
131
142
  return __generator(this, function (_a) {
132
143
  switch (_a.label) {
133
144
  case 0:
134
145
  contract = BlockchainConnector_1.default.getContractInstance(transactionOptions);
135
146
  (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
136
- orderInfoArguments = (0, utils_1.objectToTuple)(orderInfo, Order_1.OrderInfoStructure);
147
+ preparedInfo = __assign(__assign({}, orderInfo), { externalId: (0, utils_2.formatBytes32String)(orderInfo.externalId) });
148
+ orderInfoArguments = (0, utils_1.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
137
149
  return [4 /*yield*/, TxManager_1.default.execute(contract.methods.createOrder, [orderInfoArguments, holdDeposit, suspended], transactionOptions)];
138
150
  case 1:
139
151
  _a.sent();
@@ -176,18 +188,63 @@ var OrdersFactory = /** @class */ (function () {
176
188
  * @param transactionOptions - object what contains alternative action account or gas limit (optional)
177
189
  * @returns {Promise<void>} - Does not return id of created order!
178
190
  */
179
- OrdersFactory.createWorkflow = function (perentOrderInfo, subOrdersInfo, holdDeposit, transactionOptions) {
191
+ OrdersFactory.createWorkflow = function (parentOrderInfo, subOrdersInfo, holdDeposit, transactionOptions) {
180
192
  if (holdDeposit === void 0) { holdDeposit = "0"; }
181
193
  return __awaiter(this, void 0, void 0, function () {
182
- var contract, perentOrderInfoArgs, subOrdersInfoArgs;
194
+ var contract, preparedInfo, parentOrderInfoArgs, preparedSubOrdersInfo, subOrdersInfoArgs;
195
+ return __generator(this, function (_a) {
196
+ switch (_a.label) {
197
+ case 0:
198
+ contract = BlockchainConnector_1.default.getContractInstance(transactionOptions);
199
+ (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
200
+ preparedInfo = __assign(__assign({}, parentOrderInfo), { externalId: (0, utils_2.formatBytes32String)(parentOrderInfo.externalId) });
201
+ parentOrderInfoArgs = (0, utils_1.objectToTuple)(preparedInfo, Order_1.OrderInfoStructure);
202
+ subOrdersInfo.forEach(function (o) { return (o.externalId = (0, utils_2.formatBytes32String)(o.externalId)); });
203
+ preparedSubOrdersInfo = subOrdersInfo.map(function (o) { return (__assign(__assign({}, o), { externalId: (0, utils_2.formatBytes32String)(o.externalId) })); });
204
+ subOrdersInfoArgs = (0, utils_1.objectToTuple)(preparedSubOrdersInfo, Order_1.OrderInfoStructureArray);
205
+ return [4 /*yield*/, TxManager_1.default.execute(contract.methods.createWorkflow, [parentOrderInfoArgs, holdDeposit, subOrdersInfoArgs], transactionOptions)];
206
+ case 1:
207
+ _a.sent();
208
+ return [2 /*return*/];
209
+ }
210
+ });
211
+ });
212
+ };
213
+ /**
214
+ * Function for cancel workflow
215
+ * @param parentOrderId - Parent order id
216
+ * @returns {Promise<void>} - Does not return id of created order!
217
+ */
218
+ OrdersFactory.cancelWorkflow = function (perentOrderId, transactionOptions) {
219
+ return __awaiter(this, void 0, void 0, function () {
220
+ var contract;
221
+ return __generator(this, function (_a) {
222
+ switch (_a.label) {
223
+ case 0:
224
+ contract = BlockchainConnector_1.default.getContractInstance(transactionOptions);
225
+ (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
226
+ return [4 /*yield*/, TxManager_1.default.execute(contract.methods.cancelWorkflow, [perentOrderId], transactionOptions)];
227
+ case 1:
228
+ _a.sent();
229
+ return [2 /*return*/];
230
+ }
231
+ });
232
+ });
233
+ };
234
+ /**
235
+ * Function for withdraw workflow change
236
+ * @param parentOrderId - Parent order id
237
+ * @returns {Promise<void>} - Does not return id of created order!
238
+ */
239
+ OrdersFactory.withdrawWorkflowChange = function (parentOrderId, transactionOptions) {
240
+ return __awaiter(this, void 0, void 0, function () {
241
+ var contract;
183
242
  return __generator(this, function (_a) {
184
243
  switch (_a.label) {
185
244
  case 0:
186
245
  contract = BlockchainConnector_1.default.getContractInstance(transactionOptions);
187
246
  (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
188
- perentOrderInfoArgs = (0, utils_1.objectToTuple)(perentOrderInfo, Order_1.OrderInfoStructure);
189
- subOrdersInfoArgs = (0, utils_1.objectToTuple)(subOrdersInfo, Order_1.OrderInfoStructureArray);
190
- return [4 /*yield*/, TxManager_1.default.execute(contract.methods.createWorkflow, [perentOrderInfoArgs, holdDeposit, subOrdersInfoArgs], transactionOptions)];
247
+ return [4 /*yield*/, TxManager_1.default.execute(contract.methods.withdrawWorkflowChange, [parentOrderId], transactionOptions)];
191
248
  case 1:
192
249
  _a.sent();
193
250
  return [2 /*return*/];
@@ -30,6 +30,7 @@ export declare const OrderInfoStructure: {
30
30
  inputOffers: StringConstructor[];
31
31
  selectedOffers: StringConstructor[];
32
32
  };
33
+ externalId: StringConstructor;
33
34
  };
34
35
  export declare const OrderInfoStructureArray: {
35
36
  offer: StringConstructor;
@@ -42,6 +43,7 @@ export declare const OrderInfoStructureArray: {
42
43
  inputOffers: StringConstructor[];
43
44
  selectedOffers: StringConstructor[];
44
45
  };
46
+ externalId: StringConstructor;
45
47
  }[];
46
48
  export declare type OrderInfo = {
47
49
  offer: string;
@@ -27,6 +27,7 @@ exports.OrderInfoStructure = {
27
27
  encryptedArgs: String,
28
28
  status: OrderStatus,
29
29
  args: exports.OrderArgsStructure,
30
+ externalId: String,
30
31
  };
31
32
  // Array of order info structures
32
33
  exports.OrderInfoStructureArray = [exports.OrderInfoStructure];
@@ -6,6 +6,7 @@ declare class NonceTracker {
6
6
  constructor(web3: Web3);
7
7
  initAccount(address: string): Promise<void>;
8
8
  isManaged(address: string): boolean;
9
+ reinitialize(): Promise<void>;
9
10
  private checkAccount;
10
11
  getNonce(address: string): number;
11
12
  consumeNonce(address: string): number;
@@ -71,6 +71,32 @@ var NonceTracker = /** @class */ (function () {
71
71
  NonceTracker.prototype.isManaged = function (address) {
72
72
  return address in this.store;
73
73
  };
74
+ NonceTracker.prototype.reinitialize = function () {
75
+ return __awaiter(this, void 0, void 0, function () {
76
+ var _this = this;
77
+ return __generator(this, function (_a) {
78
+ 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
+ }); }))];
92
+ case 1:
93
+ _a.sent();
94
+ NonceTracker.logger.trace("All accounts has been reinitialized");
95
+ return [2 /*return*/];
96
+ }
97
+ });
98
+ });
99
+ };
74
100
  NonceTracker.prototype.checkAccount = function (address) {
75
101
  if (this.isManaged(address)) {
76
102
  return;
@@ -12,10 +12,16 @@ declare class TxManager {
12
12
  private static web3;
13
13
  private static nonceTracker;
14
14
  private static logger;
15
+ private static transactionsOnHold;
16
+ private static countOfPendingTransactions;
15
17
  static init(web3: Web3): void;
16
18
  private static checkIfInitialized;
17
19
  static initAccount(address: string): Promise<void>;
18
20
  static execute(method: (...args: ArgumentsType) => MethodReturnType, args: ArgumentsType, transactionOptions?: TransactionOptions, to?: string): Promise<TransactionReceipt>;
19
21
  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;
20
26
  }
21
27
  export default TxManager;
@@ -92,39 +92,42 @@ var TxManager = /** @class */ (function () {
92
92
  };
93
93
  TxManager.publishTransaction = function (txData, transactionOptions, transactionCall) {
94
94
  return __awaiter(this, void 0, void 0, function () {
95
- var web3, options, estimatedGas, e_1, signingKey, signed;
95
+ var web3, options, estimatedGas, e_1, signingKey, signed, data, data, e_2;
96
96
  return __generator(this, function (_a) {
97
97
  switch (_a.label) {
98
98
  case 0:
99
99
  this.checkIfInitialized();
100
100
  (0, utils_1.checkIfInitialized)();
101
101
  (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
102
+ return [4 /*yield*/, this.onStartPublishing()];
103
+ case 1:
104
+ _a.sent();
102
105
  web3 = (transactionOptions === null || transactionOptions === void 0 ? void 0 : transactionOptions.web3) || this.web3;
103
106
  return [4 /*yield*/, (0, utils_1.createTransactionOptions)(__assign({}, transactionOptions))];
104
- case 1:
107
+ case 2:
105
108
  options = _a.sent();
106
109
  if (!options.from) {
107
110
  throw Error("From account is undefined. You should pass it to transactionOptions or init action account.");
108
111
  }
109
112
  txData = __assign(__assign({}, options), txData);
110
- if (!transactionCall) return [3 /*break*/, 6];
113
+ if (!transactionCall) return [3 /*break*/, 7];
111
114
  estimatedGas = void 0;
112
- _a.label = 2;
113
- case 2:
114
- _a.trys.push([2, 4, , 5]);
115
- return [4 /*yield*/, transactionCall.estimateGas(txData)];
115
+ _a.label = 3;
116
116
  case 3:
117
- estimatedGas = _a.sent();
118
- return [3 /*break*/, 5];
117
+ _a.trys.push([3, 5, , 6]);
118
+ return [4 /*yield*/, transactionCall.estimateGas(txData)];
119
119
  case 4:
120
+ estimatedGas = _a.sent();
121
+ return [3 /*break*/, 6];
122
+ case 5:
120
123
  e_1 = _a.sent();
121
124
  TxManager.logger.debug({ error: e_1 }, "Fail to calculate estimated gas");
122
125
  estimatedGas = constants_1.defaultGasLimit;
123
- return [3 /*break*/, 5];
124
- case 5:
125
- txData.gas = Math.ceil(estimatedGas * store_1.default.gasLimitMultiplier);
126
- _a.label = 6;
126
+ return [3 /*break*/, 6];
127
127
  case 6:
128
+ txData.gas = Math.ceil(estimatedGas * store_1.default.gasLimitMultiplier);
129
+ _a.label = 7;
130
+ case 7:
128
131
  // TODO: Consider a better way to organize different strategies for publishing transactions.
129
132
  if (!(0, utils_1.checkForUsingExternalTxManager)(transactionOptions)) {
130
133
  if (this.nonceTracker.isManaged(options.from)) {
@@ -132,9 +135,12 @@ var TxManager = /** @class */ (function () {
132
135
  }
133
136
  }
134
137
  signingKey = store_1.default.keys[options.from];
135
- if (!signingKey) return [3 /*break*/, 8];
138
+ _a.label = 8;
139
+ case 8:
140
+ _a.trys.push([8, 14, , 16]);
141
+ if (!signingKey) return [3 /*break*/, 11];
136
142
  return [4 /*yield*/, web3.eth.accounts.signTransaction(txData, signingKey)];
137
- case 7:
143
+ case 9:
138
144
  signed = _a.sent();
139
145
  if (!signed.rawTransaction) {
140
146
  throw new Error("Failed to sign transaction");
@@ -143,17 +149,98 @@ var TxManager = /** @class */ (function () {
143
149
  txHash: signed.transactionHash,
144
150
  txData: lodash_1.default.omit(txData, ["data"]),
145
151
  }, "Publishing signed transaction");
146
- return [2 /*return*/, web3.eth.sendSignedTransaction(signed.rawTransaction)];
147
- case 8:
152
+ return [4 /*yield*/, web3.eth.sendSignedTransaction(signed.rawTransaction)];
153
+ case 10:
154
+ data = _a.sent();
155
+ this.onFinishPublishing();
156
+ return [2 /*return*/, data];
157
+ case 11:
148
158
  TxManager.logger.debug({
149
159
  txData: lodash_1.default.omit(txData, ["data"]),
150
160
  }, "Publishing unsigned transaction");
151
- return [2 /*return*/, web3.eth.sendTransaction(txData)];
161
+ 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];
167
+ case 14:
168
+ e_2 = _a.sent();
169
+ TxManager.logger.error(e_2, "Error during transaction execution");
170
+ return [4 /*yield*/, this.onError()];
171
+ case 15:
172
+ _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*/];
152
238
  }
153
239
  });
154
240
  });
155
241
  };
156
242
  TxManager.logger = logger_1.default.child({ className: "TxManager" });
243
+ TxManager.countOfPendingTransactions = 0;
157
244
  return TxManager;
158
245
  }());
159
246
  exports.default = TxManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@super-protocol/sdk-js",
3
- "version": "0.10.1-beta.2",
3
+ "version": "0.10.2-beta.0",
4
4
  "main": "build/index.js",
5
5
  "license": "MIT",
6
6
  "files": [