@orderly.network/hooks 2.0.1-alpha.6 → 2.0.1-preview.0
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 +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +70 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +68 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -11
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ export { unstable_serialize, default as useSWR, useSWRConfig } from 'swr';
|
|
|
3
3
|
import { get, WS, mutate as mutate$1 } from '@orderly.network/net';
|
|
4
4
|
import React, { createContext, useContext, useState, useEffect, useCallback, useMemo, useRef, useId, useLayoutEffect } from 'react';
|
|
5
5
|
import useSWRMutation from 'swr/mutation';
|
|
6
|
-
import
|
|
6
|
+
import useConstant from 'use-constant';
|
|
7
7
|
export { default as useConstant } from 'use-constant';
|
|
8
8
|
import { SimpleDI, Account, EventEmitter, EVENT_NAMES, DefaultConfigStore, LocalStorageStore } from '@orderly.network/core';
|
|
9
9
|
import { zero, getGlobalObject, getTimestamp, Decimal, timeConvertString, isTestnet, getPrecisionByNumber, camelCaseToUnderscoreCase, windowGuard, removeTrailingZeros, commify, todpIfNeed } from '@orderly.network/utils';
|
|
@@ -32,9 +32,9 @@ var __export = (target, all) => {
|
|
|
32
32
|
// src/version.ts
|
|
33
33
|
if (typeof window !== "undefined") {
|
|
34
34
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
35
|
-
window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.0.1-
|
|
35
|
+
window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.0.1-preview.0";
|
|
36
36
|
}
|
|
37
|
-
var version_default = "2.0.1-
|
|
37
|
+
var version_default = "2.0.1-preview.0";
|
|
38
38
|
var fetcher = (url, init = {}, queryOptions) => get(url, init, queryOptions?.formatter);
|
|
39
39
|
var OrderlyContext = createContext({
|
|
40
40
|
// configStore: new MemoryConfigStore(),
|
|
@@ -44,6 +44,11 @@ var OrderlyProvider = OrderlyContext.Provider;
|
|
|
44
44
|
// src/useConfig.ts
|
|
45
45
|
function useConfig(key, defaultValue) {
|
|
46
46
|
const { configStore } = useContext(OrderlyContext);
|
|
47
|
+
if (!configStore) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"useConfig must be used within OrderlyConfigProvider or OrderlyAppProvider"
|
|
50
|
+
);
|
|
51
|
+
}
|
|
47
52
|
if (typeof key !== "undefined") {
|
|
48
53
|
if (typeof defaultValue !== "undefined") {
|
|
49
54
|
return configStore.getOr(key, defaultValue);
|
|
@@ -100,7 +105,7 @@ var useAccountInstance = () => {
|
|
|
100
105
|
"keyStore is not defined, please use OrderlyProvider and provide keyStore"
|
|
101
106
|
);
|
|
102
107
|
}
|
|
103
|
-
const account5 =
|
|
108
|
+
const account5 = useConstant(() => {
|
|
104
109
|
let account6 = SimpleDI.get("account");
|
|
105
110
|
if (!account6) {
|
|
106
111
|
account6 = new Account(
|
|
@@ -347,7 +352,7 @@ var usePreLoadData = () => {
|
|
|
347
352
|
};
|
|
348
353
|
};
|
|
349
354
|
var useEventEmitter = () => {
|
|
350
|
-
return
|
|
355
|
+
return useConstant(() => {
|
|
351
356
|
let ee = SimpleDI.get("EE");
|
|
352
357
|
if (!ee) {
|
|
353
358
|
ee = new EventEmitter();
|
|
@@ -456,7 +461,7 @@ function useLocalStorage(key, initialValue) {
|
|
|
456
461
|
var WS_NAME = "nativeWebsocketClient";
|
|
457
462
|
var useWS = () => {
|
|
458
463
|
const { configStore } = useContext(OrderlyContext);
|
|
459
|
-
const ws =
|
|
464
|
+
const ws = useConstant(() => {
|
|
460
465
|
let websocketClient = SimpleDI.get(WS_NAME);
|
|
461
466
|
const account5 = SimpleDI.get(Account.instanceName);
|
|
462
467
|
if (!websocketClient) {
|
|
@@ -1697,7 +1702,7 @@ var IndexPriceCalculator = class extends BaseCalculator {
|
|
|
1697
1702
|
// src/useCalculatorService.ts
|
|
1698
1703
|
var useCalculatorService = () => {
|
|
1699
1704
|
const { get: get3 } = useSimpleDI();
|
|
1700
|
-
const calculatorService =
|
|
1705
|
+
const calculatorService = useConstant(() => {
|
|
1701
1706
|
let calculatorService2 = get3(CalculatorServiceID);
|
|
1702
1707
|
if (!calculatorService2) {
|
|
1703
1708
|
const positionCalculator = new PositionCalculator();
|
|
@@ -2731,9 +2736,12 @@ var filterMarkets = (params) => {
|
|
|
2731
2736
|
isFavorite: type == 0 /* FAVORITES */ ? true : favoriteKeys.includes(item.symbol)
|
|
2732
2737
|
}));
|
|
2733
2738
|
};
|
|
2739
|
+
function isEmpty(value) {
|
|
2740
|
+
return value === void 0 || value === null;
|
|
2741
|
+
}
|
|
2734
2742
|
function get8hFunding2(est_funding_rate, funding_period) {
|
|
2735
2743
|
let funding8h = 0;
|
|
2736
|
-
if (est_funding_rate
|
|
2744
|
+
if (isEmpty(est_funding_rate)) {
|
|
2737
2745
|
return null;
|
|
2738
2746
|
}
|
|
2739
2747
|
if (funding_period) {
|
|
@@ -2746,7 +2754,7 @@ function get24hChange2(params) {
|
|
|
2746
2754
|
if (change !== void 0) {
|
|
2747
2755
|
return change;
|
|
2748
2756
|
}
|
|
2749
|
-
if (close
|
|
2757
|
+
if (!isEmpty(close) && !isEmpty(open)) {
|
|
2750
2758
|
if (open === 0) {
|
|
2751
2759
|
return 0;
|
|
2752
2760
|
}
|
|
@@ -3474,7 +3482,7 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
|
|
|
3474
3482
|
return 0;
|
|
3475
3483
|
const getSymbolInfo = symbolInfo[symbol];
|
|
3476
3484
|
const filterAlgoOrders = orders.filter(
|
|
3477
|
-
(item) => item.algo_order_id === void 0
|
|
3485
|
+
(item) => item.algo_order_id === void 0 || item.algo_type === "BRACKET"
|
|
3478
3486
|
);
|
|
3479
3487
|
const buyOrdersQty = account.getQtyFromOrdersBySide(
|
|
3480
3488
|
filterAlgoOrders,
|
|
@@ -4104,6 +4112,7 @@ var useDeposit = (options) => {
|
|
|
4104
4112
|
}, [account5, fetchBalance, quantity, depositFee, options?.address]);
|
|
4105
4113
|
const loopGetBalance = async () => {
|
|
4106
4114
|
getBalanceListener.current && clearTimeout(getBalanceListener.current);
|
|
4115
|
+
const time = account5.walletAdapter?.chainNamespace === ChainNamespace.solana ? 1e4 : 3e3;
|
|
4107
4116
|
getBalanceListener.current = setTimeout(async () => {
|
|
4108
4117
|
try {
|
|
4109
4118
|
const balance2 = await fetchBalanceHandler(
|
|
@@ -4114,7 +4123,7 @@ var useDeposit = (options) => {
|
|
|
4114
4123
|
loopGetBalance();
|
|
4115
4124
|
} catch (err) {
|
|
4116
4125
|
}
|
|
4117
|
-
},
|
|
4126
|
+
}, time);
|
|
4118
4127
|
};
|
|
4119
4128
|
const getDepositFee = useCallback(
|
|
4120
4129
|
async (quantity2) => {
|
|
@@ -4493,19 +4502,35 @@ function tpslCalculateHelper(key, inputs, options = {}) {
|
|
|
4493
4502
|
// }),
|
|
4494
4503
|
};
|
|
4495
4504
|
}
|
|
4496
|
-
function checkNotional(
|
|
4497
|
-
|
|
4505
|
+
function checkNotional(props) {
|
|
4506
|
+
const { price, base_tick, qty, min_notional, base_dp, quote_dp, quote_tick } = props;
|
|
4507
|
+
if (price !== void 0 && qty !== void 0 && min_notional !== void 0) {
|
|
4498
4508
|
try {
|
|
4499
4509
|
const calcNotional = new Decimal(price).mul(new Decimal(qty)).toNumber();
|
|
4500
|
-
const notional = Number.parseFloat(`${
|
|
4501
|
-
|
|
4502
|
-
|
|
4510
|
+
const notional = Number.parseFloat(`${min_notional}`);
|
|
4511
|
+
if (calcNotional < notional) {
|
|
4512
|
+
let minQty = new Decimal(notional).div(price).toDecimalPlaces(base_dp, Decimal.ROUND_DOWN).add(base_tick ?? 0);
|
|
4513
|
+
if (base_tick && base_tick > 0) {
|
|
4514
|
+
minQty = new Decimal(
|
|
4515
|
+
getRoundedDownDivision(minQty.toNumber(), base_tick)
|
|
4516
|
+
);
|
|
4517
|
+
}
|
|
4518
|
+
const newMinNotional = minQty.mul(price).add(quote_tick ?? 0).toFixed(quote_dp);
|
|
4519
|
+
return `The order value should be greater or equal to ${newMinNotional} USDC`;
|
|
4520
|
+
}
|
|
4521
|
+
return void 0;
|
|
4503
4522
|
} catch (e) {
|
|
4504
4523
|
return void 0;
|
|
4505
4524
|
}
|
|
4506
4525
|
}
|
|
4507
4526
|
return void 0;
|
|
4508
4527
|
}
|
|
4528
|
+
function getRoundedDownDivision(value, tick) {
|
|
4529
|
+
const decimalValue = new Decimal(value);
|
|
4530
|
+
const decimalTick = new Decimal(tick);
|
|
4531
|
+
const quotient = decimalValue.dividedToIntegerBy(decimalTick);
|
|
4532
|
+
return quotient.mul(decimalTick).toNumber();
|
|
4533
|
+
}
|
|
4509
4534
|
|
|
4510
4535
|
// src/services/orderCreator/baseCreator.ts
|
|
4511
4536
|
var BaseOrderCreator = class {
|
|
@@ -4535,12 +4560,12 @@ var BaseOrderCreator = class {
|
|
|
4535
4560
|
const errors = {};
|
|
4536
4561
|
const { maxQty, symbol, markPrice } = configs;
|
|
4537
4562
|
let { order_quantity, total, order_price, reduce_only, order_type } = values2;
|
|
4538
|
-
const { min_notional } = symbol || {};
|
|
4563
|
+
const { min_notional, base_tick, quote_dp, quote_tick, base_dp } = symbol || {};
|
|
4539
4564
|
if (!order_quantity) {
|
|
4540
4565
|
if (total && order_price) {
|
|
4541
|
-
const { quote_dp } = configs.symbol;
|
|
4566
|
+
const { quote_dp: quote_dp2 } = configs.symbol;
|
|
4542
4567
|
const totalNumber = new Decimal(total);
|
|
4543
|
-
const qty = totalNumber.dividedBy(order_price).toFixed(
|
|
4568
|
+
const qty = totalNumber.dividedBy(order_price).toFixed(quote_dp2);
|
|
4544
4569
|
order_quantity = qty;
|
|
4545
4570
|
}
|
|
4546
4571
|
}
|
|
@@ -4550,26 +4575,34 @@ var BaseOrderCreator = class {
|
|
|
4550
4575
|
message: "Quantity is required"
|
|
4551
4576
|
};
|
|
4552
4577
|
} else {
|
|
4553
|
-
const { base_min, quote_dp, base_dp } = configs.symbol;
|
|
4578
|
+
const { base_min, quote_dp: quote_dp2, base_dp: base_dp2 } = configs.symbol;
|
|
4554
4579
|
const qty = new Decimal(order_quantity);
|
|
4555
4580
|
if (qty.lt(base_min)) {
|
|
4556
4581
|
errors.order_quantity = {
|
|
4557
4582
|
type: "min",
|
|
4558
4583
|
message: `Quantity must be greater than ${new Decimal(base_min).todp(
|
|
4559
|
-
|
|
4584
|
+
base_dp2
|
|
4560
4585
|
)}`
|
|
4561
4586
|
};
|
|
4562
4587
|
} else if (qty.gt(maxQty)) {
|
|
4563
4588
|
errors.order_quantity = {
|
|
4564
4589
|
type: "max",
|
|
4565
4590
|
message: `Quantity must be less than ${new Decimal(maxQty).todp(
|
|
4566
|
-
|
|
4591
|
+
base_dp2
|
|
4567
4592
|
)}`
|
|
4568
4593
|
};
|
|
4569
4594
|
}
|
|
4570
4595
|
}
|
|
4571
4596
|
const price = `${order_type}`.includes("MARKET") ? markPrice : order_price;
|
|
4572
|
-
const notionalHintStr = checkNotional(
|
|
4597
|
+
const notionalHintStr = checkNotional({
|
|
4598
|
+
base_tick,
|
|
4599
|
+
quote_tick,
|
|
4600
|
+
price,
|
|
4601
|
+
qty: order_quantity,
|
|
4602
|
+
min_notional,
|
|
4603
|
+
quote_dp,
|
|
4604
|
+
base_dp
|
|
4605
|
+
});
|
|
4573
4606
|
if (notionalHintStr !== void 0 && !reduce_only) {
|
|
4574
4607
|
errors.total = {
|
|
4575
4608
|
type: "min",
|
|
@@ -5842,7 +5875,7 @@ function useMaintenanceStatus() {
|
|
|
5842
5875
|
const [startTime, setStartTime] = useState();
|
|
5843
5876
|
const [endTime, setEndTime] = useState();
|
|
5844
5877
|
const [brokerName, setBrokerName] = useState("Orderly network");
|
|
5845
|
-
const { data: systemInfo, mutate: mutate3 } = useQuery(`/v1/public/system_info`, {
|
|
5878
|
+
const { data: systemInfo, mutate: mutate3 } = useQuery(`/v1/public/system_info?source=maintenance`, {
|
|
5846
5879
|
revalidateOnFocus: false,
|
|
5847
5880
|
errorRetryCount: 2,
|
|
5848
5881
|
errorRetryInterval: 200
|
|
@@ -5850,18 +5883,18 @@ function useMaintenanceStatus() {
|
|
|
5850
5883
|
const ws = useWS();
|
|
5851
5884
|
const config = useConfig();
|
|
5852
5885
|
useEffect(() => {
|
|
5853
|
-
if (!systemInfo
|
|
5886
|
+
if (!systemInfo) {
|
|
5854
5887
|
return;
|
|
5855
5888
|
}
|
|
5856
5889
|
const brokerName2 = config.get("brokerName");
|
|
5857
5890
|
if (brokerName2) {
|
|
5858
5891
|
setBrokerName(brokerName2);
|
|
5859
5892
|
}
|
|
5860
|
-
if (systemInfo.
|
|
5861
|
-
setStartTime(systemInfo.
|
|
5862
|
-
setEndTime(systemInfo.
|
|
5893
|
+
if (systemInfo.scheduled_maintenance) {
|
|
5894
|
+
setStartTime(systemInfo.scheduled_maintenance.start_time);
|
|
5895
|
+
setEndTime(systemInfo.scheduled_maintenance.end_time);
|
|
5863
5896
|
}
|
|
5864
|
-
if (systemInfo.
|
|
5897
|
+
if (systemInfo.status === 2) {
|
|
5865
5898
|
setStatus(2);
|
|
5866
5899
|
}
|
|
5867
5900
|
}, [systemInfo, config]);
|
|
@@ -6332,20 +6365,20 @@ var OrderlyConfigProvider = (props) => {
|
|
|
6332
6365
|
"If you have provided a custom `configStore` and the `brokerId` is set in the `configStore`, please remove the `brokerId` prop."
|
|
6333
6366
|
);
|
|
6334
6367
|
}
|
|
6335
|
-
const innerConfigStore =
|
|
6368
|
+
const innerConfigStore = useMemo(() => {
|
|
6336
6369
|
return new ProxyConfigStore(
|
|
6337
6370
|
configStore || new ExtendedConfigStore({ brokerId, brokerName, networkId })
|
|
6338
6371
|
);
|
|
6339
|
-
});
|
|
6340
|
-
const innerKeyStore =
|
|
6372
|
+
}, [configStore, brokerId, brokerName, networkId]);
|
|
6373
|
+
const innerKeyStore = useMemo(() => {
|
|
6341
6374
|
return keyStore || new LocalStorageStore(networkId);
|
|
6342
|
-
});
|
|
6343
|
-
const innerWalletAdapters =
|
|
6375
|
+
}, [networkId, keyStore]);
|
|
6376
|
+
const innerWalletAdapters = useMemo(() => {
|
|
6344
6377
|
return walletAdapters || [
|
|
6345
6378
|
new DefaultEVMWalletAdapter(new EthersProvider()),
|
|
6346
6379
|
new DefaultSolanaWalletAdapter()
|
|
6347
6380
|
];
|
|
6348
|
-
});
|
|
6381
|
+
}, [walletAdapters]);
|
|
6349
6382
|
useLayoutEffect(() => {
|
|
6350
6383
|
let account6 = SimpleDI.get(Account.instanceName);
|
|
6351
6384
|
if (!account6) {
|