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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.common = exports.clamp = exports.isPendingLedgerVersion = exports.hasCompleteLedgerRange = exports.getRecursive = exports.renameCounterpartyToIssuerInOrder = exports.renameCounterpartyToIssuer = exports.compareTransactions = exports.ensureLedgerVersion = exports.getXRPBalance = void 0;
  23. const _ = __importStar(require("lodash"));
  24. const assert = __importStar(require("assert"));
  25. const common = __importStar(require("../common"));
  26. exports.common = common;
  27. function clamp(value, min, max) {
  28. assert.ok(min <= max, 'Illegal clamp bounds');
  29. return Math.min(Math.max(value, min), max);
  30. }
  31. exports.clamp = clamp;
  32. function getXRPBalance(connection, address, ledgerVersion) {
  33. const request = {
  34. command: 'account_info',
  35. account: address,
  36. ledger_index: ledgerVersion
  37. };
  38. return connection
  39. .request(request)
  40. .then((data) => common.dropsToXrp(data.account_data.Balance));
  41. }
  42. exports.getXRPBalance = getXRPBalance;
  43. function getRecursiveRecur(getter, marker, limit) {
  44. return getter(marker, limit).then((data) => {
  45. const remaining = limit - data.results.length;
  46. if (remaining > 0 && data.marker != null) {
  47. return getRecursiveRecur(getter, data.marker, remaining).then((results) => data.results.concat(results));
  48. }
  49. return data.results.slice(0, limit);
  50. });
  51. }
  52. function getRecursive(getter, limit) {
  53. return getRecursiveRecur(getter, undefined, limit || Infinity);
  54. }
  55. exports.getRecursive = getRecursive;
  56. function renameCounterpartyToIssuer(obj) {
  57. const issuer = obj.counterparty != null
  58. ? obj.counterparty
  59. : obj.issuer != null
  60. ? obj.issuer
  61. : undefined;
  62. const withIssuer = Object.assign({}, obj, { issuer });
  63. delete withIssuer.counterparty;
  64. return withIssuer;
  65. }
  66. exports.renameCounterpartyToIssuer = renameCounterpartyToIssuer;
  67. function renameCounterpartyToIssuerInOrder(order) {
  68. const taker_gets = renameCounterpartyToIssuer(order.taker_gets);
  69. const taker_pays = renameCounterpartyToIssuer(order.taker_pays);
  70. const changes = { taker_gets, taker_pays };
  71. return Object.assign({}, order, _.omitBy(changes, value => value == null));
  72. }
  73. exports.renameCounterpartyToIssuerInOrder = renameCounterpartyToIssuerInOrder;
  74. function signum(num) {
  75. return num === 0 ? 0 : num > 0 ? 1 : -1;
  76. }
  77. function compareTransactions(first, second) {
  78. if (!first.outcome || !second.outcome) {
  79. return 0;
  80. }
  81. if (first.outcome.ledgerVersion === second.outcome.ledgerVersion) {
  82. return signum(first.outcome.indexInLedger - second.outcome.indexInLedger);
  83. }
  84. return first.outcome.ledgerVersion < second.outcome.ledgerVersion ? -1 : 1;
  85. }
  86. exports.compareTransactions = compareTransactions;
  87. function hasCompleteLedgerRange(connection, minLedgerVersion, maxLedgerVersion) {
  88. const firstLedgerVersion = 32570;
  89. return connection.hasLedgerVersions(minLedgerVersion || firstLedgerVersion, maxLedgerVersion);
  90. }
  91. exports.hasCompleteLedgerRange = hasCompleteLedgerRange;
  92. function isPendingLedgerVersion(connection, maxLedgerVersion) {
  93. return connection
  94. .getLedgerVersion()
  95. .then((ledgerVersion) => ledgerVersion < (maxLedgerVersion || 0));
  96. }
  97. exports.isPendingLedgerVersion = isPendingLedgerVersion;
  98. function ensureLedgerVersion(options) {
  99. if (Boolean(options) &&
  100. options.ledgerVersion != null &&
  101. options.ledgerVersion !== null) {
  102. return Promise.resolve(options);
  103. }
  104. return this.getLedgerVersion().then((ledgerVersion) => Object.assign({}, options, { ledgerVersion }));
  105. }
  106. exports.ensureLedgerVersion = ensureLedgerVersion;
  107. //# sourceMappingURL=utils.js.map