@orderly.network/hooks 1.0.6 → 1.0.7

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
@@ -1091,8 +1091,8 @@ var usePositionStream = (symbol, options) => {
1091
1091
  const fundingRates = useFundingRates();
1092
1092
  const {
1093
1093
  data,
1094
- error
1095
- // mutate: updatePositions,
1094
+ error,
1095
+ mutate: updatePositions
1096
1096
  } = usePrivateQuery(`/v1/positions`, {
1097
1097
  // revalidateOnFocus: false,
1098
1098
  // revalidateOnReconnect: false,
@@ -1104,6 +1104,10 @@ var usePositionStream = (symbol, options) => {
1104
1104
  onError: (err) => {
1105
1105
  }
1106
1106
  });
1107
+ usePositionUpdateStream((positions2) => {
1108
+ console.log("position message", positions2);
1109
+ updatePositions();
1110
+ });
1107
1111
  const { data: markPrices } = useMarkPricesStream();
1108
1112
  const formatedPositions = useMemo(() => {
1109
1113
  if (!data?.rows || !symbolInfo || !accountInfo)
@@ -1252,6 +1256,41 @@ var pathOr_unsettledPnLPathOr = pathOr(0, [
1252
1256
  "aggregated",
1253
1257
  "unsettledPnL"
1254
1258
  ]);
1259
+ var usePositionUpdateStream = (callback) => {
1260
+ const ws = useWS();
1261
+ const positionList = useRef({});
1262
+ return useSWRSubscription("positionUpdate", (key, { next }) => {
1263
+ const unsubscribe = ws.privateSubscribe(
1264
+ // { event: "subscribe", topic: "markprices" },
1265
+ "position",
1266
+ {
1267
+ onMessage: (message) => {
1268
+ const { positions: positions2 } = message;
1269
+ let update = false;
1270
+ for (const p in positions2) {
1271
+ const { symbol, positionQty } = positions2[p];
1272
+ if (positionList.current[symbol] !== positionQty) {
1273
+ update = true;
1274
+ positionList.current[symbol] = positionQty;
1275
+ }
1276
+ }
1277
+ if (update) {
1278
+ callback(positions2.current);
1279
+ }
1280
+ next(null, positionList);
1281
+ },
1282
+ // onUnsubscribe: () => {
1283
+ // return "markprices";
1284
+ // },
1285
+ onError: (error) => {
1286
+ }
1287
+ }
1288
+ );
1289
+ return () => {
1290
+ unsubscribe?.();
1291
+ };
1292
+ });
1293
+ };
1255
1294
  var useHoldingStream = () => {
1256
1295
  const ws = useWS();
1257
1296
  const { data, isLoading, mutate: mutate2 } = usePrivateQuery(
@@ -1528,11 +1567,37 @@ var useCollateral = (options = { dp: 6 }) => {
1528
1567
  var positionsPath2 = pathOr([], [0, "rows"]);
1529
1568
  var useMaxQty = (symbol, side, reduceOnly = false) => {
1530
1569
  const positionsData = usePositionStream();
1531
- const [orders] = useOrderStream({ status: OrderStatus.NEW });
1532
1570
  const { data: accountInfo } = usePrivateQuery("/v1/client/info");
1533
1571
  const symbolInfo = useSymbolsInfo();
1534
1572
  const { totalCollateral } = useCollateral();
1535
1573
  const { data: markPrices } = useMarkPricesStream();
1574
+ const {
1575
+ data: orders,
1576
+ error,
1577
+ mutate: updateOrder
1578
+ } = usePrivateQuery(`/v1/orders?status=NEW&size=99`, {
1579
+ formatter: (data) => data.rows,
1580
+ onError: (err) => {
1581
+ }
1582
+ });
1583
+ const ws = useWS();
1584
+ useEffect(() => {
1585
+ const unsubscribe = ws.privateSubscribe(
1586
+ {
1587
+ id: "executionreport_orders",
1588
+ event: "subscribe",
1589
+ topic: "executionreport",
1590
+ ts: Date.now()
1591
+ },
1592
+ {
1593
+ onMessage: (data) => {
1594
+ console.log("refresh orders", data);
1595
+ updateOrder();
1596
+ }
1597
+ }
1598
+ );
1599
+ return () => unsubscribe();
1600
+ }, []);
1536
1601
  const maxQty = useMemo(() => {
1537
1602
  if (!symbol)
1538
1603
  return 0;