@orderly.network/hooks 1.0.20 → 1.0.21

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.d.mts CHANGED
@@ -40,7 +40,7 @@ declare const useLazyQuery: <T, R = any>(query: Parameters<typeof useSWR>["0"],
40
40
  }) | undefined) => SWRMutationResponse;
41
41
 
42
42
  type HTTP_METHOD = "POST" | "PUT" | "DELETE";
43
- declare const useMutation: <T, E>(url: string, method?: HTTP_METHOD, options?: SWRMutationConfiguration<T, E> | undefined) => readonly [(data: any, params?: any) => Promise<any>, {
43
+ declare const useMutation: <T, E>(url: string, method?: HTTP_METHOD, options?: SWRMutationConfiguration<T, E> | undefined) => readonly [(data: any, params?: any, options?: any) => Promise<any>, {
44
44
  readonly data: any;
45
45
  readonly error: E | undefined;
46
46
  readonly reset: () => void;
@@ -189,6 +189,7 @@ declare const useOrderEntry: (symbol: string, side: OrderSide, reduceOnly?: bool
189
189
  freeCollateral: number;
190
190
  markPrice: any;
191
191
  onSubmit: (values: OrderEntity) => Promise<any>;
192
+ submitting: boolean;
192
193
  helper: {
193
194
  calculate: (values: any, field: string, value: any) => unknown;
194
195
  validator: (values: any) => any;
@@ -213,6 +214,7 @@ declare const useIndexPrice: (symbol: string) => swr_subscription.SWRSubscriptio
213
214
  declare const useLeverage: () => [number | undefined, {
214
215
  update: (data: any) => Promise<unknown>;
215
216
  config: number[];
217
+ isMutating: boolean;
216
218
  }];
217
219
 
218
220
  declare const useTickerStream: (symbol: string) => API.MarketInfo;
@@ -253,11 +255,15 @@ declare const useOrderStream: (params: Params) => readonly [any[] | null, {
253
255
  readonly loadMore: () => void;
254
256
  readonly cancelAllOrders: () => void;
255
257
  readonly updateOrder: (orderId: string, order: OrderEntity) => Promise<any>;
256
- readonly cancelOrder: (orderId: string, symbol?: string) => Promise<void>;
258
+ readonly cancelOrder: (orderId: string, symbol?: string) => Promise<any>;
257
259
  readonly errors: {
258
260
  readonly cancelOrder: unknown;
259
261
  readonly updateOrder: unknown;
260
262
  };
263
+ readonly submitting: {
264
+ readonly cancelOrder: boolean;
265
+ readonly updateOrder: boolean;
266
+ };
261
267
  }];
262
268
 
263
269
  interface MarketTradeStreamOptions {
@@ -369,6 +375,8 @@ declare const useSettleSubscription: (options?: {
369
375
 
370
376
  declare const usePrivateDataObserver: () => void;
371
377
 
378
+ declare function useMediaQuery(query: string): boolean;
379
+
372
380
  /** @hidden */
373
381
  declare const useWooSwapQuery: () => {
374
382
  query: (inputs: any) => Promise<any> | undefined;
@@ -446,4 +454,4 @@ declare const useSwap: () => {
446
454
  status: WS_WalletStatusEnum;
447
455
  };
448
456
 
449
- export { ConfigProviderProps, OrderlyConfigContextState, OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
457
+ export { ConfigProviderProps, OrderlyConfigContextState, OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMediaQuery, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
package/dist/index.d.ts CHANGED
@@ -40,7 +40,7 @@ declare const useLazyQuery: <T, R = any>(query: Parameters<typeof useSWR>["0"],
40
40
  }) | undefined) => SWRMutationResponse;
41
41
 
42
42
  type HTTP_METHOD = "POST" | "PUT" | "DELETE";
43
- declare const useMutation: <T, E>(url: string, method?: HTTP_METHOD, options?: SWRMutationConfiguration<T, E> | undefined) => readonly [(data: any, params?: any) => Promise<any>, {
43
+ declare const useMutation: <T, E>(url: string, method?: HTTP_METHOD, options?: SWRMutationConfiguration<T, E> | undefined) => readonly [(data: any, params?: any, options?: any) => Promise<any>, {
44
44
  readonly data: any;
45
45
  readonly error: E | undefined;
46
46
  readonly reset: () => void;
@@ -189,6 +189,7 @@ declare const useOrderEntry: (symbol: string, side: OrderSide, reduceOnly?: bool
189
189
  freeCollateral: number;
190
190
  markPrice: any;
191
191
  onSubmit: (values: OrderEntity) => Promise<any>;
192
+ submitting: boolean;
192
193
  helper: {
193
194
  calculate: (values: any, field: string, value: any) => unknown;
194
195
  validator: (values: any) => any;
@@ -213,6 +214,7 @@ declare const useIndexPrice: (symbol: string) => swr_subscription.SWRSubscriptio
213
214
  declare const useLeverage: () => [number | undefined, {
214
215
  update: (data: any) => Promise<unknown>;
215
216
  config: number[];
217
+ isMutating: boolean;
216
218
  }];
217
219
 
218
220
  declare const useTickerStream: (symbol: string) => API.MarketInfo;
@@ -253,11 +255,15 @@ declare const useOrderStream: (params: Params) => readonly [any[] | null, {
253
255
  readonly loadMore: () => void;
254
256
  readonly cancelAllOrders: () => void;
255
257
  readonly updateOrder: (orderId: string, order: OrderEntity) => Promise<any>;
256
- readonly cancelOrder: (orderId: string, symbol?: string) => Promise<void>;
258
+ readonly cancelOrder: (orderId: string, symbol?: string) => Promise<any>;
257
259
  readonly errors: {
258
260
  readonly cancelOrder: unknown;
259
261
  readonly updateOrder: unknown;
260
262
  };
263
+ readonly submitting: {
264
+ readonly cancelOrder: boolean;
265
+ readonly updateOrder: boolean;
266
+ };
261
267
  }];
262
268
 
263
269
  interface MarketTradeStreamOptions {
@@ -369,6 +375,8 @@ declare const useSettleSubscription: (options?: {
369
375
 
370
376
  declare const usePrivateDataObserver: () => void;
371
377
 
378
+ declare function useMediaQuery(query: string): boolean;
379
+
372
380
  /** @hidden */
373
381
  declare const useWooSwapQuery: () => {
374
382
  query: (inputs: any) => Promise<any> | undefined;
@@ -446,4 +454,4 @@ declare const useSwap: () => {
446
454
  status: WS_WalletStatusEnum;
447
455
  };
448
456
 
449
- export { ConfigProviderProps, OrderlyConfigContextState, OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
457
+ export { ConfigProviderProps, OrderlyConfigContextState, OrderlyConfigProvider, OrderlyContext, OrderlyProvider, WalletConnectorContext, useAccount, useAccountInfo, useAccountInstance, useBoolean, useChain, useChains, useCollateral, useConfig, useCrossSwap, useDeposit, useEventEmitter, useFundingRate, useHoldingStream, useIndexPrice, useLazyQuery, useLeverage, useLocalStorage, useMarginRatio, useMarkPrice, useMarkPricesStream, useMarketTradeStream, useMarketsStream, useMaxQty, useMediaQuery, useMutation, useOrderEntry, useOrderStream, useOrderbookStream, usePositionStream, usePreLoadData, usePrivateDataObserver, usePrivateInfiniteQuery, usePrivateQuery, useQuery, useSessionStorage, useSettleSubscription, useSwap, useSymbolsInfo, useTickerStream, useWS, useWalletConnector, useWalletSubscription, useWithdraw, useWooCrossSwapQuery, useWooSwapQuery };
package/dist/index.js CHANGED
@@ -138,7 +138,7 @@ var useMutation = (url, method = "POST", options) => {
138
138
  fetcher2,
139
139
  options
140
140
  );
141
- const mutation = async (data2, params) => {
141
+ const mutation = async (data2, params, options2) => {
142
142
  let newUrl = url;
143
143
  if (typeof params === "object" && Object.keys(params).length) {
144
144
  let search = new URLSearchParams(params);
@@ -150,15 +150,18 @@ var useMutation = (url, method = "POST", options) => {
150
150
  data: data2
151
151
  };
152
152
  const signature = await signer.sign(payload);
153
- return trigger({
154
- data: data2,
155
- params,
156
- method,
157
- signature: {
158
- ...signature,
159
- "orderly-account-id": account5.accountId
160
- }
161
- });
153
+ return trigger(
154
+ {
155
+ data: data2,
156
+ params,
157
+ method,
158
+ signature: {
159
+ ...signature,
160
+ "orderly-account-id": account5.accountId
161
+ }
162
+ },
163
+ options2
164
+ );
162
165
  };
163
166
  return [
164
167
  mutation,
@@ -795,7 +798,9 @@ var reduceItems = (depth, level, data, asks = false) => {
795
798
  const decimal = priceStr.slice(index + 1);
796
799
  const decimalDepth = depth.toString().slice(2).length;
797
800
  const decimalStr = decimal.slice(0, ramda.min(decimal.length, decimalDepth));
798
- priceKey = new utils.Decimal(priceStr.slice(0, index) + "." + decimalStr).toNumber();
801
+ priceKey = new utils.Decimal(
802
+ priceStr.slice(0, index) + "." + decimalStr
803
+ ).toNumber();
799
804
  }
800
805
  if (prices.has(priceKey)) {
801
806
  const item = prices.get(priceKey);
@@ -887,10 +892,10 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
887
892
  if (!symbol) {
888
893
  throw new Error("useOrderbookStream requires a symbol");
889
894
  }
895
+ const level = options?.level ?? 10;
890
896
  const [requestData, setRequestData] = React.useState(null);
891
897
  const [data, setData] = React.useState(initial);
892
898
  const [isLoading, setIsLoading] = React.useState(true);
893
- const [level, setLevel] = React.useState(() => options?.level ?? 10);
894
899
  const config = useSymbolsInfo()[symbol];
895
900
  const [depth, setDepth] = React.useState();
896
901
  const depths = React.useMemo(() => {
@@ -921,9 +926,13 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
921
926
  return;
922
927
  if (!!message) {
923
928
  let bids = [...message.bids.sort(bidsSortFn)];
924
- bids = bids.filter((item) => !isNaN(item[0]) && item[1] > 0);
929
+ bids = bids.filter(
930
+ (item) => !isNaN(item[0]) && item[1] > 0
931
+ );
925
932
  let asks = [...message.asks.sort(asksSortFn)];
926
- asks = asks.filter((item) => !isNaN(item[0]) && item[1] > 0);
933
+ asks = asks.filter(
934
+ (item) => !isNaN(item[0]) && item[1] > 0
935
+ );
927
936
  setRequestData({ bids, asks });
928
937
  setData({ bids: [...bids], asks: [...asks] });
929
938
  }
@@ -1351,7 +1360,10 @@ var usePositionStream = (symbol, options) => {
1351
1360
  return [
1352
1361
  {
1353
1362
  rows: positionsRows,
1354
- aggregated: formatedPositions?.[1] ?? {},
1363
+ aggregated: {
1364
+ ...formatedPositions?.[1] ?? {},
1365
+ unrealPnlROI: totalUnrealizedROI
1366
+ },
1355
1367
  totalCollateral,
1356
1368
  totalValue,
1357
1369
  totalUnrealizedROI
@@ -1426,14 +1438,14 @@ var useHoldingStream = () => {
1426
1438
  var useOrderStream = (params) => {
1427
1439
  const { status, symbol, side, size = 100 } = params;
1428
1440
  const { data: markPrices = {} } = useMarkPricesStream();
1429
- const [doCancelOrder, { error: cancelOrderError }] = useMutation(
1430
- "/v1/order",
1431
- "DELETE"
1432
- );
1433
- const [doUpdateOrder, { error: updateOrderError }] = useMutation(
1434
- "/v1/order",
1435
- "PUT"
1436
- );
1441
+ const [
1442
+ doCancelOrder,
1443
+ { error: cancelOrderError, isMutating: cancelMutating }
1444
+ ] = useMutation("/v1/order", "DELETE");
1445
+ const [
1446
+ doUpdateOrder,
1447
+ { error: updateOrderError, isMutating: updateMutating }
1448
+ ] = useMutation("/v1/order", "PUT");
1437
1449
  const ordersResponse = usePrivateInfiniteQuery(
1438
1450
  (pageIndex, previousPageData) => {
1439
1451
  if (previousPageData && !previousPageData.rows?.length)
@@ -1486,7 +1498,9 @@ var useOrderStream = (params) => {
1486
1498
  order_id: orderId,
1487
1499
  symbol: symbol2
1488
1500
  }).then((res) => {
1489
- if (res.success) ; else {
1501
+ if (res.success) {
1502
+ return res;
1503
+ } else {
1490
1504
  throw new Error(res.message);
1491
1505
  }
1492
1506
  });
@@ -1506,6 +1520,10 @@ var useOrderStream = (params) => {
1506
1520
  errors: {
1507
1521
  cancelOrder: cancelOrderError,
1508
1522
  updateOrder: updateOrderError
1523
+ },
1524
+ submitting: {
1525
+ cancelOrder: cancelMutating,
1526
+ updateOrder: updateMutating
1509
1527
  }
1510
1528
  }
1511
1529
  ];
@@ -1803,7 +1821,7 @@ var OrderFactory = class {
1803
1821
 
1804
1822
  // src/orderly/useOrderEntry.ts
1805
1823
  var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1806
- const [doCreateOrder] = useMutation("/v1/order");
1824
+ const [doCreateOrder, { data, error, reset, isMutating }] = useMutation("/v1/order");
1807
1825
  const { freeCollateral } = useCollateral();
1808
1826
  const symbolInfo = useSymbolsInfo();
1809
1827
  const baseDP = React.useMemo(
@@ -1842,9 +1860,9 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1842
1860
  if (!symbol) {
1843
1861
  throw new Error("symbol is null");
1844
1862
  }
1845
- const data = orderCreator.create(values);
1863
+ const data2 = orderCreator.create(values);
1846
1864
  return doCreateOrder({
1847
- ...data,
1865
+ ...data2,
1848
1866
  symbol
1849
1867
  });
1850
1868
  });
@@ -1876,6 +1894,7 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1876
1894
  freeCollateral,
1877
1895
  markPrice,
1878
1896
  onSubmit,
1897
+ submitting: isMutating,
1879
1898
  helper: {
1880
1899
  calculate,
1881
1900
  validator
@@ -1923,13 +1942,17 @@ var useMarketsStream = () => {
1923
1942
  (t) => t.symbol === item.symbol
1924
1943
  );
1925
1944
  if (ticker) {
1926
- return {
1945
+ const data = {
1927
1946
  ...item,
1928
1947
  ["24h_close"]: ticker.close,
1929
1948
  ["24h_open"]: ticker.open,
1930
1949
  ["24h_volumn"]: ticker.volume,
1931
1950
  change: 0
1932
1951
  };
1952
+ if (ticker.close !== void 0 && ticker.open !== void 0) {
1953
+ data["change"] = new utils.Decimal(ticker.close).minus(ticker.open).div(ticker.open).toNumber();
1954
+ }
1955
+ return data;
1933
1956
  }
1934
1957
  return item;
1935
1958
  });
@@ -1938,7 +1961,7 @@ var useMarketsStream = () => {
1938
1961
  };
1939
1962
  var useLeverage = () => {
1940
1963
  const { data, mutate: mutate2 } = usePrivateQuery("/v1/client/info");
1941
- const [update] = useMutation("/v1/client/leverage");
1964
+ const [update, { isMutating }] = useMutation("/v1/client/leverage");
1942
1965
  const { data: config } = useQuery("/v1/public/config");
1943
1966
  const updateLeverage = React.useCallback((data2) => {
1944
1967
  return update(data2).then((res) => {
@@ -1953,6 +1976,7 @@ var useLeverage = () => {
1953
1976
  ramda.prop("max_leverage", data),
1954
1977
  {
1955
1978
  update: updateLeverage,
1979
+ isMutating,
1956
1980
  // config: [1, 2, 3, 4, 5, 10, 15, 20],
1957
1981
  config: config ? config?.available_futures_leverage?.split(",").map((item) => parseInt(item)) : []
1958
1982
  }
@@ -3350,8 +3374,7 @@ var useChain = (token) => {
3350
3374
  var useWithdraw = () => {
3351
3375
  const { account: account5, state } = useAccount();
3352
3376
  const [isLoading, setIsLoading] = React.useState(false);
3353
- const { unsettledPnL, availableBalance } = useCollateral();
3354
- const { freeCollateral } = useCollateral();
3377
+ const { unsettledPnL, availableBalance, freeCollateral } = useCollateral();
3355
3378
  const withdraw = React.useCallback(
3356
3379
  (inputs) => {
3357
3380
  return account5.assetsManager.withdraw(inputs).then((res) => {
@@ -3597,6 +3620,35 @@ var useSettleSubscription = (options) => {
3597
3620
  return () => unsubscribe();
3598
3621
  });
3599
3622
  };
3623
+ function useMediaQuery(query) {
3624
+ const getMatches = (query2) => {
3625
+ if (typeof window !== "undefined") {
3626
+ return window.matchMedia(query2).matches;
3627
+ }
3628
+ return false;
3629
+ };
3630
+ const [matches, setMatches] = React.useState(getMatches(query));
3631
+ function handleChange() {
3632
+ setMatches(getMatches(query));
3633
+ }
3634
+ React.useEffect(() => {
3635
+ const matchMedia = window.matchMedia(query);
3636
+ handleChange();
3637
+ if (matchMedia.addListener) {
3638
+ matchMedia.addListener(handleChange);
3639
+ } else {
3640
+ matchMedia.addEventListener("change", handleChange);
3641
+ }
3642
+ return () => {
3643
+ if (matchMedia.removeListener) {
3644
+ matchMedia.removeListener(handleChange);
3645
+ } else {
3646
+ matchMedia.removeEventListener("change", handleChange);
3647
+ }
3648
+ };
3649
+ }, [query]);
3650
+ return matches;
3651
+ }
3600
3652
  var useWooSwapQuery = () => {
3601
3653
  const { configStore } = React.useContext(OrderlyContext);
3602
3654
  const account5 = useAccountInstance();
@@ -4238,6 +4290,7 @@ exports.useMarkPricesStream = useMarkPricesStream;
4238
4290
  exports.useMarketTradeStream = useMarketTradeStream;
4239
4291
  exports.useMarketsStream = useMarketsStream;
4240
4292
  exports.useMaxQty = useMaxQty;
4293
+ exports.useMediaQuery = useMediaQuery;
4241
4294
  exports.useMutation = useMutation;
4242
4295
  exports.useOrderEntry = useOrderEntry;
4243
4296
  exports.useOrderStream = useOrderStream;