@pioneer-platform/maya-network 8.13.1 → 8.13.3

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @pioneer-platform/maya-network
2
2
 
3
+ ## 8.13.3
4
+
5
+ ### Patch Changes
6
+
7
+ - fix(mayachain): implement consensus-based account sequence from all nodes
8
+
9
+ ## 8.13.2
10
+
11
+ ### Patch Changes
12
+
13
+ - fix(mayachain): implement consensus-based account sequence from all nodes
14
+
3
15
  ## 8.13.1
4
16
 
5
17
  ### Patch Changes
package/lib/index.d.ts CHANGED
@@ -13,11 +13,25 @@ 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<any>;
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>;
20
- declare const get_transaction: (txid: string) => Promise<never>;
34
+ declare const get_transaction: (txid: string) => Promise<any>;
21
35
  declare const broadcast_transaction: (tx: string) => Promise<any>;
22
36
  declare const get_pool: (poolId: string) => Promise<any>;
23
37
  declare const get_pools: () => 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,84 +180,127 @@ 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, result, unchainedData, unchainedError_1, _i, MAYA_NODES_1, nodeUrl, nodeResult, accountData, nodeError_1;
183
+ var tag, allEndpoints, results, successfulResults, mostRecentAccount, cacaoCoin, cacaoBalance;
184
+ var _this = this;
175
185
  return __generator(this, function (_a) {
176
186
  switch (_a.label) {
177
187
  case 0:
178
188
  tag = TAG + " | get_account_info | ";
179
- _a.label = 1;
180
- case 1:
181
- _a.trys.push([1, 3, , 10]);
182
- return [4 /*yield*/, axiosInstance({
183
- method: 'GET',
184
- url: "".concat(UNCHAINED_API, "/api/v1/account/").concat(address),
185
- timeout: 5000
186
- })
187
- // Unchained returns flat structure, wrap it for backward compatibility
188
- ];
189
- case 2:
190
- result = _a.sent();
191
- unchainedData = result.data;
192
- return [2 /*return*/, __assign({ result: {
193
- value: {
194
- account_number: String(unchainedData.accountNumber || '0'),
195
- sequence: String(unchainedData.sequence || '0'),
196
- address: unchainedData.pubkey || address,
197
- coins: unchainedData.assets || [],
198
- public_key: null // Not provided by Unchained
189
+ allEndpoints = __spreadArray([
190
+ {
191
+ name: 'Unchained',
192
+ fetch: function () { return __awaiter(_this, void 0, void 0, function () {
193
+ var result, data;
194
+ return __generator(this, function (_a) {
195
+ switch (_a.label) {
196
+ case 0: return [4 /*yield*/, axiosInstance({
197
+ method: 'GET',
198
+ url: "".concat(UNCHAINED_API, "/api/v1/account/").concat(address),
199
+ timeout: 5000
200
+ })];
201
+ case 1:
202
+ result = _a.sent();
203
+ data = result.data;
204
+ return [2 /*return*/, {
205
+ account_number: String(data.accountNumber || '0'),
206
+ sequence: parseInt(data.sequence || '0'),
207
+ address: data.pubkey || address,
208
+ coins: data.assets || [],
209
+ public_key: null,
210
+ source: 'Unchained'
211
+ }];
212
+ }
213
+ });
214
+ }); }
215
+ }
216
+ ], MAYA_NODES.map(function (nodeUrl) { return ({
217
+ name: nodeUrl,
218
+ fetch: function () { return __awaiter(_this, void 0, void 0, function () {
219
+ var accountResult, accountData, coins, balanceResult, e_2;
220
+ return __generator(this, function (_a) {
221
+ switch (_a.label) {
222
+ case 0: return [4 /*yield*/, axiosInstance({
223
+ method: 'GET',
224
+ url: "".concat(nodeUrl, "/cosmos/auth/v1beta1/accounts/").concat(address),
225
+ timeout: 8000
226
+ })];
227
+ case 1:
228
+ accountResult = _a.sent();
229
+ accountData = accountResult.data.account || accountResult.data;
230
+ coins = [];
231
+ _a.label = 2;
232
+ case 2:
233
+ _a.trys.push([2, 4, , 5]);
234
+ return [4 /*yield*/, axiosInstance({
235
+ method: 'GET',
236
+ url: "".concat(nodeUrl, "/cosmos/bank/v1beta1/balances/").concat(address),
237
+ timeout: 8000
238
+ })];
239
+ case 3:
240
+ balanceResult = _a.sent();
241
+ coins = balanceResult.data.balances || [];
242
+ return [3 /*break*/, 5];
243
+ case 4:
244
+ e_2 = _a.sent();
245
+ log.warn(tag, "Failed to fetch balances from ".concat(nodeUrl, ": ").concat(e_2.message));
246
+ return [3 /*break*/, 5];
247
+ case 5: return [2 /*return*/, {
248
+ account_number: String(accountData.account_number || '0'),
249
+ sequence: parseInt(accountData.sequence || '0'),
250
+ address: accountData.address || address,
251
+ coins: coins,
252
+ public_key: accountData.pub_key || null,
253
+ source: nodeUrl
254
+ }];
199
255
  }
200
- } }, unchainedData)];
201
- case 3:
202
- unchainedError_1 = _a.sent();
203
- log.warn(tag, "Unchained API failed, trying node fallback:", unchainedError_1.message);
204
- _i = 0, MAYA_NODES_1 = MAYA_NODES;
205
- _a.label = 4;
206
- case 4:
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
256
+ });
257
+ }); }
258
+ }); }), true);
259
+ return [4 /*yield*/, Promise.allSettled(allEndpoints.map(function (endpoint) {
260
+ return endpoint.fetch().then(function (data) { return (__assign(__assign({}, data), { endpoint: endpoint.name })); });
261
+ }))
262
+ // Extract successful responses
219
263
  ];
220
- case 6:
221
- nodeResult = _a.sent();
222
- accountData = nodeResult.data.account || nodeResult.data;
223
- log.info(tag, "Account info retrieved from ".concat(nodeUrl));
224
- // Convert Cosmos SDK format to our expected format
225
- return [2 /*return*/, __assign({ result: {
264
+ case 1:
265
+ results = _a.sent();
266
+ successfulResults = results
267
+ .filter(function (result) { return result.status === 'fulfilled'; })
268
+ .map(function (result) { return result.value; });
269
+ if (successfulResults.length === 0) {
270
+ log.error(tag, "All endpoints failed");
271
+ throw new Error("All endpoints failed for account info");
272
+ }
273
+ // Log all sequence numbers for debugging
274
+ log.info(tag, "Sequence numbers from all nodes:", successfulResults.map(function (r) { return "".concat(r.source, ": ").concat(r.sequence); }).join(', '));
275
+ mostRecentAccount = successfulResults.reduce(function (max, current) {
276
+ return current.sequence > max.sequence ? current : max;
277
+ });
278
+ log.info(tag, "Using account info from ".concat(mostRecentAccount.source, " with sequence ").concat(mostRecentAccount.sequence));
279
+ cacaoCoin = mostRecentAccount.coins.find(function (coin) { return coin.denom === 'cacao'; });
280
+ cacaoBalance = (cacaoCoin === null || cacaoCoin === void 0 ? void 0 : cacaoCoin.amount) || '0';
281
+ // Return in expected format
282
+ return [2 /*return*/, {
283
+ result: {
226
284
  value: {
227
- account_number: String(accountData.account_number || '0'),
228
- sequence: String(accountData.sequence || '0'),
229
- address: accountData.address || address,
230
- coins: accountData.coins || [],
231
- public_key: accountData.pub_key || null
285
+ account_number: mostRecentAccount.account_number,
286
+ sequence: String(mostRecentAccount.sequence),
287
+ address: mostRecentAccount.address,
288
+ coins: mostRecentAccount.coins,
289
+ public_key: mostRecentAccount.public_key
232
290
  }
233
- } }, accountData)];
234
- case 7:
235
- nodeError_1 = _a.sent();
236
- log.debug(tag, "Node ".concat(nodeUrl, " failed:"), nodeError_1.message);
237
- return [3 /*break*/, 8];
238
- case 8:
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*/];
291
+ },
292
+ accountNumber: mostRecentAccount.account_number,
293
+ sequence: String(mostRecentAccount.sequence),
294
+ balance: cacaoBalance,
295
+ assets: mostRecentAccount.coins || []
296
+ }];
245
297
  }
246
298
  });
247
299
  });
248
300
  };
249
301
  var get_balance = function (address) {
250
302
  return __awaiter(this, void 0, void 0, function () {
251
- var tag, accountInfo, balance, e_2;
303
+ var tag, accountInfo, balance, e_3;
252
304
  return __generator(this, function (_a) {
253
305
  switch (_a.label) {
254
306
  case 0:
@@ -265,9 +317,9 @@ var get_balance = function (address) {
265
317
  // Convert from base units to CACAO
266
318
  return [2 /*return*/, balance / BASE_MAYA];
267
319
  case 3:
268
- e_2 = _a.sent();
269
- log.error(tag, "Error:", e_2.message);
270
- throw e_2;
320
+ e_3 = _a.sent();
321
+ log.error(tag, "Error:", e_3.message);
322
+ throw e_3;
271
323
  case 4: return [2 /*return*/];
272
324
  }
273
325
  });
@@ -275,7 +327,7 @@ var get_balance = function (address) {
275
327
  };
276
328
  var get_balances = function (address) {
277
329
  return __awaiter(this, void 0, void 0, function () {
278
- var tag, accountInfo, output, cacaoBalance, _i, _a, asset, e_3;
330
+ var tag, accountInfo, output, cacaoBalance, _i, _a, asset, e_4;
279
331
  return __generator(this, function (_b) {
280
332
  switch (_b.label) {
281
333
  case 0:
@@ -324,9 +376,9 @@ var get_balances = function (address) {
324
376
  }
325
377
  return [2 /*return*/, output];
326
378
  case 3:
327
- e_3 = _b.sent();
328
- log.error(tag, "Error:", e_3.message);
329
- throw e_3;
379
+ e_4 = _b.sent();
380
+ log.error(tag, "Error:", e_4.message);
381
+ throw e_4;
330
382
  case 4: return [2 /*return*/];
331
383
  }
332
384
  });
@@ -334,7 +386,7 @@ var get_balances = function (address) {
334
386
  };
335
387
  var get_txs_by_address = function (address_1, cursor_1) {
336
388
  return __awaiter(this, arguments, void 0, function (address, cursor, pageSize) {
337
- var tag, params, result, e_4;
389
+ var tag, params, result, e_5;
338
390
  if (pageSize === void 0) { pageSize = 50; }
339
391
  return __generator(this, function (_a) {
340
392
  switch (_a.label) {
@@ -356,9 +408,9 @@ var get_txs_by_address = function (address_1, cursor_1) {
356
408
  result = _a.sent();
357
409
  return [2 /*return*/, result.data];
358
410
  case 3:
359
- e_4 = _a.sent();
360
- log.error(tag, "Error:", e_4.message);
361
- throw e_4;
411
+ e_5 = _a.sent();
412
+ log.error(tag, "Error:", e_5.message);
413
+ throw e_5;
362
414
  case 4: return [2 /*return*/];
363
415
  }
364
416
  });
@@ -366,26 +418,47 @@ var get_txs_by_address = function (address_1, cursor_1) {
366
418
  };
367
419
  var get_transaction = function (txid) {
368
420
  return __awaiter(this, void 0, void 0, function () {
369
- var tag;
421
+ var tag, _i, MAYA_NODES_1, nodeUrl, result, e_6, errorMsg;
370
422
  return __generator(this, function (_a) {
371
- tag = TAG + " | get_transaction | ";
372
- try {
373
- // Unchained doesn't have a direct tx lookup endpoint
374
- // Would need to implement via account txs or use Midgard/node fallback
375
- log.warn(tag, "Transaction lookup not implemented in Unchained API, use Midgard or node directly");
376
- throw new Error("Transaction lookup not available in Unchained API");
377
- }
378
- catch (e) {
379
- log.error(tag, "Error:", e.message);
380
- throw e;
423
+ switch (_a.label) {
424
+ case 0:
425
+ tag = TAG + " | get_transaction | ";
426
+ _i = 0, MAYA_NODES_1 = MAYA_NODES;
427
+ _a.label = 1;
428
+ case 1:
429
+ if (!(_i < MAYA_NODES_1.length)) return [3 /*break*/, 6];
430
+ nodeUrl = MAYA_NODES_1[_i];
431
+ _a.label = 2;
432
+ case 2:
433
+ _a.trys.push([2, 4, , 5]);
434
+ log.info(tag, "Trying to fetch tx from: ".concat(nodeUrl));
435
+ return [4 /*yield*/, axiosInstance({
436
+ method: 'GET',
437
+ url: "".concat(nodeUrl, "/cosmos/tx/v1beta1/txs/").concat(txid),
438
+ timeout: 8000
439
+ })];
440
+ case 3:
441
+ result = _a.sent();
442
+ log.info(tag, "Successfully fetched tx from ".concat(nodeUrl));
443
+ return [2 /*return*/, result.data];
444
+ case 4:
445
+ e_6 = _a.sent();
446
+ log.warn(tag, "Failed to fetch tx from ".concat(nodeUrl, ": ").concat(e_6.message));
447
+ return [3 /*break*/, 5];
448
+ case 5:
449
+ _i++;
450
+ return [3 /*break*/, 1];
451
+ case 6:
452
+ errorMsg = "Transaction lookup failed on all Maya nodes";
453
+ log.error(tag, errorMsg);
454
+ throw new Error(errorMsg);
381
455
  }
382
- return [2 /*return*/];
383
456
  });
384
457
  });
385
458
  };
386
459
  var broadcast_transaction = function (tx) {
387
460
  return __awaiter(this, void 0, void 0, function () {
388
- var tag, output, payload, result, unchainedError_2, nodeErrors, _i, MAYA_NODES_2, nodeUrl, nodeResult, txResponse, nodeError, nodeError_2, errorTxResponse, nodeErrorMsg;
461
+ var tag, output, payload, result, unchainedError_1, nodeErrors, _i, MAYA_NODES_2, nodeUrl, nodeResult, txResponse, nodeError, nodeError_1, errorTxResponse, nodeErrorMsg;
389
462
  var _a, _b, _c, _d, _e, _f;
390
463
  return __generator(this, function (_g) {
391
464
  switch (_g.label) {
@@ -422,23 +495,23 @@ var broadcast_transaction = function (tx) {
422
495
  }
423
496
  return [3 /*break*/, 4];
424
497
  case 3:
425
- unchainedError_2 = _g.sent();
426
- log.warn(tag, "Unchained API failed:", unchainedError_2.message);
427
- if ((_a = unchainedError_2.response) === null || _a === void 0 ? void 0 : _a.data) {
428
- log.error(tag, "Unchained FULL response data:", JSON.stringify(unchainedError_2.response.data, null, 2));
498
+ unchainedError_1 = _g.sent();
499
+ log.warn(tag, "Unchained API failed:", unchainedError_1.message);
500
+ if ((_a = unchainedError_1.response) === null || _a === void 0 ? void 0 : _a.data) {
501
+ log.error(tag, "Unchained FULL response data:", JSON.stringify(unchainedError_1.response.data, null, 2));
429
502
  // Check if error response contains a txhash - means broadcast succeeded despite error
430
- if (unchainedError_2.response.data.txid || unchainedError_2.response.data.txHash) {
431
- output.txid = unchainedError_2.response.data.txid || unchainedError_2.response.data.txHash;
503
+ if (unchainedError_1.response.data.txid || unchainedError_1.response.data.txHash) {
504
+ output.txid = unchainedError_1.response.data.txid || unchainedError_1.response.data.txHash;
432
505
  output.success = true;
433
506
  output.endpoint = 'Unchained';
434
- log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error: ".concat(unchainedError_2.response.data.message || unchainedError_2.message));
507
+ log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error: ".concat(unchainedError_1.response.data.message || unchainedError_1.message));
435
508
  log.info(tag, "\u2705 Broadcast SUCCESS via Unchained - txid: ".concat(output.txid));
436
509
  return [2 /*return*/, output];
437
510
  }
438
- output.error = unchainedError_2.response.data.message || unchainedError_2.response.data.error || unchainedError_2.message;
511
+ output.error = unchainedError_1.response.data.message || unchainedError_1.response.data.error || unchainedError_1.message;
439
512
  }
440
513
  else {
441
- output.error = unchainedError_2.message;
514
+ output.error = unchainedError_1.message;
442
515
  }
443
516
  return [3 /*break*/, 4];
444
517
  case 4:
@@ -486,22 +559,22 @@ var broadcast_transaction = function (tx) {
486
559
  }
487
560
  return [3 /*break*/, 9];
488
561
  case 8:
489
- nodeError_2 = _g.sent();
562
+ nodeError_1 = _g.sent();
490
563
  // Log the full response for debugging
491
- if ((_b = nodeError_2.response) === null || _b === void 0 ? void 0 : _b.data) {
492
- log.error(tag, "Node ".concat(nodeUrl, " FULL response data:"), JSON.stringify(nodeError_2.response.data, null, 2));
493
- errorTxResponse = nodeError_2.response.data.tx_response || nodeError_2.response.data;
564
+ if ((_b = nodeError_1.response) === null || _b === void 0 ? void 0 : _b.data) {
565
+ log.error(tag, "Node ".concat(nodeUrl, " FULL response data:"), JSON.stringify(nodeError_1.response.data, null, 2));
566
+ errorTxResponse = nodeError_1.response.data.tx_response || nodeError_1.response.data;
494
567
  if (errorTxResponse.txhash || errorTxResponse.hash) {
495
568
  output.txid = errorTxResponse.txhash || errorTxResponse.hash;
496
569
  output.success = true;
497
570
  output.endpoint = nodeUrl;
498
571
  output.code = errorTxResponse.code;
499
- log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error (code ".concat(errorTxResponse.code, "): ").concat(errorTxResponse.raw_log || nodeError_2.message));
572
+ log.warn(tag, "\u26A0\uFE0F Broadcast succeeded despite error (code ".concat(errorTxResponse.code, "): ").concat(errorTxResponse.raw_log || nodeError_1.message));
500
573
  log.info(tag, "\u2705 Broadcast SUCCESS via ".concat(nodeUrl, " - txid: ").concat(output.txid));
501
574
  return [2 /*return*/, output];
502
575
  }
503
576
  }
504
- nodeErrorMsg = ((_d = (_c = nodeError_2.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.raw_log) || ((_f = (_e = nodeError_2.response) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.message) || nodeError_2.message;
577
+ 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
578
  nodeErrors.push("".concat(nodeUrl, ": ").concat(nodeErrorMsg));
506
579
  log.warn(tag, "Node ".concat(nodeUrl, " failed:"), nodeErrorMsg);
507
580
  return [3 /*break*/, 9];
@@ -520,7 +593,7 @@ var broadcast_transaction = function (tx) {
520
593
  // Pool endpoints use Midgard (not available in Unchained)
521
594
  var get_pool = function (poolId) {
522
595
  return __awaiter(this, void 0, void 0, function () {
523
- var tag, params, result, e_5;
596
+ var tag, params, result, e_7;
524
597
  return __generator(this, function (_a) {
525
598
  switch (_a.label) {
526
599
  case 0:
@@ -542,9 +615,9 @@ var get_pool = function (poolId) {
542
615
  result = _a.sent();
543
616
  return [2 /*return*/, result.data];
544
617
  case 3:
545
- e_5 = _a.sent();
546
- log.error(tag, "Error:", e_5.message);
547
- throw e_5;
618
+ e_7 = _a.sent();
619
+ log.error(tag, "Error:", e_7.message);
620
+ throw e_7;
548
621
  case 4: return [2 /*return*/];
549
622
  }
550
623
  });
@@ -552,7 +625,7 @@ var get_pool = function (poolId) {
552
625
  };
553
626
  var get_pools = function () {
554
627
  return __awaiter(this, void 0, void 0, function () {
555
- var tag, result, e_6;
628
+ var tag, result, e_8;
556
629
  return __generator(this, function (_a) {
557
630
  switch (_a.label) {
558
631
  case 0:
@@ -569,9 +642,9 @@ var get_pools = function () {
569
642
  result = _a.sent();
570
643
  return [2 /*return*/, result.data];
571
644
  case 3:
572
- e_6 = _a.sent();
573
- log.error(tag, "Error:", e_6.message);
574
- throw e_6;
645
+ e_8 = _a.sent();
646
+ log.error(tag, "Error:", e_8.message);
647
+ throw e_8;
575
648
  case 4: return [2 /*return*/];
576
649
  }
577
650
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pioneer-platform/maya-network",
3
- "version": "8.13.1",
3
+ "version": "8.13.3",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "scripts": {