@orderly.network/hooks 1.0.29 → 1.1.1
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 +168 -48
- package/dist/index.d.ts +168 -48
- package/dist/index.js +1268 -353
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1269 -357
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -28,9 +28,9 @@ var useSWRSubscription__default = /*#__PURE__*/_interopDefault(useSWRSubscriptio
|
|
|
28
28
|
// src/version.ts
|
|
29
29
|
if (typeof window !== "undefined") {
|
|
30
30
|
window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
|
|
31
|
-
window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.
|
|
31
|
+
window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "1.1.1";
|
|
32
32
|
}
|
|
33
|
-
var version_default = "1.
|
|
33
|
+
var version_default = "1.1.1";
|
|
34
34
|
var fetcher = (url, init = {}, queryOptions) => net.get(url, init, queryOptions?.formatter);
|
|
35
35
|
var OrderlyContext = React.createContext({
|
|
36
36
|
// configStore: new MemoryConfigStore(),
|
|
@@ -201,7 +201,6 @@ var signatureMiddleware = (useSWRNext) => {
|
|
|
201
201
|
};
|
|
202
202
|
return useSWRNext(key, extendedFetcher, config);
|
|
203
203
|
} catch (e) {
|
|
204
|
-
console.error("signature error:", e);
|
|
205
204
|
throw e;
|
|
206
205
|
}
|
|
207
206
|
};
|
|
@@ -346,23 +345,16 @@ function useSessionStorage(key, initialValue) {
|
|
|
346
345
|
const item = window.sessionStorage.getItem(key);
|
|
347
346
|
return item ? parseJSON(item) : initialValue;
|
|
348
347
|
} catch (error) {
|
|
349
|
-
console.warn(`Error reading sessionStorage key \u201C${key}\u201D:`, error);
|
|
350
348
|
return initialValue;
|
|
351
349
|
}
|
|
352
350
|
}, [initialValue, key]);
|
|
353
351
|
const [storedValue, setStoredValue] = React.useState(readValue);
|
|
354
352
|
const setValue = (value) => {
|
|
355
|
-
if (typeof window == "undefined") {
|
|
356
|
-
console.warn(
|
|
357
|
-
`Tried setting sessionStorage key \u201C${key}\u201D even though environment is not a client`
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
353
|
try {
|
|
361
354
|
const newValue = value instanceof Function ? value(storedValue) : value;
|
|
362
355
|
window.sessionStorage.setItem(key, JSON.stringify(newValue));
|
|
363
356
|
setStoredValue(newValue);
|
|
364
357
|
} catch (error) {
|
|
365
|
-
console.warn(`Error setting sessionStorage key \u201C${key}\u201D:`, error);
|
|
366
358
|
}
|
|
367
359
|
};
|
|
368
360
|
React.useEffect(() => {
|
|
@@ -397,24 +389,18 @@ function useLocalStorage(key, initialValue) {
|
|
|
397
389
|
const item = window.localStorage.getItem(key);
|
|
398
390
|
return item ? parseJSON(item) : initialValue;
|
|
399
391
|
} catch (error) {
|
|
400
|
-
console.warn(`Error reading localStorage key \u201C${key}\u201D:`, error);
|
|
401
392
|
return initialValue;
|
|
402
393
|
}
|
|
403
394
|
}, [initialValue, key]);
|
|
404
395
|
const [storedValue, setStoredValue] = React.useState(readValue);
|
|
405
396
|
const setValue = React.useCallback(
|
|
406
397
|
(value) => {
|
|
407
|
-
if (typeof window === "undefined") {
|
|
408
|
-
console.warn(
|
|
409
|
-
`Tried setting localStorage key \u201C${key}\u201D even though environment is not a client`
|
|
410
|
-
);
|
|
411
|
-
}
|
|
412
398
|
try {
|
|
413
399
|
const newValue = value instanceof Function ? value(storedValue) : value;
|
|
414
400
|
window.localStorage.setItem(key, JSON.stringify(newValue));
|
|
401
|
+
window.dispatchEvent(new Event("storage"));
|
|
415
402
|
setStoredValue(() => newValue);
|
|
416
403
|
} catch (error) {
|
|
417
|
-
console.warn(`Error setting localStorage key \u201C${key}\u201D:`, error);
|
|
418
404
|
}
|
|
419
405
|
},
|
|
420
406
|
[storedValue]
|
|
@@ -422,6 +408,18 @@ function useLocalStorage(key, initialValue) {
|
|
|
422
408
|
React.useEffect(() => {
|
|
423
409
|
setStoredValue(readValue());
|
|
424
410
|
}, []);
|
|
411
|
+
React.useEffect(() => {
|
|
412
|
+
const handleStorageChange = (event) => {
|
|
413
|
+
if (event?.key && event.key !== key) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
setStoredValue(readValue());
|
|
417
|
+
};
|
|
418
|
+
window.addEventListener("storage", handleStorageChange);
|
|
419
|
+
return () => {
|
|
420
|
+
window.removeEventListener("storage", handleStorageChange);
|
|
421
|
+
};
|
|
422
|
+
}, [key]);
|
|
425
423
|
return [storedValue, setValue];
|
|
426
424
|
}
|
|
427
425
|
var WS_NAME = "nativeWebsocketClient";
|
|
@@ -459,36 +457,48 @@ var useWS = () => {
|
|
|
459
457
|
});
|
|
460
458
|
return ws;
|
|
461
459
|
};
|
|
462
|
-
var usePrivateDataObserver = () => {
|
|
460
|
+
var usePrivateDataObserver = (options) => {
|
|
463
461
|
const ws = useWS();
|
|
464
462
|
const { mutate: mutate2 } = useSWR.useSWRConfig();
|
|
465
463
|
const ee = useEventEmitter();
|
|
466
464
|
const { state } = useAccount();
|
|
467
|
-
const updateOrders = useDebounce.useDebouncedCallback(() => {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
state.accountId
|
|
478
|
-
])
|
|
479
|
-
);
|
|
465
|
+
const updateOrders = useDebounce.useDebouncedCallback((data) => {
|
|
466
|
+
const map = options.getKeysMap("orders");
|
|
467
|
+
map.forEach((getKey, key) => {
|
|
468
|
+
mutate2(
|
|
469
|
+
useSWRInfinite.unstable_serialize((index, prevData) => [
|
|
470
|
+
getKey(index, prevData),
|
|
471
|
+
state.accountId
|
|
472
|
+
])
|
|
473
|
+
);
|
|
474
|
+
});
|
|
480
475
|
}, 500);
|
|
481
476
|
React.useEffect(() => {
|
|
482
477
|
if (!state.accountId)
|
|
483
478
|
return;
|
|
484
479
|
const unsubscribe = ws.privateSubscribe("executionreport", {
|
|
485
480
|
onMessage: (data) => {
|
|
486
|
-
updateOrders();
|
|
481
|
+
updateOrders(data);
|
|
487
482
|
ee.emit("orders:changed", data);
|
|
488
483
|
}
|
|
489
484
|
});
|
|
490
485
|
return () => unsubscribe?.();
|
|
491
486
|
}, [state.accountId]);
|
|
487
|
+
React.useEffect(() => {
|
|
488
|
+
if (!state.accountId)
|
|
489
|
+
return;
|
|
490
|
+
const unsubscribe = ws.privateSubscribe("algoexecutionreport", {
|
|
491
|
+
onMessage: (data) => {
|
|
492
|
+
updateOrders(data);
|
|
493
|
+
if (Array.isArray(data)) {
|
|
494
|
+
data.forEach((item) => ee.emit("orders:changed", { ...item, status: item.algoStatus }));
|
|
495
|
+
} else {
|
|
496
|
+
ee.emit("orders:changed", { ...data, status: data.algoStatus });
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
return () => unsubscribe?.();
|
|
501
|
+
}, [state.accountId]);
|
|
492
502
|
React.useEffect(() => {
|
|
493
503
|
if (!state.accountId)
|
|
494
504
|
return;
|
|
@@ -547,15 +557,31 @@ var usePrivateDataObserver = () => {
|
|
|
547
557
|
var DataCenterContext = React.createContext(
|
|
548
558
|
{}
|
|
549
559
|
);
|
|
560
|
+
var useDataCenterContext = () => React.useContext(DataCenterContext);
|
|
550
561
|
var DataCenterProvider = ({ children }) => {
|
|
551
562
|
const { error, done } = usePreLoadData();
|
|
552
|
-
|
|
563
|
+
const getKeyHandlerMapRef = React.useRef(/* @__PURE__ */ new Map());
|
|
564
|
+
usePrivateDataObserver({
|
|
565
|
+
getKeysMap(type) {
|
|
566
|
+
return getKeyHandlerMapRef.current;
|
|
567
|
+
}
|
|
568
|
+
});
|
|
553
569
|
if (error) {
|
|
554
570
|
return /* @__PURE__ */ jsxRuntime.jsx("div", { children: "Data load failed" });
|
|
555
571
|
}
|
|
556
572
|
if (!done)
|
|
557
573
|
return null;
|
|
558
|
-
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
574
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
575
|
+
DataCenterContext.Provider,
|
|
576
|
+
{
|
|
577
|
+
value: {
|
|
578
|
+
regesterKeyHandler: (key, fun) => {
|
|
579
|
+
getKeyHandlerMapRef.current.set(key, fun);
|
|
580
|
+
}
|
|
581
|
+
},
|
|
582
|
+
children
|
|
583
|
+
}
|
|
584
|
+
);
|
|
559
585
|
};
|
|
560
586
|
var OrderlyConfigProvider = (props) => {
|
|
561
587
|
const [account5, setAccount] = React__default.default.useState(null);
|
|
@@ -568,9 +594,6 @@ var OrderlyConfigProvider = (props) => {
|
|
|
568
594
|
enableSwapDeposit,
|
|
569
595
|
contracts
|
|
570
596
|
} = props;
|
|
571
|
-
if (!brokerId && typeof configStore === "undefined") {
|
|
572
|
-
console.error("[OrderlyConfigProvider]: brokerId is required");
|
|
573
|
-
}
|
|
574
597
|
const innerConfigStore = useConstant4__default.default(() => {
|
|
575
598
|
return configStore || new core.DefaultConfigStore({ brokerId, networkId });
|
|
576
599
|
});
|
|
@@ -794,7 +817,7 @@ var reduceItems = (depth, level, data, asks = false) => {
|
|
|
794
817
|
const result = [];
|
|
795
818
|
if (typeof depth !== "undefined") {
|
|
796
819
|
const prices = /* @__PURE__ */ new Map();
|
|
797
|
-
for (let i = 0; i < data.length; i++) {
|
|
820
|
+
for (let i = 0; i < ramda.min(level, data.length); i++) {
|
|
798
821
|
const [price, quantity] = data[i];
|
|
799
822
|
if (isNaN(price) || isNaN(quantity))
|
|
800
823
|
continue;
|
|
@@ -829,7 +852,8 @@ var reduceItems = (depth, level, data, asks = false) => {
|
|
|
829
852
|
if (isNaN(price) || isNaN(quantity))
|
|
830
853
|
continue;
|
|
831
854
|
const newQuantity = new utils.Decimal(quantity).add(result.length > 0 ? result[result.length - 1][2] : 0).toNumber();
|
|
832
|
-
|
|
855
|
+
const newAmount = new utils.Decimal(quantity * price).add(result.length > 0 ? result[result.length - 1][3] : 0).toNumber();
|
|
856
|
+
result.push([price, quantity, newQuantity, newAmount]);
|
|
833
857
|
}
|
|
834
858
|
return result;
|
|
835
859
|
};
|
|
@@ -860,9 +884,11 @@ var reduceOrderbook = (depth, level, data) => {
|
|
|
860
884
|
}
|
|
861
885
|
}
|
|
862
886
|
asks = asks.reverse();
|
|
887
|
+
asks = asks.length < level ? paddingFn(level - asks.length).concat(asks) : asks;
|
|
888
|
+
bids = bids.length < level ? bids.concat(paddingFn(level - bids.length)) : bids;
|
|
863
889
|
return {
|
|
864
|
-
asks
|
|
865
|
-
bids
|
|
890
|
+
asks,
|
|
891
|
+
bids
|
|
866
892
|
};
|
|
867
893
|
};
|
|
868
894
|
var mergeItems = (data, update) => {
|
|
@@ -902,7 +928,7 @@ var mergeOrderbook = (data, update) => {
|
|
|
902
928
|
var INIT_DATA = { asks: [], bids: [] };
|
|
903
929
|
var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
|
|
904
930
|
if (!symbol) {
|
|
905
|
-
throw new
|
|
931
|
+
throw new types.SDKError("useOrderbookStream requires a symbol");
|
|
906
932
|
}
|
|
907
933
|
const level = options?.level ?? 10;
|
|
908
934
|
const [requestData, setRequestData] = React.useState(null);
|
|
@@ -1010,6 +1036,12 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
|
|
|
1010
1036
|
asks: [...data.asks],
|
|
1011
1037
|
bids: [...data.bids]
|
|
1012
1038
|
});
|
|
1039
|
+
React.useEffect(() => {
|
|
1040
|
+
eventEmitter.emit("orderbook:update", [
|
|
1041
|
+
reducedData.asks[0][0],
|
|
1042
|
+
reducedData.bids[0][0]
|
|
1043
|
+
]);
|
|
1044
|
+
}, [reducedData.asks[0][0], reducedData.bids[0][0]]);
|
|
1013
1045
|
return [
|
|
1014
1046
|
{
|
|
1015
1047
|
asks: reducedData.asks.slice(-level),
|
|
@@ -1020,13 +1052,28 @@ var useOrderbookStream = (symbol, initial = INIT_DATA, options) => {
|
|
|
1020
1052
|
{ onDepthChange, depth, allDepths: depths, isLoading, onItemClick }
|
|
1021
1053
|
];
|
|
1022
1054
|
};
|
|
1023
|
-
var needNumberOnlyFields = [
|
|
1055
|
+
var needNumberOnlyFields = [
|
|
1056
|
+
"order_quantity",
|
|
1057
|
+
"order_price",
|
|
1058
|
+
"total"
|
|
1059
|
+
];
|
|
1060
|
+
var cleanStringStyle = (str) => {
|
|
1061
|
+
if (typeof str !== "string") {
|
|
1062
|
+
str = str.toString();
|
|
1063
|
+
}
|
|
1064
|
+
str = str.replace(/,/g, "");
|
|
1065
|
+
str = str.replace(/[^\d.]/g, "").replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
|
|
1066
|
+
return str;
|
|
1067
|
+
};
|
|
1024
1068
|
function baseInputHandle(inputs) {
|
|
1025
1069
|
let [values, input, value, markPrice, config] = inputs;
|
|
1070
|
+
needNumberOnlyFields.forEach((field) => {
|
|
1071
|
+
if (typeof values[field] !== "undefined") {
|
|
1072
|
+
values[field] = cleanStringStyle(values[field]);
|
|
1073
|
+
}
|
|
1074
|
+
});
|
|
1026
1075
|
if (needNumberOnlyFields.includes(input)) {
|
|
1027
|
-
value = value
|
|
1028
|
-
value = value.replace(/,/g, "");
|
|
1029
|
-
value = value.replace(/[^\d.]/g, "");
|
|
1076
|
+
value = cleanStringStyle(value);
|
|
1030
1077
|
}
|
|
1031
1078
|
return [
|
|
1032
1079
|
{
|
|
@@ -1056,15 +1103,23 @@ function priceInputHandle(inputs) {
|
|
|
1056
1103
|
values.order_price = price.toDecimalPlaces(config.quoteDP).toString();
|
|
1057
1104
|
}
|
|
1058
1105
|
price.toDecimalPlaces(Math.min(priceDP, config.quoteDP));
|
|
1059
|
-
if (!values.order_quantity) {
|
|
1106
|
+
if (!values.order_quantity && !values.total) {
|
|
1060
1107
|
return [values, input, value, markPrice, config];
|
|
1061
1108
|
}
|
|
1062
|
-
const
|
|
1109
|
+
const newValue = {
|
|
1110
|
+
...values
|
|
1111
|
+
};
|
|
1112
|
+
if (values.order_quantity) {
|
|
1113
|
+
newValue.total = price.mul(values.order_quantity).todp(2).toString();
|
|
1114
|
+
} else if (values.total) {
|
|
1115
|
+
newValue.order_quantity = new utils.Decimal(values.total).div(price).todp(config.baseDP).toString();
|
|
1116
|
+
}
|
|
1063
1117
|
return [
|
|
1064
|
-
{
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
},
|
|
1118
|
+
// {
|
|
1119
|
+
// ...values,
|
|
1120
|
+
// total: total.todp(2).toString(),
|
|
1121
|
+
// },
|
|
1122
|
+
newValue,
|
|
1068
1123
|
input,
|
|
1069
1124
|
value,
|
|
1070
1125
|
markPrice,
|
|
@@ -1082,11 +1137,11 @@ function quantityInputHandle(inputs) {
|
|
|
1082
1137
|
quantity = quantity.toDecimalPlaces(config.baseDP);
|
|
1083
1138
|
values.order_quantity = quantity.toNumber();
|
|
1084
1139
|
}
|
|
1085
|
-
if (values.order_type === types.OrderType.MARKET) {
|
|
1140
|
+
if (values.order_type === types.OrderType.MARKET || values.order_type === types.OrderType.STOP_MARKET) {
|
|
1086
1141
|
const price = markPrice;
|
|
1087
1142
|
values.total = quantity.mul(price).todp(2).toNumber();
|
|
1088
1143
|
}
|
|
1089
|
-
if (values.order_type === types.OrderType.LIMIT) {
|
|
1144
|
+
if (values.order_type === types.OrderType.LIMIT || values.order_type === types.OrderType.STOP_LIMIT) {
|
|
1090
1145
|
if (values.order_price) {
|
|
1091
1146
|
const price = Number(values.order_price);
|
|
1092
1147
|
const total = quantity.mul(price);
|
|
@@ -1112,7 +1167,7 @@ function totalInputHandle(inputs) {
|
|
|
1112
1167
|
return [{ ...values, order_quantity: "" }, input, value, markPrice, config];
|
|
1113
1168
|
}
|
|
1114
1169
|
let price = markPrice;
|
|
1115
|
-
if (values.order_type === types.OrderType.LIMIT && !!values.order_price) {
|
|
1170
|
+
if ((values.order_type === types.OrderType.LIMIT || values.order_type === types.OrderType.STOP_LIMIT) && !!values.order_price) {
|
|
1116
1171
|
price = Number(values.order_price);
|
|
1117
1172
|
}
|
|
1118
1173
|
let total = new utils.Decimal(value);
|
|
@@ -1220,6 +1275,60 @@ var parseHolding = (holding, markPrices) => {
|
|
|
1220
1275
|
});
|
|
1221
1276
|
return [USDC_holding, nonUSDC];
|
|
1222
1277
|
};
|
|
1278
|
+
var useMarketsStream = () => {
|
|
1279
|
+
const ws = useWS();
|
|
1280
|
+
const { data: futures } = useQuery(`/v1/public/futures`, {
|
|
1281
|
+
revalidateOnFocus: false
|
|
1282
|
+
});
|
|
1283
|
+
const { data: tickers } = useSWRSubscription__default.default("tickers", (_, { next }) => {
|
|
1284
|
+
const unsubscribe = ws.subscribe(
|
|
1285
|
+
// { event: "subscribe", topic: "markprices" },
|
|
1286
|
+
"tickers",
|
|
1287
|
+
{
|
|
1288
|
+
onMessage: (message) => {
|
|
1289
|
+
next(null, message);
|
|
1290
|
+
}
|
|
1291
|
+
// onUnsubscribe: () => {
|
|
1292
|
+
// return "markprices";
|
|
1293
|
+
// },
|
|
1294
|
+
// onError: (error: any) => {
|
|
1295
|
+
//
|
|
1296
|
+
// },
|
|
1297
|
+
}
|
|
1298
|
+
);
|
|
1299
|
+
return () => {
|
|
1300
|
+
unsubscribe?.();
|
|
1301
|
+
};
|
|
1302
|
+
});
|
|
1303
|
+
const value = React.useMemo(() => {
|
|
1304
|
+
if (!futures)
|
|
1305
|
+
return null;
|
|
1306
|
+
if (!tickers)
|
|
1307
|
+
return futures;
|
|
1308
|
+
return futures.map((item) => {
|
|
1309
|
+
const ticker = tickers.find(
|
|
1310
|
+
(t) => t.symbol === item.symbol
|
|
1311
|
+
);
|
|
1312
|
+
if (ticker) {
|
|
1313
|
+
const data = {
|
|
1314
|
+
...item,
|
|
1315
|
+
["24h_close"]: ticker.close,
|
|
1316
|
+
["24h_open"]: ticker.open,
|
|
1317
|
+
["24h_volumn"]: ticker.volume,
|
|
1318
|
+
change: 0
|
|
1319
|
+
};
|
|
1320
|
+
if (ticker.close !== void 0 && ticker.open !== void 0) {
|
|
1321
|
+
data["change"] = new utils.Decimal(ticker.close).minus(ticker.open).div(ticker.open).toNumber();
|
|
1322
|
+
}
|
|
1323
|
+
return data;
|
|
1324
|
+
}
|
|
1325
|
+
return item;
|
|
1326
|
+
});
|
|
1327
|
+
}, [futures, tickers]);
|
|
1328
|
+
return { data: value };
|
|
1329
|
+
};
|
|
1330
|
+
|
|
1331
|
+
// src/orderly/usePositionStream.ts
|
|
1223
1332
|
var usePositionStream = (symbol, options) => {
|
|
1224
1333
|
const symbolInfo = useSymbolsInfo();
|
|
1225
1334
|
const { data: accountInfo } = usePrivateQuery("/v1/client/info");
|
|
@@ -1235,7 +1344,7 @@ var usePositionStream = (symbol, options) => {
|
|
|
1235
1344
|
const {
|
|
1236
1345
|
data,
|
|
1237
1346
|
error,
|
|
1238
|
-
mutate:
|
|
1347
|
+
mutate: refreshPositions
|
|
1239
1348
|
} = usePrivateQuery(`/v1/positions`, {
|
|
1240
1349
|
// revalidateOnFocus: false,
|
|
1241
1350
|
// revalidateOnReconnect: false,
|
|
@@ -1248,6 +1357,20 @@ var usePositionStream = (symbol, options) => {
|
|
|
1248
1357
|
}
|
|
1249
1358
|
});
|
|
1250
1359
|
const { data: markPrices } = useMarkPricesStream();
|
|
1360
|
+
const [priceMode, setPriceMode] = React.useState(options?.calcMode || "markPrice");
|
|
1361
|
+
React.useEffect(() => {
|
|
1362
|
+
if (options?.calcMode && priceMode !== options?.calcMode) {
|
|
1363
|
+
setPriceMode(options?.calcMode);
|
|
1364
|
+
}
|
|
1365
|
+
}, [options?.calcMode]);
|
|
1366
|
+
const { data: tickers } = useMarketsStream();
|
|
1367
|
+
const tickerPrices = React.useMemo(() => {
|
|
1368
|
+
const data2 = /* @__PURE__ */ Object.create(null);
|
|
1369
|
+
tickers?.forEach((item) => {
|
|
1370
|
+
data2[item.symbol] = item["24h_close"];
|
|
1371
|
+
});
|
|
1372
|
+
return data2;
|
|
1373
|
+
}, [tickers]);
|
|
1251
1374
|
const formatedPositions = React.useMemo(() => {
|
|
1252
1375
|
if (!data?.rows || !symbolInfo || !accountInfo)
|
|
1253
1376
|
return null;
|
|
@@ -1256,6 +1379,11 @@ var usePositionStream = (symbol, options) => {
|
|
|
1256
1379
|
});
|
|
1257
1380
|
let unrealPnL_total = utils.zero, notional_total = utils.zero, unsettlementPnL_total = utils.zero;
|
|
1258
1381
|
const formatted = filteredData.map((item) => {
|
|
1382
|
+
const unRealizedPrice = ramda.propOr(
|
|
1383
|
+
item.mark_price,
|
|
1384
|
+
item.symbol,
|
|
1385
|
+
priceMode === "markPrice" ? markPrices : tickerPrices
|
|
1386
|
+
);
|
|
1259
1387
|
const price = ramda.propOr(
|
|
1260
1388
|
item.mark_price,
|
|
1261
1389
|
item.symbol,
|
|
@@ -1265,8 +1393,8 @@ var usePositionStream = (symbol, options) => {
|
|
|
1265
1393
|
const notional = perp.positions.notional(item.position_qty, price);
|
|
1266
1394
|
const unrealPnl = perp.positions.unrealizedPnL({
|
|
1267
1395
|
qty: item.position_qty,
|
|
1268
|
-
openPrice: item
|
|
1269
|
-
markPrice:
|
|
1396
|
+
openPrice: item?.average_open_price,
|
|
1397
|
+
markPrice: unRealizedPrice
|
|
1270
1398
|
});
|
|
1271
1399
|
const imr = perp.account.IMR({
|
|
1272
1400
|
maxLeverage: accountInfo.max_leverage,
|
|
@@ -1313,7 +1441,16 @@ var usePositionStream = (symbol, options) => {
|
|
|
1313
1441
|
unsettledPnL: unsettlementPnL_total.toNumber()
|
|
1314
1442
|
}
|
|
1315
1443
|
];
|
|
1316
|
-
}, [
|
|
1444
|
+
}, [
|
|
1445
|
+
data?.rows,
|
|
1446
|
+
symbolInfo,
|
|
1447
|
+
accountInfo,
|
|
1448
|
+
markPrices,
|
|
1449
|
+
priceMode,
|
|
1450
|
+
tickerPrices,
|
|
1451
|
+
symbol,
|
|
1452
|
+
holding
|
|
1453
|
+
]);
|
|
1317
1454
|
const [totalCollateral, totalValue, totalUnrealizedROI] = React.useMemo(() => {
|
|
1318
1455
|
if (!holding || !markPrices) {
|
|
1319
1456
|
return [utils.zero, utils.zero, 0];
|
|
@@ -1343,7 +1480,7 @@ var usePositionStream = (symbol, options) => {
|
|
|
1343
1480
|
if (!symbolInfo || !accountInfo)
|
|
1344
1481
|
return formatedPositions[0];
|
|
1345
1482
|
const total = totalCollateral.toNumber();
|
|
1346
|
-
|
|
1483
|
+
let rows = formatedPositions[0].filter((item) => item.position_qty !== 0).map((item) => {
|
|
1347
1484
|
const info = symbolInfo?.[item.symbol];
|
|
1348
1485
|
const MMR = perp.positions.MMR({
|
|
1349
1486
|
baseMMR: info("base_mmr"),
|
|
@@ -1359,15 +1496,29 @@ var usePositionStream = (symbol, options) => {
|
|
|
1359
1496
|
markPrice: item.mark_price,
|
|
1360
1497
|
MMR
|
|
1361
1498
|
}),
|
|
1362
|
-
est_liq_price:
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
}),
|
|
1368
|
-
MMR
|
|
1499
|
+
// est_liq_price: positions.liqPrice({
|
|
1500
|
+
// markPrice: item.mark_price,
|
|
1501
|
+
// totalCollateral: total,
|
|
1502
|
+
// positionQty: item.position_qty,
|
|
1503
|
+
// MMR,
|
|
1504
|
+
// }),
|
|
1505
|
+
mmr: MMR
|
|
1506
|
+
};
|
|
1507
|
+
});
|
|
1508
|
+
rows = rows.map((item) => {
|
|
1509
|
+
const est_liq_price = perp.positions.liqPrice({
|
|
1510
|
+
markPrice: item.mark_price,
|
|
1511
|
+
totalCollateral: total,
|
|
1512
|
+
positionQty: item.position_qty,
|
|
1513
|
+
positions: rows,
|
|
1514
|
+
MMR: item.mmr
|
|
1515
|
+
});
|
|
1516
|
+
return {
|
|
1517
|
+
...item,
|
|
1518
|
+
est_liq_price
|
|
1369
1519
|
};
|
|
1370
1520
|
});
|
|
1521
|
+
return rows;
|
|
1371
1522
|
}, [formatedPositions, symbolInfo, accountInfo, totalCollateral]);
|
|
1372
1523
|
return [
|
|
1373
1524
|
{
|
|
@@ -1386,10 +1537,8 @@ var usePositionStream = (symbol, options) => {
|
|
|
1386
1537
|
loading: false,
|
|
1387
1538
|
// showSymbol,
|
|
1388
1539
|
error,
|
|
1389
|
-
loadMore: () => {
|
|
1390
|
-
|
|
1391
|
-
refresh: () => {
|
|
1392
|
-
}
|
|
1540
|
+
// loadMore: () => {},
|
|
1541
|
+
refresh: refreshPositions
|
|
1393
1542
|
}
|
|
1394
1543
|
];
|
|
1395
1544
|
};
|
|
@@ -1450,6 +1599,7 @@ var useHoldingStream = () => {
|
|
|
1450
1599
|
var useOrderStream = (params) => {
|
|
1451
1600
|
const { status, symbol, side, size = 100 } = params;
|
|
1452
1601
|
const { data: markPrices = {} } = useMarkPricesStream();
|
|
1602
|
+
const { regesterKeyHandler } = useDataCenterContext();
|
|
1453
1603
|
const [
|
|
1454
1604
|
doCancelOrder,
|
|
1455
1605
|
{ error: cancelOrderError, isMutating: cancelMutating }
|
|
@@ -1458,65 +1608,124 @@ var useOrderStream = (params) => {
|
|
|
1458
1608
|
doUpdateOrder,
|
|
1459
1609
|
{ error: updateOrderError, isMutating: updateMutating }
|
|
1460
1610
|
] = useMutation("/v1/order", "PUT");
|
|
1461
|
-
const
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
}
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
},
|
|
1480
|
-
{
|
|
1481
|
-
initialSize: 1,
|
|
1482
|
-
// revalidateFirstPage: false,
|
|
1483
|
-
// onError: (err) => {
|
|
1484
|
-
// console.error("fetch failed::::", err);
|
|
1485
|
-
// },
|
|
1486
|
-
formatter: (data) => data
|
|
1611
|
+
const [
|
|
1612
|
+
doCanceAlgolOrder,
|
|
1613
|
+
{ error: cancelAlgoOrderError, isMutating: cancelAlgoMutating }
|
|
1614
|
+
] = useMutation("/v1/algo/order", "DELETE");
|
|
1615
|
+
const [
|
|
1616
|
+
doUpdateAlgoOrder,
|
|
1617
|
+
{ error: updateAlgoOrderError, isMutating: updateAlgoMutating }
|
|
1618
|
+
] = useMutation("/v1/algo/order", "PUT");
|
|
1619
|
+
const getKey = (pageIndex, previousPageData) => {
|
|
1620
|
+
if (previousPageData && !previousPageData.rows?.length)
|
|
1621
|
+
return null;
|
|
1622
|
+
const search = new URLSearchParams([
|
|
1623
|
+
["size", size.toString()],
|
|
1624
|
+
["page", `${pageIndex + 1}`],
|
|
1625
|
+
["source_type", "ALL"]
|
|
1626
|
+
]);
|
|
1627
|
+
if (status) {
|
|
1628
|
+
search.set(`status`, status);
|
|
1487
1629
|
}
|
|
1488
|
-
|
|
1489
|
-
|
|
1630
|
+
if (symbol) {
|
|
1631
|
+
search.set(`symbol`, symbol);
|
|
1632
|
+
}
|
|
1633
|
+
if (side) {
|
|
1634
|
+
search.set(`side`, side);
|
|
1635
|
+
}
|
|
1636
|
+
return `/v1/orders?${search.toString()}`;
|
|
1637
|
+
};
|
|
1638
|
+
React.useEffect(() => {
|
|
1639
|
+
const key = `orders:${status}:${symbol}:${side}`;
|
|
1640
|
+
regesterKeyHandler(key, getKey);
|
|
1641
|
+
}, [status, symbol, side]);
|
|
1642
|
+
const ordersResponse = usePrivateInfiniteQuery(getKey, {
|
|
1643
|
+
initialSize: 1,
|
|
1644
|
+
// revalidateFirstPage: false,
|
|
1645
|
+
// onError: (err) => {
|
|
1646
|
+
// console.error("fetch failed::::", err);
|
|
1647
|
+
// },
|
|
1648
|
+
formatter: (data) => data
|
|
1649
|
+
});
|
|
1650
|
+
const flattenOrders = React.useMemo(() => {
|
|
1490
1651
|
if (!ordersResponse.data) {
|
|
1491
1652
|
return null;
|
|
1492
1653
|
}
|
|
1493
|
-
return ordersResponse.data?.map((item) => item.rows)?.flat()
|
|
1654
|
+
return ordersResponse.data?.map((item) => item.rows)?.flat();
|
|
1655
|
+
}, [ordersResponse.data]);
|
|
1656
|
+
const orders = React.useMemo(() => {
|
|
1657
|
+
if (!flattenOrders) {
|
|
1658
|
+
return null;
|
|
1659
|
+
}
|
|
1660
|
+
if (status !== types.OrderStatus.NEW && status !== types.OrderStatus.INCOMPLETE) {
|
|
1661
|
+
return flattenOrders;
|
|
1662
|
+
}
|
|
1663
|
+
return flattenOrders.map((item) => {
|
|
1494
1664
|
return {
|
|
1495
1665
|
...item,
|
|
1496
1666
|
mark_price: markPrices[item.symbol] ?? 0
|
|
1497
1667
|
};
|
|
1498
1668
|
});
|
|
1499
|
-
}, [
|
|
1669
|
+
}, [flattenOrders, markPrices, status]);
|
|
1500
1670
|
const total = React.useMemo(() => {
|
|
1501
1671
|
return ordersResponse.data?.[0]?.meta?.total || 0;
|
|
1502
1672
|
}, [ordersResponse.data?.[0]?.meta?.total]);
|
|
1503
1673
|
const cancelAllOrders = React.useCallback(() => {
|
|
1504
1674
|
}, [ordersResponse.data]);
|
|
1505
|
-
const
|
|
1506
|
-
|
|
1675
|
+
const _updateOrder = React.useCallback((orderId, order3, type) => {
|
|
1676
|
+
switch (type) {
|
|
1677
|
+
case "algoOrder":
|
|
1678
|
+
return doUpdateAlgoOrder({
|
|
1679
|
+
order_id: orderId,
|
|
1680
|
+
price: order3["order_price"],
|
|
1681
|
+
quantity: order3["order_quantity"],
|
|
1682
|
+
trigger_price: order3["trigger_price"]
|
|
1683
|
+
});
|
|
1684
|
+
default:
|
|
1685
|
+
return doUpdateOrder({ ...order3, order_id: orderId });
|
|
1686
|
+
}
|
|
1687
|
+
}, []);
|
|
1688
|
+
const updateOrder = React.useCallback((orderId, order3) => {
|
|
1689
|
+
return _updateOrder(orderId, order3, "normalOrder");
|
|
1690
|
+
}, []);
|
|
1691
|
+
const updateAlgoOrder = React.useCallback((orderId, order3) => {
|
|
1692
|
+
return _updateOrder(orderId, order3, "algoOrder");
|
|
1693
|
+
}, []);
|
|
1694
|
+
const _cancelOrder = React.useCallback((orderId, type, symbol2) => {
|
|
1695
|
+
switch (type) {
|
|
1696
|
+
case "algoOrder":
|
|
1697
|
+
return doCanceAlgolOrder(null, {
|
|
1698
|
+
// @ts-ignore
|
|
1699
|
+
order_id: orderId,
|
|
1700
|
+
symbol: symbol2,
|
|
1701
|
+
source: `SDK${version_default}`
|
|
1702
|
+
}).then((res) => {
|
|
1703
|
+
if (res.success) {
|
|
1704
|
+
ordersResponse.mutate();
|
|
1705
|
+
return res;
|
|
1706
|
+
} else {
|
|
1707
|
+
throw new Error(res.message);
|
|
1708
|
+
}
|
|
1709
|
+
});
|
|
1710
|
+
default:
|
|
1711
|
+
return doCancelOrder(null, {
|
|
1712
|
+
order_id: orderId,
|
|
1713
|
+
symbol: symbol2,
|
|
1714
|
+
source: `SDK_${version_default}`
|
|
1715
|
+
}).then((res) => {
|
|
1716
|
+
if (res.success) {
|
|
1717
|
+
return res;
|
|
1718
|
+
} else {
|
|
1719
|
+
throw new Error(res.message);
|
|
1720
|
+
}
|
|
1721
|
+
});
|
|
1722
|
+
}
|
|
1507
1723
|
}, []);
|
|
1508
1724
|
const cancelOrder = React.useCallback((orderId, symbol2) => {
|
|
1509
|
-
return
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
}).then((res) => {
|
|
1514
|
-
if (res.success) {
|
|
1515
|
-
return res;
|
|
1516
|
-
} else {
|
|
1517
|
-
throw new Error(res.message);
|
|
1518
|
-
}
|
|
1519
|
-
});
|
|
1725
|
+
return _cancelOrder(orderId, "normalOrder", symbol2);
|
|
1726
|
+
}, []);
|
|
1727
|
+
const cancelAlgoOrder = React.useCallback((orderId, symbol2) => {
|
|
1728
|
+
return _cancelOrder(orderId, "algoOrder", symbol2);
|
|
1520
1729
|
}, []);
|
|
1521
1730
|
const loadMore = () => {
|
|
1522
1731
|
ordersResponse.setSize(ordersResponse.size + 1);
|
|
@@ -1526,17 +1735,24 @@ var useOrderStream = (params) => {
|
|
|
1526
1735
|
{
|
|
1527
1736
|
total,
|
|
1528
1737
|
isLoading: ordersResponse.isLoading,
|
|
1738
|
+
refresh: ordersResponse.mutate,
|
|
1529
1739
|
loadMore,
|
|
1530
1740
|
cancelAllOrders,
|
|
1531
1741
|
updateOrder,
|
|
1532
1742
|
cancelOrder,
|
|
1743
|
+
updateAlgoOrder,
|
|
1744
|
+
cancelAlgoOrder,
|
|
1533
1745
|
errors: {
|
|
1534
1746
|
cancelOrder: cancelOrderError,
|
|
1535
|
-
updateOrder: updateOrderError
|
|
1747
|
+
updateOrder: updateOrderError,
|
|
1748
|
+
cancelAlgoOrder: cancelAlgoOrderError,
|
|
1749
|
+
updateAlgoOrder: updateAlgoOrderError
|
|
1536
1750
|
},
|
|
1537
1751
|
submitting: {
|
|
1538
1752
|
cancelOrder: cancelMutating,
|
|
1539
|
-
updateOrder: updateMutating
|
|
1753
|
+
updateOrder: updateMutating,
|
|
1754
|
+
cancelAlgoOrder: cancelAlgoMutating,
|
|
1755
|
+
updateAlglOrder: updateAlgoMutating
|
|
1540
1756
|
}
|
|
1541
1757
|
}
|
|
1542
1758
|
];
|
|
@@ -1547,31 +1763,32 @@ var positionsPath = ramda.pathOr([], [0, "rows"]);
|
|
|
1547
1763
|
ramda.pathOr(0, [0, "totalCollateral"]);
|
|
1548
1764
|
var useCollateral = (options = { dp: 6 }) => {
|
|
1549
1765
|
const { dp } = options;
|
|
1550
|
-
const
|
|
1766
|
+
const positions3 = usePositionStream();
|
|
1551
1767
|
const [orders] = useOrderStream({ status: types.OrderStatus.NEW });
|
|
1552
1768
|
const { data: accountInfo } = usePrivateQuery("/v1/client/info");
|
|
1553
1769
|
const symbolInfo = useSymbolsInfo();
|
|
1554
1770
|
const { data: markPrices } = useMarkPricesStream();
|
|
1555
1771
|
const { usdc } = useHoldingStream();
|
|
1772
|
+
const filterAlgoOrders = orders?.filter((item) => item.algo_order_id === void 0) ?? [];
|
|
1556
1773
|
const [totalCollateral, totalValue] = React.useMemo(() => {
|
|
1557
1774
|
return [
|
|
1558
|
-
ramda.pathOr(utils.zero, [0, "totalCollateral"],
|
|
1559
|
-
ramda.pathOr(utils.zero, [0, "totalValue"],
|
|
1775
|
+
ramda.pathOr(utils.zero, [0, "totalCollateral"], positions3),
|
|
1776
|
+
ramda.pathOr(utils.zero, [0, "totalValue"], positions3)
|
|
1560
1777
|
];
|
|
1561
|
-
}, [
|
|
1778
|
+
}, [positions3, markPrices]);
|
|
1562
1779
|
const totalInitialMarginWithOrders = React.useMemo(() => {
|
|
1563
1780
|
if (!accountInfo || !symbolInfo || !markPrices) {
|
|
1564
1781
|
return 0;
|
|
1565
1782
|
}
|
|
1566
1783
|
return perp.account.totalInitialMarginWithOrders({
|
|
1567
|
-
positions: positionsPath(
|
|
1568
|
-
orders:
|
|
1784
|
+
positions: positionsPath(positions3),
|
|
1785
|
+
orders: filterAlgoOrders,
|
|
1569
1786
|
markPrices,
|
|
1570
1787
|
IMR_Factors: accountInfo.imr_factor,
|
|
1571
1788
|
maxLeverage: accountInfo.max_leverage,
|
|
1572
1789
|
symbolInfo
|
|
1573
1790
|
});
|
|
1574
|
-
}, [
|
|
1791
|
+
}, [positions3, filterAlgoOrders, markPrices, accountInfo, symbolInfo]);
|
|
1575
1792
|
const freeCollateral = React.useMemo(() => {
|
|
1576
1793
|
return perp.account.freeCollateral({
|
|
1577
1794
|
totalCollateral,
|
|
@@ -1581,15 +1798,18 @@ var useCollateral = (options = { dp: 6 }) => {
|
|
|
1581
1798
|
const availableBalance = React.useMemo(() => {
|
|
1582
1799
|
return perp.account.availableBalance({
|
|
1583
1800
|
USDCHolding: usdc?.holding ?? 0,
|
|
1584
|
-
unsettlementPnL: pathOr_unsettledPnLPathOr(
|
|
1801
|
+
unsettlementPnL: pathOr_unsettledPnLPathOr(positions3)
|
|
1585
1802
|
});
|
|
1586
|
-
}, [usdc, pathOr_unsettledPnLPathOr(
|
|
1803
|
+
}, [usdc?.holding, pathOr_unsettledPnLPathOr(positions3)]);
|
|
1587
1804
|
return {
|
|
1588
1805
|
totalCollateral: totalCollateral.toDecimalPlaces(dp).toNumber(),
|
|
1589
1806
|
freeCollateral: freeCollateral.toDecimalPlaces(dp).toNumber(),
|
|
1590
1807
|
totalValue: totalValue.toDecimalPlaces(dp).toNumber(),
|
|
1591
1808
|
availableBalance,
|
|
1592
|
-
unsettledPnL: pathOr_unsettledPnLPathOr(
|
|
1809
|
+
unsettledPnL: pathOr_unsettledPnLPathOr(positions3),
|
|
1810
|
+
accountInfo,
|
|
1811
|
+
// @hidden
|
|
1812
|
+
positions: positionsPath(positions3)
|
|
1593
1813
|
};
|
|
1594
1814
|
};
|
|
1595
1815
|
var positionsPath2 = ramda.pathOr([], [0, "rows"]);
|
|
@@ -1603,8 +1823,8 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
|
|
|
1603
1823
|
const maxQty = React.useMemo(() => {
|
|
1604
1824
|
if (!symbol)
|
|
1605
1825
|
return 0;
|
|
1606
|
-
const
|
|
1607
|
-
const positionQty = perp.account.getQtyFromPositions(
|
|
1826
|
+
const positions3 = positionsPath2(positionsData);
|
|
1827
|
+
const positionQty = perp.account.getQtyFromPositions(positions3, symbol);
|
|
1608
1828
|
if (reduceOnly) {
|
|
1609
1829
|
if (positionQty > 0) {
|
|
1610
1830
|
if (side === types.OrderSide.BUY) {
|
|
@@ -1625,20 +1845,21 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
|
|
|
1625
1845
|
if (!markPrices || !markPrices[symbol] || !orders || !accountInfo)
|
|
1626
1846
|
return 0;
|
|
1627
1847
|
const getSymbolInfo = symbolInfo[symbol];
|
|
1848
|
+
const filterAlgoOrders = orders.filter((item) => item.algo_order_id === void 0);
|
|
1628
1849
|
const buyOrdersQty = perp.account.getQtyFromOrdersBySide(
|
|
1629
|
-
|
|
1850
|
+
filterAlgoOrders,
|
|
1630
1851
|
symbol,
|
|
1631
1852
|
types.OrderSide.BUY
|
|
1632
1853
|
);
|
|
1633
1854
|
const sellOrdersQty = perp.account.getQtyFromOrdersBySide(
|
|
1634
|
-
|
|
1855
|
+
filterAlgoOrders,
|
|
1635
1856
|
symbol,
|
|
1636
1857
|
types.OrderSide.SELL
|
|
1637
1858
|
);
|
|
1638
|
-
const otherPositions =
|
|
1859
|
+
const otherPositions = positions3.filter(
|
|
1639
1860
|
(item) => item.symbol !== symbol
|
|
1640
1861
|
);
|
|
1641
|
-
const otherOrders =
|
|
1862
|
+
const otherOrders = filterAlgoOrders.filter(
|
|
1642
1863
|
(item) => item.symbol !== symbol
|
|
1643
1864
|
);
|
|
1644
1865
|
const otherIMs = perp.account.otherIMs({
|
|
@@ -1674,27 +1895,36 @@ var useMaxQty = (symbol, side, reduceOnly = false) => {
|
|
|
1674
1895
|
totalCollateral,
|
|
1675
1896
|
reduceOnly
|
|
1676
1897
|
]);
|
|
1677
|
-
return maxQty;
|
|
1898
|
+
return Math.max(maxQty, 0);
|
|
1678
1899
|
};
|
|
1679
|
-
var { maxPrice, minPrice } = perp.order;
|
|
1900
|
+
var { maxPrice, minPrice, scropePrice } = perp.order;
|
|
1680
1901
|
var BaseOrderCreator = class {
|
|
1681
1902
|
baseOrder(data) {
|
|
1682
|
-
const
|
|
1683
|
-
|
|
1903
|
+
const order3 = {
|
|
1904
|
+
symbol: data.symbol,
|
|
1684
1905
|
order_type: data.order_type === types.OrderType.LIMIT ? !!data.order_type_ext ? data.order_type_ext : data.order_type : data.order_type,
|
|
1685
1906
|
side: data.side,
|
|
1686
1907
|
reduce_only: data.reduce_only,
|
|
1687
|
-
order_quantity: data.order_quantity
|
|
1908
|
+
order_quantity: data.order_quantity,
|
|
1909
|
+
total: data.total
|
|
1688
1910
|
};
|
|
1689
1911
|
if (data.visible_quantity === 0) {
|
|
1690
|
-
|
|
1912
|
+
order3.visible_quantity = data.visible_quantity;
|
|
1691
1913
|
}
|
|
1692
|
-
return
|
|
1914
|
+
return order3;
|
|
1693
1915
|
}
|
|
1694
1916
|
baseValidate(values, configs) {
|
|
1695
1917
|
const errors = {};
|
|
1696
1918
|
const { maxQty } = configs;
|
|
1697
|
-
|
|
1919
|
+
let { order_quantity, total, order_price } = values;
|
|
1920
|
+
if (!order_quantity) {
|
|
1921
|
+
if (total && order_price) {
|
|
1922
|
+
const { quote_dp } = configs.symbol;
|
|
1923
|
+
const totalNumber = new utils.Decimal(total);
|
|
1924
|
+
const qty = totalNumber.dividedBy(order_price).toFixed(quote_dp);
|
|
1925
|
+
order_quantity = qty;
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1698
1928
|
if (!order_quantity) {
|
|
1699
1929
|
errors.order_quantity = {
|
|
1700
1930
|
type: "required",
|
|
@@ -1740,13 +1970,28 @@ var BaseOrderCreator = class {
|
|
|
1740
1970
|
}
|
|
1741
1971
|
return Promise.resolve(errors);
|
|
1742
1972
|
}
|
|
1973
|
+
fixOrderQuantity(order3, config) {
|
|
1974
|
+
if (!order3.order_quantity && order3.total && order3.order_price) {
|
|
1975
|
+
const { base_dp } = config.symbol;
|
|
1976
|
+
const totalNumber = new utils.Decimal(order3.total);
|
|
1977
|
+
const qty = totalNumber.div(order3.order_price).toDecimalPlaces(base_dp);
|
|
1978
|
+
order3.order_quantity = qty.toNumber();
|
|
1979
|
+
delete order3.total;
|
|
1980
|
+
}
|
|
1981
|
+
return order3;
|
|
1982
|
+
}
|
|
1743
1983
|
};
|
|
1744
1984
|
var LimitOrderCreator = class extends BaseOrderCreator {
|
|
1745
|
-
create(values) {
|
|
1746
|
-
|
|
1985
|
+
create(values, config) {
|
|
1986
|
+
const order3 = {
|
|
1747
1987
|
...this.baseOrder(values),
|
|
1748
1988
|
order_price: values.order_price
|
|
1749
1989
|
};
|
|
1990
|
+
this.fixOrderQuantity(order3, config);
|
|
1991
|
+
delete order3["total"];
|
|
1992
|
+
delete order3["trigger_price"];
|
|
1993
|
+
delete order3["isStopOrder"];
|
|
1994
|
+
return order3;
|
|
1750
1995
|
}
|
|
1751
1996
|
validate(values, config) {
|
|
1752
1997
|
return this.baseValidate(values, config).then((errors) => {
|
|
@@ -1759,22 +2004,34 @@ var LimitOrderCreator = class extends BaseOrderCreator {
|
|
|
1759
2004
|
} else {
|
|
1760
2005
|
const price = new utils.Decimal(order_price);
|
|
1761
2006
|
const { symbol } = config;
|
|
1762
|
-
const { price_range } = symbol;
|
|
2007
|
+
const { price_range, price_scope } = symbol;
|
|
1763
2008
|
const maxPriceNumber = maxPrice(config.markPrice, price_range);
|
|
1764
2009
|
const minPriceNumber = minPrice(config.markPrice, price_range);
|
|
1765
|
-
|
|
1766
|
-
|
|
2010
|
+
const scropePriceNumbere = scropePrice(
|
|
2011
|
+
config.markPrice,
|
|
2012
|
+
price_scope,
|
|
2013
|
+
side
|
|
2014
|
+
);
|
|
2015
|
+
const priceRange = side === "BUY" ? {
|
|
2016
|
+
min: scropePriceNumbere,
|
|
2017
|
+
max: maxPriceNumber
|
|
2018
|
+
} : {
|
|
2019
|
+
min: minPriceNumber,
|
|
2020
|
+
max: scropePriceNumbere
|
|
2021
|
+
};
|
|
2022
|
+
if (price.gt(priceRange.max)) {
|
|
1767
2023
|
errors.order_price = {
|
|
1768
2024
|
type: "max",
|
|
1769
|
-
message: `
|
|
1770
|
-
|
|
2025
|
+
message: `Price must be less than ${new utils.Decimal(
|
|
2026
|
+
priceRange.max
|
|
1771
2027
|
).todp(symbol.quote_dp)}`
|
|
1772
2028
|
};
|
|
1773
|
-
}
|
|
2029
|
+
}
|
|
2030
|
+
if (price.lt(priceRange.min)) {
|
|
1774
2031
|
errors.order_price = {
|
|
1775
2032
|
type: "min",
|
|
1776
|
-
message: `
|
|
1777
|
-
|
|
2033
|
+
message: `Price must be greater than ${new utils.Decimal(
|
|
2034
|
+
priceRange.min
|
|
1778
2035
|
).todp(symbol.quote_dp)}`
|
|
1779
2036
|
};
|
|
1780
2037
|
}
|
|
@@ -1787,6 +2044,9 @@ var MarketOrderCreator = class extends BaseOrderCreator {
|
|
|
1787
2044
|
create(values) {
|
|
1788
2045
|
const data = this.baseOrder(values);
|
|
1789
2046
|
delete data["order_price"];
|
|
2047
|
+
delete data["total"];
|
|
2048
|
+
delete data["trigger_price"];
|
|
2049
|
+
delete data["isStopOrder"];
|
|
1790
2050
|
return {
|
|
1791
2051
|
...data
|
|
1792
2052
|
};
|
|
@@ -1801,6 +2061,110 @@ var FOKOrderCreator = class extends LimitOrderCreator {
|
|
|
1801
2061
|
};
|
|
1802
2062
|
var IOCOrderCreator = class extends LimitOrderCreator {
|
|
1803
2063
|
};
|
|
2064
|
+
var StopLimitOrderCreator = class extends LimitOrderCreator {
|
|
2065
|
+
create(values, config) {
|
|
2066
|
+
const order3 = {
|
|
2067
|
+
...this.baseOrder(values),
|
|
2068
|
+
order_price: values.order_price,
|
|
2069
|
+
trigger_price: values.trigger_price,
|
|
2070
|
+
algo_type: "STOP",
|
|
2071
|
+
type: "LIMIT",
|
|
2072
|
+
quantity: values["order_quantity"],
|
|
2073
|
+
price: values["order_price"],
|
|
2074
|
+
trigger_price_type: "MARK_PRICE"
|
|
2075
|
+
};
|
|
2076
|
+
this.fixOrderQuantity(order3, config);
|
|
2077
|
+
delete order3["order_quantity"];
|
|
2078
|
+
delete order3["order_price"];
|
|
2079
|
+
delete order3["isStopOrder"];
|
|
2080
|
+
delete order3["total"];
|
|
2081
|
+
return order3;
|
|
2082
|
+
}
|
|
2083
|
+
validate(values, config) {
|
|
2084
|
+
return this.baseValidate(values, config).then((errors) => {
|
|
2085
|
+
const { order_price, trigger_price, side } = values;
|
|
2086
|
+
if (!order_price) {
|
|
2087
|
+
errors.order_price = {
|
|
2088
|
+
type: "required",
|
|
2089
|
+
message: "price is required"
|
|
2090
|
+
};
|
|
2091
|
+
}
|
|
2092
|
+
if (!trigger_price) {
|
|
2093
|
+
errors.trigger_price = {
|
|
2094
|
+
type: "required",
|
|
2095
|
+
message: "Trigger price is required"
|
|
2096
|
+
};
|
|
2097
|
+
}
|
|
2098
|
+
if (trigger_price && order_price) {
|
|
2099
|
+
const price = new utils.Decimal(order_price);
|
|
2100
|
+
const { symbol } = config;
|
|
2101
|
+
const { price_range, price_scope } = symbol;
|
|
2102
|
+
const maxPriceNumber = maxPrice(trigger_price, price_range);
|
|
2103
|
+
const minPriceNumber = minPrice(trigger_price, price_range);
|
|
2104
|
+
const scropePriceNumbere = scropePrice(
|
|
2105
|
+
trigger_price,
|
|
2106
|
+
price_scope,
|
|
2107
|
+
side
|
|
2108
|
+
);
|
|
2109
|
+
const priceRange = side === "BUY" ? {
|
|
2110
|
+
min: scropePriceNumbere,
|
|
2111
|
+
max: maxPriceNumber
|
|
2112
|
+
} : {
|
|
2113
|
+
min: minPriceNumber,
|
|
2114
|
+
max: scropePriceNumbere
|
|
2115
|
+
};
|
|
2116
|
+
if (price.gt(priceRange.max)) {
|
|
2117
|
+
errors.order_price = {
|
|
2118
|
+
type: "max",
|
|
2119
|
+
message: `Price must be less than ${new utils.Decimal(
|
|
2120
|
+
priceRange.max
|
|
2121
|
+
).todp(symbol.quote_dp)}`
|
|
2122
|
+
};
|
|
2123
|
+
}
|
|
2124
|
+
if (price.lt(priceRange.min)) {
|
|
2125
|
+
errors.order_price = {
|
|
2126
|
+
type: "min",
|
|
2127
|
+
message: `Price must be greater than ${new utils.Decimal(
|
|
2128
|
+
priceRange.min
|
|
2129
|
+
).todp(symbol.quote_dp)}`
|
|
2130
|
+
};
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
return errors;
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
var StopMarketOrderCreator = class extends LimitOrderCreator {
|
|
2138
|
+
create(values, _) {
|
|
2139
|
+
const result = {
|
|
2140
|
+
...this.baseOrder(values),
|
|
2141
|
+
order_price: values.order_price,
|
|
2142
|
+
trigger_price: values.trigger_price,
|
|
2143
|
+
algo_type: "STOP",
|
|
2144
|
+
type: "MARKET",
|
|
2145
|
+
quantity: values["order_quantity"],
|
|
2146
|
+
price: values["order_price"],
|
|
2147
|
+
trigger_price_type: "MARK_PRICE"
|
|
2148
|
+
};
|
|
2149
|
+
delete result["order_quantity"];
|
|
2150
|
+
delete result["order_price"];
|
|
2151
|
+
delete result["isStopOrder"];
|
|
2152
|
+
delete result["total"];
|
|
2153
|
+
return result;
|
|
2154
|
+
}
|
|
2155
|
+
validate(values, config) {
|
|
2156
|
+
return this.baseValidate(values, config).then((errors) => {
|
|
2157
|
+
const { order_price, trigger_price, side } = values;
|
|
2158
|
+
if (!trigger_price) {
|
|
2159
|
+
errors.trigger_price = {
|
|
2160
|
+
type: "required",
|
|
2161
|
+
message: "Trigger price is required"
|
|
2162
|
+
};
|
|
2163
|
+
}
|
|
2164
|
+
return errors;
|
|
2165
|
+
});
|
|
2166
|
+
}
|
|
2167
|
+
};
|
|
1804
2168
|
var GeneralOrderCreator = class extends BaseOrderCreator {
|
|
1805
2169
|
create(data) {
|
|
1806
2170
|
return {
|
|
@@ -1813,6 +2177,15 @@ var GeneralOrderCreator = class extends BaseOrderCreator {
|
|
|
1813
2177
|
return super.baseValidate(values, configs);
|
|
1814
2178
|
}
|
|
1815
2179
|
};
|
|
2180
|
+
var availableOrderTypes = [
|
|
2181
|
+
types.OrderType.LIMIT,
|
|
2182
|
+
types.OrderType.MARKET,
|
|
2183
|
+
types.OrderType.IOC,
|
|
2184
|
+
types.OrderType.FOK,
|
|
2185
|
+
types.OrderType.POST_ONLY,
|
|
2186
|
+
types.OrderType.STOP_LIMIT,
|
|
2187
|
+
types.OrderType.STOP_MARKET
|
|
2188
|
+
];
|
|
1816
2189
|
var OrderFactory = class {
|
|
1817
2190
|
static create(type) {
|
|
1818
2191
|
switch (type) {
|
|
@@ -1826,17 +2199,68 @@ var OrderFactory = class {
|
|
|
1826
2199
|
return new FOKOrderCreator();
|
|
1827
2200
|
case types.OrderType.POST_ONLY:
|
|
1828
2201
|
return new PostOnlyOrderCreator();
|
|
2202
|
+
case types.OrderType.STOP_LIMIT:
|
|
2203
|
+
return new StopLimitOrderCreator();
|
|
2204
|
+
case types.OrderType.STOP_MARKET:
|
|
2205
|
+
return new StopMarketOrderCreator();
|
|
1829
2206
|
default:
|
|
1830
2207
|
return new GeneralOrderCreator();
|
|
1831
2208
|
}
|
|
1832
2209
|
}
|
|
1833
2210
|
};
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
2211
|
+
function useOrderEntry(symbolOrOrder, sideOrOptions, reduceOnly, options) {
|
|
2212
|
+
if (typeof symbolOrOrder === "object") {
|
|
2213
|
+
if (!symbolOrOrder.symbol) {
|
|
2214
|
+
throw new types.SDKError("symbol is required");
|
|
2215
|
+
}
|
|
2216
|
+
if (!symbolOrOrder.side) {
|
|
2217
|
+
throw new types.SDKError("Order side is required");
|
|
2218
|
+
}
|
|
2219
|
+
if (!symbolOrOrder.order_type) {
|
|
2220
|
+
throw new types.SDKError("order_type is required");
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
const prevOrderData = React.useRef(null);
|
|
2224
|
+
const orderDataCache = React.useRef(null);
|
|
2225
|
+
const notSupportData = React.useRef({});
|
|
2226
|
+
const [doCreateOrder, { data, error, reset, isMutating }] = useMutation(orderDataCache?.current?.isStopOrder ? "/v1/algo/order" : "/v1/order");
|
|
2227
|
+
const [errors, setErrors] = React.useState(null);
|
|
2228
|
+
const ee = useEventEmitter();
|
|
2229
|
+
const fieldDirty = React.useRef({});
|
|
2230
|
+
const submitted = React.useRef(false);
|
|
2231
|
+
const askAndBid = React.useRef([]);
|
|
2232
|
+
const onOrderbookUpdate = useDebounce.useDebouncedCallback((data2) => {
|
|
2233
|
+
askAndBid.current = data2;
|
|
2234
|
+
}, 200);
|
|
2235
|
+
const { freeCollateral, totalCollateral, positions: positions3, accountInfo } = useCollateral();
|
|
1839
2236
|
const symbolInfo = useSymbolsInfo();
|
|
2237
|
+
const symbol = React.useMemo(() => {
|
|
2238
|
+
if (typeof symbolOrOrder === "string") {
|
|
2239
|
+
return symbolOrOrder;
|
|
2240
|
+
}
|
|
2241
|
+
return symbolOrOrder.symbol;
|
|
2242
|
+
}, [symbolOrOrder]);
|
|
2243
|
+
const optionsValue = React.useMemo(() => {
|
|
2244
|
+
if (typeof sideOrOptions === "object") {
|
|
2245
|
+
return sideOrOptions;
|
|
2246
|
+
}
|
|
2247
|
+
return options;
|
|
2248
|
+
}, [sideOrOptions]);
|
|
2249
|
+
const isReduceOnly = React.useMemo(() => {
|
|
2250
|
+
if (typeof reduceOnly === "boolean") {
|
|
2251
|
+
return reduceOnly;
|
|
2252
|
+
}
|
|
2253
|
+
if (typeof symbolOrOrder === "object") {
|
|
2254
|
+
return !!symbolOrOrder.reduce_only;
|
|
2255
|
+
}
|
|
2256
|
+
return false;
|
|
2257
|
+
}, [symbolOrOrder, reduceOnly]);
|
|
2258
|
+
const sideValue = React.useMemo(() => {
|
|
2259
|
+
if (typeof symbolOrOrder === "object") {
|
|
2260
|
+
return symbolOrOrder.side;
|
|
2261
|
+
}
|
|
2262
|
+
return sideOrOptions;
|
|
2263
|
+
}, [symbolOrOrder, sideOrOptions]);
|
|
1840
2264
|
const baseDP = React.useMemo(
|
|
1841
2265
|
() => utils.getPrecisionByNumber(symbolInfo[symbol]("base_tick", 0)),
|
|
1842
2266
|
[symbolInfo]
|
|
@@ -1844,42 +2268,123 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
|
|
|
1844
2268
|
const quoteDP = React.useMemo(() => {
|
|
1845
2269
|
return utils.getPrecisionByNumber(symbolInfo[symbol]("quote_tick", 0));
|
|
1846
2270
|
}, [symbolInfo]);
|
|
2271
|
+
const baseIMR = React.useMemo(() => symbolInfo[symbol]("base_imr"), [symbolInfo]);
|
|
2272
|
+
const baseMMR = React.useMemo(() => symbolInfo[symbol]("base_mmr"), [symbolInfo]);
|
|
1847
2273
|
const { data: markPrice } = useMarkPrice(symbol);
|
|
1848
|
-
const
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
2274
|
+
const diffOrderEntry = (prev, current) => {
|
|
2275
|
+
if (!prev)
|
|
2276
|
+
return null;
|
|
2277
|
+
let key, value;
|
|
2278
|
+
const keys = Object.keys(current);
|
|
2279
|
+
for (let i = 0; i < keys.length; i++) {
|
|
2280
|
+
const k = keys[i];
|
|
2281
|
+
let preveValue = prev[k];
|
|
2282
|
+
let currentValue = current[k];
|
|
2283
|
+
if (typeof preveValue === "undefined" && typeof currentValue === "undefined")
|
|
2284
|
+
continue;
|
|
2285
|
+
if (preveValue !== currentValue) {
|
|
2286
|
+
key = k;
|
|
2287
|
+
value = currentValue;
|
|
2288
|
+
break;
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
if (!key)
|
|
2292
|
+
return null;
|
|
2293
|
+
return { key, value };
|
|
2294
|
+
};
|
|
2295
|
+
const maxQty = useMaxQty(symbol, sideValue, isReduceOnly);
|
|
2296
|
+
const parsedData = React.useMemo(() => {
|
|
2297
|
+
if (typeof symbolOrOrder === "string") {
|
|
2298
|
+
return null;
|
|
2299
|
+
}
|
|
2300
|
+
if (typeof symbolOrOrder.order_quantity === "string") {
|
|
2301
|
+
symbolOrOrder.order_quantity = symbolOrOrder.order_quantity.replace(
|
|
2302
|
+
/,/g,
|
|
2303
|
+
""
|
|
2304
|
+
);
|
|
2305
|
+
}
|
|
2306
|
+
if (typeof symbolOrOrder.order_price === "string") {
|
|
2307
|
+
symbolOrOrder.order_price = symbolOrOrder.order_price.replace(/,/g, "");
|
|
2308
|
+
}
|
|
2309
|
+
if (typeof symbolOrOrder.total === "string") {
|
|
2310
|
+
symbolOrOrder.total = symbolOrOrder.total.replace(/,/g, "");
|
|
2311
|
+
}
|
|
2312
|
+
if (typeof symbolOrOrder.order_quantity === "number") {
|
|
2313
|
+
symbolOrOrder.order_quantity = new utils.Decimal(symbolOrOrder.order_quantity).toDecimalPlaces(baseDP).toString();
|
|
2314
|
+
}
|
|
2315
|
+
return symbolOrOrder;
|
|
2316
|
+
}, [symbolOrOrder]);
|
|
2317
|
+
const createOrder = (values) => {
|
|
2318
|
+
if (!values.symbol) {
|
|
2319
|
+
throw new types.SDKError("symbol is error");
|
|
2320
|
+
}
|
|
2321
|
+
if (!values.side) {
|
|
2322
|
+
throw new types.SDKError("side is error");
|
|
2323
|
+
}
|
|
2324
|
+
if (!values || typeof values.order_type === "undefined" || !ramda.includes(values.order_type, availableOrderTypes)) {
|
|
2325
|
+
throw new types.SDKError("order_type is error");
|
|
1857
2326
|
}
|
|
1858
2327
|
const orderCreator = OrderFactory.create(
|
|
1859
|
-
|
|
2328
|
+
values.order_type_ext ? values.order_type_ext : values.order_type
|
|
1860
2329
|
);
|
|
1861
2330
|
if (!orderCreator) {
|
|
1862
|
-
return Promise.reject(new
|
|
2331
|
+
return Promise.reject(new types.SDKError("orderCreator is null"));
|
|
1863
2332
|
}
|
|
1864
|
-
return
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
2333
|
+
return new Promise((resolve, reject) => {
|
|
2334
|
+
return orderCreator.validate(values, {
|
|
2335
|
+
symbol: symbolInfo[symbol](),
|
|
2336
|
+
// token: tokenInfo[symbol](),
|
|
2337
|
+
maxQty,
|
|
2338
|
+
markPrice
|
|
2339
|
+
}).then((errors2) => {
|
|
2340
|
+
submitted.current = true;
|
|
2341
|
+
if (errors2.order_price || errors2.order_quantity || errors2.trigger_price) {
|
|
2342
|
+
setErrors(errors2);
|
|
2343
|
+
reject(
|
|
2344
|
+
errors2.order_price?.message || errors2.order_quantity?.message
|
|
2345
|
+
);
|
|
2346
|
+
} else {
|
|
2347
|
+
const data2 = orderCreator.create(values, {
|
|
2348
|
+
symbol: symbolInfo[symbol](),
|
|
2349
|
+
maxQty,
|
|
2350
|
+
markPrice
|
|
2351
|
+
});
|
|
2352
|
+
return doCreateOrder(
|
|
2353
|
+
ramda.omit(["order_type_ext"], {
|
|
2354
|
+
// ...values,
|
|
2355
|
+
// ...omit(["order_price"], values),
|
|
2356
|
+
...data2
|
|
2357
|
+
})
|
|
2358
|
+
).then((res) => {
|
|
2359
|
+
if (res.success) {
|
|
2360
|
+
resolve(res.data);
|
|
2361
|
+
} else {
|
|
2362
|
+
reject(res);
|
|
2363
|
+
}
|
|
2364
|
+
}, reject);
|
|
2365
|
+
}
|
|
1880
2366
|
});
|
|
1881
2367
|
});
|
|
1882
2368
|
};
|
|
2369
|
+
const onSubmit = (values) => {
|
|
2370
|
+
if (typeof reduceOnly === "boolean" && reduceOnly && !values.reduce_only) {
|
|
2371
|
+
return Promise.reject(
|
|
2372
|
+
new types.SDKError(
|
|
2373
|
+
"The reduceOny parameter of hook does not match your order data"
|
|
2374
|
+
)
|
|
2375
|
+
);
|
|
2376
|
+
}
|
|
2377
|
+
return createOrder({
|
|
2378
|
+
...values,
|
|
2379
|
+
symbol
|
|
2380
|
+
});
|
|
2381
|
+
};
|
|
2382
|
+
const submit = React.useCallback(() => {
|
|
2383
|
+
if (typeof symbolOrOrder === "string") {
|
|
2384
|
+
throw new types.SDKError("Function is not supported, please use onSubmit()");
|
|
2385
|
+
}
|
|
2386
|
+
return createOrder(symbolOrOrder);
|
|
2387
|
+
}, [symbolOrOrder]);
|
|
1883
2388
|
const calculate = React.useCallback(
|
|
1884
2389
|
(values, field, value) => {
|
|
1885
2390
|
const fieldHandler = getCalculateHandler(field);
|
|
@@ -1902,75 +2407,407 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
|
|
|
1902
2407
|
markPrice
|
|
1903
2408
|
});
|
|
1904
2409
|
};
|
|
2410
|
+
const formattedOrder = React.useMemo(() => {
|
|
2411
|
+
if (!parsedData) {
|
|
2412
|
+
return notSupportData.current;
|
|
2413
|
+
}
|
|
2414
|
+
if (!prevOrderData.current) {
|
|
2415
|
+
prevOrderData.current = parsedData;
|
|
2416
|
+
orderDataCache.current = {
|
|
2417
|
+
...parsedData,
|
|
2418
|
+
total: ""
|
|
2419
|
+
};
|
|
2420
|
+
return orderDataCache.current;
|
|
2421
|
+
}
|
|
2422
|
+
const item = diffOrderEntry(prevOrderData.current, parsedData);
|
|
2423
|
+
if (!item) {
|
|
2424
|
+
return orderDataCache.current;
|
|
2425
|
+
}
|
|
2426
|
+
if (typeof parsedData.order_price !== "undefined") {
|
|
2427
|
+
fieldDirty.current.order_price = true;
|
|
2428
|
+
}
|
|
2429
|
+
if (typeof parsedData.order_quantity !== "undefined") {
|
|
2430
|
+
fieldDirty.current.order_quantity = true;
|
|
2431
|
+
}
|
|
2432
|
+
const values = calculate(parsedData, item.key, item.value);
|
|
2433
|
+
values.isStopOrder = values.order_type?.startsWith("STOP") || false;
|
|
2434
|
+
values.total = values.total || "";
|
|
2435
|
+
prevOrderData.current = parsedData;
|
|
2436
|
+
orderDataCache.current = values;
|
|
2437
|
+
return values;
|
|
2438
|
+
}, [
|
|
2439
|
+
parsedData?.order_price,
|
|
2440
|
+
parsedData?.side,
|
|
2441
|
+
parsedData?.order_quantity,
|
|
2442
|
+
parsedData?.visible_quantity,
|
|
2443
|
+
parsedData?.order_type,
|
|
2444
|
+
parsedData?.order_type_ext,
|
|
2445
|
+
parsedData?.symbol,
|
|
2446
|
+
parsedData?.total,
|
|
2447
|
+
parsedData?.reduce_only,
|
|
2448
|
+
parsedData?.trigger_price,
|
|
2449
|
+
markPrice
|
|
2450
|
+
]);
|
|
2451
|
+
React.useEffect(() => {
|
|
2452
|
+
if (!markPrice)
|
|
2453
|
+
return;
|
|
2454
|
+
validator(formattedOrder)?.then((err) => {
|
|
2455
|
+
setErrors(err);
|
|
2456
|
+
});
|
|
2457
|
+
}, [
|
|
2458
|
+
formattedOrder.broker_id,
|
|
2459
|
+
formattedOrder.order_quantity,
|
|
2460
|
+
formattedOrder.total,
|
|
2461
|
+
formattedOrder.trigger_price,
|
|
2462
|
+
markPrice
|
|
2463
|
+
]);
|
|
2464
|
+
React.useEffect(() => {
|
|
2465
|
+
if (!optionsValue?.watchOrderbook)
|
|
2466
|
+
return;
|
|
2467
|
+
ee.on("orderbook:update", onOrderbookUpdate);
|
|
2468
|
+
return () => {
|
|
2469
|
+
ee.off("orderbook_update", onOrderbookUpdate);
|
|
2470
|
+
};
|
|
2471
|
+
}, [optionsValue?.watchOrderbook]);
|
|
2472
|
+
React.useEffect(() => {
|
|
2473
|
+
askAndBid.current = [];
|
|
2474
|
+
}, [parsedData?.symbol]);
|
|
2475
|
+
const getPriceAndQty = (symbolOrOrder2) => {
|
|
2476
|
+
let quantity = Number(symbolOrOrder2.order_quantity);
|
|
2477
|
+
const orderPrice = Number(symbolOrOrder2.order_price);
|
|
2478
|
+
if (isNaN(quantity) || quantity <= 0 || askAndBid.current.length === 0)
|
|
2479
|
+
return null;
|
|
2480
|
+
if ((symbolOrOrder2.order_type === types.OrderType.LIMIT || symbolOrOrder2.order_type === types.OrderType.STOP_LIMIT) && isNaN(orderPrice))
|
|
2481
|
+
return null;
|
|
2482
|
+
let price;
|
|
2483
|
+
if (symbolOrOrder2.order_type === types.OrderType.MARKET || symbolOrOrder2.order_type === types.OrderType.STOP_MARKET) {
|
|
2484
|
+
if (symbolOrOrder2.side === types.OrderSide.BUY) {
|
|
2485
|
+
price = askAndBid.current[0];
|
|
2486
|
+
} else {
|
|
2487
|
+
price = askAndBid.current[1];
|
|
2488
|
+
}
|
|
2489
|
+
} else {
|
|
2490
|
+
if (symbolOrOrder2.side === types.OrderSide.BUY) {
|
|
2491
|
+
if (orderPrice >= askAndBid.current[0]) {
|
|
2492
|
+
price = askAndBid.current[0];
|
|
2493
|
+
} else {
|
|
2494
|
+
price = orderPrice;
|
|
2495
|
+
}
|
|
2496
|
+
} else {
|
|
2497
|
+
if (orderPrice <= askAndBid.current[1]) {
|
|
2498
|
+
price = askAndBid.current[1];
|
|
2499
|
+
} else {
|
|
2500
|
+
price = orderPrice;
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
if (symbolOrOrder2.side === types.OrderSide.SELL) {
|
|
2505
|
+
quantity = -quantity;
|
|
2506
|
+
}
|
|
2507
|
+
return { price, quantity };
|
|
2508
|
+
};
|
|
2509
|
+
const estLiqPrice = React.useMemo(() => {
|
|
2510
|
+
if (!accountInfo || !parsedData || !markPrice)
|
|
2511
|
+
return null;
|
|
2512
|
+
const result = getPriceAndQty(formattedOrder);
|
|
2513
|
+
if (result === null)
|
|
2514
|
+
return null;
|
|
2515
|
+
const { price, quantity } = result;
|
|
2516
|
+
if (!price || !quantity)
|
|
2517
|
+
return null;
|
|
2518
|
+
const liqPrice = perp.order.estLiqPrice({
|
|
2519
|
+
markPrice,
|
|
2520
|
+
baseIMR,
|
|
2521
|
+
baseMMR,
|
|
2522
|
+
totalCollateral,
|
|
2523
|
+
positions: positions3,
|
|
2524
|
+
IMR_Factor: accountInfo["imr_factor"][symbol],
|
|
2525
|
+
newOrder: {
|
|
2526
|
+
qty: quantity,
|
|
2527
|
+
price,
|
|
2528
|
+
symbol: parsedData.symbol
|
|
2529
|
+
}
|
|
2530
|
+
});
|
|
2531
|
+
if (liqPrice <= 0)
|
|
2532
|
+
return null;
|
|
2533
|
+
return liqPrice;
|
|
2534
|
+
}, [
|
|
2535
|
+
markPrice,
|
|
2536
|
+
baseIMR,
|
|
2537
|
+
baseMMR,
|
|
2538
|
+
totalCollateral,
|
|
2539
|
+
formattedOrder?.order_price,
|
|
2540
|
+
formattedOrder?.order_quantity,
|
|
2541
|
+
formattedOrder?.total,
|
|
2542
|
+
formattedOrder?.trigger_price,
|
|
2543
|
+
accountInfo
|
|
2544
|
+
]);
|
|
2545
|
+
const estLeverage = React.useMemo(() => {
|
|
2546
|
+
if (!accountInfo || !parsedData)
|
|
2547
|
+
return null;
|
|
2548
|
+
const result = getPriceAndQty(formattedOrder);
|
|
2549
|
+
if (result === null || !result.price || !result.quantity)
|
|
2550
|
+
return null;
|
|
2551
|
+
const leverage = perp.order.estLeverage({
|
|
2552
|
+
totalCollateral,
|
|
2553
|
+
positions: positions3,
|
|
2554
|
+
newOrder: {
|
|
2555
|
+
symbol: parsedData.symbol,
|
|
2556
|
+
qty: result.quantity,
|
|
2557
|
+
price: result.price
|
|
2558
|
+
}
|
|
2559
|
+
});
|
|
2560
|
+
return leverage;
|
|
2561
|
+
}, [
|
|
2562
|
+
baseIMR,
|
|
2563
|
+
baseMMR,
|
|
2564
|
+
totalCollateral,
|
|
2565
|
+
positions3,
|
|
2566
|
+
formattedOrder?.order_price,
|
|
2567
|
+
formattedOrder?.order_quantity,
|
|
2568
|
+
formattedOrder?.total,
|
|
2569
|
+
formattedOrder?.trigger_price
|
|
2570
|
+
]);
|
|
1905
2571
|
return {
|
|
1906
2572
|
maxQty,
|
|
1907
2573
|
freeCollateral,
|
|
1908
2574
|
markPrice,
|
|
1909
2575
|
onSubmit,
|
|
2576
|
+
submit,
|
|
1910
2577
|
submitting: isMutating,
|
|
2578
|
+
formattedOrder,
|
|
2579
|
+
// errors,
|
|
2580
|
+
estLiqPrice,
|
|
2581
|
+
estLeverage,
|
|
1911
2582
|
helper: {
|
|
1912
2583
|
calculate,
|
|
1913
2584
|
validator
|
|
2585
|
+
// clearErrors,
|
|
2586
|
+
},
|
|
2587
|
+
metaState: {
|
|
2588
|
+
dirty: fieldDirty.current,
|
|
2589
|
+
submitted: submitted.current,
|
|
2590
|
+
errors
|
|
1914
2591
|
},
|
|
1915
2592
|
symbolConfig: symbolInfo[symbol]()
|
|
1916
2593
|
};
|
|
1917
|
-
}
|
|
2594
|
+
}
|
|
1918
2595
|
|
|
1919
2596
|
// src/orderly/useAccountInfo.ts
|
|
1920
2597
|
var useAccountInfo = () => {
|
|
1921
2598
|
return usePrivateQuery("/v1/client/info");
|
|
1922
2599
|
};
|
|
1923
|
-
var
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
2600
|
+
var MarketsType = /* @__PURE__ */ ((MarketsType2) => {
|
|
2601
|
+
MarketsType2[MarketsType2["FAVORITES"] = 0] = "FAVORITES";
|
|
2602
|
+
MarketsType2[MarketsType2["RECENT"] = 1] = "RECENT";
|
|
2603
|
+
MarketsType2[MarketsType2["ALL"] = 2] = "ALL";
|
|
2604
|
+
return MarketsType2;
|
|
2605
|
+
})(MarketsType || {});
|
|
2606
|
+
var useMarkets = (type) => {
|
|
2607
|
+
const marketsKey = "markets";
|
|
2608
|
+
const { data } = useMarketsStream();
|
|
2609
|
+
const { configStore } = React.useContext(OrderlyContext);
|
|
2610
|
+
const publicInfo = useSymbolsInfo();
|
|
2611
|
+
if (!configStore.get(marketsKey)) {
|
|
2612
|
+
const jsonStr = localStorage.getItem(marketsKey);
|
|
2613
|
+
if (jsonStr) {
|
|
2614
|
+
configStore.set(marketsKey, JSON.parse(jsonStr));
|
|
2615
|
+
} else {
|
|
2616
|
+
const defaultTab = { name: "Popular", id: 1 };
|
|
2617
|
+
configStore.set(marketsKey, {
|
|
2618
|
+
recent: [],
|
|
2619
|
+
favorites: [
|
|
2620
|
+
{ name: "PERP_ETH_USDC", tabs: [{ ...defaultTab }] },
|
|
2621
|
+
{ name: "PERP_BTC_USDC", tabs: [{ ...defaultTab }] }
|
|
2622
|
+
],
|
|
2623
|
+
favoriteTabs: [{ ...defaultTab }],
|
|
2624
|
+
lastSelectFavoriteTab: { ...defaultTab }
|
|
2625
|
+
});
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
const getFavoriteTabs = React.useMemo(() => {
|
|
2629
|
+
const tabs2 = configStore.get(marketsKey)["favoriteTabs"];
|
|
2630
|
+
return tabs2 || [{ name: "Popular", id: 1 }];
|
|
2631
|
+
}, []);
|
|
2632
|
+
const getFavorites = React.useMemo(() => {
|
|
2633
|
+
const curData = configStore.get(marketsKey)["favorites"] || [];
|
|
2634
|
+
const tabs2 = getFavoriteTabs;
|
|
2635
|
+
const result = [];
|
|
2636
|
+
for (let index = 0; index < curData.length; index++) {
|
|
2637
|
+
const favData = curData[index];
|
|
2638
|
+
var favTabs = favData.tabs.filter((tab) => tabs2.findIndex((item) => tab.id === item.id) !== -1);
|
|
2639
|
+
if (favTabs.length > 0) {
|
|
2640
|
+
result.push({ ...favData, tabs: favTabs });
|
|
1942
2641
|
}
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
2642
|
+
}
|
|
2643
|
+
configStore.set(marketsKey, { ...configStore.getOr(marketsKey, {}), favorites: result });
|
|
2644
|
+
return result;
|
|
2645
|
+
}, [configStore]);
|
|
2646
|
+
const getRecent = React.useMemo(() => {
|
|
2647
|
+
const curData = configStore.get(marketsKey)["recent"];
|
|
2648
|
+
return (curData || []).filter((e) => e);
|
|
2649
|
+
}, []);
|
|
2650
|
+
const [favoriteTabs, setFavoriteTabs] = React.useState(getFavoriteTabs);
|
|
2651
|
+
const [favorites, setFavorites] = React.useState(getFavorites);
|
|
2652
|
+
const [recent, setRecent] = React.useState(getRecent);
|
|
2653
|
+
const updateFavoriteTabs = (tab, operator) => {
|
|
2654
|
+
const saveTabs = (tabs3) => {
|
|
2655
|
+
setFavoriteTabs(tabs3);
|
|
2656
|
+
configStore.set(marketsKey, {
|
|
2657
|
+
...configStore.getOr(marketsKey, {}),
|
|
2658
|
+
"favoriteTabs": tabs3
|
|
2659
|
+
});
|
|
1946
2660
|
};
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
)
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
2661
|
+
if (Array.isArray(tab)) {
|
|
2662
|
+
saveTabs(tab);
|
|
2663
|
+
return;
|
|
2664
|
+
}
|
|
2665
|
+
var tabs2 = [...favoriteTabs];
|
|
2666
|
+
const index = tabs2.findIndex((item) => item.id === tab.id);
|
|
2667
|
+
if (operator?.add) {
|
|
2668
|
+
tabs2.push(tab);
|
|
2669
|
+
} else if (operator?.update) {
|
|
2670
|
+
if (index !== -1) {
|
|
2671
|
+
tabs2[index] = tab;
|
|
2672
|
+
}
|
|
2673
|
+
} else if (operator?.delete) {
|
|
2674
|
+
if (index !== -1) {
|
|
2675
|
+
tabs2.splice(index, 1);
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
saveTabs(tabs2);
|
|
2679
|
+
};
|
|
2680
|
+
const setRecentData = (symbol) => {
|
|
2681
|
+
const curData = [...recent];
|
|
2682
|
+
const index = curData.findIndex((item) => item.name == symbol.symbol);
|
|
2683
|
+
if (index !== -1) {
|
|
2684
|
+
curData.splice(index, 1);
|
|
2685
|
+
}
|
|
2686
|
+
curData.unshift({ name: symbol.symbol });
|
|
2687
|
+
configStore.set(marketsKey, {
|
|
2688
|
+
...configStore.getOr(marketsKey, {}),
|
|
2689
|
+
"recent": curData
|
|
2690
|
+
});
|
|
2691
|
+
setRecent(curData);
|
|
2692
|
+
};
|
|
2693
|
+
const setFavoritesData = (symbol, tab, remove = false) => {
|
|
2694
|
+
const curData = [...favorites];
|
|
2695
|
+
const index = curData.findIndex((item) => item.name == symbol.symbol);
|
|
2696
|
+
if (index === -1) {
|
|
2697
|
+
if (Array.isArray(tab)) {
|
|
2698
|
+
if (tab.length > 0) {
|
|
2699
|
+
curData.unshift({ name: symbol.symbol, tabs: tab });
|
|
2700
|
+
}
|
|
2701
|
+
} else {
|
|
2702
|
+
if (!remove) {
|
|
2703
|
+
curData.unshift({ name: symbol.symbol, tabs: [tab] });
|
|
1967
2704
|
}
|
|
1968
|
-
return data;
|
|
1969
2705
|
}
|
|
1970
|
-
|
|
2706
|
+
} else {
|
|
2707
|
+
const favorite = curData[index];
|
|
2708
|
+
if (Array.isArray(tab)) {
|
|
2709
|
+
if (tab.length === 0) {
|
|
2710
|
+
curData.splice(index, 1);
|
|
2711
|
+
} else {
|
|
2712
|
+
curData[index] = { ...favorite, tabs: tab };
|
|
2713
|
+
}
|
|
2714
|
+
} else {
|
|
2715
|
+
if (remove) {
|
|
2716
|
+
const tabs2 = favorite.tabs.filter((tab2) => tab2.id != tab2.id);
|
|
2717
|
+
if (tabs2.length === 0) {
|
|
2718
|
+
curData.splice(index, 1);
|
|
2719
|
+
} else {
|
|
2720
|
+
curData[index] = { ...favorite, tabs: tabs2 };
|
|
2721
|
+
}
|
|
2722
|
+
} else {
|
|
2723
|
+
const tabs2 = favorite.tabs;
|
|
2724
|
+
tabs2.push(tab);
|
|
2725
|
+
curData[index] = { ...favorite, tabs: tabs2 };
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
}
|
|
2729
|
+
configStore.set(marketsKey, {
|
|
2730
|
+
...configStore.getOr(marketsKey, {}),
|
|
2731
|
+
"favorites": curData
|
|
1971
2732
|
});
|
|
1972
|
-
|
|
1973
|
-
|
|
2733
|
+
setFavorites(() => curData);
|
|
2734
|
+
};
|
|
2735
|
+
const getData = (type2) => {
|
|
2736
|
+
const localData = type2 === 0 /* FAVORITES */ ? [...favorites] : [...recent];
|
|
2737
|
+
const keys = localData.map((item) => item.name);
|
|
2738
|
+
const filter = type2 == 2 /* ALL */ ? data : data?.filter((item) => keys.includes(item.symbol));
|
|
2739
|
+
const favoritesData = [...favorites];
|
|
2740
|
+
const favoriteKeys = favoritesData.map((item) => item.name);
|
|
2741
|
+
if (filter) {
|
|
2742
|
+
for (let index = 0; index < filter.length; index++) {
|
|
2743
|
+
const element = filter[index];
|
|
2744
|
+
const isFavorite = type2 == 0 /* FAVORITES */ ? true : favoriteKeys.includes(element.symbol);
|
|
2745
|
+
const fIndex = favoritesData.findIndex((item) => item.name === element.symbol);
|
|
2746
|
+
const tabs2 = fIndex === -1 ? [] : favoritesData[fIndex].tabs;
|
|
2747
|
+
let imr = void 0;
|
|
2748
|
+
if (publicInfo) {
|
|
2749
|
+
imr = publicInfo?.[element.symbol]("base_imr");
|
|
2750
|
+
}
|
|
2751
|
+
filter[index] = {
|
|
2752
|
+
...filter[index],
|
|
2753
|
+
// @ts-ignore
|
|
2754
|
+
isFavorite,
|
|
2755
|
+
tabs: tabs2,
|
|
2756
|
+
leverage: imr ? 1 / imr : void 0
|
|
2757
|
+
};
|
|
2758
|
+
}
|
|
2759
|
+
}
|
|
2760
|
+
return filter;
|
|
2761
|
+
};
|
|
2762
|
+
const addToHistory = (symbol) => {
|
|
2763
|
+
setRecentData(symbol);
|
|
2764
|
+
};
|
|
2765
|
+
const updateSymbolFavoriteState = (symbol, tab, del = false) => {
|
|
2766
|
+
setFavoritesData(symbol, tab, del);
|
|
2767
|
+
};
|
|
2768
|
+
const markets = getData(type);
|
|
2769
|
+
const pinToTop = (symbol) => {
|
|
2770
|
+
const index = favorites.findIndex((item) => item.name === symbol.symbol);
|
|
2771
|
+
if (index !== -1) {
|
|
2772
|
+
const element = favorites[index];
|
|
2773
|
+
const list = [...favorites];
|
|
2774
|
+
list.splice(index, 1);
|
|
2775
|
+
list.unshift(element);
|
|
2776
|
+
configStore.set(marketsKey, {
|
|
2777
|
+
...configStore.getOr(marketsKey, {}),
|
|
2778
|
+
"favorites": list
|
|
2779
|
+
});
|
|
2780
|
+
setFavorites(list);
|
|
2781
|
+
}
|
|
2782
|
+
};
|
|
2783
|
+
const tabs = React.useMemo(() => {
|
|
2784
|
+
return favoriteTabs;
|
|
2785
|
+
}, [favoriteTabs]);
|
|
2786
|
+
const getLastSelFavTab = () => {
|
|
2787
|
+
const curData = configStore.get(marketsKey)["lastSelectedFavoriteTab"];
|
|
2788
|
+
return curData || { name: "Popular", id: 1 };
|
|
2789
|
+
};
|
|
2790
|
+
const updateSelectedFavoriteTab = (tab) => {
|
|
2791
|
+
configStore.set(marketsKey, {
|
|
2792
|
+
...configStore.getOr(marketsKey, {}),
|
|
2793
|
+
lastSelectedFavoriteTab: tab
|
|
2794
|
+
});
|
|
2795
|
+
};
|
|
2796
|
+
return [
|
|
2797
|
+
markets || [],
|
|
2798
|
+
{
|
|
2799
|
+
favoriteTabs: tabs,
|
|
2800
|
+
favorites,
|
|
2801
|
+
recent,
|
|
2802
|
+
addToHistory,
|
|
2803
|
+
// updateFavoriteTabs("tab", operator: {add/update/delete})
|
|
2804
|
+
updateFavoriteTabs,
|
|
2805
|
+
updateSymbolFavoriteState,
|
|
2806
|
+
pinToTop,
|
|
2807
|
+
getLastSelFavTab,
|
|
2808
|
+
updateSelectedFavoriteTab
|
|
2809
|
+
}
|
|
2810
|
+
];
|
|
1974
2811
|
};
|
|
1975
2812
|
var useLeverage = () => {
|
|
1976
2813
|
const { data, mutate: mutate2 } = usePrivateQuery("/v1/client/info");
|
|
@@ -2091,7 +2928,7 @@ var useMarketTradeStream = (symbol, options = {}) => {
|
|
|
2091
2928
|
return { data: trades, isLoading };
|
|
2092
2929
|
};
|
|
2093
2930
|
var useMarginRatio = () => {
|
|
2094
|
-
const [{ rows }] = usePositionStream();
|
|
2931
|
+
const [{ rows, aggregated }] = usePositionStream();
|
|
2095
2932
|
const { data: markPrices } = useMarkPricesStream();
|
|
2096
2933
|
const { totalCollateral } = useCollateral();
|
|
2097
2934
|
const marginRatio = React.useMemo(() => {
|
|
@@ -2108,7 +2945,20 @@ var useMarginRatio = () => {
|
|
|
2108
2945
|
const currentLeverage = React.useMemo(() => {
|
|
2109
2946
|
return perp.account.currentLeverage(marginRatio);
|
|
2110
2947
|
}, [marginRatio]);
|
|
2111
|
-
|
|
2948
|
+
const mmr = React.useMemo(() => {
|
|
2949
|
+
if (!rows || rows.length <= 0)
|
|
2950
|
+
return null;
|
|
2951
|
+
let positionsMM = utils.zero;
|
|
2952
|
+
for (let index = 0; index < rows.length; index++) {
|
|
2953
|
+
const item = rows[index];
|
|
2954
|
+
positionsMM = positionsMM.add(item.mm);
|
|
2955
|
+
}
|
|
2956
|
+
return perp.account.MMR({
|
|
2957
|
+
positionsMMR: positionsMM.toNumber(),
|
|
2958
|
+
positionsNotional: aggregated.notional
|
|
2959
|
+
});
|
|
2960
|
+
}, [rows, aggregated]);
|
|
2961
|
+
return { marginRatio, currentLeverage, mmr };
|
|
2112
2962
|
};
|
|
2113
2963
|
|
|
2114
2964
|
// src/woo/constants.ts
|
|
@@ -3124,53 +3974,41 @@ var woofiDexCrossChainRouterAbi = [
|
|
|
3124
3974
|
];
|
|
3125
3975
|
var nativeTokenAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
3126
3976
|
var isNativeTokenChecker = (address) => address === nativeTokenAddress;
|
|
3127
|
-
|
|
3128
|
-
// src/orderly/useChains.ts
|
|
3129
3977
|
var useChains = (networkId, options = {}) => {
|
|
3130
|
-
const {
|
|
3131
|
-
const { configStore
|
|
3978
|
+
const { pick: pick3, crossEnabled, wooSwapEnabled, ...swrOptions } = options;
|
|
3979
|
+
const { configStore } = React.useContext(OrderlyContext);
|
|
3132
3980
|
const field = options?.pick;
|
|
3981
|
+
const filterFun = React.useRef(options?.filter);
|
|
3982
|
+
filterFun.current = options?.filter;
|
|
3133
3983
|
const map = React.useRef(
|
|
3134
3984
|
/* @__PURE__ */ new Map()
|
|
3135
3985
|
);
|
|
3136
|
-
const
|
|
3986
|
+
const commonSwrOpts = {
|
|
3987
|
+
revalidateIfStale: false,
|
|
3988
|
+
revalidateOnFocus: false,
|
|
3989
|
+
revalidateOnReconnect: false,
|
|
3990
|
+
// If false, undefined data gets cached against the key.
|
|
3991
|
+
revalidateOnMount: true,
|
|
3992
|
+
// dont duplicate a request w/ same key for 1hr
|
|
3993
|
+
dedupingInterval: 36e5,
|
|
3994
|
+
...swrOptions
|
|
3995
|
+
};
|
|
3996
|
+
const { data: wooSupportData, error: swapSupportError } = useSWR__default.default(
|
|
3137
3997
|
() => wooSwapEnabled ? `${configStore.get("swapSupportApiUrl")}/swap_support` : null,
|
|
3138
|
-
// `${configStore.get("swapSupportApiUrl")}/swap_support`,
|
|
3139
3998
|
(url) => fetch(url).then((res) => res.json()),
|
|
3140
|
-
{
|
|
3141
|
-
revalidateIfStale: false,
|
|
3142
|
-
revalidateOnFocus: false,
|
|
3143
|
-
revalidateOnReconnect: false,
|
|
3144
|
-
revalidateOnMount: true,
|
|
3145
|
-
// If false, undefined data gets cached against the key.
|
|
3146
|
-
dedupingInterval: 36e5,
|
|
3147
|
-
// dont duplicate a request w/ same key for 1hr
|
|
3148
|
-
...swrOptions
|
|
3149
|
-
}
|
|
3999
|
+
{ ...commonSwrOpts, ...swrOptions }
|
|
3150
4000
|
);
|
|
3151
4001
|
const { data: chainInfos, error: chainInfoErr } = useQuery(
|
|
3152
4002
|
"/v1/public/chain_info",
|
|
3153
4003
|
{
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
revalidateOnReconnect: false,
|
|
3157
|
-
revalidateOnMount: true,
|
|
3158
|
-
dedupingInterval: 36e5,
|
|
3159
|
-
formatter: (data2) => data2.rows
|
|
4004
|
+
...commonSwrOpts,
|
|
4005
|
+
formatter: (data) => data.rows
|
|
3160
4006
|
}
|
|
3161
4007
|
);
|
|
3162
4008
|
const { data: orderlyChains, error: tokenError } = useQuery(
|
|
3163
|
-
// wooSwapEnabled ? "/v1/public/token" :
|
|
3164
4009
|
"https://api-evm.orderly.org/v1/public/token",
|
|
3165
|
-
{
|
|
3166
|
-
revalidateIfStale: false,
|
|
3167
|
-
revalidateOnFocus: false,
|
|
3168
|
-
revalidateOnReconnect: false,
|
|
3169
|
-
revalidateOnMount: true,
|
|
3170
|
-
dedupingInterval: 36e5
|
|
3171
|
-
}
|
|
4010
|
+
{ ...commonSwrOpts }
|
|
3172
4011
|
);
|
|
3173
|
-
configStore.get("apiBaseUrl");
|
|
3174
4012
|
const chains = React.useMemo(() => {
|
|
3175
4013
|
let orderlyChainsArr = [];
|
|
3176
4014
|
const orderlyChainIds = /* @__PURE__ */ new Set();
|
|
@@ -3185,6 +4023,7 @@ var useChains = (networkId, options = {}) => {
|
|
|
3185
4023
|
// "public_rpc_url": "https://arb1.arbitrum.io/rpc",
|
|
3186
4024
|
chain_id: chainId,
|
|
3187
4025
|
withdrawal_fee: chain.withdrawal_fee,
|
|
4026
|
+
cross_chain_withdrawal_fee: chain.cross_chain_withdrawal_fee,
|
|
3188
4027
|
bridgeless: true
|
|
3189
4028
|
},
|
|
3190
4029
|
token_infos: [
|
|
@@ -3195,13 +4034,13 @@ var useChains = (networkId, options = {}) => {
|
|
|
3195
4034
|
}
|
|
3196
4035
|
]
|
|
3197
4036
|
};
|
|
3198
|
-
if (typeof
|
|
3199
|
-
if (!
|
|
4037
|
+
if (typeof filterFun.current === "function") {
|
|
4038
|
+
if (!filterFun.current(_chain))
|
|
3200
4039
|
return;
|
|
3201
4040
|
}
|
|
3202
|
-
if (_chain.chain_id
|
|
3203
|
-
const index = testnetArr
|
|
3204
|
-
(item2) => item2.network_infos.chain_id
|
|
4041
|
+
if (utils.isTestnet(_chain.chain_id)) {
|
|
4042
|
+
const index = testnetArr?.findIndex(
|
|
4043
|
+
(item2) => utils.isTestnet(item2.network_infos.chain_id)
|
|
3205
4044
|
);
|
|
3206
4045
|
if (index > -1) {
|
|
3207
4046
|
testnetArr[index] = _chain;
|
|
@@ -3211,40 +4050,16 @@ var useChains = (networkId, options = {}) => {
|
|
|
3211
4050
|
orderlyChainsArr.push(_chain);
|
|
3212
4051
|
});
|
|
3213
4052
|
});
|
|
3214
|
-
let testnetArr = [
|
|
3215
|
-
|
|
3216
|
-
|
|
3217
|
-
|
|
3218
|
-
name: "Arbitrum Goerli",
|
|
3219
|
-
shortName: "Arbitrum Goerli",
|
|
3220
|
-
public_rpc_url: "https://goerli-rollup.arbitrum.io/rpc",
|
|
3221
|
-
chain_id: 421613,
|
|
3222
|
-
currency_symbol: "ETH",
|
|
3223
|
-
bridge_enable: true,
|
|
3224
|
-
mainnet: false,
|
|
3225
|
-
explorer_base_url: "https://goerli.arbiscan.io/",
|
|
3226
|
-
est_txn_mins: null,
|
|
3227
|
-
woofi_dex_cross_chain_router: "",
|
|
3228
|
-
woofi_dex_depositor: ""
|
|
3229
|
-
},
|
|
3230
|
-
token_infos: [
|
|
3231
|
-
{
|
|
3232
|
-
symbol: "USDC",
|
|
3233
|
-
address: "0xfd064A18f3BF249cf1f87FC203E90D8f650f2d63",
|
|
3234
|
-
decimals: 6,
|
|
3235
|
-
swap_enable: false,
|
|
3236
|
-
woofi_dex_precision: 2
|
|
3237
|
-
}
|
|
3238
|
-
]
|
|
3239
|
-
}
|
|
3240
|
-
];
|
|
4053
|
+
let testnetArr = [...types.TestnetChains];
|
|
4054
|
+
testnetArr.forEach((chain) => {
|
|
4055
|
+
map.current.set(chain.network_infos?.chain_id, chain);
|
|
4056
|
+
});
|
|
3241
4057
|
let mainnetArr = [];
|
|
3242
|
-
map.current.set(421613, testnetArr[0]);
|
|
3243
4058
|
if (wooSwapEnabled) {
|
|
3244
|
-
if (!
|
|
3245
|
-
return
|
|
3246
|
-
Object.keys(
|
|
3247
|
-
const chain =
|
|
4059
|
+
if (!wooSupportData || !wooSupportData.data)
|
|
4060
|
+
return wooSupportData;
|
|
4061
|
+
Object.keys(wooSupportData.data).forEach((key) => {
|
|
4062
|
+
const chain = wooSupportData.data[key];
|
|
3248
4063
|
const item = ramda.mergeDeepRight(chain, {
|
|
3249
4064
|
name: key,
|
|
3250
4065
|
network_infos: {
|
|
@@ -3258,8 +4073,8 @@ var useChains = (networkId, options = {}) => {
|
|
|
3258
4073
|
if (item.token_infos?.length === 0)
|
|
3259
4074
|
return;
|
|
3260
4075
|
map.current.set(item.network_infos.chain_id, item);
|
|
3261
|
-
if (typeof
|
|
3262
|
-
if (!
|
|
4076
|
+
if (typeof filterFun.current === "function") {
|
|
4077
|
+
if (!filterFun.current(item))
|
|
3263
4078
|
return;
|
|
3264
4079
|
}
|
|
3265
4080
|
if (item.network_infos.mainnet) {
|
|
@@ -3297,24 +4112,16 @@ var useChains = (networkId, options = {}) => {
|
|
|
3297
4112
|
};
|
|
3298
4113
|
}
|
|
3299
4114
|
map.current.set(_chain.network_infos.chain_id, _chain);
|
|
3300
|
-
if (_chain.network_infos.chain_id
|
|
4115
|
+
if (utils.isTestnet(_chain.network_infos.chain_id)) {
|
|
3301
4116
|
const index = testnetArr.findIndex(
|
|
3302
|
-
(item) => item.network_infos.chain_id
|
|
4117
|
+
(item) => utils.isTestnet(item.network_infos.chain_id)
|
|
3303
4118
|
);
|
|
3304
4119
|
if (index > -1) {
|
|
3305
4120
|
testnetArr[index] = _chain;
|
|
3306
4121
|
}
|
|
3307
4122
|
}
|
|
3308
|
-
if (
|
|
3309
|
-
|
|
3310
|
-
(item) => item.network_infos.chain_id === 420
|
|
3311
|
-
);
|
|
3312
|
-
if (index > -1) {
|
|
3313
|
-
testnetArr[index] = _chain;
|
|
3314
|
-
}
|
|
3315
|
-
}
|
|
3316
|
-
if (typeof options?.filter === "function") {
|
|
3317
|
-
if (!options.filter(_chain))
|
|
4123
|
+
if (typeof filterFun.current === "function") {
|
|
4124
|
+
if (!filterFun.current(_chain))
|
|
3318
4125
|
return;
|
|
3319
4126
|
}
|
|
3320
4127
|
mainnetArr.push(_chain);
|
|
@@ -3341,10 +4148,9 @@ var useChains = (networkId, options = {}) => {
|
|
|
3341
4148
|
mainnet: mainnetArr
|
|
3342
4149
|
};
|
|
3343
4150
|
}, [
|
|
3344
|
-
|
|
4151
|
+
wooSupportData,
|
|
3345
4152
|
networkId,
|
|
3346
4153
|
field,
|
|
3347
|
-
options,
|
|
3348
4154
|
orderlyChains,
|
|
3349
4155
|
wooSwapEnabled,
|
|
3350
4156
|
chainInfos
|
|
@@ -3355,7 +4161,9 @@ var useChains = (networkId, options = {}) => {
|
|
|
3355
4161
|
if (chain) {
|
|
3356
4162
|
chain.nativeToken = chain.token_infos?.find(
|
|
3357
4163
|
(item) => item.address === nativeTokenAddress
|
|
3358
|
-
)
|
|
4164
|
+
) || {
|
|
4165
|
+
symbol: chain.network_infos?.currency_symbol
|
|
4166
|
+
};
|
|
3359
4167
|
}
|
|
3360
4168
|
if (typeof field2 === "string") {
|
|
3361
4169
|
return ramda.prop(field2, chain);
|
|
@@ -3409,7 +4217,21 @@ var useWithdraw = () => {
|
|
|
3409
4217
|
const maxAmount = React.useMemo(() => {
|
|
3410
4218
|
return freeCollateral;
|
|
3411
4219
|
}, [freeCollateral]);
|
|
3412
|
-
|
|
4220
|
+
const availableWithdraw = React.useMemo(() => {
|
|
4221
|
+
if (unsettledPnL < 0) {
|
|
4222
|
+
return freeCollateral;
|
|
4223
|
+
} else {
|
|
4224
|
+
return freeCollateral - unsettledPnL;
|
|
4225
|
+
}
|
|
4226
|
+
}, [freeCollateral, unsettledPnL]);
|
|
4227
|
+
return {
|
|
4228
|
+
withdraw,
|
|
4229
|
+
isLoading,
|
|
4230
|
+
maxAmount,
|
|
4231
|
+
availableBalance,
|
|
4232
|
+
availableWithdraw,
|
|
4233
|
+
unsettledPnL
|
|
4234
|
+
};
|
|
3413
4235
|
};
|
|
3414
4236
|
var useDeposit = (options) => {
|
|
3415
4237
|
const { enableSwapDeposit } = React.useContext(OrderlyContext);
|
|
@@ -3419,28 +4241,41 @@ var useDeposit = (options) => {
|
|
|
3419
4241
|
const [_, { findByChainId }] = useChains(void 0, {
|
|
3420
4242
|
wooSwapEnabled: enableSwapDeposit
|
|
3421
4243
|
});
|
|
4244
|
+
const [quantity, setQuantity] = React.useState("");
|
|
4245
|
+
const [depositFee, setDepositFee] = React.useState(0n);
|
|
4246
|
+
const [depositFeeRevalidating, setDepositFeeRevalidating] = React.useState(false);
|
|
3422
4247
|
const [balance, setBalance] = React.useState("0");
|
|
3423
4248
|
const [allowance, setAllowance] = React.useState("0");
|
|
3424
4249
|
const { account: account5, state } = useAccount();
|
|
3425
4250
|
const prevAddress = React.useRef();
|
|
3426
4251
|
const getBalanceListener = React.useRef();
|
|
4252
|
+
const targetChain = React.useMemo(() => {
|
|
4253
|
+
let chain;
|
|
4254
|
+
if (networkId === "testnet") {
|
|
4255
|
+
chain = findByChainId(types.ARBITRUM_TESTNET_CHAINID);
|
|
4256
|
+
} else {
|
|
4257
|
+
chain = findByChainId(options?.srcChainId);
|
|
4258
|
+
if (!chain?.network_infos?.bridgeless) {
|
|
4259
|
+
chain = findByChainId(types.ARBITRUM_MAINNET_CHAINID);
|
|
4260
|
+
}
|
|
4261
|
+
}
|
|
4262
|
+
return chain;
|
|
4263
|
+
}, [networkId, findByChainId, options?.srcChainId]);
|
|
3427
4264
|
const dst = React.useMemo(() => {
|
|
3428
|
-
|
|
3429
|
-
const USDC = chain?.token_infos.find(
|
|
3430
|
-
(token) => token.symbol === "USDC"
|
|
3431
|
-
);
|
|
3432
|
-
if (!chain) {
|
|
4265
|
+
if (!targetChain) {
|
|
3433
4266
|
throw new Error("dst chain not found");
|
|
3434
4267
|
}
|
|
4268
|
+
const USDC = targetChain?.token_infos.find(
|
|
4269
|
+
(token) => token.symbol === "USDC"
|
|
4270
|
+
);
|
|
3435
4271
|
return {
|
|
3436
4272
|
symbol: "USDC",
|
|
3437
4273
|
address: USDC?.address,
|
|
3438
4274
|
decimals: USDC?.decimals,
|
|
3439
|
-
chainId:
|
|
3440
|
-
network:
|
|
3441
|
-
// chainId: 42161,
|
|
4275
|
+
chainId: targetChain.network_infos.chain_id,
|
|
4276
|
+
network: targetChain.network_infos.shortName
|
|
3442
4277
|
};
|
|
3443
|
-
}, [
|
|
4278
|
+
}, [targetChain]);
|
|
3444
4279
|
const isNativeToken = React.useMemo(
|
|
3445
4280
|
() => isNativeTokenChecker(options?.address || ""),
|
|
3446
4281
|
[options?.address]
|
|
@@ -3467,7 +4302,6 @@ var useDeposit = (options) => {
|
|
|
3467
4302
|
const balance2 = await fetchBalanceHandler(address, decimals);
|
|
3468
4303
|
setBalance(() => balance2);
|
|
3469
4304
|
} catch (e) {
|
|
3470
|
-
console.warn("----- refresh balance error -----", e);
|
|
3471
4305
|
setBalance(() => "0");
|
|
3472
4306
|
}
|
|
3473
4307
|
},
|
|
@@ -3517,18 +4351,30 @@ var useDeposit = (options) => {
|
|
|
3517
4351
|
}
|
|
3518
4352
|
}
|
|
3519
4353
|
}, [options, dst]);
|
|
4354
|
+
const queryBalance = useDebounce.useDebouncedCallback(
|
|
4355
|
+
(tokenAddress, decimals) => {
|
|
4356
|
+
fetchBalance(options?.address, options?.decimals).finally(() => {
|
|
4357
|
+
setBalanceRevalidating(false);
|
|
4358
|
+
});
|
|
4359
|
+
},
|
|
4360
|
+
100
|
|
4361
|
+
);
|
|
4362
|
+
const queryAllowance = useDebounce.useDebouncedCallback(
|
|
4363
|
+
(tokenAddress, vaultAddress) => {
|
|
4364
|
+
getAllowance(tokenAddress, vaultAddress);
|
|
4365
|
+
},
|
|
4366
|
+
100
|
|
4367
|
+
);
|
|
3520
4368
|
React.useEffect(() => {
|
|
3521
4369
|
if (state.status < types.AccountStatusEnum.Connected)
|
|
3522
4370
|
return;
|
|
3523
4371
|
setBalanceRevalidating(true);
|
|
3524
|
-
|
|
3525
|
-
setBalanceRevalidating(false);
|
|
3526
|
-
});
|
|
4372
|
+
queryBalance(options?.address, options?.decimals);
|
|
3527
4373
|
if (dst.chainId !== options?.srcChainId) {
|
|
3528
|
-
|
|
4374
|
+
queryAllowance(options?.address, options?.crossChainRouteAddress);
|
|
3529
4375
|
} else {
|
|
3530
4376
|
if (dst.symbol !== options?.srcToken) {
|
|
3531
|
-
|
|
4377
|
+
queryAllowance(options?.address, options?.depositorAddress);
|
|
3532
4378
|
} else {
|
|
3533
4379
|
getAllowanceByDefaultAddress(options?.address);
|
|
3534
4380
|
}
|
|
@@ -3545,41 +4391,72 @@ var useDeposit = (options) => {
|
|
|
3545
4391
|
dst.symbol
|
|
3546
4392
|
]);
|
|
3547
4393
|
const approve = React.useCallback(
|
|
3548
|
-
(amount) => {
|
|
4394
|
+
async (amount) => {
|
|
3549
4395
|
if (!options?.address) {
|
|
3550
4396
|
throw new Error("address is required");
|
|
3551
4397
|
}
|
|
3552
4398
|
const vaultAddress = getVaultAddress();
|
|
3553
4399
|
return account5.assetsManager.approve(options.address, amount, vaultAddress).then((result) => {
|
|
3554
4400
|
if (typeof amount !== "undefined") {
|
|
3555
|
-
setAllowance(
|
|
4401
|
+
setAllowance(
|
|
4402
|
+
(value) => new utils.Decimal(value).add(amount || types.MaxUint256.toString()).toString()
|
|
4403
|
+
);
|
|
3556
4404
|
}
|
|
3557
4405
|
return result;
|
|
3558
4406
|
});
|
|
3559
4407
|
},
|
|
3560
4408
|
[account5, getAllowance, options?.address]
|
|
3561
4409
|
);
|
|
3562
|
-
const deposit = React.useCallback(
|
|
3563
|
-
(
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
},
|
|
3570
|
-
[account5, fetchBalance, getAllowance]
|
|
3571
|
-
);
|
|
4410
|
+
const deposit = React.useCallback(async () => {
|
|
4411
|
+
return account5.assetsManager.deposit(quantity, depositFee).then((res) => {
|
|
4412
|
+
setAllowance((value) => new utils.Decimal(value).sub(quantity).toString());
|
|
4413
|
+
setBalance((value) => new utils.Decimal(value).sub(quantity).toString());
|
|
4414
|
+
return res;
|
|
4415
|
+
});
|
|
4416
|
+
}, [account5, fetchBalance, getAllowance, quantity, depositFee]);
|
|
3572
4417
|
const loopGetBalance = async () => {
|
|
3573
4418
|
getBalanceListener.current && clearTimeout(getBalanceListener.current);
|
|
3574
4419
|
getBalanceListener.current = setTimeout(async () => {
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
4420
|
+
try {
|
|
4421
|
+
const balance2 = await fetchBalanceHandler(
|
|
4422
|
+
options?.address,
|
|
4423
|
+
options?.decimals
|
|
4424
|
+
);
|
|
4425
|
+
setBalance(balance2);
|
|
4426
|
+
loopGetBalance();
|
|
4427
|
+
} catch (err) {
|
|
4428
|
+
}
|
|
3581
4429
|
}, 3e3);
|
|
3582
4430
|
};
|
|
4431
|
+
const getDepositFee = React.useCallback(
|
|
4432
|
+
async (quantity2) => {
|
|
4433
|
+
return account5.assetsManager.getDepositFee(
|
|
4434
|
+
quantity2,
|
|
4435
|
+
targetChain?.network_infos
|
|
4436
|
+
);
|
|
4437
|
+
},
|
|
4438
|
+
[account5, targetChain]
|
|
4439
|
+
);
|
|
4440
|
+
const enquiryDepositFee = React.useCallback(() => {
|
|
4441
|
+
if (isNaN(Number(quantity)) || !quantity) {
|
|
4442
|
+
setDepositFee(0n);
|
|
4443
|
+
setDepositFeeRevalidating(false);
|
|
4444
|
+
return;
|
|
4445
|
+
}
|
|
4446
|
+
setDepositFeeRevalidating(true);
|
|
4447
|
+
getDepositFee(quantity).then((res = 0n) => {
|
|
4448
|
+
const fee = BigInt(
|
|
4449
|
+
new utils.Decimal(res.toString()).mul(types.DEPOSIT_FEE_RATE).toFixed(0, utils.Decimal.ROUND_UP).toString()
|
|
4450
|
+
);
|
|
4451
|
+
setDepositFee(fee);
|
|
4452
|
+
}).catch((error) => {
|
|
4453
|
+
}).finally(() => {
|
|
4454
|
+
setDepositFeeRevalidating(false);
|
|
4455
|
+
});
|
|
4456
|
+
}, [quantity]);
|
|
4457
|
+
React.useEffect(() => {
|
|
4458
|
+
enquiryDepositFee();
|
|
4459
|
+
}, [quantity]);
|
|
3583
4460
|
React.useEffect(() => {
|
|
3584
4461
|
if (!options?.address) {
|
|
3585
4462
|
return;
|
|
@@ -3590,16 +4467,25 @@ var useDeposit = (options) => {
|
|
|
3590
4467
|
};
|
|
3591
4468
|
}, [options?.address, options?.decimals]);
|
|
3592
4469
|
return {
|
|
4470
|
+
/** orderly support chain dst */
|
|
3593
4471
|
dst,
|
|
3594
4472
|
balance,
|
|
3595
4473
|
allowance,
|
|
3596
4474
|
isNativeToken,
|
|
3597
4475
|
balanceRevalidating,
|
|
3598
4476
|
allowanceRevalidating,
|
|
4477
|
+
/** input quantiy */
|
|
4478
|
+
quantity,
|
|
4479
|
+
/** orderly deposit fee, unit: wei */
|
|
4480
|
+
depositFee,
|
|
4481
|
+
/** enquiring depositFee status on chain */
|
|
4482
|
+
depositFeeRevalidating,
|
|
3599
4483
|
approve,
|
|
3600
4484
|
deposit,
|
|
3601
4485
|
fetchBalances,
|
|
3602
|
-
fetchBalance: fetchBalanceHandler
|
|
4486
|
+
fetchBalance: fetchBalanceHandler,
|
|
4487
|
+
/** set input quantity */
|
|
4488
|
+
setQuantity
|
|
3603
4489
|
};
|
|
3604
4490
|
};
|
|
3605
4491
|
var useWalletSubscription = (options) => {
|
|
@@ -3642,6 +4528,32 @@ var useSettleSubscription = (options) => {
|
|
|
3642
4528
|
return () => unsubscribe();
|
|
3643
4529
|
});
|
|
3644
4530
|
};
|
|
4531
|
+
var useSymbolPriceRange = (symbol, side, price) => {
|
|
4532
|
+
const config = useSymbolsInfo();
|
|
4533
|
+
const priceRange = config?.[symbol]("price_range");
|
|
4534
|
+
const priceScrope = config?.[symbol]("price_scope");
|
|
4535
|
+
const { data: prices } = useMarkPricesStream();
|
|
4536
|
+
const markPrice = price || prices?.[symbol];
|
|
4537
|
+
const range = React.useMemo(() => {
|
|
4538
|
+
if (config === void 0 || markPrice === void 0) {
|
|
4539
|
+
return void 0;
|
|
4540
|
+
}
|
|
4541
|
+
if (priceRange === void 0 || Number.isNaN(markPrice)) {
|
|
4542
|
+
return void 0;
|
|
4543
|
+
}
|
|
4544
|
+
if (side === "BUY") {
|
|
4545
|
+
return {
|
|
4546
|
+
max: new utils.Decimal(markPrice).mul(1 + priceRange).toNumber(),
|
|
4547
|
+
min: new utils.Decimal(markPrice).mul(1 - priceScrope).toNumber()
|
|
4548
|
+
};
|
|
4549
|
+
}
|
|
4550
|
+
return {
|
|
4551
|
+
max: new utils.Decimal(markPrice).mul(1 + priceScrope).toNumber(),
|
|
4552
|
+
min: new utils.Decimal(markPrice).mul(1 - priceRange).toNumber()
|
|
4553
|
+
};
|
|
4554
|
+
}, [symbol, side, priceRange, markPrice]);
|
|
4555
|
+
return range;
|
|
4556
|
+
};
|
|
3645
4557
|
function useMediaQuery(query) {
|
|
3646
4558
|
const getMatches = (query2) => {
|
|
3647
4559
|
if (typeof window !== "undefined") {
|
|
@@ -3681,8 +4593,8 @@ var useWooSwapQuery = () => {
|
|
|
3681
4593
|
return;
|
|
3682
4594
|
start();
|
|
3683
4595
|
const params = {
|
|
3684
|
-
|
|
3685
|
-
network: "arbitrum",
|
|
4596
|
+
network: inputs.srcNetwork,
|
|
4597
|
+
// network: "arbitrum",
|
|
3686
4598
|
from_token: inputs.srcToken,
|
|
3687
4599
|
to_token: inputs.dstToken,
|
|
3688
4600
|
//account.assetsManager.usdcAddress,
|
|
@@ -4285,6 +5197,7 @@ Object.defineProperty(exports, 'useConstant', {
|
|
|
4285
5197
|
enumerable: true,
|
|
4286
5198
|
get: function () { return useConstant4__default.default; }
|
|
4287
5199
|
});
|
|
5200
|
+
exports.MarketsType = MarketsType;
|
|
4288
5201
|
exports.OrderlyConfigProvider = OrderlyConfigProvider;
|
|
4289
5202
|
exports.OrderlyContext = OrderlyContext;
|
|
4290
5203
|
exports.OrderlyProvider = OrderlyProvider;
|
|
@@ -4310,6 +5223,7 @@ exports.useMarginRatio = useMarginRatio;
|
|
|
4310
5223
|
exports.useMarkPrice = useMarkPrice;
|
|
4311
5224
|
exports.useMarkPricesStream = useMarkPricesStream;
|
|
4312
5225
|
exports.useMarketTradeStream = useMarketTradeStream;
|
|
5226
|
+
exports.useMarkets = useMarkets;
|
|
4313
5227
|
exports.useMarketsStream = useMarketsStream;
|
|
4314
5228
|
exports.useMaxQty = useMaxQty;
|
|
4315
5229
|
exports.useMediaQuery = useMediaQuery;
|
|
@@ -4326,6 +5240,7 @@ exports.useQuery = useQuery;
|
|
|
4326
5240
|
exports.useSessionStorage = useSessionStorage;
|
|
4327
5241
|
exports.useSettleSubscription = useSettleSubscription;
|
|
4328
5242
|
exports.useSwap = useSwap;
|
|
5243
|
+
exports.useSymbolPriceRange = useSymbolPriceRange;
|
|
4329
5244
|
exports.useSymbolsInfo = useSymbolsInfo;
|
|
4330
5245
|
exports.useTickerStream = useTickerStream;
|
|
4331
5246
|
exports.useWS = useWS;
|