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.

balances.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. const utils = __importStar(require("./utils"));
  23. const common_1 = require("../common");
  24. function getTrustlineBalanceAmount(trustline) {
  25. return {
  26. currency: trustline.specification.currency,
  27. counterparty: trustline.specification.counterparty,
  28. value: trustline.state.balance
  29. };
  30. }
  31. function formatBalances(options, balances) {
  32. const result = balances.trustlines.map(getTrustlineBalanceAmount);
  33. if (!(options.counterparty || (options.currency && options.currency !== 'XRP'))) {
  34. const xrpBalance = {
  35. currency: 'XRP',
  36. value: balances.xrp
  37. };
  38. result.unshift(xrpBalance);
  39. }
  40. if (options.limit && result.length > options.limit) {
  41. const toRemove = result.length - options.limit;
  42. result.splice(-toRemove, toRemove);
  43. }
  44. return result;
  45. }
  46. function getLedgerVersionHelper(connection, optionValue) {
  47. if (optionValue != null && optionValue !== null) {
  48. return Promise.resolve(optionValue);
  49. }
  50. return connection.getLedgerVersion();
  51. }
  52. function getBalances(address, options = {}) {
  53. common_1.validate.getTrustlines({ address, options });
  54. address = common_1.ensureClassicAddress(address);
  55. return Promise.all([
  56. getLedgerVersionHelper(this.connection, options.ledgerVersion).then((ledgerVersion) => utils.getXRPBalance(this.connection, address, ledgerVersion)),
  57. this.getTrustlines(address, options)
  58. ]).then((results) => formatBalances(options, { xrp: results[0], trustlines: results[1] }));
  59. }
  60. exports.default = getBalances;
  61. //# sourceMappingURL=balances.js.map