@pear-protocol/symmio-client 0.3.14 → 0.3.16

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
@@ -24739,6 +24739,28 @@ function readTimestamp(source) {
24739
24739
  const parsed = Number(timestamp);
24740
24740
  return Number.isFinite(parsed) ? parsed : void 0;
24741
24741
  }
24742
+ function hasAccountDataShape(value) {
24743
+ return isRecord(value) && ("equity" in value || "maintenanceMargin" in value || "availableForOrder" in value || "totalLocked" in value);
24744
+ }
24745
+ function getSymmAccountData(response) {
24746
+ if (!isRecord(response)) {
24747
+ return void 0;
24748
+ }
24749
+ const data = response.data;
24750
+ if (isRecord(data)) {
24751
+ if (hasAccountDataShape(data.accountData)) {
24752
+ return data.accountData;
24753
+ }
24754
+ if (hasAccountDataShape(data)) {
24755
+ return data;
24756
+ }
24757
+ return void 0;
24758
+ }
24759
+ if (hasAccountDataShape(response)) {
24760
+ return response;
24761
+ }
24762
+ return void 0;
24763
+ }
24742
24764
  function getBalanceInfoContainer(response) {
24743
24765
  if (!isRecord(response)) {
24744
24766
  return void 0;
@@ -24854,6 +24876,38 @@ function computeSymmAccountOverview({
24854
24876
  timestamp: readTimestamp(balanceInfo)
24855
24877
  };
24856
24878
  }
24879
+ function computeSymmAccountOverviewFromData({
24880
+ accountData,
24881
+ upnl,
24882
+ netDeposited
24883
+ }) {
24884
+ const equity = readNumber(accountData, ["equity"]);
24885
+ const maintenanceMargin = readNumber(accountData, [
24886
+ "maintenanceMargin",
24887
+ "maintenance_margin"
24888
+ ]);
24889
+ const availableForOrder = readNumber(accountData, [
24890
+ "availableForOrder",
24891
+ "available_for_order"
24892
+ ]);
24893
+ const totalLocked = readNumber(accountData, ["totalLocked", "total_locked"]);
24894
+ return {
24895
+ allocatedBalance: "0",
24896
+ upnl: toDisplayString(toNumber(upnl)),
24897
+ equity: toDisplayString(equity),
24898
+ totalAccountValue: toDisplayString(equity),
24899
+ maintenanceMargin: toDisplayString(maintenanceMargin),
24900
+ accountHealthData: accountData.accountHealthData,
24901
+ totalLocked: toDisplayString(totalLocked),
24902
+ marginUsed: toDisplayString(totalLocked),
24903
+ totalPendingLocked: "0",
24904
+ availableForOrder: toDisplayString(availableForOrder),
24905
+ availableMargin: toDisplayString(availableForOrder),
24906
+ availableBalance: toDisplayString(availableForOrder),
24907
+ netDeposited: toDisplayString(toNumber(netDeposited)),
24908
+ notional: "0"
24909
+ };
24910
+ }
24857
24911
  function computeSymmNetDeposited(totals, decimals = 6) {
24858
24912
  if (!totals) {
24859
24913
  return "0";
@@ -24889,6 +24943,138 @@ function parseRawCollateralUnits(value) {
24889
24943
  return BigInt(value);
24890
24944
  }
24891
24945
 
24892
- export { ALL_TRADING_SELECTORS, AffiliateFeeLevel, ApprovalState, CHAIN_NAMES, CLEARING_HOUSE_ADDRESS, CLOSE_QUOTE_SELECTOR, COLLATERAL_ADDRESS, COLLATERAL_DECIMALS, ClearingHouseABI, ClearingHouseLiquidationStatus, DEFAULT_PARTY_B_ADDRESS, DEFAULT_PRECISION, ERC20ABI, FALLBACK_CHAIN_ID, InstantCloseStatus, LIMIT_ORDER_DEADLINE, LiquidationStatus, MARKET_ORDER_DEADLINE, MARKET_PRICE_COEFFICIENT, MAX_LEVERAGE, MULTI_ACCOUNT_ADDRESS, MUON_APP_NAME, MUON_BASE_URLS, MultiAccountABI, MuonClient, MuonVerifierABI, OrderType, PositionType, QuoteStatus, SEND_QUOTE_WITH_AFFILIATE_SELECTOR, SIGNATURE_STORE_ADDRESS, STANDARD_WITHDRAW_COOLDOWN, SYMMIO_DIAMOND_ADDRESS, SignatureStoreABI, SoftLiquidationLevel, SupportedChainId, SymmioDiamondABI, SymmioSDKError, V3_CHAIN_IDS, account_exports as accountActions, admin_exports as adminActions, allocate_exports as allocateActions, applySlippage, approval_exports as approvalActions, calculateGasMargin, cancel_exports as cancelActions, close_exports as closeActions, computeSymmAccountOverview, computeSymmNetDeposited, delegation_exports as delegationActions, deposit_exports as depositActions, encodeCall, formatPrice, fromWei, getAddress, getSymmAccountBalanceInfo, instant_exports as instantActions, signature_exports as signatureActions, stats_exports as statsActions, toWei, trade_exports as tradeActions, validateAddress, validateAmount, validateChainId, validateDeadline, validateQuantity, withdraw_exports as withdrawActions, wrapInProxyCall };
24946
+ // src/utils/account-pnl.ts
24947
+ function toNumber2(value) {
24948
+ if (value === null || value === void 0 || value === "") {
24949
+ return 0;
24950
+ }
24951
+ const parsed = Number(value);
24952
+ return Number.isFinite(parsed) ? parsed : 0;
24953
+ }
24954
+ function readOpenedPrice(leg) {
24955
+ return toNumber2(leg.openedPrice ?? leg.opened_price);
24956
+ }
24957
+ function readCurrentPrice(leg) {
24958
+ const parsed = Number(leg.currentPrice ?? leg.current_price);
24959
+ return Number.isFinite(parsed) ? parsed : void 0;
24960
+ }
24961
+ function readMarkPrice(leg, markPrices) {
24962
+ const symbol = leg.symbol?.trim().toUpperCase();
24963
+ if (symbol) {
24964
+ const markPrice = Number(markPrices[symbol]);
24965
+ if (Number.isFinite(markPrice)) {
24966
+ return markPrice;
24967
+ }
24968
+ }
24969
+ return readCurrentPrice(leg);
24970
+ }
24971
+ function getSymbol(leg) {
24972
+ return leg.symbol?.trim().toUpperCase() || void 0;
24973
+ }
24974
+ function computeLegUpnl(leg, side, markPrices) {
24975
+ const markPrice = readMarkPrice(leg, markPrices);
24976
+ if (markPrice === void 0) {
24977
+ return { upnl: 0, missingSymbol: getSymbol(leg) };
24978
+ }
24979
+ const quantity = toNumber2(leg.quantity);
24980
+ const openedPrice = readOpenedPrice(leg);
24981
+ const direction = side === "short" ? -1 : 1;
24982
+ return {
24983
+ upnl: quantity * (markPrice - openedPrice) * direction
24984
+ };
24985
+ }
24986
+ function computeSymmPositionUpnl(position, markPrices = {}) {
24987
+ const missingSymbols = /* @__PURE__ */ new Set();
24988
+ let upnl = 0;
24989
+ let legsCount = 0;
24990
+ for (const leg of position.longAssets ?? []) {
24991
+ legsCount += 1;
24992
+ const result = computeLegUpnl(leg, "long", markPrices);
24993
+ upnl += result.upnl;
24994
+ if (result.missingSymbol) missingSymbols.add(result.missingSymbol);
24995
+ }
24996
+ for (const leg of position.shortAssets ?? []) {
24997
+ legsCount += 1;
24998
+ const result = computeLegUpnl(leg, "short", markPrices);
24999
+ upnl += result.upnl;
25000
+ if (result.missingSymbol) missingSymbols.add(result.missingSymbol);
25001
+ }
25002
+ if (legsCount === 0) {
25003
+ return {
25004
+ upnl: toNumber2(position.unrealizedPnl ?? position.unrealized_pnl),
25005
+ missingSymbols: [],
25006
+ positionsCount: 1,
25007
+ legsCount,
25008
+ usedFallbackPositions: 1
25009
+ };
25010
+ }
25011
+ return {
25012
+ upnl,
25013
+ missingSymbols: Array.from(missingSymbols),
25014
+ positionsCount: 1,
25015
+ legsCount,
25016
+ usedFallbackPositions: 0
25017
+ };
25018
+ }
25019
+ function computeSymmPositionsUpnl(positions = [], markPrices = {}) {
25020
+ const missingSymbols = /* @__PURE__ */ new Set();
25021
+ let upnl = 0;
25022
+ let legsCount = 0;
25023
+ let usedFallbackPositions = 0;
25024
+ for (const position of positions) {
25025
+ const result = computeSymmPositionUpnl(position, markPrices);
25026
+ upnl += result.upnl;
25027
+ legsCount += result.legsCount;
25028
+ usedFallbackPositions += result.usedFallbackPositions;
25029
+ result.missingSymbols.forEach((symbol) => missingSymbols.add(symbol));
25030
+ }
25031
+ return {
25032
+ upnl,
25033
+ missingSymbols: Array.from(missingSymbols),
25034
+ positionsCount: positions.length,
25035
+ legsCount,
25036
+ usedFallbackPositions
25037
+ };
25038
+ }
25039
+ function readMessageNumber(message, keys) {
25040
+ for (const key of keys) {
25041
+ if (key in message) {
25042
+ return toNumber2(message[key]);
25043
+ }
25044
+ }
25045
+ return 0;
25046
+ }
25047
+ function normalizeSymmUpnlWebSocketMessage(raw) {
25048
+ if (typeof raw !== "object" || raw === null) {
25049
+ return void 0;
25050
+ }
25051
+ const message = raw;
25052
+ const timestamp = readMessageNumber(message, ["timestamp"]);
25053
+ return {
25054
+ upnl: readMessageNumber(message, ["upnl"]),
25055
+ timestamp: timestamp || void 0,
25056
+ availableBalance: readMessageNumber(message, [
25057
+ "availableBalance",
25058
+ "available_balance"
25059
+ ]),
25060
+ allocatedBalance: readMessageNumber(message, [
25061
+ "allocatedBalance",
25062
+ "allocated_balance"
25063
+ ]),
25064
+ cva: readMessageNumber(message, ["cva"]),
25065
+ lf: readMessageNumber(message, ["lf"]),
25066
+ partyAmm: readMessageNumber(message, ["partyAmm", "party_a_mm", "mm"]),
25067
+ pendingPartyAmm: readMessageNumber(message, [
25068
+ "pendingPartyAmm",
25069
+ "pending_party_a_mm",
25070
+ "pending_mm"
25071
+ ]),
25072
+ pendingCva: readMessageNumber(message, ["pendingCva", "pending_cva"]),
25073
+ pendingLf: readMessageNumber(message, ["pendingLf", "pending_lf"]),
25074
+ raw
25075
+ };
25076
+ }
25077
+
25078
+ export { ALL_TRADING_SELECTORS, AffiliateFeeLevel, ApprovalState, CHAIN_NAMES, CLEARING_HOUSE_ADDRESS, CLOSE_QUOTE_SELECTOR, COLLATERAL_ADDRESS, COLLATERAL_DECIMALS, ClearingHouseABI, ClearingHouseLiquidationStatus, DEFAULT_PARTY_B_ADDRESS, DEFAULT_PRECISION, ERC20ABI, FALLBACK_CHAIN_ID, InstantCloseStatus, LIMIT_ORDER_DEADLINE, LiquidationStatus, MARKET_ORDER_DEADLINE, MARKET_PRICE_COEFFICIENT, MAX_LEVERAGE, MULTI_ACCOUNT_ADDRESS, MUON_APP_NAME, MUON_BASE_URLS, MultiAccountABI, MuonClient, MuonVerifierABI, OrderType, PositionType, QuoteStatus, SEND_QUOTE_WITH_AFFILIATE_SELECTOR, SIGNATURE_STORE_ADDRESS, STANDARD_WITHDRAW_COOLDOWN, SYMMIO_DIAMOND_ADDRESS, SignatureStoreABI, SoftLiquidationLevel, SupportedChainId, SymmioDiamondABI, SymmioSDKError, V3_CHAIN_IDS, account_exports as accountActions, admin_exports as adminActions, allocate_exports as allocateActions, applySlippage, approval_exports as approvalActions, calculateGasMargin, cancel_exports as cancelActions, close_exports as closeActions, computeSymmAccountOverview, computeSymmAccountOverviewFromData, computeSymmNetDeposited, computeSymmPositionUpnl, computeSymmPositionsUpnl, delegation_exports as delegationActions, deposit_exports as depositActions, encodeCall, formatPrice, fromWei, getAddress, getSymmAccountBalanceInfo, getSymmAccountData, instant_exports as instantActions, normalizeSymmUpnlWebSocketMessage, signature_exports as signatureActions, stats_exports as statsActions, toWei, trade_exports as tradeActions, validateAddress, validateAmount, validateChainId, validateDeadline, validateQuantity, withdraw_exports as withdrawActions, wrapInProxyCall };
24893
25079
  //# sourceMappingURL=index.mjs.map
24894
25080
  //# sourceMappingURL=index.mjs.map