@orderly.network/hooks 0.0.93 → 0.0.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -9,7 +9,7 @@ import { SimpleDI, Account, EventEmitter, utils } from '@orderly.network/core';
9
9
  import { AccountStatusEnum, OrderStatus, OrderSide, chainsInfoMap, ARBITRUM_TESTNET_CHAINID, ARBITRUM_MAINNET_CHAINID, WS_WalletStatusEnum, OrderType } from '@orderly.network/types';
10
10
  import { Decimal, zero, getPrecisionByNumber, timeConvertString } from '@orderly.network/utils';
11
11
  import useSWRSubscription from 'swr/subscription';
12
- import { pathOr, propOr, compose, head, prop, mergeDeepRight, pick } from 'ramda';
12
+ import { pathOr, propOr, compose, head, prop, mergeDeepRight, pick, min } from 'ramda';
13
13
  import { positions, account, order } from '@orderly.network/futures';
14
14
  import useSWRInfinite from 'swr/infinite';
15
15
  export * from 'use-debounce';
@@ -657,6 +657,14 @@ var reduceItems = (depth, level, data, asks = false) => {
657
657
  } else {
658
658
  priceKey = new Decimal(Math.floor(price / depth)).mul(depth).toNumber();
659
659
  }
660
+ if (depth < 1 && depth > 0 && priceKey.toString().indexOf(".") !== -1) {
661
+ const priceStr = price.toString();
662
+ const index = priceStr.indexOf(".");
663
+ const decimal = priceStr.slice(index + 1);
664
+ const decimalDepth = depth.toString().slice(2).length;
665
+ const decimalStr = decimal.slice(0, min(decimal.length, decimalDepth));
666
+ priceKey = new Decimal(priceStr.slice(0, index) + "." + decimalStr).toNumber();
667
+ }
660
668
  if (prices.has(priceKey)) {
661
669
  const item = prices.get(priceKey);
662
670
  const itemPrice = new Decimal(item[1]).add(quantity).toNumber();
@@ -677,8 +685,32 @@ var reduceItems = (depth, level, data, asks = false) => {
677
685
  return result;
678
686
  };
679
687
  var reduceOrderbook = (depth, level, data) => {
680
- const asks = reduceItems(depth, level, data.asks, true).reverse();
681
- const bids = reduceItems(depth, level, data.bids);
688
+ let asks = reduceItems(depth, level, data.asks, true);
689
+ let bids = reduceItems(depth, level, data.bids);
690
+ if (asks.length !== 0 && bids.length !== 0 && asks[0][0] <= bids[0][0]) {
691
+ if (asks.length === 1) {
692
+ const [price, qty, newQuantity] = asks[0];
693
+ asks.shift();
694
+ asks.push([price + (depth === void 0 ? 0 : depth), qty, newQuantity]);
695
+ } else {
696
+ const [bidPrice] = bids[0];
697
+ while (asks.length > 0) {
698
+ const [askPrice, askQty, newQuantity] = asks[0];
699
+ if (askPrice <= bidPrice) {
700
+ asks.shift();
701
+ for (let index = 0; index < asks.length; index++) {
702
+ if (index === 0) {
703
+ asks[index][1] += askQty;
704
+ }
705
+ asks[index][2] += newQuantity;
706
+ }
707
+ } else {
708
+ break;
709
+ }
710
+ }
711
+ }
712
+ }
713
+ asks = asks.reverse();
682
714
  return {
683
715
  asks: asks.length < level ? paddingFn(level - asks.length).concat(asks) : asks,
684
716
  bids: bids.length < level ? bids.concat(paddingFn(level - bids.length)) : bids
@@ -821,7 +853,10 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
821
853
  useEffect(() => {
822
854
  prevMiddlePrice.current = middlePrice;
823
855
  }, [middlePrice]);
824
- const reducedData = reduceOrderbook(depth, level, data);
856
+ const reducedData = reduceOrderbook(depth, level, {
857
+ asks: [...data.asks],
858
+ bids: [...data.bids]
859
+ });
825
860
  return [
826
861
  {
827
862
  asks: reducedData.asks.slice(-level),