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.

getOrderbook.js 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  16. const flatMap_1 = __importDefault(require("lodash/flatMap"));
  17. const errors_1 = require("../errors");
  18. const Offer_1 = require("../models/ledger/Offer");
  19. const DEFAULT_LIMIT = 20;
  20. function sortOffers(offers) {
  21. return offers.sort((offerA, offerB) => {
  22. var _a, _b;
  23. const qualityA = (_a = offerA.quality) !== null && _a !== void 0 ? _a : 0;
  24. const qualityB = (_b = offerB.quality) !== null && _b !== void 0 ? _b : 0;
  25. return new bignumber_js_1.default(qualityA).comparedTo(qualityB);
  26. });
  27. }
  28. const getOrderbookOptionsSet = new Set([
  29. 'limit',
  30. 'ledger_index',
  31. 'ledger_hash',
  32. 'taker',
  33. ]);
  34. function getOrderbook(takerPays, takerGets, options = {}) {
  35. var _a, _b;
  36. return __awaiter(this, void 0, void 0, function* () {
  37. Object.keys(options).forEach((key) => {
  38. if (!getOrderbookOptionsSet.has(key)) {
  39. throw new errors_1.ValidationError(`Unexpected option: ${key}`, options);
  40. }
  41. });
  42. if (options.limit && typeof options.limit !== 'number') {
  43. throw new errors_1.ValidationError('limit must be a number', options.limit);
  44. }
  45. if (options.ledger_index &&
  46. !(typeof options.ledger_index === 'number' ||
  47. (typeof options.ledger_index === 'string' &&
  48. ['validated', 'closed', 'current'].includes(options.ledger_index)))) {
  49. throw new errors_1.ValidationError('ledger_index must be a number or a string of "validated", "closed", or "current"', options.ledger_index);
  50. }
  51. if (options.ledger_hash !== undefined &&
  52. options.ledger_hash !== null &&
  53. typeof options.ledger_hash !== 'string') {
  54. throw new errors_1.ValidationError('ledger_hash must be a string', options.ledger_hash);
  55. }
  56. if (options.taker !== undefined && typeof options.taker !== 'string') {
  57. throw new errors_1.ValidationError('taker must be a string', options.taker);
  58. }
  59. const request = {
  60. command: 'book_offers',
  61. taker_pays: takerPays,
  62. taker_gets: takerGets,
  63. ledger_index: (_a = options.ledger_index) !== null && _a !== void 0 ? _a : 'validated',
  64. ledger_hash: options.ledger_hash === null ? undefined : options.ledger_hash,
  65. limit: (_b = options.limit) !== null && _b !== void 0 ? _b : DEFAULT_LIMIT,
  66. taker: options.taker ? options.taker : undefined,
  67. };
  68. const directOfferResults = yield this.requestAll(request);
  69. request.taker_gets = takerPays;
  70. request.taker_pays = takerGets;
  71. const reverseOfferResults = yield this.requestAll(request);
  72. const directOffers = (0, flatMap_1.default)(directOfferResults, (directOfferResult) => directOfferResult.result.offers);
  73. const reverseOffers = (0, flatMap_1.default)(reverseOfferResults, (reverseOfferResult) => reverseOfferResult.result.offers);
  74. const orders = [...directOffers, ...reverseOffers];
  75. const buy = [];
  76. const sell = [];
  77. orders.forEach((order) => {
  78. if ((order.Flags & Offer_1.OfferFlags.lsfSell) === 0) {
  79. buy.push(order);
  80. }
  81. else {
  82. sell.push(order);
  83. }
  84. });
  85. return {
  86. buy: sortOffers(buy).slice(0, options.limit),
  87. sell: sortOffers(sell).slice(0, options.limit),
  88. };
  89. });
  90. }
  91. exports.default = getOrderbook;
  92. //# sourceMappingURL=getOrderbook.js.map