@pear-protocol/hyperliquid-sdk 0.0.37 → 0.0.38

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
@@ -150,7 +150,7 @@ interface PositionAssetDetailDto {
150
150
  entryPrice: number;
151
151
  platformSize: number;
152
152
  actualSize: number;
153
- cumFunding: CumFundingDto;
153
+ leverage: number;
154
154
  marginUsed: number;
155
155
  entryPositionValue: number;
156
156
  positionValue: number;
@@ -168,12 +168,10 @@ interface TpSlThreshold {
168
168
  interface OpenPositionDto {
169
169
  positionId: string;
170
170
  address: string;
171
- leverage: number;
172
171
  stopLoss: TpSlThreshold | null;
173
172
  takeProfit: TpSlThreshold | null;
174
173
  entryRatio: number;
175
174
  markRatio: number;
176
- netFunding: number;
177
175
  entryPositionValue: number;
178
176
  positionValue: number;
179
177
  marginUsed: number;
package/dist/index.js CHANGED
@@ -8348,74 +8348,62 @@ async function cancelTwap(baseUrl, accessToken, orderId) {
8348
8348
  }
8349
8349
  }
8350
8350
 
8351
- const calculatePositionAsset = (asset, currentPrice, totalInitialPositionSize, leverage, cumFunding, isLong = true) => {
8352
- const initialPositionValue = asset.entryPrice * asset.size;
8353
- const positionValue = currentPrice * asset.size;
8354
- const marginUsed = positionValue / leverage;
8351
+ const calculatePositionAsset = (asset, currentPrice, totalInitialPositionSize, leverage, isLong = true) => {
8352
+ const entryNotional = asset.entryPrice * asset.size;
8353
+ const currentNotional = currentPrice * asset.size;
8354
+ const marginUsed = currentNotional / (leverage || 1);
8355
8355
  const unrealizedPnl = isLong
8356
- ? positionValue - initialPositionValue
8357
- : initialPositionValue - positionValue;
8356
+ ? currentNotional - entryNotional
8357
+ : entryNotional - currentNotional;
8358
8358
  return {
8359
8359
  coin: asset.coin,
8360
8360
  entryPrice: asset.entryPrice,
8361
8361
  actualSize: asset.size,
8362
+ leverage: leverage,
8362
8363
  marginUsed: marginUsed,
8363
- positionValue: positionValue,
8364
+ positionValue: currentNotional,
8364
8365
  unrealizedPnl: unrealizedPnl,
8365
- entryPositionValue: initialPositionValue,
8366
- initialWeight: initialPositionValue / totalInitialPositionSize,
8367
- cumFunding: {
8368
- allTime: parseFloat((cumFunding === null || cumFunding === void 0 ? void 0 : cumFunding.allTime) || "0"),
8369
- sinceChange: parseFloat((cumFunding === null || cumFunding === void 0 ? void 0 : cumFunding.sinceChange) || "0"),
8370
- sinceOpen: parseFloat((cumFunding === null || cumFunding === void 0 ? void 0 : cumFunding.sinceOpen) || "0"),
8371
- }
8366
+ entryPositionValue: entryNotional,
8367
+ initialWeight: totalInitialPositionSize > 0 ? entryNotional / totalInitialPositionSize : 0,
8372
8368
  };
8373
8369
  };
8374
8370
  const buildPositionValue = (rawPositions, webData2, allMids) => {
8375
8371
  return rawPositions.map((position) => {
8376
- const webData2Position = webData2.clearinghouseState.assetPositions.find(ap => {
8377
- var _a, _b, _c, _d;
8378
- const longCoin = (_b = (_a = position.longAssets) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.coin;
8379
- const shortCoin = (_d = (_c = position.shortAssets) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.coin;
8380
- return ap.position.coin === longCoin || ap.position.coin === shortCoin;
8381
- });
8382
8372
  let mappedPosition = {
8383
8373
  positionId: position.positionId,
8384
8374
  address: position.address,
8385
- leverage: webData2Position === null || webData2Position === void 0 ? void 0 : webData2Position.position.leverage.value, // TODO: consider if user manually change leverage from the HL UI
8386
8375
  entryRatio: 1,
8387
8376
  marginUsed: 0,
8388
8377
  markRatio: 1,
8389
8378
  unrealizedPnl: 0,
8390
- netFunding: 0,
8391
8379
  positionValue: 0,
8380
+ entryPositionValue: 0,
8392
8381
  takeProfit: position.takeProfit,
8393
8382
  stopLoss: position.stopLoss,
8394
8383
  };
8395
8384
  const totalInitialPositionSize = position.longAssets.reduce((acc, asset) => acc + (asset.entryPrice * asset.size), 0) +
8396
8385
  position.shortAssets.reduce((acc, asset) => acc + (asset.entryPrice * asset.size), 0);
8397
8386
  mappedPosition.longAssets = position.longAssets.map(longAsset => {
8398
- var _a;
8387
+ var _a, _b, _c;
8399
8388
  const currentPrice = parseFloat(allMids.mids[longAsset.coin]);
8400
- const cumFunding = (_a = webData2.clearinghouseState.assetPositions.find(ap => ap.position.coin === longAsset.coin)) === null || _a === void 0 ? void 0 : _a.position.cumFunding;
8401
- const mappedPositionAssets = calculatePositionAsset(longAsset, currentPrice, totalInitialPositionSize, mappedPosition.leverage, cumFunding, true);
8389
+ const assetState = (_a = webData2.clearinghouseState.assetPositions.find(ap => ap.position.coin === longAsset.coin)) === null || _a === void 0 ? void 0 : _a.position;
8390
+ const leverage = (_c = (_b = assetState === null || assetState === void 0 ? void 0 : assetState.leverage) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : 0;
8391
+ const mappedPositionAssets = calculatePositionAsset(longAsset, currentPrice, totalInitialPositionSize, leverage, true);
8402
8392
  mappedPosition.entryPositionValue += mappedPositionAssets.entryPositionValue;
8403
8393
  mappedPosition.unrealizedPnl += mappedPositionAssets.unrealizedPnl;
8404
8394
  mappedPosition.positionValue += mappedPositionAssets.positionValue;
8405
8395
  mappedPosition.marginUsed += mappedPositionAssets.marginUsed;
8406
- mappedPosition.netFunding += mappedPositionAssets.cumFunding.sinceOpen;
8407
- // Calculate weighted entry and mark ratios for long positions
8408
8396
  mappedPosition.entryRatio *= Math.pow(longAsset.entryPrice, mappedPositionAssets.initialWeight);
8409
8397
  mappedPosition.markRatio *= Math.pow(currentPrice, mappedPositionAssets.initialWeight);
8410
8398
  return mappedPositionAssets;
8411
8399
  });
8412
8400
  mappedPosition.shortAssets = position.shortAssets.map(shortAsset => {
8413
- var _a;
8401
+ var _a, _b, _c;
8414
8402
  const currentPrice = parseFloat(allMids.mids[shortAsset.coin]);
8415
- const cumFunding = (_a = webData2.clearinghouseState.assetPositions.find(ap => ap.position.coin === shortAsset.coin)) === null || _a === void 0 ? void 0 : _a.position.cumFunding;
8416
- const mappedPositionAssets = calculatePositionAsset(shortAsset, currentPrice, totalInitialPositionSize, mappedPosition.leverage, cumFunding, false);
8403
+ const assetState = (_a = webData2.clearinghouseState.assetPositions.find(ap => ap.position.coin === shortAsset.coin)) === null || _a === void 0 ? void 0 : _a.position;
8404
+ const leverage = (_c = (_b = assetState === null || assetState === void 0 ? void 0 : assetState.leverage) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : 0;
8405
+ const mappedPositionAssets = calculatePositionAsset(shortAsset, currentPrice, totalInitialPositionSize, leverage, false);
8417
8406
  mappedPosition.entryPositionValue += mappedPositionAssets.entryPositionValue;
8418
- mappedPosition.netFunding += mappedPositionAssets.cumFunding.sinceOpen;
8419
8407
  mappedPosition.unrealizedPnl += mappedPositionAssets.unrealizedPnl;
8420
8408
  mappedPosition.positionValue += mappedPositionAssets.positionValue;
8421
8409
  mappedPosition.marginUsed += mappedPositionAssets.marginUsed;
@@ -8423,6 +8411,7 @@ const buildPositionValue = (rawPositions, webData2, allMids) => {
8423
8411
  mappedPosition.markRatio *= Math.pow(currentPrice, -mappedPositionAssets.initialWeight);
8424
8412
  return mappedPositionAssets;
8425
8413
  });
8414
+ mappedPosition.positionValue = mappedPosition.entryPositionValue + mappedPosition.unrealizedPnl;
8426
8415
  return mappedPosition;
8427
8416
  });
8428
8417
  };
@@ -8540,15 +8529,11 @@ function useOrders() {
8540
8529
  if (!hasAssets) {
8541
8530
  return {
8542
8531
  ...ord,
8543
- leverage: pos.leverage,
8544
8532
  longAssets: mapAssets(pos.longAssets),
8545
8533
  shortAssets: mapAssets(pos.shortAssets),
8546
8534
  };
8547
8535
  }
8548
- // Always sync leverage from position for TP/SL
8549
- if (ord.leverage !== pos.leverage) {
8550
- return { ...ord, leverage: pos.leverage };
8551
- }
8536
+ // Leverage is now tracked per-asset in positions; keep order leverage as-is
8552
8537
  return ord;
8553
8538
  });
8554
8539
  }, [openOrders, positionsById]);
package/dist/types.d.ts CHANGED
@@ -171,7 +171,7 @@ export interface PositionAssetDetailDto {
171
171
  entryPrice: number;
172
172
  platformSize: number;
173
173
  actualSize: number;
174
- cumFunding: CumFundingDto;
174
+ leverage: number;
175
175
  marginUsed: number;
176
176
  entryPositionValue: number;
177
177
  positionValue: number;
@@ -189,12 +189,10 @@ export interface TpSlThreshold {
189
189
  export interface OpenPositionDto {
190
190
  positionId: string;
191
191
  address: string;
192
- leverage: number;
193
192
  stopLoss: TpSlThreshold | null;
194
193
  takeProfit: TpSlThreshold | null;
195
194
  entryRatio: number;
196
195
  markRatio: number;
197
- netFunding: number;
198
196
  entryPositionValue: number;
199
197
  positionValue: number;
200
198
  marginUsed: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pear-protocol/hyperliquid-sdk",
3
- "version": "0.0.37",
3
+ "version": "0.0.38",
4
4
  "description": "React SDK for Pear Protocol Hyperliquid API integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",