@pioneer-platform/maya-network 8.13.1 → 8.13.2
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.
- package/CHANGELOG.md +6 -0
- package/lib/index.d.ts +15 -1
- package/lib/index.js +118 -84
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -13,7 +13,21 @@ declare const BASE_MAYA = 10000000000;
|
|
|
13
13
|
// Implementation
|
|
14
14
|
//**********************************/
|
|
15
15
|
declare const get_info: () => Promise<any>;
|
|
16
|
-
declare const get_account_info: (address: string) => Promise<
|
|
16
|
+
declare const get_account_info: (address: string) => Promise<{
|
|
17
|
+
result: {
|
|
18
|
+
value: {
|
|
19
|
+
account_number: any;
|
|
20
|
+
sequence: string;
|
|
21
|
+
address: any;
|
|
22
|
+
coins: any;
|
|
23
|
+
public_key: any;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
accountNumber: any;
|
|
27
|
+
sequence: string;
|
|
28
|
+
balance: any;
|
|
29
|
+
assets: any;
|
|
30
|
+
}>;
|
|
17
31
|
declare const get_balance: (address: string) => Promise<number>;
|
|
18
32
|
declare const get_balances: (address: string) => Promise<any[]>;
|
|
19
33
|
declare const get_txs_by_address: (address: string, cursor?: string, pageSize?: number) => Promise<any>;
|
package/lib/index.js
CHANGED
|
@@ -52,6 +52,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
52
52
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
53
53
|
}
|
|
54
54
|
};
|
|
55
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
56
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
57
|
+
if (ar || !(i in from)) {
|
|
58
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
59
|
+
ar[i] = from[i];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
63
|
+
};
|
|
55
64
|
var TAG = " | mayachain-api | ";
|
|
56
65
|
var log = require('@pioneer-platform/loggerdog')();
|
|
57
66
|
var axiosLib = require('axios');
|
|
@@ -171,77 +180,102 @@ var get_info = function () {
|
|
|
171
180
|
};
|
|
172
181
|
var get_account_info = function (address) {
|
|
173
182
|
return __awaiter(this, void 0, void 0, function () {
|
|
174
|
-
var tag,
|
|
175
|
-
|
|
176
|
-
|
|
183
|
+
var tag, allEndpoints, results, successfulResults, mostRecentAccount;
|
|
184
|
+
var _this = this;
|
|
185
|
+
var _a, _b;
|
|
186
|
+
return __generator(this, function (_c) {
|
|
187
|
+
switch (_c.label) {
|
|
177
188
|
case 0:
|
|
178
189
|
tag = TAG + " | get_account_info | ";
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
190
|
+
allEndpoints = __spreadArray([
|
|
191
|
+
{
|
|
192
|
+
name: 'Unchained',
|
|
193
|
+
fetch: function () { return __awaiter(_this, void 0, void 0, function () {
|
|
194
|
+
var result, data;
|
|
195
|
+
return __generator(this, function (_a) {
|
|
196
|
+
switch (_a.label) {
|
|
197
|
+
case 0: return [4 /*yield*/, axiosInstance({
|
|
198
|
+
method: 'GET',
|
|
199
|
+
url: "".concat(UNCHAINED_API, "/api/v1/account/").concat(address),
|
|
200
|
+
timeout: 5000
|
|
201
|
+
})];
|
|
202
|
+
case 1:
|
|
203
|
+
result = _a.sent();
|
|
204
|
+
data = result.data;
|
|
205
|
+
return [2 /*return*/, {
|
|
206
|
+
account_number: String(data.accountNumber || '0'),
|
|
207
|
+
sequence: parseInt(data.sequence || '0'),
|
|
208
|
+
address: data.pubkey || address,
|
|
209
|
+
coins: data.assets || [],
|
|
210
|
+
public_key: null,
|
|
211
|
+
source: 'Unchained'
|
|
212
|
+
}];
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}); }
|
|
216
|
+
}
|
|
217
|
+
], MAYA_NODES.map(function (nodeUrl) { return ({
|
|
218
|
+
name: nodeUrl,
|
|
219
|
+
fetch: function () { return __awaiter(_this, void 0, void 0, function () {
|
|
220
|
+
var result, accountData;
|
|
221
|
+
return __generator(this, function (_a) {
|
|
222
|
+
switch (_a.label) {
|
|
223
|
+
case 0: return [4 /*yield*/, axiosInstance({
|
|
224
|
+
method: 'GET',
|
|
225
|
+
url: "".concat(nodeUrl, "/cosmos/auth/v1beta1/accounts/").concat(address),
|
|
226
|
+
timeout: 8000
|
|
227
|
+
})];
|
|
228
|
+
case 1:
|
|
229
|
+
result = _a.sent();
|
|
230
|
+
accountData = result.data.account || result.data;
|
|
231
|
+
return [2 /*return*/, {
|
|
232
|
+
account_number: String(accountData.account_number || '0'),
|
|
233
|
+
sequence: parseInt(accountData.sequence || '0'),
|
|
234
|
+
address: accountData.address || address,
|
|
235
|
+
coins: accountData.coins || [],
|
|
236
|
+
public_key: accountData.pub_key || null,
|
|
237
|
+
source: nodeUrl
|
|
238
|
+
}];
|
|
199
239
|
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
if (!(_i < MAYA_NODES_1.length)) return [3 /*break*/, 9];
|
|
208
|
-
nodeUrl = MAYA_NODES_1[_i];
|
|
209
|
-
_a.label = 5;
|
|
210
|
-
case 5:
|
|
211
|
-
_a.trys.push([5, 7, , 8]);
|
|
212
|
-
log.debug(tag, "Trying node: ".concat(nodeUrl));
|
|
213
|
-
return [4 /*yield*/, axiosInstance({
|
|
214
|
-
method: 'GET',
|
|
215
|
-
url: "".concat(nodeUrl, "/cosmos/auth/v1beta1/accounts/").concat(address),
|
|
216
|
-
timeout: 10000
|
|
217
|
-
})
|
|
218
|
-
// Cosmos SDK returns { account: {...} } - wrap for compatibility
|
|
240
|
+
});
|
|
241
|
+
}); }
|
|
242
|
+
}); }), true);
|
|
243
|
+
return [4 /*yield*/, Promise.allSettled(allEndpoints.map(function (endpoint) {
|
|
244
|
+
return endpoint.fetch().then(function (data) { return (__assign(__assign({}, data), { endpoint: endpoint.name })); });
|
|
245
|
+
}))
|
|
246
|
+
// Extract successful responses
|
|
219
247
|
];
|
|
220
|
-
case
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
248
|
+
case 1:
|
|
249
|
+
results = _c.sent();
|
|
250
|
+
successfulResults = results
|
|
251
|
+
.filter(function (result) { return result.status === 'fulfilled'; })
|
|
252
|
+
.map(function (result) { return result.value; });
|
|
253
|
+
if (successfulResults.length === 0) {
|
|
254
|
+
log.error(tag, "All endpoints failed");
|
|
255
|
+
throw new Error("All endpoints failed for account info");
|
|
256
|
+
}
|
|
257
|
+
// Log all sequence numbers for debugging
|
|
258
|
+
log.info(tag, "Sequence numbers from all nodes:", successfulResults.map(function (r) { return "".concat(r.source, ": ").concat(r.sequence); }).join(', '));
|
|
259
|
+
mostRecentAccount = successfulResults.reduce(function (max, current) {
|
|
260
|
+
return current.sequence > max.sequence ? current : max;
|
|
261
|
+
});
|
|
262
|
+
log.info(tag, "Using account info from ".concat(mostRecentAccount.source, " with sequence ").concat(mostRecentAccount.sequence));
|
|
263
|
+
// Return in expected format
|
|
264
|
+
return [2 /*return*/, {
|
|
265
|
+
result: {
|
|
226
266
|
value: {
|
|
227
|
-
account_number:
|
|
228
|
-
sequence: String(
|
|
229
|
-
address:
|
|
230
|
-
coins:
|
|
231
|
-
public_key:
|
|
267
|
+
account_number: mostRecentAccount.account_number,
|
|
268
|
+
sequence: String(mostRecentAccount.sequence),
|
|
269
|
+
address: mostRecentAccount.address,
|
|
270
|
+
coins: mostRecentAccount.coins,
|
|
271
|
+
public_key: mostRecentAccount.public_key
|
|
232
272
|
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
_i++;
|
|
240
|
-
return [3 /*break*/, 4];
|
|
241
|
-
case 9:
|
|
242
|
-
log.error(tag, "All endpoints failed");
|
|
243
|
-
throw new Error("All endpoints failed for account info");
|
|
244
|
-
case 10: return [2 /*return*/];
|
|
273
|
+
},
|
|
274
|
+
accountNumber: mostRecentAccount.account_number,
|
|
275
|
+
sequence: String(mostRecentAccount.sequence),
|
|
276
|
+
balance: ((_b = (_a = mostRecentAccount.coins) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.amount) || '0',
|
|
277
|
+
assets: mostRecentAccount.coins || []
|
|
278
|
+
}];
|
|
245
279
|
}
|
|
246
280
|
});
|
|
247
281
|
});
|
|
@@ -385,7 +419,7 @@ var get_transaction = function (txid) {
|
|
|
385
419
|
};
|
|
386
420
|
var broadcast_transaction = function (tx) {
|
|
387
421
|
return __awaiter(this, void 0, void 0, function () {
|
|
388
|
-
var tag, output, payload, result,
|
|
422
|
+
var tag, output, payload, result, unchainedError_1, nodeErrors, _i, MAYA_NODES_1, nodeUrl, nodeResult, txResponse, nodeError, nodeError_1, errorTxResponse, nodeErrorMsg;
|
|
389
423
|
var _a, _b, _c, _d, _e, _f;
|
|
390
424
|
return __generator(this, function (_g) {
|
|
391
425
|
switch (_g.label) {
|
|
@@ -422,32 +456,32 @@ var broadcast_transaction = function (tx) {
|
|
|
422
456
|
}
|
|
423
457
|
return [3 /*break*/, 4];
|
|
424
458
|
case 3:
|
|
425
|
-
|
|
426
|
-
log.warn(tag, "Unchained API failed:",
|
|
427
|
-
if ((_a =
|
|
428
|
-
log.error(tag, "Unchained FULL response data:", JSON.stringify(
|
|
459
|
+
unchainedError_1 = _g.sent();
|
|
460
|
+
log.warn(tag, "Unchained API failed:", unchainedError_1.message);
|
|
461
|
+
if ((_a = unchainedError_1.response) === null || _a === void 0 ? void 0 : _a.data) {
|
|
462
|
+
log.error(tag, "Unchained FULL response data:", JSON.stringify(unchainedError_1.response.data, null, 2));
|
|
429
463
|
// Check if error response contains a txhash - means broadcast succeeded despite error
|
|
430
|
-
if (
|
|
431
|
-
output.txid =
|
|
464
|
+
if (unchainedError_1.response.data.txid || unchainedError_1.response.data.txHash) {
|
|
465
|
+
output.txid = unchainedError_1.response.data.txid || unchainedError_1.response.data.txHash;
|
|
432
466
|
output.success = true;
|
|
433
467
|
output.endpoint = 'Unchained';
|
|
434
|
-
log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error: ".concat(
|
|
468
|
+
log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error: ".concat(unchainedError_1.response.data.message || unchainedError_1.message));
|
|
435
469
|
log.info(tag, "\u2705 Broadcast SUCCESS via Unchained - txid: ".concat(output.txid));
|
|
436
470
|
return [2 /*return*/, output];
|
|
437
471
|
}
|
|
438
|
-
output.error =
|
|
472
|
+
output.error = unchainedError_1.response.data.message || unchainedError_1.response.data.error || unchainedError_1.message;
|
|
439
473
|
}
|
|
440
474
|
else {
|
|
441
|
-
output.error =
|
|
475
|
+
output.error = unchainedError_1.message;
|
|
442
476
|
}
|
|
443
477
|
return [3 /*break*/, 4];
|
|
444
478
|
case 4:
|
|
445
479
|
nodeErrors = [];
|
|
446
|
-
_i = 0,
|
|
480
|
+
_i = 0, MAYA_NODES_1 = MAYA_NODES;
|
|
447
481
|
_g.label = 5;
|
|
448
482
|
case 5:
|
|
449
|
-
if (!(_i <
|
|
450
|
-
nodeUrl =
|
|
483
|
+
if (!(_i < MAYA_NODES_1.length)) return [3 /*break*/, 10];
|
|
484
|
+
nodeUrl = MAYA_NODES_1[_i];
|
|
451
485
|
_g.label = 6;
|
|
452
486
|
case 6:
|
|
453
487
|
_g.trys.push([6, 8, , 9]);
|
|
@@ -486,22 +520,22 @@ var broadcast_transaction = function (tx) {
|
|
|
486
520
|
}
|
|
487
521
|
return [3 /*break*/, 9];
|
|
488
522
|
case 8:
|
|
489
|
-
|
|
523
|
+
nodeError_1 = _g.sent();
|
|
490
524
|
// Log the full response for debugging
|
|
491
|
-
if ((_b =
|
|
492
|
-
log.error(tag, "Node ".concat(nodeUrl, " FULL response data:"), JSON.stringify(
|
|
493
|
-
errorTxResponse =
|
|
525
|
+
if ((_b = nodeError_1.response) === null || _b === void 0 ? void 0 : _b.data) {
|
|
526
|
+
log.error(tag, "Node ".concat(nodeUrl, " FULL response data:"), JSON.stringify(nodeError_1.response.data, null, 2));
|
|
527
|
+
errorTxResponse = nodeError_1.response.data.tx_response || nodeError_1.response.data;
|
|
494
528
|
if (errorTxResponse.txhash || errorTxResponse.hash) {
|
|
495
529
|
output.txid = errorTxResponse.txhash || errorTxResponse.hash;
|
|
496
530
|
output.success = true;
|
|
497
531
|
output.endpoint = nodeUrl;
|
|
498
532
|
output.code = errorTxResponse.code;
|
|
499
|
-
log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error (code ".concat(errorTxResponse.code, "): ").concat(errorTxResponse.raw_log ||
|
|
533
|
+
log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error (code ".concat(errorTxResponse.code, "): ").concat(errorTxResponse.raw_log || nodeError_1.message));
|
|
500
534
|
log.info(tag, "\u2705 Broadcast SUCCESS via ".concat(nodeUrl, " - txid: ").concat(output.txid));
|
|
501
535
|
return [2 /*return*/, output];
|
|
502
536
|
}
|
|
503
537
|
}
|
|
504
|
-
nodeErrorMsg = ((_d = (_c =
|
|
538
|
+
nodeErrorMsg = ((_d = (_c = nodeError_1.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.raw_log) || ((_f = (_e = nodeError_1.response) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.message) || nodeError_1.message;
|
|
505
539
|
nodeErrors.push("".concat(nodeUrl, ": ").concat(nodeErrorMsg));
|
|
506
540
|
log.warn(tag, "Node ".concat(nodeUrl, " failed:"), nodeErrorMsg);
|
|
507
541
|
return [3 /*break*/, 9];
|