@pear-protocol/hyperliquid-sdk 0.1.13 → 0.1.15

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.ts CHANGED
@@ -228,6 +228,7 @@ interface TwapMonitoringDto {
228
228
  interface TradeHistoryAssetDataDto {
229
229
  coin: string;
230
230
  entryWeight: number;
231
+ closeWeight: number;
231
232
  entryPrice: number;
232
233
  limitPrice: number;
233
234
  leverage: number;
@@ -283,6 +284,7 @@ interface PositionAssetDetailDto {
283
284
  unrealizedPnl: number;
284
285
  liquidationPrice: number;
285
286
  initialWeight: number;
287
+ currentWeight: number;
286
288
  fundingPaid?: number;
287
289
  metadata?: TokenMetadata | null;
288
290
  }
package/dist/index.js CHANGED
@@ -1566,17 +1566,33 @@ const useTradeHistories = () => {
1566
1566
  const enrichedTradeHistories = useMemo(() => {
1567
1567
  if (!tradeHistories)
1568
1568
  return null;
1569
- return tradeHistories.map((history) => ({
1570
- ...history,
1571
- closedLongAssets: history.closedLongAssets.map((asset) => ({
1572
- ...asset,
1573
- metadata: getAssetByName(asset.coin),
1574
- })),
1575
- closedShortAssets: history.closedShortAssets.map((asset) => ({
1576
- ...asset,
1577
- metadata: getAssetByName(asset.coin),
1578
- })),
1579
- }));
1569
+ return tradeHistories.map((history) => {
1570
+ const totalClosePositionSize = history.closedLongAssets.reduce((acc, asset) => acc + asset.limitPrice * asset.size, 0) +
1571
+ history.closedShortAssets.reduce((acc, asset) => acc + asset.limitPrice * asset.size, 0);
1572
+ return {
1573
+ ...history,
1574
+ closedLongAssets: history.closedLongAssets.map((asset) => {
1575
+ const closeNotional = asset.limitPrice * asset.size;
1576
+ return {
1577
+ ...asset,
1578
+ closeWeight: totalClosePositionSize > 0
1579
+ ? closeNotional / totalClosePositionSize
1580
+ : 0,
1581
+ metadata: getAssetByName(asset.coin),
1582
+ };
1583
+ }),
1584
+ closedShortAssets: history.closedShortAssets.map((asset) => {
1585
+ const closeNotional = asset.limitPrice * asset.size;
1586
+ return {
1587
+ ...asset,
1588
+ closeWeight: totalClosePositionSize > 0
1589
+ ? closeNotional / totalClosePositionSize
1590
+ : 0,
1591
+ metadata: getAssetByName(asset.coin),
1592
+ };
1593
+ }),
1594
+ };
1595
+ });
1580
1596
  }, [tradeHistories, getAssetByName]);
1581
1597
  const isLoading = useMemo(() => {
1582
1598
  return tradeHistories === null && context.isConnected;
@@ -6964,7 +6980,7 @@ async function updateLeverage(baseUrl, positionId, payload) {
6964
6980
  }
6965
6981
  }
6966
6982
 
6967
- const calculatePositionAsset = (asset, currentPrice, totalInitialPositionSize, leverage, metadata, isLong = true) => {
6983
+ const calculatePositionAsset = (asset, currentPrice, totalInitialPositionSize, totalCurrentPositionSize, leverage, metadata, isLong = true) => {
6968
6984
  var _a;
6969
6985
  const entryNotional = asset.entryPrice * asset.size;
6970
6986
  const currentNotional = currentPrice * asset.size;
@@ -6985,6 +7001,7 @@ const calculatePositionAsset = (asset, currentPrice, totalInitialPositionSize, l
6985
7001
  unrealizedPnl: unrealizedPnl,
6986
7002
  entryPositionValue: entryNotional,
6987
7003
  initialWeight: totalInitialPositionSize > 0 ? entryNotional / totalInitialPositionSize : 0,
7004
+ currentWeight: totalCurrentPositionSize > 0 ? currentNotional / totalCurrentPositionSize : 0,
6988
7005
  fundingPaid: (_a = asset.fundingPaid) !== null && _a !== void 0 ? _a : 0,
6989
7006
  metadata,
6990
7007
  };
@@ -7009,13 +7026,23 @@ const buildPositionValue = (rawPositions, clearinghouseState, getAssetByName) =>
7009
7026
  let entryMarginUsed = 0;
7010
7027
  const totalInitialPositionSize = position.longAssets.reduce((acc, asset) => acc + asset.entryPrice * asset.size, 0) +
7011
7028
  position.shortAssets.reduce((acc, asset) => acc + asset.entryPrice * asset.size, 0);
7029
+ const totalCurrentPositionSize = position.longAssets.reduce((acc, asset) => {
7030
+ var _a, _b;
7031
+ const currentPrice = (_b = (_a = getAssetByName(asset.coin)) === null || _a === void 0 ? void 0 : _a.currentPrice) !== null && _b !== void 0 ? _b : 0;
7032
+ return acc + currentPrice * asset.size;
7033
+ }, 0) +
7034
+ position.shortAssets.reduce((acc, asset) => {
7035
+ var _a, _b;
7036
+ const currentPrice = (_b = (_a = getAssetByName(asset.coin)) === null || _a === void 0 ? void 0 : _a.currentPrice) !== null && _b !== void 0 ? _b : 0;
7037
+ return acc + currentPrice * asset.size;
7038
+ }, 0);
7012
7039
  mappedPosition.longAssets = position.longAssets.map((longAsset) => {
7013
7040
  var _a, _b, _c, _d;
7014
7041
  const metadata = getAssetByName(longAsset.coin);
7015
7042
  const currentPrice = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.currentPrice) !== null && _a !== void 0 ? _a : 0;
7016
7043
  const assetState = (_b = clearinghouseState.assetPositions.find((ap) => ap.position.coin === longAsset.coin)) === null || _b === void 0 ? void 0 : _b.position;
7017
7044
  const leverage = (_d = (_c = assetState === null || assetState === void 0 ? void 0 : assetState.leverage) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : longAsset.leverage;
7018
- const mappedPositionAssets = calculatePositionAsset(longAsset, currentPrice, totalInitialPositionSize, leverage, metadata, true);
7045
+ const mappedPositionAssets = calculatePositionAsset(longAsset, currentPrice, totalInitialPositionSize, totalCurrentPositionSize, leverage, metadata, true);
7019
7046
  mappedPosition.entryPositionValue +=
7020
7047
  mappedPositionAssets.entryPositionValue;
7021
7048
  mappedPosition.unrealizedPnl += mappedPositionAssets.unrealizedPnl;
@@ -7023,7 +7050,7 @@ const buildPositionValue = (rawPositions, clearinghouseState, getAssetByName) =>
7023
7050
  mappedPosition.marginUsed += mappedPositionAssets.marginUsed;
7024
7051
  entryMarginUsed += mappedPositionAssets.entryMarginUsed;
7025
7052
  mappedPosition.entryRatio *= Math.pow(longAsset.entryPrice, mappedPositionAssets.initialWeight);
7026
- mappedPosition.markRatio *= Math.pow(currentPrice, mappedPositionAssets.initialWeight);
7053
+ mappedPosition.markRatio *= Math.pow(currentPrice, mappedPositionAssets.currentWeight);
7027
7054
  return mappedPositionAssets;
7028
7055
  });
7029
7056
  mappedPosition.shortAssets = position.shortAssets.map((shortAsset) => {
@@ -7032,7 +7059,7 @@ const buildPositionValue = (rawPositions, clearinghouseState, getAssetByName) =>
7032
7059
  const currentPrice = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.currentPrice) !== null && _a !== void 0 ? _a : 0;
7033
7060
  const assetState = (_b = clearinghouseState.assetPositions.find((ap) => ap.position.coin === shortAsset.coin)) === null || _b === void 0 ? void 0 : _b.position;
7034
7061
  const leverage = (_d = (_c = assetState === null || assetState === void 0 ? void 0 : assetState.leverage) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : shortAsset.leverage;
7035
- const mappedPositionAssets = calculatePositionAsset(shortAsset, currentPrice, totalInitialPositionSize, leverage, metadata, false);
7062
+ const mappedPositionAssets = calculatePositionAsset(shortAsset, currentPrice, totalInitialPositionSize, totalCurrentPositionSize, leverage, metadata, false);
7036
7063
  mappedPosition.entryPositionValue +=
7037
7064
  mappedPositionAssets.entryPositionValue;
7038
7065
  mappedPosition.unrealizedPnl += mappedPositionAssets.unrealizedPnl;
@@ -7040,7 +7067,7 @@ const buildPositionValue = (rawPositions, clearinghouseState, getAssetByName) =>
7040
7067
  mappedPosition.marginUsed += mappedPositionAssets.marginUsed;
7041
7068
  entryMarginUsed += mappedPositionAssets.entryMarginUsed;
7042
7069
  mappedPosition.entryRatio *= Math.pow(shortAsset.entryPrice, -mappedPositionAssets.initialWeight);
7043
- mappedPosition.markRatio *= Math.pow(currentPrice, -mappedPositionAssets.initialWeight);
7070
+ mappedPosition.markRatio *= Math.pow(currentPrice, -mappedPositionAssets.currentWeight);
7044
7071
  return mappedPositionAssets;
7045
7072
  });
7046
7073
  mappedPosition.positionValue =
package/dist/types.d.ts CHANGED
@@ -200,6 +200,7 @@ export interface TwapMonitoringDto {
200
200
  export interface TradeHistoryAssetDataDto {
201
201
  coin: string;
202
202
  entryWeight: number;
203
+ closeWeight: number;
203
204
  entryPrice: number;
204
205
  limitPrice: number;
205
206
  leverage: number;
@@ -255,6 +256,7 @@ export interface PositionAssetDetailDto {
255
256
  unrealizedPnl: number;
256
257
  liquidationPrice: number;
257
258
  initialWeight: number;
259
+ currentWeight: number;
258
260
  fundingPaid?: number;
259
261
  metadata?: TokenMetadata | null;
260
262
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",