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.

getBalanceChanges.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  7. const flatten_1 = __importDefault(require("lodash/flatten"));
  8. const groupBy_1 = __importDefault(require("lodash/groupBy"));
  9. const xrpConversion_1 = require("./xrpConversion");
  10. function normalizeNode(affectedNode) {
  11. const diffType = Object.keys(affectedNode)[0];
  12. const node = affectedNode[diffType];
  13. return Object.assign(Object.assign({}, node), { NodeType: diffType, LedgerEntryType: node.LedgerEntryType, LedgerIndex: node.LedgerIndex, NewFields: node.NewFields, FinalFields: node.FinalFields, PreviousFields: node.PreviousFields });
  14. }
  15. function normalizeNodes(metadata) {
  16. if (metadata.AffectedNodes.length === 0) {
  17. return [];
  18. }
  19. return metadata.AffectedNodes.map(normalizeNode);
  20. }
  21. function groupByAccount(balanceChanges) {
  22. const grouped = (0, groupBy_1.default)(balanceChanges, (node) => node.account);
  23. return Object.entries(grouped).map(([account, items]) => {
  24. return { account, balances: items.map((item) => item.balance) };
  25. });
  26. }
  27. function getValue(balance) {
  28. if (typeof balance === 'string') {
  29. return new bignumber_js_1.default(balance);
  30. }
  31. return new bignumber_js_1.default(balance.value);
  32. }
  33. function computeBalanceChange(node) {
  34. var _a, _b, _c;
  35. let value = null;
  36. if ((_a = node.NewFields) === null || _a === void 0 ? void 0 : _a.Balance) {
  37. value = getValue(node.NewFields.Balance);
  38. }
  39. else if (((_b = node.PreviousFields) === null || _b === void 0 ? void 0 : _b.Balance) && ((_c = node.FinalFields) === null || _c === void 0 ? void 0 : _c.Balance)) {
  40. value = getValue(node.FinalFields.Balance).minus(getValue(node.PreviousFields.Balance));
  41. }
  42. if (value === null || value.isZero()) {
  43. return null;
  44. }
  45. return value;
  46. }
  47. function getXRPQuantity(node) {
  48. var _a, _b, _c;
  49. const value = computeBalanceChange(node);
  50. if (value === null) {
  51. return null;
  52. }
  53. return {
  54. account: ((_b = (_a = node.FinalFields) === null || _a === void 0 ? void 0 : _a.Account) !== null && _b !== void 0 ? _b : (_c = node.NewFields) === null || _c === void 0 ? void 0 : _c.Account),
  55. balance: {
  56. currency: 'XRP',
  57. value: (0, xrpConversion_1.dropsToXrp)(value).toString(),
  58. },
  59. };
  60. }
  61. function flipTrustlinePerspective(balanceChange) {
  62. const negatedBalance = new bignumber_js_1.default(balanceChange.balance.value).negated();
  63. return {
  64. account: balanceChange.balance.issuer,
  65. balance: {
  66. issuer: balanceChange.account,
  67. currency: balanceChange.balance.currency,
  68. value: negatedBalance.toString(),
  69. },
  70. };
  71. }
  72. function getTrustlineQuantity(node) {
  73. var _a, _b;
  74. const value = computeBalanceChange(node);
  75. if (value === null) {
  76. return null;
  77. }
  78. const fields = node.NewFields == null ? node.FinalFields : node.NewFields;
  79. const result = {
  80. account: (_a = fields === null || fields === void 0 ? void 0 : fields.LowLimit) === null || _a === void 0 ? void 0 : _a.issuer,
  81. balance: {
  82. issuer: (_b = fields === null || fields === void 0 ? void 0 : fields.HighLimit) === null || _b === void 0 ? void 0 : _b.issuer,
  83. currency: (fields === null || fields === void 0 ? void 0 : fields.Balance).currency,
  84. value: value.toString(),
  85. },
  86. };
  87. return [result, flipTrustlinePerspective(result)];
  88. }
  89. function getBalanceChanges(metadata) {
  90. const quantities = normalizeNodes(metadata).map((node) => {
  91. if (node.LedgerEntryType === 'AccountRoot') {
  92. const xrpQuantity = getXRPQuantity(node);
  93. if (xrpQuantity == null) {
  94. return [];
  95. }
  96. return [xrpQuantity];
  97. }
  98. if (node.LedgerEntryType === 'RippleState') {
  99. const trustlineQuantity = getTrustlineQuantity(node);
  100. if (trustlineQuantity == null) {
  101. return [];
  102. }
  103. return trustlineQuantity;
  104. }
  105. return [];
  106. });
  107. return groupByAccount((0, flatten_1.default)(quantities));
  108. }
  109. exports.default = getBalanceChanges;
  110. //# sourceMappingURL=getBalanceChanges.js.map