@super-protocol/sdk-js 0.6.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.
Files changed (132) hide show
  1. package/build/BlockchainConnector.d.ts +48 -0
  2. package/build/BlockchainConnector.js +259 -0
  3. package/build/TIIGenerator.d.ts +21 -0
  4. package/build/TIIGenerator.js +251 -0
  5. package/build/constants.d.ts +7 -0
  6. package/build/constants.js +10 -0
  7. package/build/contracts/ActiveOffersList.json +131 -0
  8. package/build/contracts/ActiveOrdersList.json +74 -0
  9. package/build/contracts/Marks.json +79 -0
  10. package/build/contracts/Offers.json +999 -0
  11. package/build/contracts/Orders.json +984 -0
  12. package/build/contracts/Providers.json +440 -0
  13. package/build/contracts/ProvidersOffers.json +254 -0
  14. package/build/contracts/Staking.json +323 -0
  15. package/build/contracts/Superpro.json +197 -0
  16. package/build/contracts/SuperproToken.json +356 -0
  17. package/build/crypto/Crypto.d.ts +37 -0
  18. package/build/crypto/Crypto.js +181 -0
  19. package/build/crypto/index.d.ts +2 -0
  20. package/build/crypto/index.js +7 -0
  21. package/build/crypto/nodejs/AES.d.ts +24 -0
  22. package/build/crypto/nodejs/AES.js +152 -0
  23. package/build/crypto/nodejs/ARIA.d.ts +24 -0
  24. package/build/crypto/nodejs/ARIA.js +151 -0
  25. package/build/crypto/nodejs/ECIES.d.ts +6 -0
  26. package/build/crypto/nodejs/ECIES.js +94 -0
  27. package/build/crypto/nodejs/NativeCrypto.d.ts +54 -0
  28. package/build/crypto/nodejs/NativeCrypto.js +214 -0
  29. package/build/crypto/nodejs/RSA-Hybrid.d.ts +15 -0
  30. package/build/crypto/nodejs/RSA-Hybrid.js +153 -0
  31. package/build/index.d.ts +65 -0
  32. package/build/index.js +117 -0
  33. package/build/logger.d.ts +4 -0
  34. package/build/logger.js +8 -0
  35. package/build/models/Ballot.d.ts +13 -0
  36. package/build/models/Ballot.js +76 -0
  37. package/build/models/Offer.d.ts +70 -0
  38. package/build/models/Offer.js +273 -0
  39. package/build/models/Order.d.ts +125 -0
  40. package/build/models/Order.js +540 -0
  41. package/build/models/Provider.d.ts +40 -0
  42. package/build/models/Provider.js +167 -0
  43. package/build/models/TCB.d.ts +80 -0
  44. package/build/models/TCB.js +315 -0
  45. package/build/models/TeeOffer.d.ts +118 -0
  46. package/build/models/TeeOffer.js +436 -0
  47. package/build/proto/Compression.d.ts +33 -0
  48. package/build/proto/Compression.js +146 -0
  49. package/build/proto/TRI.d.ts +122 -0
  50. package/build/proto/TRI.js +316 -0
  51. package/build/providers/storage/ChunksDownloadDecorator.d.ts +38 -0
  52. package/build/providers/storage/ChunksDownloadDecorator.js +284 -0
  53. package/build/providers/storage/IStorageProvider.d.ts +17 -0
  54. package/build/providers/storage/IStorageProvider.js +2 -0
  55. package/build/providers/storage/StorjStorageProvider.d.ts +26 -0
  56. package/build/providers/storage/StorjStorageProvider.js +389 -0
  57. package/build/providers/storage/getStorageProvider.d.ts +4 -0
  58. package/build/providers/storage/getStorageProvider.js +16 -0
  59. package/build/staticModels/ActiveOffers.d.ts +29 -0
  60. package/build/staticModels/ActiveOffers.js +144 -0
  61. package/build/staticModels/ActiveOrders.d.ts +21 -0
  62. package/build/staticModels/ActiveOrders.js +120 -0
  63. package/build/staticModels/Consensus.d.ts +63 -0
  64. package/build/staticModels/Consensus.js +373 -0
  65. package/build/staticModels/Epochs.d.ts +23 -0
  66. package/build/staticModels/Epochs.js +97 -0
  67. package/build/staticModels/LastBlocks.d.ts +25 -0
  68. package/build/staticModels/LastBlocks.js +96 -0
  69. package/build/staticModels/Marks.d.ts +13 -0
  70. package/build/staticModels/Marks.js +113 -0
  71. package/build/staticModels/OffersFactory.d.ts +37 -0
  72. package/build/staticModels/OffersFactory.js +227 -0
  73. package/build/staticModels/OrdersFactory.d.ts +137 -0
  74. package/build/staticModels/OrdersFactory.js +539 -0
  75. package/build/staticModels/ProviderRegistry.d.ts +81 -0
  76. package/build/staticModels/ProviderRegistry.js +311 -0
  77. package/build/staticModels/Staking.d.ts +26 -0
  78. package/build/staticModels/Staking.js +132 -0
  79. package/build/staticModels/Superpro.d.ts +20 -0
  80. package/build/staticModels/Superpro.js +99 -0
  81. package/build/staticModels/SuperproToken.d.ts +30 -0
  82. package/build/staticModels/SuperproToken.js +125 -0
  83. package/build/staticModels/Suspicious.d.ts +26 -0
  84. package/build/staticModels/Suspicious.js +99 -0
  85. package/build/staticModels/TeeOffersFactory.d.ts +36 -0
  86. package/build/staticModels/TeeOffersFactory.js +206 -0
  87. package/build/staticModels/Voting.d.ts +35 -0
  88. package/build/staticModels/Voting.js +111 -0
  89. package/build/store.d.ts +12 -0
  90. package/build/store.js +13 -0
  91. package/build/types/Ballot.d.ts +75 -0
  92. package/build/types/Ballot.js +49 -0
  93. package/build/types/Epoch.d.ts +21 -0
  94. package/build/types/Epoch.js +18 -0
  95. package/build/types/Events.d.ts +18 -0
  96. package/build/types/Events.js +2 -0
  97. package/build/types/Marks.d.ts +5 -0
  98. package/build/types/Marks.js +9 -0
  99. package/build/types/Offer.d.ts +63 -0
  100. package/build/types/Offer.js +41 -0
  101. package/build/types/Order.d.ts +61 -0
  102. package/build/types/Order.js +36 -0
  103. package/build/types/Origins.d.ts +12 -0
  104. package/build/types/Origins.js +10 -0
  105. package/build/types/Provider.d.ts +25 -0
  106. package/build/types/Provider.js +19 -0
  107. package/build/types/Staking.d.ts +27 -0
  108. package/build/types/Staking.js +22 -0
  109. package/build/types/Superpro.d.ts +24 -0
  110. package/build/types/Superpro.js +29 -0
  111. package/build/types/TcbData.d.ts +26 -0
  112. package/build/types/TcbData.js +21 -0
  113. package/build/types/TeeOffer.d.ts +22 -0
  114. package/build/types/TeeOffer.js +15 -0
  115. package/build/types/Web3.d.ts +26 -0
  116. package/build/types/Web3.js +2 -0
  117. package/build/types/blockchainConnector/StorageAccess.d.ts +8 -0
  118. package/build/types/blockchainConnector/StorageAccess.js +2 -0
  119. package/build/types/storage/StorageAccess.d.ts +6 -0
  120. package/build/types/storage/StorageAccess.js +2 -0
  121. package/build/types/storage/StorageFileAccess.d.ts +5 -0
  122. package/build/types/storage/StorageFileAccess.js +2 -0
  123. package/build/types/storage/StorageObject.d.ts +8 -0
  124. package/build/types/storage/StorageObject.js +2 -0
  125. package/build/utils/NonceTracker.d.ts +12 -0
  126. package/build/utils/NonceTracker.js +84 -0
  127. package/build/utils/TxManager.d.ts +21 -0
  128. package/build/utils/TxManager.js +159 -0
  129. package/build/utils.d.ts +38 -0
  130. package/build/utils.js +195 -0
  131. package/package.json +68 -0
  132. package/readme.md +40 -0
@@ -0,0 +1,26 @@
1
+ import Web3 from "web3";
2
+ export declare type ContractEvent = {
3
+ returnValues: {
4
+ [key: string]: unknown;
5
+ };
6
+ };
7
+ export declare type TransactionOptions = {
8
+ from?: string;
9
+ gas?: number;
10
+ gasPrice?: string;
11
+ web3?: Web3;
12
+ };
13
+ export declare type Transaction = {
14
+ hash: string;
15
+ nonce: number;
16
+ blockHash: string | null;
17
+ blockNumber: number | null;
18
+ transactionIndex: number | null;
19
+ from: string;
20
+ to: string | null;
21
+ value: string;
22
+ gasPrice: string;
23
+ gas: number;
24
+ input: string;
25
+ timestamp: number;
26
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { Transaction } from "../Web3";
2
+ declare type BlockchainTransaction = {
3
+ transactionsByAddress: {
4
+ [p: string]: Transaction[];
5
+ };
6
+ lastBlock: number;
7
+ };
8
+ export default BlockchainTransaction;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ import { StorageType } from "@super-protocol/dto-js";
2
+ declare type StorageAccess = {
3
+ storageType: StorageType;
4
+ credentials: any;
5
+ };
6
+ export default StorageAccess;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ import StorageAccess from "./StorageAccess";
2
+ declare type StorageFileAccess = StorageAccess & {
3
+ filepath: string;
4
+ };
5
+ export default StorageFileAccess;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ declare type StorageObject = {
2
+ name: string;
3
+ size: number;
4
+ isFolder: boolean;
5
+ childrenCount: number;
6
+ createdAt: Date;
7
+ };
8
+ export default StorageObject;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import Web3 from "web3";
2
+ declare class NonceTracker {
3
+ private web3;
4
+ private store;
5
+ constructor(web3: Web3);
6
+ initAccount(address: string): Promise<void>;
7
+ isManaged(address: string): boolean;
8
+ private checkAccount;
9
+ getNonce(address: string): number;
10
+ consumeNonce(address: string): number;
11
+ }
12
+ export default NonceTracker;
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var NonceTracker = /** @class */ (function () {
40
+ function NonceTracker(web3) {
41
+ this.web3 = web3;
42
+ this.store = {};
43
+ }
44
+ NonceTracker.prototype.initAccount = function (address) {
45
+ return __awaiter(this, void 0, void 0, function () {
46
+ var txCount;
47
+ return __generator(this, function (_a) {
48
+ switch (_a.label) {
49
+ case 0:
50
+ if (address in this.store) {
51
+ return [2 /*return*/];
52
+ }
53
+ return [4 /*yield*/, this.web3.eth.getTransactionCount(address)];
54
+ case 1:
55
+ txCount = _a.sent();
56
+ if (address in this.store) {
57
+ return [2 /*return*/];
58
+ }
59
+ this.store[address] = txCount;
60
+ return [2 /*return*/];
61
+ }
62
+ });
63
+ });
64
+ };
65
+ NonceTracker.prototype.isManaged = function (address) {
66
+ return address in this.store;
67
+ };
68
+ NonceTracker.prototype.checkAccount = function (address) {
69
+ if (this.isManaged(address)) {
70
+ return;
71
+ }
72
+ throw Error("".concat(address, " account is not initialized. You must call initAccount before using it."));
73
+ };
74
+ NonceTracker.prototype.getNonce = function (address) {
75
+ this.checkAccount(address);
76
+ return this.store[address];
77
+ };
78
+ NonceTracker.prototype.consumeNonce = function (address) {
79
+ this.checkAccount(address);
80
+ return this.store[address]++;
81
+ };
82
+ return NonceTracker;
83
+ }());
84
+ exports.default = NonceTracker;
@@ -0,0 +1,21 @@
1
+ import { TransactionReceipt } from "web3-core";
2
+ import { ContractSendMethod } from "web3-eth-contract";
3
+ import { TransactionOptions } from "../types/Web3";
4
+ import Web3 from "web3";
5
+ declare type ArgumentsType = any | any[];
6
+ declare type MethodReturnType = ContractSendMethod & {
7
+ _parent: {
8
+ _address: string;
9
+ };
10
+ };
11
+ declare class TxManager {
12
+ private static web3;
13
+ private static nonceTracker;
14
+ private static logger;
15
+ static init(web3: Web3): void;
16
+ private static checkIfInitialized;
17
+ static initAccount(address: string): Promise<void>;
18
+ static execute(method: (...args: ArgumentsType) => MethodReturnType, args: ArgumentsType, transactionOptions?: TransactionOptions, to?: string): Promise<TransactionReceipt>;
19
+ static publishTransaction(txData: Record<string, any>, transactionOptions?: TransactionOptions, transactionCall?: MethodReturnType): Promise<TransactionReceipt>;
20
+ }
21
+ export default TxManager;
@@ -0,0 +1,159 @@
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
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ var NonceTracker_1 = __importDefault(require("./NonceTracker"));
54
+ var logger_1 = __importDefault(require("../logger"));
55
+ var store_1 = __importDefault(require("../store"));
56
+ var utils_1 = require("../utils");
57
+ var Superpro_1 = __importDefault(require("../staticModels/Superpro"));
58
+ var constants_1 = require("../constants");
59
+ var lodash_1 = __importDefault(require("lodash"));
60
+ var TxManager = /** @class */ (function () {
61
+ function TxManager() {
62
+ }
63
+ TxManager.init = function (web3) {
64
+ this.web3 = web3;
65
+ this.nonceTracker = new NonceTracker_1.default(web3);
66
+ };
67
+ TxManager.checkIfInitialized = function () {
68
+ if (!this.web3) {
69
+ throw Error("TxManager should be initialized before using.");
70
+ }
71
+ };
72
+ TxManager.initAccount = function (address) {
73
+ return __awaiter(this, void 0, void 0, function () {
74
+ return __generator(this, function (_a) {
75
+ return [2 /*return*/, this.nonceTracker.initAccount(address)];
76
+ });
77
+ });
78
+ };
79
+ TxManager.execute = function (method, args, transactionOptions, to) {
80
+ if (to === void 0) { to = Superpro_1.default.address; }
81
+ return __awaiter(this, void 0, void 0, function () {
82
+ var transaction, txData;
83
+ return __generator(this, function (_a) {
84
+ transaction = method.apply(void 0, args);
85
+ txData = {
86
+ to: to,
87
+ data: transaction.encodeABI(),
88
+ };
89
+ return [2 /*return*/, TxManager.publishTransaction(txData, transactionOptions, transaction)];
90
+ });
91
+ });
92
+ };
93
+ TxManager.publishTransaction = function (txData, transactionOptions, transactionCall) {
94
+ return __awaiter(this, void 0, void 0, function () {
95
+ var web3, options, estimatedGas, e_1, signingKey, signed;
96
+ return __generator(this, function (_a) {
97
+ switch (_a.label) {
98
+ case 0:
99
+ this.checkIfInitialized();
100
+ (0, utils_1.checkIfInitialized)();
101
+ (0, utils_1.checkIfActionAccountInitialized)(transactionOptions);
102
+ web3 = (transactionOptions === null || transactionOptions === void 0 ? void 0 : transactionOptions.web3) || this.web3;
103
+ return [4 /*yield*/, (0, utils_1.createTransactionOptions)(__assign({}, transactionOptions))];
104
+ case 1:
105
+ options = _a.sent();
106
+ if (!options.from) {
107
+ throw Error("From account is undefined. You should pass it to transactionOptions or init action account.");
108
+ }
109
+ txData = __assign(__assign({}, options), txData);
110
+ if (!transactionCall) return [3 /*break*/, 6];
111
+ estimatedGas = void 0;
112
+ _a.label = 2;
113
+ case 2:
114
+ _a.trys.push([2, 4, , 5]);
115
+ return [4 /*yield*/, transactionCall.estimateGas(txData)];
116
+ case 3:
117
+ estimatedGas = _a.sent();
118
+ return [3 /*break*/, 5];
119
+ case 4:
120
+ e_1 = _a.sent();
121
+ TxManager.logger.debug({ error: e_1 }, "Fail to calculate estimated gas");
122
+ 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;
127
+ case 6:
128
+ // TODO: Consider a better way to organize different strategies for publishing transactions.
129
+ if (!(0, utils_1.checkForUsingExternalTxManager)(transactionOptions)) {
130
+ if (this.nonceTracker.isManaged(options.from)) {
131
+ txData.nonce = this.nonceTracker.consumeNonce(options.from);
132
+ }
133
+ }
134
+ signingKey = store_1.default.keys[options.from];
135
+ if (!signingKey) return [3 /*break*/, 8];
136
+ return [4 /*yield*/, web3.eth.accounts.signTransaction(txData, signingKey)];
137
+ case 7:
138
+ signed = _a.sent();
139
+ if (!signed.rawTransaction) {
140
+ throw new Error("Failed to sign transaction");
141
+ }
142
+ TxManager.logger.debug({
143
+ txHash: signed.transactionHash,
144
+ txData: lodash_1.default.omit(txData, ["data"]),
145
+ }, "Publishing signed transaction");
146
+ return [2 /*return*/, web3.eth.sendSignedTransaction(signed.rawTransaction)];
147
+ case 8:
148
+ TxManager.logger.debug({
149
+ txData: lodash_1.default.omit(txData, ["data"]),
150
+ }, "Publishing unsigned transaction");
151
+ return [2 /*return*/, web3.eth.sendTransaction(txData)];
152
+ }
153
+ });
154
+ });
155
+ };
156
+ TxManager.logger = logger_1.default.child({ className: "TxManager" });
157
+ return TxManager;
158
+ }());
159
+ exports.default = TxManager;
@@ -0,0 +1,38 @@
1
+ import { TransactionOptions } from "./types/Web3";
2
+ import Web3 from "web3";
3
+ /**
4
+ * Function for checking if BlockchainConnector initialized (required for get and set methods)
5
+ * Used in all models constructors
6
+ */
7
+ export declare const checkIfInitialized: () => void;
8
+ /**
9
+ * Function for checking if provider action account initialized (required for set methods)
10
+ * Used in all set methods
11
+ */
12
+ export declare const checkIfActionAccountInitialized: (transactionOptions?: TransactionOptions) => void;
13
+ /**
14
+ * Function for checking if current configuration supposed to use external transaction manager like MetaMask and etc.
15
+ */
16
+ export declare const checkForUsingExternalTxManager: (transactionOptions?: TransactionOptions) => boolean;
17
+ /**
18
+ * Updates gas price determined by the last few blocks median
19
+ */
20
+ export declare const getGasPrice: (web3: Web3) => Promise<string>;
21
+ /**
22
+ * Merge transaction options from arguments and from store
23
+ * Used in all set methods
24
+ */
25
+ export declare const createTransactionOptions: (options?: TransactionOptions) => Promise<TransactionOptions>;
26
+ export declare const isNodeJS: () => boolean;
27
+ declare type FormatFunctions = {
28
+ $obj?: (value: unknown) => unknown;
29
+ $tuple?: (value: unknown) => unknown;
30
+ };
31
+ declare type FormatItem = Format | Object | ((value: unknown) => unknown) | null | FormatFunctions;
32
+ declare type Format = FormatItem[] | {
33
+ [key: string]: FormatItem;
34
+ };
35
+ export declare const tupleToObject: <T>(data: unknown[], format: Format) => T;
36
+ export declare const objectToTuple: (data: unknown, format: Format) => unknown[];
37
+ export declare const getTimestamp: () => Promise<string | number>;
38
+ export {};
package/build/utils.js ADDED
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.getTimestamp = exports.objectToTuple = exports.tupleToObject = exports.isNodeJS = exports.createTransactionOptions = exports.getGasPrice = exports.checkForUsingExternalTxManager = exports.checkIfActionAccountInitialized = exports.checkIfInitialized = void 0;
43
+ var store_1 = __importDefault(require("./store"));
44
+ var lodash_1 = require("lodash");
45
+ /**
46
+ * Function for checking if BlockchainConnector initialized (required for get and set methods)
47
+ * Used in all models constructors
48
+ */
49
+ var checkIfInitialized = function () {
50
+ if (!store_1.default.isInitialized)
51
+ throw new Error("BlockchainConnector is not initialized, needs to run 'await BlockchainConnector.init(CONFIG)' first");
52
+ };
53
+ exports.checkIfInitialized = checkIfInitialized;
54
+ /**
55
+ * Function for checking if provider action account initialized (required for set methods)
56
+ * Used in all set methods
57
+ */
58
+ var checkIfActionAccountInitialized = function (transactionOptions) {
59
+ if (!store_1.default.actionAccount && !(transactionOptions === null || transactionOptions === void 0 ? void 0 : transactionOptions.web3))
60
+ throw new Error("Provider action account is not initialized, needs to run 'BlockchainConnector.initActionAccount(SECRET_KEY)' first");
61
+ };
62
+ exports.checkIfActionAccountInitialized = checkIfActionAccountInitialized;
63
+ /**
64
+ * Function for checking if current configuration supposed to use external transaction manager like MetaMask and etc.
65
+ */
66
+ var checkForUsingExternalTxManager = function (transactionOptions) {
67
+ // TODO: Agree on more proper way of signaling, that we use an external transaction manager, than just passing a web3 instance.
68
+ return !!(transactionOptions === null || transactionOptions === void 0 ? void 0 : transactionOptions.web3);
69
+ };
70
+ exports.checkForUsingExternalTxManager = checkForUsingExternalTxManager;
71
+ /**
72
+ * Updates gas price determined by the last few blocks median
73
+ */
74
+ var getGasPrice = function (web3) { return __awaiter(void 0, void 0, void 0, function () {
75
+ return __generator(this, function (_a) {
76
+ return [2 /*return*/, web3.eth.getGasPrice()];
77
+ });
78
+ }); };
79
+ exports.getGasPrice = getGasPrice;
80
+ /**
81
+ * Merge transaction options from arguments and from store
82
+ * Used in all set methods
83
+ */
84
+ var createTransactionOptions = function (options) { return __awaiter(void 0, void 0, void 0, function () {
85
+ var web3, _a;
86
+ return __generator(this, function (_b) {
87
+ switch (_b.label) {
88
+ case 0:
89
+ if (!options)
90
+ options = {};
91
+ if (!options.from)
92
+ options.from = store_1.default.actionAccount;
93
+ if (!options.gas)
94
+ options.gas = store_1.default.gasLimit;
95
+ if (!!options.gasPrice) return [3 /*break*/, 3];
96
+ if (!store_1.default.gasPrice) return [3 /*break*/, 1];
97
+ options.gasPrice = store_1.default.gasPrice;
98
+ return [3 /*break*/, 3];
99
+ case 1:
100
+ web3 = options.web3 || store_1.default.web3;
101
+ if (!web3) {
102
+ throw Error("web3 is undefined, define it in transaction options or initialize BlockchainConnector with web3 instance.");
103
+ }
104
+ _a = options;
105
+ return [4 /*yield*/, (0, exports.getGasPrice)(web3)];
106
+ case 2:
107
+ _a.gasPrice = _b.sent();
108
+ _b.label = 3;
109
+ case 3:
110
+ delete options.web3;
111
+ return [2 /*return*/, options];
112
+ }
113
+ });
114
+ }); };
115
+ exports.createTransactionOptions = createTransactionOptions;
116
+ var isNodeJS = function () {
117
+ // @ts-ignore
118
+ return typeof window === "undefined";
119
+ };
120
+ exports.isNodeJS = isNodeJS;
121
+ var tupleToObject = function (data, format) {
122
+ var processItem = function (dataItem, formatItem) {
123
+ if (formatItem === null || formatItem === void 0 ? void 0 : formatItem.$obj) {
124
+ return formatItem.$obj(dataItem);
125
+ }
126
+ else if (typeof formatItem === "function") {
127
+ return formatItem(dataItem);
128
+ }
129
+ else if (typeof formatItem === "object" && typeof dataItem === "object") {
130
+ return (0, exports.tupleToObject)(dataItem, formatItem);
131
+ }
132
+ else {
133
+ return dataItem;
134
+ }
135
+ };
136
+ if ((0, lodash_1.isArray)(format)) {
137
+ var result = data.map(function (dataItem, index) {
138
+ var formatItem = index < format.length ? format[index] : format[format.length - 1];
139
+ return processItem(dataItem, formatItem);
140
+ });
141
+ return result;
142
+ }
143
+ else {
144
+ var result_1 = {};
145
+ Object.keys(format).forEach(function (key, index) {
146
+ var formatItem = format[key];
147
+ var dataItem = data[index];
148
+ result_1[key] = processItem(dataItem, formatItem);
149
+ });
150
+ return result_1;
151
+ }
152
+ };
153
+ exports.tupleToObject = tupleToObject;
154
+ var objectToTuple = function (data, format) {
155
+ var processItem = function (dataItem, formatItem) {
156
+ if (formatItem === null || formatItem === void 0 ? void 0 : formatItem.$tuple) {
157
+ return formatItem.$tuple(dataItem);
158
+ }
159
+ else if (typeof formatItem === "object" && typeof dataItem === "object") {
160
+ return (0, exports.objectToTuple)(dataItem, formatItem);
161
+ }
162
+ else {
163
+ return dataItem;
164
+ }
165
+ };
166
+ if ((0, lodash_1.isArray)(format)) {
167
+ return data.map(function (dataItem, index) {
168
+ var formatItem = index < format.length ? format[index] : format[format.length - 1];
169
+ return processItem(dataItem, formatItem);
170
+ });
171
+ }
172
+ else {
173
+ return Object.keys(format).map(function (key) {
174
+ var dataItem = data[key];
175
+ var formatItem = format[key];
176
+ return processItem(dataItem, formatItem);
177
+ });
178
+ }
179
+ };
180
+ exports.objectToTuple = objectToTuple;
181
+ var getTimestamp = function () { return __awaiter(void 0, void 0, void 0, function () {
182
+ var endBlockIndex, block;
183
+ return __generator(this, function (_a) {
184
+ switch (_a.label) {
185
+ case 0: return [4 /*yield*/, store_1.default.web3.eth.getBlockNumber()];
186
+ case 1:
187
+ endBlockIndex = _a.sent();
188
+ return [4 /*yield*/, store_1.default.web3.eth.getBlock(endBlockIndex, true)];
189
+ case 2:
190
+ block = _a.sent();
191
+ return [2 /*return*/, block.timestamp];
192
+ }
193
+ });
194
+ }); };
195
+ exports.getTimestamp = getTimestamp;
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@super-protocol/sdk-js",
3
+ "version": "0.6.0",
4
+ "main": "build/index.js",
5
+ "license": "MIT",
6
+ "files": [
7
+ "build/**/*"
8
+ ],
9
+ "scripts": {
10
+ "test": "jest",
11
+ "test:watch": "jest --watch",
12
+ "test:cov": "jest --coverage",
13
+ "test:debug": "node --inspect-brk node_modules/.bin/jest -i",
14
+ "prettier": "prettier --write ./src/**/* ./test/**/*",
15
+ "lint": "eslint --ext .ts src",
16
+ "lint:fix": "eslint --ext .ts src --fix",
17
+ "build": "rm -rf ./build && tsc",
18
+ "prepare": "npm run build",
19
+ "docs": "typedoc src/index.ts --excludePrivate --plugin typedoc-plugin-markdown --out ./auto-docs",
20
+ "docs-cleanup": "rm -rf auto-docs",
21
+ "bump": "npm version prerelease --preid=beta && git push && git push --tags",
22
+ "patch:beta": "npm version prepatch --preid=beta && git push && git push --tags",
23
+ "minor:beta": "npm version preminor --preid=beta && git push && git push --tags",
24
+ "major:beta": "npm version premajor --preid=beta && git push && git push --tags",
25
+ "patch": "npm version patch && git push && git push --tags",
26
+ "minor": "npm version minor && git push && git push --tags",
27
+ "major": "npm version major && git push && git push --tags",
28
+ "dto:update": "npm run dto:clean && npm run dto:generate",
29
+ "dto:clean": "rm -rf ./src/proto/*",
30
+ "dto:generate": "cd ./dto/src && protoc --plugin=../../node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=esModuleInterop=true --ts_proto_opt=unrecognizedEnum=false --ts_proto_out=../../src/proto ./*"
31
+ },
32
+ "devDependencies": {
33
+ "@types/eccrypto": "^1.1.3",
34
+ "@types/jest": "^27.0.3",
35
+ "@types/lodash": "^4.14.176",
36
+ "@types/node": "^17.0.0",
37
+ "@types/node-gzip": "^1.1.0",
38
+ "@typescript-eslint/eslint-plugin": "^5.8.0",
39
+ "@typescript-eslint/parser": "^5.8.0",
40
+ "eslint": "^8.5.0",
41
+ "eslint-config-prettier": "^8.3.0",
42
+ "eslint-plugin-prettier": "^4.0.0",
43
+ "eslint-plugin-tsdoc": "^0.2.14",
44
+ "jest": "^27.3.1",
45
+ "pandoc-bin": "^0.2.0",
46
+ "prettier": "^2.4.1",
47
+ "ts-jest": "^27.0.7",
48
+ "ts-proto": "^1.110.4",
49
+ "typedoc": "^0.22.11",
50
+ "typedoc-plugin-markdown": "^3.11.12",
51
+ "typescript": "^4.4.4",
52
+ "ts-protoc-gen": "^0.15.0"
53
+ },
54
+ "dependencies": {
55
+ "@super-protocol/dto-js": "0.0.13",
56
+ "@super-protocol/tee-lib": "^0.1.2",
57
+ "@super-protocol/uplink-nodejs": "1.2.12",
58
+ "eccrypto": "^1.1.6",
59
+ "ethers": "^5.5.3",
60
+ "hybrid-crypto-js": "^0.2.4",
61
+ "lodash": "^4.17.21",
62
+ "node-gzip": "^1.1.2",
63
+ "pino": "^7.2.0",
64
+ "protobufjs": "^6.11.2",
65
+ "web3": "^1.7.1"
66
+ },
67
+ "repository": "git://github.com/Super-Protocol/sdk-js.git"
68
+ }