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.

orderbook.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  22. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  23. return new (P || (P = Promise))(function (resolve, reject) {
  24. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  25. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  26. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  27. step((generator = generator.apply(thisArg, _arguments || [])).next());
  28. });
  29. };
  30. var __importDefault = (this && this.__importDefault) || function (mod) {
  31. return (mod && mod.__esModule) ? mod : { "default": mod };
  32. };
  33. Object.defineProperty(exports, "__esModule", { value: true });
  34. exports.getOrderbook = exports.formatBidsAndAsks = void 0;
  35. const _ = __importStar(require("lodash"));
  36. const utils = __importStar(require("./utils"));
  37. const orderbook_order_1 = require("./parse/orderbook-order");
  38. const common_1 = require("../common");
  39. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  40. function isSameIssue(a, b) {
  41. return a.currency === b.currency && a.counterparty === b.counterparty;
  42. }
  43. function directionFilter(direction, order) {
  44. return order.specification.direction === direction;
  45. }
  46. function flipOrder(order) {
  47. const specification = order.specification;
  48. const flippedSpecification = {
  49. quantity: specification.totalPrice,
  50. totalPrice: specification.quantity,
  51. direction: specification.direction === 'buy' ? 'sell' : 'buy'
  52. };
  53. const newSpecification = _.merge({}, specification, flippedSpecification);
  54. return _.merge({}, order, { specification: newSpecification });
  55. }
  56. function alignOrder(base, order) {
  57. const quantity = order.specification.quantity;
  58. return isSameIssue(quantity, base) ? order : flipOrder(order);
  59. }
  60. function formatBidsAndAsks(orderbook, offers) {
  61. const orders = offers
  62. .sort((a, b) => {
  63. return new bignumber_js_1.default(a.quality).comparedTo(b.quality);
  64. })
  65. .map(orderbook_order_1.parseOrderbookOrder);
  66. const alignedOrders = orders.map(_.partial(alignOrder, orderbook.base));
  67. const bids = alignedOrders.filter(_.partial(directionFilter, 'buy'));
  68. const asks = alignedOrders.filter(_.partial(directionFilter, 'sell'));
  69. return { bids, asks };
  70. }
  71. exports.formatBidsAndAsks = formatBidsAndAsks;
  72. function makeRequest(api, taker, options, takerGets, takerPays) {
  73. return __awaiter(this, void 0, void 0, function* () {
  74. const orderData = utils.renameCounterpartyToIssuerInOrder({
  75. taker_gets: takerGets,
  76. taker_pays: takerPays
  77. });
  78. return api._requestAll('book_offers', {
  79. taker_gets: orderData.taker_gets,
  80. taker_pays: orderData.taker_pays,
  81. ledger_index: options.ledgerVersion || 'validated',
  82. limit: options.limit,
  83. taker
  84. });
  85. });
  86. }
  87. function getOrderbook(address, orderbook, options = {}) {
  88. return __awaiter(this, void 0, void 0, function* () {
  89. common_1.validate.getOrderbook({ address, orderbook, options });
  90. const [directOfferResults, reverseOfferResults] = yield Promise.all([
  91. makeRequest(this, address, options, orderbook.base, orderbook.counter),
  92. makeRequest(this, address, options, orderbook.counter, orderbook.base)
  93. ]);
  94. const directOffers = _.flatMap(directOfferResults, (directOfferResult) => directOfferResult.offers);
  95. const reverseOffers = _.flatMap(reverseOfferResults, (reverseOfferResult) => reverseOfferResult.offers);
  96. return formatBidsAndAsks(orderbook, [...directOffers, ...reverseOffers]);
  97. });
  98. }
  99. exports.getOrderbook = getOrderbook;
  100. //# sourceMappingURL=orderbook.js.map