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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.getBalances = exports.getXrpBalance = void 0;
  16. const flatMap_1 = __importDefault(require("lodash/flatMap"));
  17. const utils_1 = require("../utils");
  18. function formatBalances(trustlines) {
  19. return trustlines.map((trustline) => ({
  20. value: trustline.balance,
  21. currency: trustline.currency,
  22. issuer: trustline.account,
  23. }));
  24. }
  25. function getXrpBalance(address, options = {}) {
  26. var _a;
  27. return __awaiter(this, void 0, void 0, function* () {
  28. const xrpRequest = {
  29. command: 'account_info',
  30. account: address,
  31. ledger_index: (_a = options.ledger_index) !== null && _a !== void 0 ? _a : 'validated',
  32. ledger_hash: options.ledger_hash,
  33. };
  34. const response = yield this.request(xrpRequest);
  35. return (0, utils_1.dropsToXrp)(response.result.account_data.Balance);
  36. });
  37. }
  38. exports.getXrpBalance = getXrpBalance;
  39. function getBalances(address, options = {}) {
  40. var _a;
  41. return __awaiter(this, void 0, void 0, function* () {
  42. const balances = [];
  43. let xrpPromise = Promise.resolve('');
  44. if (!options.peer) {
  45. xrpPromise = this.getXrpBalance(address, {
  46. ledger_hash: options.ledger_hash,
  47. ledger_index: options.ledger_index,
  48. });
  49. }
  50. const linesRequest = {
  51. command: 'account_lines',
  52. account: address,
  53. ledger_index: (_a = options.ledger_index) !== null && _a !== void 0 ? _a : 'validated',
  54. ledger_hash: options.ledger_hash,
  55. peer: options.peer,
  56. limit: options.limit,
  57. };
  58. const linesPromise = this.requestAll(linesRequest);
  59. yield Promise.all([xrpPromise, linesPromise]).then(([xrpBalance, linesResponses]) => {
  60. const accountLinesBalance = (0, flatMap_1.default)(linesResponses, (response) => formatBalances(response.result.lines));
  61. if (xrpBalance !== '') {
  62. balances.push({ currency: 'XRP', value: xrpBalance });
  63. }
  64. balances.push(...accountLinesBalance);
  65. });
  66. return balances.slice(0, options.limit);
  67. });
  68. }
  69. exports.getBalances = getBalances;
  70. //# sourceMappingURL=balances.js.map