@orderly.network/hooks 2.0.1-alpha.1 → 2.0.1-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -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-alpha.1";
35
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.0.1-alpha.3";
36
36
  }
37
- var version_default = "2.0.1-alpha.1";
37
+ var version_default = "2.0.1-alpha.3";
38
38
  var fetcher = (url, init = {}, queryOptions) => get(url, init, queryOptions?.formatter);
39
39
  var OrderlyContext = createContext({
40
40
  // configStore: new MemoryConfigStore(),
@@ -987,6 +987,9 @@ var MarkPriceCalculator = class extends BaseCalculator {
987
987
 
988
988
  // src/orderly/calculator/calculatorContext.ts
989
989
  var CalculatorContext = class _CalculatorContext {
990
+ static get instance() {
991
+ return this._instance;
992
+ }
990
993
  static create(scope, data) {
991
994
  if (!this._instance) {
992
995
  this._instance = new _CalculatorContext(scope, data);
@@ -1021,6 +1024,9 @@ var CalculatorContext = class _CalculatorContext {
1021
1024
  this.accountInfo = void 0;
1022
1025
  this.portfolio = void 0;
1023
1026
  }
1027
+ deleteByName(name) {
1028
+ delete this.output[name];
1029
+ }
1024
1030
  // get positions(): API.PositionTPSLExt[] {
1025
1031
  // if (this.output.positionCalculator) return this.output.positionCalculator;
1026
1032
  // return usePositionStore.getState().positions;
@@ -1050,32 +1056,36 @@ var CalculatorService = class {
1050
1056
  this.calculators = new Map(calculators);
1051
1057
  }
1052
1058
  register(scope, calculator) {
1053
- const count = this.referenceCount.get(calculator.name);
1059
+ const ref_count_name = `${scope}_${calculator.name}`;
1060
+ const count = this.referenceCount.get(ref_count_name);
1054
1061
  if (typeof count !== "undefined" && count > 0) {
1055
- this.referenceCount.set(calculator.name, count + 1);
1062
+ this.referenceCount.set(ref_count_name, count + 1);
1056
1063
  return;
1057
1064
  }
1058
- const queue = this.calculators.get(scope);
1059
- if (Array.isArray(queue)) {
1060
- queue.push(calculator);
1065
+ const calculators = this.calculators.get(scope);
1066
+ if (Array.isArray(calculators)) {
1067
+ calculators.push(calculator);
1061
1068
  } else {
1062
1069
  this.calculators.set(scope, [calculator]);
1063
1070
  }
1064
- this.referenceCount.set(calculator.name, 1);
1071
+ this.referenceCount.set(ref_count_name, 1);
1065
1072
  }
1066
1073
  unregister(scope, calculator) {
1067
- const count = this.referenceCount.get(calculator.name);
1074
+ const ref_count_name = `${scope}_${calculator.name}`;
1075
+ const count = this.referenceCount.get(ref_count_name);
1068
1076
  if (typeof count !== "undefined" && count > 1) {
1069
- this.referenceCount.set(calculator.name, count - 1);
1077
+ this.referenceCount.set(ref_count_name, count - 1);
1070
1078
  return;
1071
1079
  }
1072
- const queue = this.calculators.get(scope);
1073
- if (Array.isArray(queue)) {
1074
- const index = queue.indexOf(calculator);
1080
+ const calculators = this.calculators.get(scope);
1081
+ if (Array.isArray(calculators)) {
1082
+ const index = calculators.findIndex((c) => c.name === calculator.name);
1075
1083
  if (index > -1) {
1076
- queue.splice(index, 1);
1084
+ calculators[index].destroy?.();
1085
+ calculators.splice(index, 1);
1077
1086
  }
1078
1087
  }
1088
+ this.referenceCount.delete(ref_count_name);
1079
1089
  }
1080
1090
  async calc(scope, data, options) {
1081
1091
  if (scope !== "position" /* POSITION */) {
@@ -1242,6 +1252,11 @@ var usePositionStore = create()(
1242
1252
  state.positions[key] = positions2;
1243
1253
  });
1244
1254
  },
1255
+ closePosition: (symbol) => {
1256
+ set((state) => {
1257
+ delete state.positions[symbol];
1258
+ });
1259
+ },
1245
1260
  clearAll: () => {
1246
1261
  set((state) => {
1247
1262
  state.positions = {
@@ -1479,7 +1494,7 @@ var PositionCalculator = class extends BaseCalculator {
1479
1494
  }
1480
1495
  preprocess(data) {
1481
1496
  let rows = data.rows;
1482
- if (this.symbol !== AllPositions) {
1497
+ if (this.symbol !== AllPositions && Array.isArray(rows)) {
1483
1498
  rows = rows.filter((item) => item.symbol === this.symbol);
1484
1499
  }
1485
1500
  return {
@@ -1489,7 +1504,20 @@ var PositionCalculator = class extends BaseCalculator {
1489
1504
  }
1490
1505
  getPosition(_, ctx) {
1491
1506
  let positions2 = ctx.get((output) => output[this.name]) || usePositionStore.getState().positions[this.symbol];
1492
- return positions2;
1507
+ if (this.symbol === AllPositions) {
1508
+ return positions2;
1509
+ }
1510
+ if (positions2 && Array.isArray(positions2.rows)) {
1511
+ return positions2;
1512
+ }
1513
+ return this.preprocess(this.getAllPositions(ctx));
1514
+ }
1515
+ destroy() {
1516
+ usePositionStore.getState().actions.closePosition(this.symbol);
1517
+ CalculatorContext.instance?.deleteByName(this.name);
1518
+ }
1519
+ getAllPositions(ctx) {
1520
+ return ctx.get((output) => output[AllPositions]) || usePositionStore.getState().positions[AllPositions];
1493
1521
  }
1494
1522
  };
1495
1523
  PositionCalculator.logPosition = (symbol = "all") => {
@@ -1557,15 +1585,13 @@ var PortfolioCalculator = class extends BaseCalculator {
1557
1585
  if (scope === "markPrice" /* MARK_PRICE */) {
1558
1586
  markPrices = data;
1559
1587
  } else {
1560
- markPrices = ctx.get((cache) => cache[this.name]);
1561
- }
1562
- if (scope === "position" /* POSITION */) {
1563
- positions2 = data;
1564
- } else {
1565
- positions2 = ctx.get(
1566
- (output) => output.positionCalculator_all
1588
+ markPrices = ctx.get(
1589
+ (cache) => cache[MarketCalculatorName]
1567
1590
  );
1568
1591
  }
1592
+ positions2 = ctx.get(
1593
+ (output) => output.positionCalculator_all
1594
+ );
1569
1595
  let holding = portfolio.holding;
1570
1596
  const accountInfo = ctx.accountInfo;
1571
1597
  const symbolsInfo = ctx.symbolsInfo;
@@ -1624,7 +1650,8 @@ var PortfolioCalculator = class extends BaseCalculator {
1624
1650
  totalUnrealizedROI,
1625
1651
  freeCollateral,
1626
1652
  availableBalance,
1627
- unsettledPnL
1653
+ unsettledPnL,
1654
+ holding
1628
1655
  };
1629
1656
  }
1630
1657
  update(data, scope) {
@@ -1635,7 +1662,8 @@ var PortfolioCalculator = class extends BaseCalculator {
1635
1662
  freeCollateral: data.freeCollateral,
1636
1663
  availableBalance: data.availableBalance,
1637
1664
  totalUnrealizedROI: data.totalUnrealizedROI,
1638
- unsettledPnL: data.unsettledPnL
1665
+ unsettledPnL: data.unsettledPnL,
1666
+ holding: Array.isArray(data.holding) ? data.holding : []
1639
1667
  });
1640
1668
  }
1641
1669
  }
@@ -2903,7 +2931,7 @@ var useFundingRate = (symbol) => {
2903
2931
  if (getTimestamp() > next_funding_time) {
2904
2932
  return null;
2905
2933
  }
2906
- return (Number(est_funding_rate2) * 100).toFixed(4);
2934
+ return new Decimal(Number(est_funding_rate2) * 100).toFixed(4, Decimal.ROUND_DOWN);
2907
2935
  }, [data]);
2908
2936
  return {
2909
2937
  ...data,
@@ -2971,11 +2999,27 @@ var usePositionStream = (symbol = "all", options) => {
2971
2999
  "position" /* POSITION */,
2972
3000
  positionCalculator.current
2973
3001
  );
3002
+ calculatorService.register(
3003
+ "markPrice" /* MARK_PRICE */,
3004
+ positionCalculator.current
3005
+ );
3006
+ calculatorService.register(
3007
+ "indexPrice" /* INDEX_PRICE */,
3008
+ positionCalculator.current
3009
+ );
2974
3010
  return () => {
2975
3011
  calculatorService.unregister(
2976
3012
  "position" /* POSITION */,
2977
3013
  positionCalculator.current
2978
3014
  );
3015
+ calculatorService.unregister(
3016
+ "markPrice" /* MARK_PRICE */,
3017
+ positionCalculator.current
3018
+ );
3019
+ calculatorService.unregister(
3020
+ "indexPrice" /* INDEX_PRICE */,
3021
+ positionCalculator.current
3022
+ );
2979
3023
  };
2980
3024
  }, [symbol]);
2981
3025
  const formattedPositions = usePositionStore((state) => {
@@ -4047,6 +4091,16 @@ var useDeposit = (options) => {
4047
4091
  [account5, getAllowance, options?.address, dst]
4048
4092
  );
4049
4093
  const deposit = useCallback(async () => {
4094
+ if (!options?.address) {
4095
+ throw new Error("address is required");
4096
+ }
4097
+ const _allowance = await account5.assetsManager.getAllowance({
4098
+ address: options?.address
4099
+ });
4100
+ setAllowance(() => _allowance);
4101
+ if (new Decimal(quantity).greaterThan(_allowance)) {
4102
+ throw new Error("Insufficient allowance");
4103
+ }
4050
4104
  return account5.assetsManager.deposit(quantity, depositFee).then((res) => {
4051
4105
  updateAllowanceWhenTxSuccess(res.hash);
4052
4106
  setBalance((value) => new Decimal(value).sub(quantity).toString());
@@ -5856,6 +5910,7 @@ var usePrivateDataObserver = (options) => {
5856
5910
  onError: (error) => {
5857
5911
  statusActions.updateApiError("positions", error.message);
5858
5912
  }
5913
+ // revalidateOnFocus: false,
5859
5914
  });
5860
5915
  useEffect(() => {
5861
5916
  const handler = (state2) => {
@@ -5884,6 +5939,7 @@ var usePrivateDataObserver = (options) => {
5884
5939
  "/v1/client/holding",
5885
5940
  {
5886
5941
  formatter: (data) => data.holding
5942
+ // revalidateOnFocus: false,
5887
5943
  }
5888
5944
  );
5889
5945
  useEffect(() => {
@@ -13669,7 +13725,7 @@ var useOrderEntry2 = (symbol, options) => {
13669
13725
  throw new Error("symbol is required and must be a string");
13670
13726
  }
13671
13727
  const ee = useEventEmitter();
13672
- const fieldDirty = useRef({});
13728
+ useRef({});
13673
13729
  const [meta, setMeta] = useState({
13674
13730
  dirty: {},
13675
13731
  submitted: false,
@@ -13753,11 +13809,17 @@ var useOrderEntry2 = (symbol, options) => {
13753
13809
  return true;
13754
13810
  };
13755
13811
  const setValue = (key, value, options2) => {
13756
- const { shouldUpdateLastChangedField = true } = options2 || {};
13812
+ const { shouldUpdateLastChangedField = true, shouldUpdateDirty = true } = options2 || {};
13757
13813
  if (!canSetTPSLPrice(key, formattedOrder.order_type)) {
13758
13814
  return;
13759
13815
  }
13760
- fieldDirty.current[key] = true;
13816
+ if (shouldUpdateDirty) {
13817
+ setMeta(
13818
+ produce((draft) => {
13819
+ draft.dirty[key] = true;
13820
+ })
13821
+ );
13822
+ }
13761
13823
  const values2 = setValueInternal(key, value, prepareData());
13762
13824
  if (values2) {
13763
13825
  interactiveValidate(values2);
@@ -13845,9 +13907,10 @@ var useOrderEntry2 = (symbol, options) => {
13845
13907
  })
13846
13908
  );
13847
13909
  };
13848
- const submitOrder = async () => {
13910
+ const submitOrder = async (options2) => {
13849
13911
  const creator = getOrderCreator(formattedOrder);
13850
13912
  const errors = await validate(formattedOrder, creator, prepareData());
13913
+ const { resetOnSuccess = true } = options2 || {};
13851
13914
  setMeta(
13852
13915
  produce((draft) => {
13853
13916
  draft.submitted = true;
@@ -13864,10 +13927,11 @@ var useOrderEntry2 = (symbol, options) => {
13864
13927
  }
13865
13928
  const order = generateOrder(creator, prepareData());
13866
13929
  const result = await doCreateOrder(order);
13867
- if (result.success) {
13930
+ if (result.success && resetOnSuccess) {
13868
13931
  reset();
13869
13932
  resetMetaState();
13870
13933
  }
13934
+ return result;
13871
13935
  };
13872
13936
  return {
13873
13937
  ...orderEntryActions,