@orderly.network/hooks 2.0.1-alpha.0 → 2.0.1-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -27,7 +27,7 @@ declare global {
27
27
  };
28
28
  }
29
29
  }
30
- declare const _default: "2.0.1-alpha.0";
30
+ declare const _default: "2.0.1-alpha.2";
31
31
 
32
32
  type useQueryOptions<T> = SWRConfiguration & {
33
33
  formatter?: (data: any) => T;
@@ -836,6 +836,7 @@ declare const useMarkPriceBySymbol: (symbol: string) => number;
836
836
  type PositionActions = {
837
837
  setPositions: (key: string, positions: API.PositionsTPSLExt) => void;
838
838
  clearAll: () => void;
839
+ closePosition: (symbol: string) => void;
839
840
  };
840
841
  declare const usePositionActions: () => PositionActions;
841
842
 
package/dist/index.d.ts CHANGED
@@ -27,7 +27,7 @@ declare global {
27
27
  };
28
28
  }
29
29
  }
30
- declare const _default: "2.0.1-alpha.0";
30
+ declare const _default: "2.0.1-alpha.2";
31
31
 
32
32
  type useQueryOptions<T> = SWRConfiguration & {
33
33
  formatter?: (data: any) => T;
@@ -836,6 +836,7 @@ declare const useMarkPriceBySymbol: (symbol: string) => number;
836
836
  type PositionActions = {
837
837
  setPositions: (key: string, positions: API.PositionsTPSLExt) => void;
838
838
  clearAll: () => void;
839
+ closePosition: (symbol: string) => void;
839
840
  };
840
841
  declare const usePositionActions: () => PositionActions;
841
842
 
package/dist/index.js CHANGED
@@ -40,9 +40,9 @@ var __export = (target, all) => {
40
40
  // src/version.ts
41
41
  if (typeof window !== "undefined") {
42
42
  window.__ORDERLY_VERSION__ = window.__ORDERLY_VERSION__ || {};
43
- window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.0.1-alpha.0";
43
+ window.__ORDERLY_VERSION__["@orderly.network/hooks"] = "2.0.1-alpha.2";
44
44
  }
45
- var version_default = "2.0.1-alpha.0";
45
+ var version_default = "2.0.1-alpha.2";
46
46
  var fetcher = (url, init = {}, queryOptions) => net.get(url, init, queryOptions?.formatter);
47
47
  var OrderlyContext = React.createContext({
48
48
  // configStore: new MemoryConfigStore(),
@@ -995,6 +995,9 @@ var MarkPriceCalculator = class extends BaseCalculator {
995
995
 
996
996
  // src/orderly/calculator/calculatorContext.ts
997
997
  var CalculatorContext = class _CalculatorContext {
998
+ static get instance() {
999
+ return this._instance;
1000
+ }
998
1001
  static create(scope, data) {
999
1002
  if (!this._instance) {
1000
1003
  this._instance = new _CalculatorContext(scope, data);
@@ -1029,6 +1032,9 @@ var CalculatorContext = class _CalculatorContext {
1029
1032
  this.accountInfo = void 0;
1030
1033
  this.portfolio = void 0;
1031
1034
  }
1035
+ deleteByName(name) {
1036
+ delete this.output[name];
1037
+ }
1032
1038
  // get positions(): API.PositionTPSLExt[] {
1033
1039
  // if (this.output.positionCalculator) return this.output.positionCalculator;
1034
1040
  // return usePositionStore.getState().positions;
@@ -1058,32 +1064,36 @@ var CalculatorService = class {
1058
1064
  this.calculators = new Map(calculators);
1059
1065
  }
1060
1066
  register(scope, calculator) {
1061
- const count = this.referenceCount.get(calculator.name);
1067
+ const ref_count_name = `${scope}_${calculator.name}`;
1068
+ const count = this.referenceCount.get(ref_count_name);
1062
1069
  if (typeof count !== "undefined" && count > 0) {
1063
- this.referenceCount.set(calculator.name, count + 1);
1070
+ this.referenceCount.set(ref_count_name, count + 1);
1064
1071
  return;
1065
1072
  }
1066
- const queue = this.calculators.get(scope);
1067
- if (Array.isArray(queue)) {
1068
- queue.push(calculator);
1073
+ const calculators = this.calculators.get(scope);
1074
+ if (Array.isArray(calculators)) {
1075
+ calculators.push(calculator);
1069
1076
  } else {
1070
1077
  this.calculators.set(scope, [calculator]);
1071
1078
  }
1072
- this.referenceCount.set(calculator.name, 1);
1079
+ this.referenceCount.set(ref_count_name, 1);
1073
1080
  }
1074
1081
  unregister(scope, calculator) {
1075
- const count = this.referenceCount.get(calculator.name);
1082
+ const ref_count_name = `${scope}_${calculator.name}`;
1083
+ const count = this.referenceCount.get(ref_count_name);
1076
1084
  if (typeof count !== "undefined" && count > 1) {
1077
- this.referenceCount.set(calculator.name, count - 1);
1085
+ this.referenceCount.set(ref_count_name, count - 1);
1078
1086
  return;
1079
1087
  }
1080
- const queue = this.calculators.get(scope);
1081
- if (Array.isArray(queue)) {
1082
- const index = queue.indexOf(calculator);
1088
+ const calculators = this.calculators.get(scope);
1089
+ if (Array.isArray(calculators)) {
1090
+ const index = calculators.findIndex((c) => c.name === calculator.name);
1083
1091
  if (index > -1) {
1084
- queue.splice(index, 1);
1092
+ calculators[index].destroy?.();
1093
+ calculators.splice(index, 1);
1085
1094
  }
1086
1095
  }
1096
+ this.referenceCount.delete(ref_count_name);
1087
1097
  }
1088
1098
  async calc(scope, data, options) {
1089
1099
  if (scope !== "position" /* POSITION */) {
@@ -1250,6 +1260,11 @@ var usePositionStore = zustand.create()(
1250
1260
  state.positions[key] = positions2;
1251
1261
  });
1252
1262
  },
1263
+ closePosition: (symbol) => {
1264
+ set((state) => {
1265
+ delete state.positions[symbol];
1266
+ });
1267
+ },
1253
1268
  clearAll: () => {
1254
1269
  set((state) => {
1255
1270
  state.positions = {
@@ -1487,7 +1502,7 @@ var PositionCalculator = class extends BaseCalculator {
1487
1502
  }
1488
1503
  preprocess(data) {
1489
1504
  let rows = data.rows;
1490
- if (this.symbol !== AllPositions) {
1505
+ if (this.symbol !== AllPositions && Array.isArray(rows)) {
1491
1506
  rows = rows.filter((item) => item.symbol === this.symbol);
1492
1507
  }
1493
1508
  return {
@@ -1497,7 +1512,20 @@ var PositionCalculator = class extends BaseCalculator {
1497
1512
  }
1498
1513
  getPosition(_, ctx) {
1499
1514
  let positions2 = ctx.get((output) => output[this.name]) || usePositionStore.getState().positions[this.symbol];
1500
- return positions2;
1515
+ if (this.symbol === AllPositions) {
1516
+ return positions2;
1517
+ }
1518
+ if (positions2 && Array.isArray(positions2.rows)) {
1519
+ return positions2;
1520
+ }
1521
+ return this.preprocess(this.getAllPositions(ctx));
1522
+ }
1523
+ destroy() {
1524
+ usePositionStore.getState().actions.closePosition(this.symbol);
1525
+ CalculatorContext.instance?.deleteByName(this.name);
1526
+ }
1527
+ getAllPositions(ctx) {
1528
+ return ctx.get((output) => output[AllPositions]) || usePositionStore.getState().positions[AllPositions];
1501
1529
  }
1502
1530
  };
1503
1531
  PositionCalculator.logPosition = (symbol = "all") => {
@@ -1565,15 +1593,13 @@ var PortfolioCalculator = class extends BaseCalculator {
1565
1593
  if (scope === "markPrice" /* MARK_PRICE */) {
1566
1594
  markPrices = data;
1567
1595
  } else {
1568
- markPrices = ctx.get((cache) => cache[this.name]);
1569
- }
1570
- if (scope === "position" /* POSITION */) {
1571
- positions2 = data;
1572
- } else {
1573
- positions2 = ctx.get(
1574
- (output) => output.positionCalculator_all
1596
+ markPrices = ctx.get(
1597
+ (cache) => cache[MarketCalculatorName]
1575
1598
  );
1576
1599
  }
1600
+ positions2 = ctx.get(
1601
+ (output) => output.positionCalculator_all
1602
+ );
1577
1603
  let holding = portfolio.holding;
1578
1604
  const accountInfo = ctx.accountInfo;
1579
1605
  const symbolsInfo = ctx.symbolsInfo;
@@ -1632,7 +1658,8 @@ var PortfolioCalculator = class extends BaseCalculator {
1632
1658
  totalUnrealizedROI,
1633
1659
  freeCollateral,
1634
1660
  availableBalance,
1635
- unsettledPnL
1661
+ unsettledPnL,
1662
+ holding
1636
1663
  };
1637
1664
  }
1638
1665
  update(data, scope) {
@@ -1643,7 +1670,8 @@ var PortfolioCalculator = class extends BaseCalculator {
1643
1670
  freeCollateral: data.freeCollateral,
1644
1671
  availableBalance: data.availableBalance,
1645
1672
  totalUnrealizedROI: data.totalUnrealizedROI,
1646
- unsettledPnL: data.unsettledPnL
1673
+ unsettledPnL: data.unsettledPnL,
1674
+ holding: Array.isArray(data.holding) ? data.holding : []
1647
1675
  });
1648
1676
  }
1649
1677
  }
@@ -2911,7 +2939,7 @@ var useFundingRate = (symbol) => {
2911
2939
  if (utils.getTimestamp() > next_funding_time) {
2912
2940
  return null;
2913
2941
  }
2914
- return (Number(est_funding_rate2) * 100).toFixed(4);
2942
+ return new utils.Decimal(Number(est_funding_rate2) * 100).toFixed(4, utils.Decimal.ROUND_DOWN);
2915
2943
  }, [data]);
2916
2944
  return {
2917
2945
  ...data,
@@ -2979,11 +3007,27 @@ var usePositionStream = (symbol = "all", options) => {
2979
3007
  "position" /* POSITION */,
2980
3008
  positionCalculator.current
2981
3009
  );
3010
+ calculatorService.register(
3011
+ "markPrice" /* MARK_PRICE */,
3012
+ positionCalculator.current
3013
+ );
3014
+ calculatorService.register(
3015
+ "indexPrice" /* INDEX_PRICE */,
3016
+ positionCalculator.current
3017
+ );
2982
3018
  return () => {
2983
3019
  calculatorService.unregister(
2984
3020
  "position" /* POSITION */,
2985
3021
  positionCalculator.current
2986
3022
  );
3023
+ calculatorService.unregister(
3024
+ "markPrice" /* MARK_PRICE */,
3025
+ positionCalculator.current
3026
+ );
3027
+ calculatorService.unregister(
3028
+ "indexPrice" /* INDEX_PRICE */,
3029
+ positionCalculator.current
3030
+ );
2987
3031
  };
2988
3032
  }, [symbol]);
2989
3033
  const formattedPositions = usePositionStore((state) => {
@@ -4055,6 +4099,16 @@ var useDeposit = (options) => {
4055
4099
  [account5, getAllowance, options?.address, dst]
4056
4100
  );
4057
4101
  const deposit = React.useCallback(async () => {
4102
+ if (!options?.address) {
4103
+ throw new Error("address is required");
4104
+ }
4105
+ const _allowance = await account5.assetsManager.getAllowance({
4106
+ address: options?.address
4107
+ });
4108
+ setAllowance(() => _allowance);
4109
+ if (new utils.Decimal(quantity).greaterThan(_allowance)) {
4110
+ throw new Error("Insufficient allowance");
4111
+ }
4058
4112
  return account5.assetsManager.deposit(quantity, depositFee).then((res) => {
4059
4113
  updateAllowanceWhenTxSuccess(res.hash);
4060
4114
  setBalance((value) => new utils.Decimal(value).sub(quantity).toString());
@@ -5864,6 +5918,7 @@ var usePrivateDataObserver = (options) => {
5864
5918
  onError: (error) => {
5865
5919
  statusActions.updateApiError("positions", error.message);
5866
5920
  }
5921
+ // revalidateOnFocus: false,
5867
5922
  });
5868
5923
  React.useEffect(() => {
5869
5924
  const handler = (state2) => {
@@ -5892,6 +5947,7 @@ var usePrivateDataObserver = (options) => {
5892
5947
  "/v1/client/holding",
5893
5948
  {
5894
5949
  formatter: (data) => data.holding
5950
+ // revalidateOnFocus: false,
5895
5951
  }
5896
5952
  );
5897
5953
  React.useEffect(() => {
@@ -13853,9 +13909,10 @@ var useOrderEntry2 = (symbol, options) => {
13853
13909
  })
13854
13910
  );
13855
13911
  };
13856
- const submitOrder = async () => {
13912
+ const submitOrder = async (options2) => {
13857
13913
  const creator = getOrderCreator(formattedOrder);
13858
13914
  const errors = await validate(formattedOrder, creator, prepareData());
13915
+ const { resetOnSuccess = true } = options2 || {};
13859
13916
  setMeta(
13860
13917
  immer$1.produce((draft) => {
13861
13918
  draft.submitted = true;
@@ -13872,10 +13929,11 @@ var useOrderEntry2 = (symbol, options) => {
13872
13929
  }
13873
13930
  const order = generateOrder(creator, prepareData());
13874
13931
  const result = await doCreateOrder(order);
13875
- if (result.success) {
13932
+ if (result.success && resetOnSuccess) {
13876
13933
  reset();
13877
13934
  resetMetaState();
13878
13935
  }
13936
+ return result;
13879
13937
  };
13880
13938
  return {
13881
13939
  ...orderEntryActions,