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.

utils.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. exports.isPartialPayment = exports.adjustQualityForXRP = exports.parseTimestamp = exports.hexToString = exports.parseMemos = exports.parseOutcome = exports.parseQuality = void 0;
  26. const ripple_lib_transactionparser_1 = __importDefault(require("ripple-lib-transactionparser"));
  27. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  28. const common = __importStar(require("../../common"));
  29. const amount_1 = __importDefault(require("./amount"));
  30. function adjustQualityForXRP(quality, takerGetsCurrency, takerPaysCurrency) {
  31. const numeratorShift = takerPaysCurrency === 'XRP' ? -6 : 0;
  32. const denominatorShift = takerGetsCurrency === 'XRP' ? -6 : 0;
  33. const shift = numeratorShift - denominatorShift;
  34. return shift === 0
  35. ? quality
  36. : new bignumber_js_1.default(quality).shiftedBy(shift).toString();
  37. }
  38. exports.adjustQualityForXRP = adjustQualityForXRP;
  39. function parseQuality(quality) {
  40. if (typeof quality !== 'number') {
  41. return undefined;
  42. }
  43. return new bignumber_js_1.default(quality).shiftedBy(-9).toNumber();
  44. }
  45. exports.parseQuality = parseQuality;
  46. function parseTimestamp(rippleTime) {
  47. if (typeof rippleTime !== 'number') {
  48. return undefined;
  49. }
  50. return common.rippleTimeToISO8601(rippleTime);
  51. }
  52. exports.parseTimestamp = parseTimestamp;
  53. function removeEmptyCounterparty(amount) {
  54. if (amount.counterparty === '') {
  55. delete amount.counterparty;
  56. }
  57. }
  58. function removeEmptyCounterpartyInBalanceChanges(balanceChanges) {
  59. Object.entries(balanceChanges).forEach(([_, changes]) => {
  60. changes.forEach(removeEmptyCounterparty);
  61. });
  62. }
  63. function removeEmptyCounterpartyInOrderbookChanges(orderbookChanges) {
  64. Object.entries(orderbookChanges).forEach(([_, changes]) => {
  65. changes.forEach((change) => {
  66. Object.entries(change).forEach(removeEmptyCounterparty);
  67. });
  68. });
  69. }
  70. function isPartialPayment(tx) {
  71. return (tx.Flags & common.txFlags.Payment.PartialPayment) !== 0;
  72. }
  73. exports.isPartialPayment = isPartialPayment;
  74. function parseDeliveredAmount(tx) {
  75. if (tx.TransactionType !== 'Payment' ||
  76. tx.meta.TransactionResult !== 'tesSUCCESS') {
  77. return undefined;
  78. }
  79. if (tx.meta.delivered_amount && tx.meta.delivered_amount === 'unavailable') {
  80. return undefined;
  81. }
  82. if (tx.meta.delivered_amount) {
  83. return amount_1.default(tx.meta.delivered_amount);
  84. }
  85. if (tx.meta.DeliveredAmount) {
  86. return amount_1.default(tx.meta.DeliveredAmount);
  87. }
  88. if (tx.Amount && !isPartialPayment(tx)) {
  89. return amount_1.default(tx.Amount);
  90. }
  91. if (tx.Amount && tx.ledger_index > 4594094) {
  92. return amount_1.default(tx.Amount);
  93. }
  94. return undefined;
  95. }
  96. function parseOutcome(tx) {
  97. const metadata = tx.meta || tx.metaData;
  98. if (!metadata) {
  99. return undefined;
  100. }
  101. const balanceChanges = ripple_lib_transactionparser_1.default.parseBalanceChanges(metadata);
  102. const orderbookChanges = ripple_lib_transactionparser_1.default.parseOrderbookChanges(metadata);
  103. const channelChanges = ripple_lib_transactionparser_1.default.parseChannelChanges(metadata);
  104. removeEmptyCounterpartyInBalanceChanges(balanceChanges);
  105. removeEmptyCounterpartyInOrderbookChanges(orderbookChanges);
  106. return common.removeUndefined({
  107. result: tx.meta.TransactionResult,
  108. timestamp: parseTimestamp(tx.date),
  109. fee: common.dropsToXrp(tx.Fee),
  110. balanceChanges: balanceChanges,
  111. orderbookChanges: orderbookChanges,
  112. channelChanges: channelChanges,
  113. ledgerVersion: tx.ledger_index,
  114. indexInLedger: tx.meta.TransactionIndex,
  115. deliveredAmount: parseDeliveredAmount(tx)
  116. });
  117. }
  118. exports.parseOutcome = parseOutcome;
  119. function hexToString(hex) {
  120. return hex ? Buffer.from(hex, 'hex').toString('utf-8') : undefined;
  121. }
  122. exports.hexToString = hexToString;
  123. function parseMemos(tx) {
  124. if (!Array.isArray(tx.Memos) || tx.Memos.length === 0) {
  125. return undefined;
  126. }
  127. return tx.Memos.map((m) => {
  128. return common.removeUndefined({
  129. type: m.Memo.parsed_memo_type || hexToString(m.Memo.MemoType),
  130. format: m.Memo.parsed_memo_format || hexToString(m.Memo.MemoFormat),
  131. data: m.Memo.parsed_memo_data || hexToString(m.Memo.MemoData)
  132. });
  133. });
  134. }
  135. exports.parseMemos = parseMemos;
  136. //# sourceMappingURL=utils.js.map