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.

partialPayment.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.handleStreamPartialPayment = exports.handlePartialPayment = void 0;
  7. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  8. const ripple_binary_codec_1 = require("ripple-binary-codec");
  9. const transactions_1 = require("../models/transactions");
  10. const utils_1 = require("../models/utils");
  11. const WARN_PARTIAL_PAYMENT_CODE = 2001;
  12. function amountsEqual(amt1, amt2) {
  13. if (typeof amt1 === 'string' && typeof amt2 === 'string') {
  14. return amt1 === amt2;
  15. }
  16. if (typeof amt1 === 'string' || typeof amt2 === 'string') {
  17. return false;
  18. }
  19. const aValue = new bignumber_js_1.default(amt1.value);
  20. const bValue = new bignumber_js_1.default(amt2.value);
  21. return (amt1.currency === amt2.currency &&
  22. amt1.issuer === amt2.issuer &&
  23. aValue.isEqualTo(bValue));
  24. }
  25. function isPartialPayment(tx, metadata) {
  26. var _a;
  27. if (tx == null || metadata == null || tx.TransactionType !== 'Payment') {
  28. return false;
  29. }
  30. let meta = metadata;
  31. if (typeof meta === 'string') {
  32. if (meta === 'unavailable') {
  33. return false;
  34. }
  35. meta = (0, ripple_binary_codec_1.decode)(meta);
  36. }
  37. const tfPartial = typeof tx.Flags === 'number'
  38. ? (0, utils_1.isFlagEnabled)(tx.Flags, transactions_1.PaymentFlags.tfPartialPayment)
  39. : (_a = tx.Flags) === null || _a === void 0 ? void 0 : _a.tfPartialPayment;
  40. if (!tfPartial) {
  41. return false;
  42. }
  43. const delivered = meta.delivered_amount;
  44. const amount = tx.Amount;
  45. if (delivered === undefined) {
  46. return false;
  47. }
  48. return !amountsEqual(delivered, amount);
  49. }
  50. function txHasPartialPayment(response) {
  51. return isPartialPayment(response.result, response.result.meta);
  52. }
  53. function txEntryHasPartialPayment(response) {
  54. return isPartialPayment(response.result.tx_json, response.result.metadata);
  55. }
  56. function accountTxHasPartialPayment(response) {
  57. const { transactions } = response.result;
  58. const foo = transactions.some((tx) => isPartialPayment(tx.tx, tx.meta));
  59. return foo;
  60. }
  61. function hasPartialPayment(command, response) {
  62. switch (command) {
  63. case 'tx':
  64. return txHasPartialPayment(response);
  65. case 'transaction_entry':
  66. return txEntryHasPartialPayment(response);
  67. case 'account_tx':
  68. return accountTxHasPartialPayment(response);
  69. default:
  70. return false;
  71. }
  72. }
  73. function handlePartialPayment(command, response) {
  74. var _a;
  75. if (hasPartialPayment(command, response)) {
  76. const warnings = (_a = response.warnings) !== null && _a !== void 0 ? _a : [];
  77. const warning = {
  78. id: WARN_PARTIAL_PAYMENT_CODE,
  79. message: 'This response contains a Partial Payment',
  80. };
  81. warnings.push(warning);
  82. response.warnings = warnings;
  83. }
  84. }
  85. exports.handlePartialPayment = handlePartialPayment;
  86. function handleStreamPartialPayment(stream, log) {
  87. var _a;
  88. if (isPartialPayment(stream.transaction, stream.meta)) {
  89. const warnings = (_a = stream.warnings) !== null && _a !== void 0 ? _a : [];
  90. const warning = {
  91. id: WARN_PARTIAL_PAYMENT_CODE,
  92. message: 'This response contains a Partial Payment',
  93. };
  94. warnings.push(warning);
  95. stream.warnings = warnings;
  96. log('Partial payment received', JSON.stringify(stream));
  97. }
  98. }
  99. exports.handleStreamPartialPayment = handleStreamPartialPayment;
  100. //# sourceMappingURL=partialPayment.js.map