@orderly.network/hooks 0.0.92 → 0.0.94
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.js +69 -42
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
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) {
|
|
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,48 +685,66 @@ var reduceItems = (depth, level, data, asks = false) => {
|
|
|
677
685
|
return result;
|
|
678
686
|
};
|
|
679
687
|
var reduceOrderbook = (depth, level, data) => {
|
|
680
|
-
|
|
681
|
-
|
|
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
|
|
685
717
|
};
|
|
686
718
|
};
|
|
687
|
-
var
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
} else
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
});
|
|
706
|
-
update.bids.forEach((element) => {
|
|
707
|
-
for (let index = 0; index < bids.length; index++) {
|
|
708
|
-
if (element[1] === 0) {
|
|
709
|
-
if (element[0] === bids[index][0]) {
|
|
710
|
-
bids.splice(index, 1);
|
|
711
|
-
break;
|
|
719
|
+
var mergeItems = (data, update) => {
|
|
720
|
+
if (data.length === 0)
|
|
721
|
+
return update;
|
|
722
|
+
data = data.filter(([price]) => !isNaN(price));
|
|
723
|
+
while (update.length > 0) {
|
|
724
|
+
const item = update.shift();
|
|
725
|
+
if (item) {
|
|
726
|
+
const [price, quantity] = item;
|
|
727
|
+
const index = data.findIndex(([p], index2) => p === price);
|
|
728
|
+
if (index === -1) {
|
|
729
|
+
if (quantity === 0)
|
|
730
|
+
continue;
|
|
731
|
+
data.push(item);
|
|
732
|
+
} else {
|
|
733
|
+
if (quantity === 0) {
|
|
734
|
+
data.splice(index, 1);
|
|
735
|
+
} else {
|
|
736
|
+
data[index] = item;
|
|
712
737
|
}
|
|
713
|
-
} else if (element[0] === bids[index][0]) {
|
|
714
|
-
bids[index] = element;
|
|
715
|
-
break;
|
|
716
|
-
} else if (element[0] > bids[index][0]) {
|
|
717
|
-
bids.splice(index, 0, element);
|
|
718
|
-
break;
|
|
719
738
|
}
|
|
720
739
|
}
|
|
721
|
-
}
|
|
740
|
+
}
|
|
741
|
+
return data;
|
|
742
|
+
};
|
|
743
|
+
var mergeOrderbook = (data, update) => {
|
|
744
|
+
let asks = [...data.asks];
|
|
745
|
+
let bids = [...data.bids];
|
|
746
|
+
asks = mergeItems(asks, update.asks).sort(asksSortFn);
|
|
747
|
+
bids = mergeItems(bids, update.bids).sort(bidsSortFn);
|
|
722
748
|
return {
|
|
723
749
|
asks,
|
|
724
750
|
bids
|
|
@@ -762,14 +788,12 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
|
|
|
762
788
|
if (ignore)
|
|
763
789
|
return;
|
|
764
790
|
if (!!message) {
|
|
765
|
-
let bids = message.bids.sort(bidsSortFn);
|
|
766
|
-
bids = bids.filter((item) => !isNaN(item[0]));
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
asks = asks.filter((item) => !isNaN(item[0]));
|
|
770
|
-
asks = asks.filter((item) => item[1] > 0);
|
|
791
|
+
let bids = [...message.bids.sort(bidsSortFn)];
|
|
792
|
+
bids = bids.filter((item) => !isNaN(item[0]) && item[1] > 0);
|
|
793
|
+
let asks = [...message.asks.sort(asksSortFn)];
|
|
794
|
+
asks = asks.filter((item) => !isNaN(item[0]) && item[1] > 0);
|
|
771
795
|
setRequestData({ bids, asks });
|
|
772
|
-
setData({ bids, asks });
|
|
796
|
+
setData({ bids: [...bids], asks: [...asks] });
|
|
773
797
|
}
|
|
774
798
|
setIsLoading(false);
|
|
775
799
|
}
|
|
@@ -829,7 +853,10 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
|
|
|
829
853
|
useEffect(() => {
|
|
830
854
|
prevMiddlePrice.current = middlePrice;
|
|
831
855
|
}, [middlePrice]);
|
|
832
|
-
const reducedData = reduceOrderbook(depth, level,
|
|
856
|
+
const reducedData = reduceOrderbook(depth, level, {
|
|
857
|
+
asks: [...data.asks],
|
|
858
|
+
bids: [...data.bids]
|
|
859
|
+
});
|
|
833
860
|
return [
|
|
834
861
|
{
|
|
835
862
|
asks: reducedData.asks.slice(-level),
|