@orderly.network/hooks 0.0.11 → 0.0.13

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 CHANGED
@@ -90,9 +90,6 @@ var fetcher = (url, init = {}, queryOptions) => net.get(url, init, queryOptions
90
90
  var useQuery = (query, options) => {
91
91
  const { apiBaseUrl } = React.useContext(OrderlyContext);
92
92
  const _a = options || {}, { formatter } = _a, swrOptions = __objRest(_a, ["formatter"]);
93
- if (typeof query === "string" && !query.startsWith("/public")) {
94
- throw new Error("useQuery is only for public api");
95
- }
96
93
  if (typeof apiBaseUrl === "undefined") {
97
94
  throw new Error("please add OrderlyProvider to your app");
98
95
  }
@@ -125,36 +122,51 @@ var useAccountInstance = () => {
125
122
 
126
123
  // src/useMutation.ts
127
124
  var fetcher2 = (url, options) => {
128
- return net.post(url, options.arg.data, {
125
+ console.log("muation fetcher", url, options);
126
+ const init = {
127
+ method: options.arg.method,
129
128
  headers: __spreadValues({}, options.arg.signature)
130
- });
131
- };
132
- var deleteFetcher = (url, options) => {
133
- return net.del(url, options.arg.data, {
134
- headers: __spreadValues({}, options.arg.signature)
135
- });
129
+ };
130
+ if (options.arg.data) {
131
+ init.body = JSON.stringify(options.arg.data);
132
+ }
133
+ if (typeof options.arg.params === "object" && Object.keys(options.arg.params).length) {
134
+ let search = new URLSearchParams(options.arg.params);
135
+ url = `${url}?${search.toString()}`;
136
+ }
137
+ return net.mutate(url, init);
136
138
  };
137
- var useMutation = (url, options, method = "POST") => {
139
+ var useMutation = (url, method = "POST", options) => {
138
140
  const { apiBaseUrl } = React.useContext(OrderlyContext);
141
+ let fullUrl = url;
139
142
  if (!url.startsWith("http")) {
140
- url = `${apiBaseUrl}${url}`;
143
+ fullUrl = `${apiBaseUrl}${url}`;
141
144
  }
142
145
  const account4 = useAccountInstance();
143
146
  const signer = account4.signer;
144
147
  const { trigger, data, error, reset, isMutating } = useSWRMutation__default.default(
145
- url,
146
- method === "POST" ? fetcher2 : deleteFetcher,
148
+ fullUrl,
149
+ // method === "POST" ? fetcher : deleteFetcher,
150
+ fetcher2,
147
151
  options
148
152
  );
149
- const mutation = (data2) => __async(void 0, null, function* () {
153
+ const mutation = (data2, params) => __async(void 0, null, function* () {
154
+ let newUrl = url;
155
+ if (typeof params === "object" && Object.keys(params).length) {
156
+ let search = new URLSearchParams(params);
157
+ newUrl = `${url}?${search.toString()}`;
158
+ }
150
159
  const payload = {
151
160
  method,
152
- url,
161
+ url: newUrl,
153
162
  data: data2
154
163
  };
164
+ console.log("payload", payload);
155
165
  const signature = yield signer.sign(payload);
156
166
  return trigger({
157
167
  data: data2,
168
+ params,
169
+ method,
158
170
  signature: __spreadProps(__spreadValues({}, signature), {
159
171
  "orderly-account-id": account4.accountId
160
172
  })
@@ -180,7 +192,7 @@ var signatureMiddleware = (useSWRNext) => {
180
192
  const signer = account4.signer;
181
193
  const payload = {
182
194
  method: "GET",
183
- url: fullUrl
195
+ url
184
196
  };
185
197
  const signature = yield signer.sign(payload);
186
198
  return fetcher4(fullUrl, {
@@ -398,7 +410,12 @@ var useTickerStream = (symbol) => {
398
410
  if (!symbol) {
399
411
  throw new Error("useFuturesForSymbol requires a symbol");
400
412
  }
401
- const { data: info } = useQuery(`/public/futures/${symbol}`);
413
+ const { data: info } = useQuery(
414
+ `/v1/public/futures/${symbol}`,
415
+ {
416
+ revalidateOnFocus: false
417
+ }
418
+ );
402
419
  const ws = useWS();
403
420
  const { data: ticker } = useSWRSubscription__default.default(
404
421
  `${symbol}@ticker`,
@@ -635,7 +652,7 @@ function createGetter(data, depth = 2) {
635
652
 
636
653
  // src/orderly/useSymbolsInfo.ts
637
654
  var useSymbolsInfo = () => {
638
- const { data = {} } = useQuery(`/public/info`, {
655
+ const { data = {} } = useQuery(`/v1/public/info`, {
639
656
  focusThrottleInterval: 1e3 * 60 * 60 * 24,
640
657
  revalidateOnFocus: false,
641
658
  formatter(data2) {
@@ -663,7 +680,7 @@ var useSymbolsInfo = () => {
663
680
  // src/orderly/useTokenInfo.ts
664
681
  var useTokenInfo = () => {
665
682
  const { data = {} } = useQuery(
666
- "/public/token",
683
+ "/v1/public/token",
667
684
  {
668
685
  focusThrottleInterval: 1e3 * 60 * 60 * 24,
669
686
  revalidateOnFocus: false,
@@ -810,7 +827,7 @@ var getCalculateHandler = (fieldName) => {
810
827
  // src/orderly/useFundingRates.ts
811
828
  var useFundingRates = () => {
812
829
  const { data } = useQuery(
813
- `/public/funding_rates`,
830
+ `/v1/public/funding_rates`,
814
831
  {
815
832
  focusThrottleInterval: 1e3 * 60 * 60 * 24,
816
833
  revalidateOnFocus: false,
@@ -864,9 +881,9 @@ var usePositionStream = (symbol, options) => {
864
881
  symbol
865
882
  );
866
883
  const symbolInfo = useSymbolsInfo();
867
- const { data: accountInfo } = usePrivateQuery("/client/info");
884
+ const { data: accountInfo } = usePrivateQuery("/v1/client/info");
868
885
  const fundingRates = useFundingRates();
869
- const { data, error } = usePrivateQuery(`/positions`, __spreadProps(__spreadValues({}, options), {
886
+ const { data, error } = usePrivateQuery(`/v1/positions`, __spreadProps(__spreadValues({}, options), {
870
887
  formatter: (data2) => data2,
871
888
  onError: (err) => {
872
889
  console.log("usePositionStream error", err);
@@ -968,15 +985,18 @@ var positionsPath = ramda.pathOr([], [0, "rows"]);
968
985
  var useCollateral = (options = { dp: 6 }) => {
969
986
  const { dp } = options;
970
987
  const positions2 = usePositionStream();
971
- const { data: orders } = usePrivateQuery(`/orders`);
972
- const { data: accountInfo } = usePrivateQuery("/client/info");
988
+ const { data: orders } = usePrivateQuery(`/v1/orders`);
989
+ const { data: accountInfo } = usePrivateQuery("/v1/client/info");
973
990
  const symbolInfo = useSymbolsInfo();
974
991
  const { data: markPrices } = useMarkPricesStream();
975
- const { data: holding } = usePrivateQuery("/client/holding", {
976
- formatter: (data) => {
977
- return data.holding;
992
+ const { data: holding } = usePrivateQuery(
993
+ "/v1/client/holding",
994
+ {
995
+ formatter: (data) => {
996
+ return data.holding;
997
+ }
978
998
  }
979
- });
999
+ );
980
1000
  const [totalCollateral, totalValue] = React.useMemo(() => {
981
1001
  if (!holding || !markPrices) {
982
1002
  return [utils.zero, utils.zero];
@@ -1034,8 +1054,8 @@ var useCollateral = (options = { dp: 6 }) => {
1034
1054
  var positionsPath2 = ramda.pathOr([], [0, "rows"]);
1035
1055
  var useMaxQty = (symbol, side, reduceOnly = false) => {
1036
1056
  const positionsData = usePositionStream();
1037
- const { data: orders } = usePrivateQuery(`/orders`);
1038
- const { data: accountInfo } = usePrivateQuery("/client/info");
1057
+ const { data: orders } = usePrivateQuery(`/v1/orders`);
1058
+ const { data: accountInfo } = usePrivateQuery("/v1/client/info");
1039
1059
  const symbolInfo = useSymbolsInfo();
1040
1060
  const { totalCollateral } = useCollateral();
1041
1061
  const { data: markPrices } = useMarkPricesStream();
@@ -1133,7 +1153,6 @@ var BaseOrderCreator = class {
1133
1153
  baseValidate(values, configs) {
1134
1154
  const errors = {};
1135
1155
  const { maxQty } = configs;
1136
- console.log("baseValidate", values, configs);
1137
1156
  const { order_quantity, total } = values;
1138
1157
  if (!order_quantity) {
1139
1158
  errors.order_quantity = {
@@ -1254,10 +1273,9 @@ var OrderFactory = class {
1254
1273
  }
1255
1274
  }
1256
1275
  };
1257
-
1258
- // src/orderly/useOrderEntry.ts
1259
1276
  var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1260
- const [mutation] = useMutation("/order");
1277
+ const { mutate: mutate2 } = useSWR.useSWRConfig();
1278
+ const [doCreateOrder] = useMutation("/v1/order");
1261
1279
  const { freeCollateral } = useCollateral();
1262
1280
  const symbolInfo = useSymbolsInfo();
1263
1281
  const tokenInfo = useTokenInfo();
@@ -1298,9 +1316,14 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1298
1316
  throw new Error("symbol is null");
1299
1317
  }
1300
1318
  const data = orderCreator.create(values);
1301
- return mutation(__spreadProps(__spreadValues({}, data), {
1319
+ return doCreateOrder(__spreadProps(__spreadValues({}, data), {
1302
1320
  symbol
1303
- }));
1321
+ })).then((res) => {
1322
+ if (res.success) {
1323
+ mutate2("/v1/orders?size=100&page=1$status=NEW");
1324
+ }
1325
+ return res;
1326
+ });
1304
1327
  });
1305
1328
  };
1306
1329
  const calculate = React.useCallback(
@@ -1339,7 +1362,12 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1339
1362
  };
1340
1363
  };
1341
1364
  var useFetures = () => {
1342
- const { data, isLoading, error } = useQuery(`/public/futures`);
1365
+ const { data, isLoading, error } = useQuery(
1366
+ `/v1/public/futures`,
1367
+ {
1368
+ revalidateOnFocus: false
1369
+ }
1370
+ );
1343
1371
  const [sortedData, setSortedData] = React.useState(data);
1344
1372
  const ws = useWebSocketClient();
1345
1373
  React.useEffect(() => {
@@ -1374,11 +1402,13 @@ var useFetures = () => {
1374
1402
 
1375
1403
  // src/orderly/useAccountInfo.ts
1376
1404
  var useAccountInfo = () => {
1377
- return usePrivateQuery("/client/info");
1405
+ return usePrivateQuery("/v1/client/info");
1378
1406
  };
1379
1407
  var useMarketsStream = () => {
1380
1408
  const ws = useWS();
1381
- const { data: futures } = useQuery(`/public/futures`);
1409
+ const { data: futures } = useQuery(`/v1/public/futures`, {
1410
+ revalidateOnFocus: false
1411
+ });
1382
1412
  const { data: tickers } = useSWRSubscription__default.default("tickers", (_, { next }) => {
1383
1413
  const unsubscribe = ws.subscribe(
1384
1414
  // { event: "subscribe", topic: "markprices" },
@@ -1427,12 +1457,15 @@ var useFundingRate = (symbol) => {
1427
1457
  throw new Error("useFuturesForSymbol requires a symbol");
1428
1458
  }
1429
1459
  const [countDown, setCountDown] = React.useState("00:00:00");
1430
- const { data } = useQuery(`/public/funding_rate/${symbol}`, {
1431
- fallbackData: {
1432
- est_funding_rate: 0,
1433
- next_funing_time: 0
1460
+ const { data } = useQuery(
1461
+ `/v1/public/funding_rate/${symbol}`,
1462
+ {
1463
+ fallbackData: {
1464
+ est_funding_rate: 0,
1465
+ next_funing_time: 0
1466
+ }
1434
1467
  }
1435
- });
1468
+ );
1436
1469
  React.useEffect(() => {
1437
1470
  if (!data)
1438
1471
  return;
@@ -1484,7 +1517,9 @@ var useOrderStream = ({
1484
1517
  symbol
1485
1518
  } = {}) => {
1486
1519
  const { data: markPrices = {} } = useMarkPricesStream();
1487
- const res = usePrivateInfiniteQuery(
1520
+ const [doCancelOrder] = useMutation("/v1/order", "DELETE");
1521
+ const [doUpdateOrder] = useMutation("/v1/order", "PUT");
1522
+ const ordersResponse = usePrivateInfiniteQuery(
1488
1523
  (pageIndex, previousPageData) => {
1489
1524
  const search = new URLSearchParams([
1490
1525
  ["size", "100"],
@@ -1494,7 +1529,7 @@ var useOrderStream = ({
1494
1529
  if (symbol) {
1495
1530
  search.set(`symbol`, symbol);
1496
1531
  }
1497
- return `/orders?${search.toString()}`;
1532
+ return `/v1/orders?${search.toString()}`;
1498
1533
  },
1499
1534
  {
1500
1535
  initialSize: 1,
@@ -1505,21 +1540,35 @@ var useOrderStream = ({
1505
1540
  );
1506
1541
  const orders = React.useMemo(() => {
1507
1542
  var _a;
1508
- if (!res.data) {
1543
+ if (!ordersResponse.data) {
1509
1544
  return null;
1510
1545
  }
1511
- return (_a = res.data) == null ? void 0 : _a.flat().map((item) => {
1546
+ return (_a = ordersResponse.data) == null ? void 0 : _a.flat().map((item) => {
1512
1547
  var _a2;
1513
1548
  return __spreadProps(__spreadValues({}, item), {
1514
1549
  mark_price: (_a2 = markPrices[item.symbol]) != null ? _a2 : 0
1515
1550
  });
1516
1551
  });
1517
- }, [res.data, markPrices]);
1552
+ }, [ordersResponse.data, markPrices]);
1518
1553
  const cancelAllOrders = React.useCallback(() => {
1519
- }, [res.data]);
1520
- const updateOrder = React.useCallback((id, data) => {
1554
+ }, [ordersResponse.data]);
1555
+ const updateOrder = React.useCallback((orderId, order2) => {
1556
+ console.log("updateOrder", order2, orderId);
1557
+ return doUpdateOrder(__spreadProps(__spreadValues({}, order2), { order_id: orderId }));
1521
1558
  }, []);
1522
- const cancelOrder = React.useCallback((id) => {
1559
+ const cancelOrder = React.useCallback((orderId, symbol2) => {
1560
+ return doCancelOrder(null, {
1561
+ order_id: orderId,
1562
+ symbol: symbol2
1563
+ }).then((res) => {
1564
+ if (res.success) {
1565
+ return ordersResponse.mutate().then(() => {
1566
+ return res;
1567
+ });
1568
+ } else {
1569
+ throw new Error(res.message);
1570
+ }
1571
+ });
1523
1572
  }, []);
1524
1573
  return [
1525
1574
  orders,
@@ -1543,7 +1592,7 @@ var useTradeStream = (symbol) => {
1543
1592
  throw new Error("useTradeStream: symbol is required");
1544
1593
  }
1545
1594
  const { data, isLoading } = useQuery(
1546
- `/public/market_trades?symbol=${symbol}&limit=20`
1595
+ `/v1/public/market_trades?symbol=${symbol}&limit=20`
1547
1596
  );
1548
1597
  return { data, isLoading };
1549
1598
  };
@@ -1619,14 +1668,14 @@ __export(apis_exports, {
1619
1668
 
1620
1669
  // src/apis/useInfo.ts
1621
1670
  var useInfo = () => {
1622
- return useQuery("/public/info", {
1671
+ return useQuery("/v1/public/info", {
1623
1672
  // refreshInterval: 1000 * 60 * 60 * 24,
1624
1673
  });
1625
1674
  };
1626
1675
 
1627
1676
  // src/apis/useToken.ts
1628
1677
  var useToken = () => {
1629
- return useQuery("/public/token");
1678
+ return useQuery("/v1/public/token");
1630
1679
  };
1631
1680
 
1632
1681
  // src/apis/useFundingRateBySymbol.ts
@@ -1639,7 +1688,7 @@ var useFundingRateBySymbol = (symbol) => {
1639
1688
 
1640
1689
  // src/apis/useFundingRate.ts
1641
1690
  var useFundingRate2 = (symbol) => {
1642
- return useQuery(`/public/funding_rate`);
1691
+ return useQuery(`/v1/public/funding_rate`);
1643
1692
  };
1644
1693
 
1645
1694
  Object.defineProperty(exports, 'useSWR', {