You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transaction.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  22. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  23. return new (P || (P = Promise))(function (resolve, reject) {
  24. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  25. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  26. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  27. step((generator = generator.apply(thisArg, _arguments || [])).next());
  28. });
  29. };
  30. var __importDefault = (this && this.__importDefault) || function (mod) {
  31. return (mod && mod.__esModule) ? mod : { "default": mod };
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. const utils = __importStar(require("./utils"));
  35. const transaction_1 = __importDefault(require("./parse/transaction"));
  36. const common_1 = require("../common");
  37. function attachTransactionDate(connection, tx) {
  38. if (tx.date) {
  39. return Promise.resolve(tx);
  40. }
  41. const ledgerVersion = tx.ledger_index || tx.LedgerSequence;
  42. if (!ledgerVersion) {
  43. return new Promise(() => {
  44. const error = new common_1.errors.NotFoundError('Transaction has not been validated yet; try again later');
  45. error.data = {
  46. details: '(ledger_index and LedgerSequence not found in tx)'
  47. };
  48. throw error;
  49. });
  50. }
  51. const request = {
  52. command: 'ledger',
  53. ledger_index: ledgerVersion
  54. };
  55. return connection
  56. .request(request)
  57. .then((data) => {
  58. if (typeof data.ledger.close_time === 'number') {
  59. return Object.assign({ date: data.ledger.close_time }, tx);
  60. }
  61. throw new common_1.errors.UnexpectedError('Ledger missing close_time');
  62. })
  63. .catch((error) => {
  64. if (error instanceof common_1.errors.UnexpectedError) {
  65. throw error;
  66. }
  67. throw new common_1.errors.NotFoundError('Transaction ledger not found');
  68. });
  69. }
  70. function isTransactionInRange(tx, options) {
  71. return ((!options.minLedgerVersion ||
  72. tx.ledger_index >= options.minLedgerVersion) &&
  73. (!options.maxLedgerVersion || tx.ledger_index <= options.maxLedgerVersion));
  74. }
  75. function convertError(connection, options, error) {
  76. let shouldUseNotFoundError = false;
  77. if ((error.data && error.data.error === 'txnNotFound') ||
  78. error.message === 'txnNotFound') {
  79. shouldUseNotFoundError = true;
  80. }
  81. const _error = shouldUseNotFoundError
  82. ? new common_1.errors.NotFoundError('Transaction not found')
  83. : error;
  84. if (_error instanceof common_1.errors.NotFoundError) {
  85. return utils
  86. .hasCompleteLedgerRange(connection, options.minLedgerVersion, options.maxLedgerVersion)
  87. .then((hasCompleteLedgerRange) => {
  88. if (!hasCompleteLedgerRange) {
  89. return utils
  90. .isPendingLedgerVersion(connection, options.maxLedgerVersion)
  91. .then((isPendingLedgerVersion) => {
  92. return isPendingLedgerVersion
  93. ? new common_1.errors.PendingLedgerVersionError()
  94. : new common_1.errors.MissingLedgerHistoryError();
  95. });
  96. }
  97. return _error;
  98. });
  99. }
  100. return Promise.resolve(_error);
  101. }
  102. function formatResponse(options, tx) {
  103. if (tx.validated !== true || !isTransactionInRange(tx, options)) {
  104. throw new common_1.errors.NotFoundError('Transaction not found');
  105. }
  106. return transaction_1.default(tx, options.includeRawTransaction);
  107. }
  108. function getTransaction(id, options = {}) {
  109. return __awaiter(this, void 0, void 0, function* () {
  110. common_1.validate.getTransaction({ id, options });
  111. const _options = yield utils.ensureLedgerVersion.call(this, options);
  112. try {
  113. const tx = yield this.request('tx', {
  114. transaction: id,
  115. binary: false
  116. });
  117. const txWithDate = yield attachTransactionDate(this.connection, tx);
  118. return formatResponse(_options, txWithDate);
  119. }
  120. catch (error) {
  121. throw yield convertError(this.connection, _options, error);
  122. }
  123. });
  124. }
  125. exports.default = getTransaction;
  126. //# sourceMappingURL=transaction.js.map