1 | "use strict";
|
2 |
|
3 | var __assign = (this && this.__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 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
12 | return new (P || (P = Promise))(function (resolve, reject) {
|
13 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
14 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
15 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
16 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
17 | });
|
18 | };
|
19 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
20 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
21 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
22 | function verb(n) { return function (v) { return step([n, v]); }; }
|
23 | function step(op) {
|
24 | if (f) throw new TypeError("Generator is already executing.");
|
25 | while (_) try {
|
26 | 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;
|
27 | if (y = 0, t) op = [op[0] & 2, t.value];
|
28 | switch (op[0]) {
|
29 | case 0: case 1: t = op; break;
|
30 | case 4: _.label++; return { value: op[1], done: false };
|
31 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
32 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
33 | default:
|
34 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
35 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
36 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
37 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
38 | if (t[2]) _.ops.pop();
|
39 | _.trys.pop(); continue;
|
40 | }
|
41 | op = body.call(thisArg, _);
|
42 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
43 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
44 | }
|
45 | };
|
46 |
|
47 |
|
48 |
|
49 | function getInfo() {
|
50 | return __awaiter(this, void 0, void 0, function () {
|
51 | var info;
|
52 | return __generator(this, function (_a) {
|
53 | switch (_a.label) {
|
54 | case 0: return [4 , this.eos.rpc.get_info({})];
|
55 | case 1:
|
56 | info = _a.sent();
|
57 | this.chainInfo = info;
|
58 | return [2 , info];
|
59 | }
|
60 | });
|
61 | });
|
62 | }
|
63 |
|
64 | function hasTransaction(block, transactionId) {
|
65 | if (block.transactions) {
|
66 | var result = block.transactions.find(function (transaction) { return transaction.trx.id === transactionId; });
|
67 | if (result !== undefined) {
|
68 | return true;
|
69 | }
|
70 | }
|
71 | return false;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function awaitTransaction(func, blocksToCheck, checkInterval, getBlockAttempts) {
|
80 | var _this = this;
|
81 | if (blocksToCheck === void 0) { blocksToCheck = 20; }
|
82 | if (checkInterval === void 0) { checkInterval = 400; }
|
83 | if (getBlockAttempts === void 0) { getBlockAttempts = 5; }
|
84 | return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
85 | var preCommitInfo, preCommitHeadBlockNum, transaction, error_1, blockNumToCheck, blockToCheck, getBlockAttempt, intConfirm;
|
86 | var _this = this;
|
87 | return __generator(this, function (_a) {
|
88 | switch (_a.label) {
|
89 | case 0: return [4 , getInfo.bind(this)()];
|
90 | case 1:
|
91 | preCommitInfo = _a.sent();
|
92 | preCommitHeadBlockNum = preCommitInfo.head_block_num;
|
93 | _a.label = 2;
|
94 | case 2:
|
95 | _a.trys.push([2, 4, , 5]);
|
96 | return [4 , func()];
|
97 | case 3:
|
98 | transaction = _a.sent();
|
99 | return [3 , 5];
|
100 | case 4:
|
101 | error_1 = _a.sent();
|
102 | reject(new Error("Await Transaction Failure: " + JSON.stringify(error_1)));
|
103 | return [3 , 5];
|
104 | case 5:
|
105 | blockNumToCheck = preCommitHeadBlockNum + 1;
|
106 | getBlockAttempt = 1;
|
107 | intConfirm = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
|
108 | var error_2;
|
109 | return __generator(this, function (_a) {
|
110 | switch (_a.label) {
|
111 | case 0:
|
112 | _a.trys.push([0, 2, , 3]);
|
113 | return [4 , this.eos.rpc.get_block(blockNumToCheck)];
|
114 | case 1:
|
115 | blockToCheck = _a.sent();
|
116 | if (hasTransaction(blockToCheck, transaction.transaction_id)) {
|
117 | clearInterval(intConfirm);
|
118 | resolve(transaction);
|
119 | }
|
120 | getBlockAttempt = 1;
|
121 | blockNumToCheck += 1;
|
122 | return [3 , 3];
|
123 | case 2:
|
124 | error_2 = _a.sent();
|
125 | if (getBlockAttempt >= getBlockAttempts) {
|
126 | clearInterval(intConfirm);
|
127 | reject(new Error("Await Transaction Failure: Failure to find a block, after " + getBlockAttempt + " attempts to check block " + blockNumToCheck + "."));
|
128 | }
|
129 | getBlockAttempt += 1;
|
130 | return [3 , 3];
|
131 | case 3:
|
132 | if (blockNumToCheck > preCommitHeadBlockNum + blocksToCheck) {
|
133 | clearInterval(intConfirm);
|
134 | reject(new Error("Await Transaction Timeout: Waited for " + blocksToCheck + " blocks ~(" + blocksToCheck / 2 + " seconds) starting with block num: " + preCommitHeadBlockNum + ". This does not mean the transaction failed just that the transaction wasn't found in a block before timeout"));
|
135 | }
|
136 | return [2 ];
|
137 | }
|
138 | });
|
139 | }); }, checkInterval);
|
140 | return [2 ];
|
141 | }
|
142 | });
|
143 | }); });
|
144 | }
|
145 | function getAllTableRows(params, key_field, json) {
|
146 | if (key_field === void 0) { key_field = 'id'; }
|
147 | if (json === void 0) { json = true; }
|
148 | return __awaiter(this, void 0, void 0, function () {
|
149 | var results, lowerBound, limit, parameters;
|
150 | return __generator(this, function (_a) {
|
151 | switch (_a.label) {
|
152 | case 0:
|
153 | results = [];
|
154 | lowerBound = 0;
|
155 | limit = -1;
|
156 | parameters = __assign({}, params, { json: json, lower_bound: params.lower_bound || lowerBound, scope: params.scope || params.code, limit: params.limit || limit });
|
157 | return [4 , this.eos.rpc.get_table_rows(parameters)];
|
158 | case 1:
|
159 | results = _a.sent();
|
160 | return [2 , results.rows];
|
161 | }
|
162 | });
|
163 | });
|
164 | }
|
165 |
|
166 | function checkPubKeytoAccount(account, publicKey) {
|
167 | return __awaiter(this, void 0, void 0, function () {
|
168 | var keyaccounts, accounts;
|
169 | return __generator(this, function (_a) {
|
170 | switch (_a.label) {
|
171 | case 0: return [4 , this.eos.rpc.history_get_key_accounts(publicKey)];
|
172 | case 1:
|
173 | keyaccounts = _a.sent();
|
174 | return [4 , keyaccounts.account_names];
|
175 | case 2:
|
176 | accounts = _a.sent();
|
177 | if (accounts.includes(account)) {
|
178 | return [2 , true];
|
179 | }
|
180 | return [2 , false];
|
181 | }
|
182 | });
|
183 | });
|
184 | }
|
185 |
|
186 | function transact(actions, broadcast, blocksBehind, expireSeconds) {
|
187 | if (broadcast === void 0) { broadcast = true; }
|
188 | if (blocksBehind === void 0) { blocksBehind = 3; }
|
189 | if (expireSeconds === void 0) { expireSeconds = 30; }
|
190 | return this.eos.transact({
|
191 | actions: actions
|
192 | }, {
|
193 | blocksBehind: blocksBehind,
|
194 | broadcast: broadcast,
|
195 | expireSeconds: expireSeconds,
|
196 | });
|
197 | }
|
198 | module.exports = {
|
199 | awaitTransaction: awaitTransaction,
|
200 | getAllTableRows: getAllTableRows,
|
201 | hasTransaction: hasTransaction,
|
202 | checkPubKeytoAccount: checkPubKeytoAccount,
|
203 | transact: transact,
|
204 | };
|
205 |
|
\ | No newline at end of file |