@pear-protocol/symmio-client 0.3.13 → 0.3.14

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.
@@ -578,7 +578,7 @@ var symmKeys = {
578
578
  signature: (address, chainId) => ["symm", "signature", address, chainId],
579
579
  auth: (accountAddress, chainId, signerAddress) => ["symm", "auth", accountAddress, chainId, signerAddress],
580
580
  approval: (owner, spender, chainId, token) => ["symm", "approval", owner, spender, chainId, token],
581
- balances: (address, chainId) => ["symm", "balances", address, chainId],
581
+ balances: (address, chainId, multiAccountAddress) => multiAccountAddress === void 0 ? ["symm", "balances", address, chainId] : ["symm", "balances", address, chainId, multiAccountAddress],
582
582
  positions: (params) => ["symm", "positions", params],
583
583
  openOrders: (params) => ["symm", "openOrders", params],
584
584
  tradeHistory: (params) => ["symm", "tradeHistory", params],
@@ -25642,7 +25642,7 @@ function useSymmBalances(params) {
25642
25642
  const internalEnabled = !!symmCoreClient && !!userAddress;
25643
25643
  return reactQuery.useQuery({
25644
25644
  ...params.query,
25645
- queryKey: symmKeys.balances(userAddress, chainId),
25645
+ queryKey: symmKeys.balances(userAddress, chainId, multiAccountAddress),
25646
25646
  queryFn: () => symmCoreClient.accounts.getBalanceInfo({
25647
25647
  userAddress,
25648
25648
  chainId,
@@ -25651,6 +25651,242 @@ function useSymmBalances(params) {
25651
25651
  enabled: internalEnabled && (params.query?.enabled ?? true)
25652
25652
  });
25653
25653
  }
25654
+
25655
+ // src/utils/account-overview.ts
25656
+ var BALANCE_INFO_KEYS = [
25657
+ "allocatedBalance",
25658
+ "allocated_balance",
25659
+ "cva",
25660
+ "lf",
25661
+ "partyAmm",
25662
+ "partyAMM",
25663
+ "party_a_mm",
25664
+ "mm",
25665
+ "lockedPartyAMM",
25666
+ "upnl"
25667
+ ];
25668
+ function isRecord(value) {
25669
+ return typeof value === "object" && value !== null;
25670
+ }
25671
+ function hasBalanceInfoShape(value) {
25672
+ return isRecord(value) && BALANCE_INFO_KEYS.some((key) => key in value);
25673
+ }
25674
+ function readValue(source, keys) {
25675
+ for (const key of keys) {
25676
+ if (key in source) {
25677
+ return source[key];
25678
+ }
25679
+ }
25680
+ return void 0;
25681
+ }
25682
+ function toNumber(value) {
25683
+ if (value === null || value === void 0 || value === "") {
25684
+ return 0;
25685
+ }
25686
+ if (typeof value === "bigint") {
25687
+ return Number(value);
25688
+ }
25689
+ const parsed = Number(value);
25690
+ return Number.isFinite(parsed) ? parsed : 0;
25691
+ }
25692
+ function toDisplayString(value) {
25693
+ if (Object.is(value, -0) || Math.abs(value) < Number.EPSILON) {
25694
+ return "0";
25695
+ }
25696
+ return String(value);
25697
+ }
25698
+ function readNumber(source, keys) {
25699
+ return toNumber(readValue(source, keys));
25700
+ }
25701
+ function readTimestamp(source) {
25702
+ const timestamp = readValue(source, ["timestamp"]);
25703
+ if (timestamp === void 0 || timestamp === null) {
25704
+ return void 0;
25705
+ }
25706
+ const parsed = Number(timestamp);
25707
+ return Number.isFinite(parsed) ? parsed : void 0;
25708
+ }
25709
+ function getBalanceInfoContainer(response) {
25710
+ if (!isRecord(response)) {
25711
+ return void 0;
25712
+ }
25713
+ if (hasBalanceInfoShape(response)) {
25714
+ return response;
25715
+ }
25716
+ const data = response.data;
25717
+ if (isRecord(data)) {
25718
+ return data;
25719
+ }
25720
+ return response;
25721
+ }
25722
+ function getSymmAccountBalanceInfo(response, accountAddress) {
25723
+ const container = getBalanceInfoContainer(response);
25724
+ if (!container) {
25725
+ return void 0;
25726
+ }
25727
+ if (hasBalanceInfoShape(container)) {
25728
+ return container;
25729
+ }
25730
+ if (accountAddress) {
25731
+ const normalizedAddress = accountAddress.toLowerCase();
25732
+ const direct = container[accountAddress] ?? container[normalizedAddress];
25733
+ if (isRecord(direct) && hasBalanceInfoShape(direct)) {
25734
+ return direct;
25735
+ }
25736
+ const matchingEntry = Object.entries(container).find(
25737
+ ([key]) => key.toLowerCase() === normalizedAddress
25738
+ );
25739
+ if (isRecord(matchingEntry?.[1]) && hasBalanceInfoShape(matchingEntry[1])) {
25740
+ return matchingEntry[1];
25741
+ }
25742
+ }
25743
+ const values = Object.values(container);
25744
+ if (values.length === 1 && isRecord(values[0]) && hasBalanceInfoShape(values[0])) {
25745
+ return values[0];
25746
+ }
25747
+ return void 0;
25748
+ }
25749
+ function computeSymmAccountOverview({
25750
+ balanceInfo,
25751
+ upnl: upnlOverride,
25752
+ netDeposited
25753
+ }) {
25754
+ const allocatedBalance = readNumber(balanceInfo, [
25755
+ "allocatedBalance",
25756
+ "allocated_balance"
25757
+ ]);
25758
+ const cva = readNumber(balanceInfo, ["cva", "lockedCVA", "locked_cva"]);
25759
+ const lf = readNumber(balanceInfo, ["lf", "lockedLF", "locked_lf"]);
25760
+ const partyAmm = readNumber(balanceInfo, [
25761
+ "partyAmm",
25762
+ "partyAMM",
25763
+ "party_a_mm",
25764
+ "mm",
25765
+ "lockedPartyAMM",
25766
+ "locked_party_a_mm"
25767
+ ]);
25768
+ const pendingCva = readNumber(balanceInfo, [
25769
+ "pendingCva",
25770
+ "pending_cva",
25771
+ "pendingLockedCVA",
25772
+ "pending_locked_cva"
25773
+ ]);
25774
+ const pendingLf = readNumber(balanceInfo, [
25775
+ "pendingLf",
25776
+ "pending_lf",
25777
+ "pendingLockedLF",
25778
+ "pending_locked_lf"
25779
+ ]);
25780
+ const pendingPartyAmm = readNumber(balanceInfo, [
25781
+ "pendingPartyAmm",
25782
+ "pendingPartyAMM",
25783
+ "pending_party_a_mm",
25784
+ "pendingMm",
25785
+ "pending_mm",
25786
+ "pendingLockedPartyAMM",
25787
+ "pending_locked_party_a_mm"
25788
+ ]);
25789
+ const upnl = upnlOverride === void 0 ? readNumber(balanceInfo, ["upnl"]) : toNumber(upnlOverride);
25790
+ const computedTotalLocked = cva + lf + partyAmm;
25791
+ const totalLocked = readValue(
25792
+ balanceInfo,
25793
+ ["totalLocked", "total_locked"]
25794
+ ) === void 0 ? computedTotalLocked : readNumber(balanceInfo, ["totalLocked", "total_locked"]);
25795
+ const totalPendingLocked = readValue(balanceInfo, [
25796
+ "totalPendingLocked",
25797
+ "total_pending_locked"
25798
+ ]) === void 0 ? pendingCva + pendingLf + pendingPartyAmm : readNumber(balanceInfo, ["totalPendingLocked", "total_pending_locked"]);
25799
+ const maintenanceMargin = cva + lf;
25800
+ const equity = allocatedBalance + upnl;
25801
+ const availableForOrder = upnl >= 0 ? allocatedBalance + upnl - totalLocked - totalPendingLocked : allocatedBalance - cva - lf - totalPendingLocked - Math.max(Math.abs(upnl), partyAmm);
25802
+ const availableBalance = readNumber(balanceInfo, [
25803
+ "availableBalance",
25804
+ "available_balance"
25805
+ ]);
25806
+ const notional = readNumber(balanceInfo, ["notional"]);
25807
+ return {
25808
+ allocatedBalance: toDisplayString(allocatedBalance),
25809
+ upnl: toDisplayString(upnl),
25810
+ equity: toDisplayString(equity),
25811
+ totalAccountValue: toDisplayString(equity),
25812
+ maintenanceMargin: toDisplayString(maintenanceMargin),
25813
+ totalLocked: toDisplayString(totalLocked),
25814
+ marginUsed: toDisplayString(computedTotalLocked),
25815
+ totalPendingLocked: toDisplayString(totalPendingLocked),
25816
+ availableForOrder: toDisplayString(availableForOrder),
25817
+ availableMargin: toDisplayString(availableForOrder),
25818
+ availableBalance: toDisplayString(availableBalance),
25819
+ netDeposited: toDisplayString(toNumber(netDeposited)),
25820
+ notional: toDisplayString(notional),
25821
+ timestamp: readTimestamp(balanceInfo)
25822
+ };
25823
+ }
25824
+ function computeSymmNetDeposited(totals, decimals = 6) {
25825
+ if (!totals) {
25826
+ return "0";
25827
+ }
25828
+ const deposit2 = parseRawCollateralUnits(totals.deposit);
25829
+ const withdraw2 = parseRawCollateralUnits(totals.withdraw);
25830
+ const value = deposit2 - withdraw2;
25831
+ const negative = value < 0n;
25832
+ const absolute = negative ? -value : value;
25833
+ const scale = 10n ** BigInt(decimals);
25834
+ const whole = absolute / scale;
25835
+ const fraction = absolute % scale;
25836
+ const fractionText = fraction.toString().padStart(decimals, "0").replace(/0+$/, "");
25837
+ const amount = fractionText ? `${whole}.${fractionText}` : whole.toString();
25838
+ return negative ? `-${amount}` : amount;
25839
+ }
25840
+ function parseRawCollateralUnits(value) {
25841
+ if (value === null || value === void 0 || value === "") {
25842
+ return 0n;
25843
+ }
25844
+ if (typeof value === "bigint") {
25845
+ return value;
25846
+ }
25847
+ if (typeof value === "number") {
25848
+ if (!Number.isSafeInteger(value)) {
25849
+ throw new Error("Raw collateral unit numbers must be safe integers");
25850
+ }
25851
+ return BigInt(value);
25852
+ }
25853
+ if (!/^-?\d+$/.test(value)) {
25854
+ throw new Error("Raw collateral unit strings must be base-10 integers");
25855
+ }
25856
+ return BigInt(value);
25857
+ }
25858
+
25859
+ // src/react/hooks/use-symm-account-overview.ts
25860
+ function useSymmAccountOverview(params) {
25861
+ const {
25862
+ userAddress,
25863
+ accountAddress,
25864
+ multiAccountAddress,
25865
+ upnl,
25866
+ netDeposited
25867
+ } = params;
25868
+ const query = useSymmBalances({
25869
+ userAddress,
25870
+ multiAccountAddress,
25871
+ chainId: params.chainId,
25872
+ query: params.query
25873
+ });
25874
+ const data = react.useMemo(() => {
25875
+ if (query.data === void 0) {
25876
+ return void 0;
25877
+ }
25878
+ const balanceInfo = getSymmAccountBalanceInfo(
25879
+ query.data,
25880
+ accountAddress ?? userAddress
25881
+ );
25882
+ return {
25883
+ response: query.data,
25884
+ balanceInfo,
25885
+ overview: balanceInfo ? computeSymmAccountOverview({ balanceInfo, upnl, netDeposited }) : void 0
25886
+ };
25887
+ }, [accountAddress, netDeposited, query.data, upnl, userAddress]);
25888
+ return { ...query, data };
25889
+ }
25654
25890
  function useSymmOpenBasketMutation(options) {
25655
25891
  const { symmCoreClient } = useSymmContext();
25656
25892
  const queryClient = reactQuery.useQueryClient();
@@ -27224,10 +27460,14 @@ function getSymmErrorMessage(error) {
27224
27460
  }
27225
27461
 
27226
27462
  exports.SymmProvider = SymmProvider;
27463
+ exports.computeSymmAccountOverview = computeSymmAccountOverview;
27464
+ exports.computeSymmNetDeposited = computeSymmNetDeposited;
27465
+ exports.getSymmAccountBalanceInfo = getSymmAccountBalanceInfo;
27227
27466
  exports.getSymmErrorMessage = getSymmErrorMessage;
27228
27467
  exports.symmKeys = symmKeys;
27229
27468
  exports.useBinanceMarkPriceStore = useBinanceMarkPriceStore;
27230
27469
  exports.useSymmAccountData = useSymmAccountData;
27470
+ exports.useSymmAccountOverview = useSymmAccountOverview;
27231
27471
  exports.useSymmAccountSummary = useSymmAccountSummary;
27232
27472
  exports.useSymmAccountsApi = useSymmAccountsApi;
27233
27473
  exports.useSymmAccountsLength = useSymmAccountsLength;