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.

pathfind.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. var __importDefault = (this && this.__importDefault) || function (mod) {
  22. return (mod && mod.__esModule) ? mod : { "default": mod };
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. const _ = __importStar(require("lodash"));
  26. const amount_1 = __importDefault(require("./amount"));
  27. function parsePaths(paths) {
  28. return paths.map((steps) => steps.map((step) => _.omit(step, ['type', 'type_hex'])));
  29. }
  30. function removeAnyCounterpartyEncoding(address, amount) {
  31. return amount.counterparty === address
  32. ? _.omit(amount, 'counterparty')
  33. : amount;
  34. }
  35. function createAdjustment(address, adjustmentWithoutAddress) {
  36. const amountKey = Object.keys(adjustmentWithoutAddress)[0];
  37. const amount = adjustmentWithoutAddress[amountKey];
  38. return _.set({ address: address }, amountKey, removeAnyCounterpartyEncoding(address, amount));
  39. }
  40. function parseAlternative(sourceAddress, destinationAddress, destinationAmount, alternative) {
  41. const amounts = alternative.destination_amount != null
  42. ? {
  43. source: { amount: amount_1.default(alternative.source_amount) },
  44. destination: { minAmount: amount_1.default(alternative.destination_amount) }
  45. }
  46. : {
  47. source: { maxAmount: amount_1.default(alternative.source_amount) },
  48. destination: { amount: amount_1.default(destinationAmount) }
  49. };
  50. return {
  51. source: createAdjustment(sourceAddress, amounts.source),
  52. destination: createAdjustment(destinationAddress, amounts.destination),
  53. paths: JSON.stringify(parsePaths(alternative.paths_computed))
  54. };
  55. }
  56. function parsePathfind(pathfindResult) {
  57. const sourceAddress = pathfindResult.source_account;
  58. const destinationAddress = pathfindResult.destination_account;
  59. const destinationAmount = pathfindResult.destination_amount;
  60. return pathfindResult.alternatives.map((alt) => parseAlternative(sourceAddress, destinationAddress, destinationAmount, alt));
  61. }
  62. exports.default = parsePathfind;
  63. //# sourceMappingURL=pathfind.js.map