@compass-labs/widgets 0.1.52 → 0.1.53

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
@@ -4632,6 +4632,14 @@ function formatCurrency(amount) {
4632
4632
  maximumFractionDigits: 2
4633
4633
  }).format(amount);
4634
4634
  }
4635
+ function formatTokenAmount(num) {
4636
+ if (num === 0) return "0";
4637
+ const abs = Math.abs(num);
4638
+ const decimals = abs >= 1 ? 2 : Math.max(2, -Math.floor(Math.log10(abs)) + 1);
4639
+ const factor = Math.pow(10, decimals);
4640
+ const floored = Math.floor(abs * factor) / factor;
4641
+ return floored.toLocaleString(void 0, { minimumFractionDigits: 2, maximumFractionDigits: decimals });
4642
+ }
4635
4643
  function PositionCard({ position, chainId }) {
4636
4644
  const [isHistoryExpanded, setIsHistoryExpanded] = useState(false);
4637
4645
  const formatShortDate = (dateStr) => {
@@ -4656,7 +4664,7 @@ function PositionCard({ position, chainId }) {
4656
4664
  /* @__PURE__ */ jsxs("div", { style: { display: "flex", flexDirection: "column", gap: "2px" }, children: [
4657
4665
  /* @__PURE__ */ jsx("span", { style: { fontWeight: 600, color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: position.marketName }),
4658
4666
  /* @__PURE__ */ jsxs("span", { style: { color: "var(--compass-color-text-tertiary)", fontSize: "0.75rem" }, children: [
4659
- position.amount.toLocaleString(),
4667
+ formatTokenAmount(position.amount),
4660
4668
  " ",
4661
4669
  position.token
4662
4670
  ] })
@@ -4774,7 +4782,7 @@ function PositionCard({ position, chainId }) {
4774
4782
  color: tx.type === "deposit" ? "var(--compass-color-success)" : "var(--compass-color-error)"
4775
4783
  }, children: [
4776
4784
  tx.type === "deposit" ? "+" : "-",
4777
- tx.amount,
4785
+ formatTokenAmount(tx.amount),
4778
4786
  " ",
4779
4787
  tx.token
4780
4788
  ] }),
@@ -4972,6 +4980,13 @@ function formatCurrency2(amount) {
4972
4980
  maximumFractionDigits: 2
4973
4981
  }).format(floored);
4974
4982
  }
4983
+ function formatAmount2(value) {
4984
+ const num = typeof value === "string" ? parseFloat(value) : value;
4985
+ if (isNaN(num) || num === 0) return "0.00";
4986
+ const abs = Math.abs(num);
4987
+ const decimals = abs >= 1 ? 2 : Math.max(2, -Math.floor(Math.log10(abs)) + 1);
4988
+ return truncate2(num, decimals);
4989
+ }
4975
4990
  function EarnAccount({
4976
4991
  showHeader = true,
4977
4992
  showInterestRate = true,
@@ -6529,7 +6544,7 @@ function EarnAccount({
6529
6544
  children: [
6530
6545
  /* @__PURE__ */ jsx("span", { className: "font-semibold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", flex: 1, marginRight: "12px" }, children: symbol }),
6531
6546
  /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-end", style: { flexShrink: 0 }, children: [
6532
- /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", whiteSpace: "nowrap" }, children: truncate2(parseFloat(data.balance), 2) }),
6547
+ /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", whiteSpace: "nowrap" }, children: formatAmount2(data.balance) }),
6533
6548
  /* @__PURE__ */ jsx("span", { style: { color: "var(--compass-color-text-tertiary)", fontSize: "0.75rem", whiteSpace: "nowrap" }, children: formatCurrency2(data.usdValue) })
6534
6549
  ] })
6535
6550
  ]
@@ -6588,7 +6603,7 @@ function EarnAccount({
6588
6603
  {
6589
6604
  isOpen: isEarningsModalOpen,
6590
6605
  onClose: () => setIsEarningsModalOpen(false),
6591
- positions,
6606
+ positions: [...positions].sort((a, b) => b.performance - a.performance || b.amount - a.amount),
6592
6607
  totalEarned,
6593
6608
  isLoading: positionQuery.isLoading,
6594
6609
  chainId: CHAIN_ID
@@ -6859,12 +6874,14 @@ function formatUsd(value) {
6859
6874
  const floored = floorTo(num, 2);
6860
6875
  return `$${floored.toLocaleString(void 0, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;
6861
6876
  }
6862
- function formatNumber(value, decimals = 2) {
6877
+ function formatNumber(value, minSignificant = 2) {
6863
6878
  if (value === null) return "-";
6864
6879
  const num = parseFloat(value);
6865
- if (isNaN(num)) return "-";
6866
- if (Math.abs(num) < 0.01 && num !== 0) return "<0.01";
6880
+ if (isNaN(num) || num === 0) return "0";
6881
+ const abs = Math.abs(num);
6882
+ const decimals = abs >= 1 ? 2 : Math.max(2, -Math.floor(Math.log10(abs)) + (minSignificant - 1));
6867
6883
  const floored = floorTo(num, decimals);
6884
+ if (floored === 0 && num !== 0) return num > 0 ? "<0.01" : ">-0.01";
6868
6885
  return floored.toLocaleString(void 0, { minimumFractionDigits: 2, maximumFractionDigits: decimals });
6869
6886
  }
6870
6887
  function formatApy(value) {
@@ -6899,7 +6916,9 @@ var EVENT_CONFIG = {
6899
6916
  withdraw: { label: "Withdraw", color: "var(--compass-color-warning)", mutedColor: "var(--compass-color-warning-muted)", icon: "out", sign: "-" },
6900
6917
  borrow: { label: "Borrow", color: "var(--compass-color-success)", mutedColor: "var(--compass-color-success-muted)", icon: "in", sign: "+" },
6901
6918
  repay: { label: "Repay", color: "var(--compass-color-warning)", mutedColor: "var(--compass-color-warning-muted)", icon: "out", sign: "-" },
6902
- liquidation: { label: "Liquidation", color: "var(--compass-color-error)", mutedColor: "var(--compass-color-error-muted, rgba(239,68,68,0.1))", icon: "out", sign: "-" }
6919
+ liquidation: { label: "Liquidation", color: "var(--compass-color-error)", mutedColor: "var(--compass-color-error-muted, rgba(239,68,68,0.1))", icon: "out", sign: "-" },
6920
+ transfer_in: { label: "Transfer In", color: "var(--compass-color-success)", mutedColor: "var(--compass-color-success-muted)", icon: "in", sign: "+" },
6921
+ transfer_out: { label: "Transfer Out", color: "var(--compass-color-warning)", mutedColor: "var(--compass-color-warning-muted)", icon: "out", sign: "-" }
6903
6922
  };
6904
6923
  function CreditEventHistory({ events, tokenSymbol }) {
6905
6924
  const [isExpanded, setIsExpanded] = useState(false);
@@ -7074,10 +7093,9 @@ function PositionsView({ allowedCollateralTokens, allowedDebtTokens, onBorrow, o
7074
7093
  }
7075
7094
  if (!data) return null;
7076
7095
  const { collateralPositions, debtPositions, accountSummary } = data;
7077
- const visibleCollateral = collateralPositions.filter(
7078
- (p) => p.usdValue !== null && parseFloat(p.usdValue) >= 0.01
7079
- );
7080
- const hasPositions = visibleCollateral.length > 0 || debtPositions.length > 0;
7096
+ const visibleCollateral = collateralPositions.filter((p) => p.usdValue !== null && parseFloat(p.usdValue) >= 0.01).sort((a, b) => parseFloat(b.usdValue ?? "0") - parseFloat(a.usdValue ?? "0"));
7097
+ const sortedDebt = [...debtPositions].sort((a, b) => parseFloat(b.usdValue ?? "0") - parseFloat(a.usdValue ?? "0"));
7098
+ const hasPositions = visibleCollateral.length > 0 || sortedDebt.length > 0;
7081
7099
  const totalCollateralUsd = formatUsd(accountSummary.totalCollateralUsd);
7082
7100
  const totalDebtUsd = formatUsd(accountSummary.totalDebtUsd);
7083
7101
  return /* @__PURE__ */ jsxs("div", { style: { display: "flex", flexDirection: "column", gap: "10px" }, children: [
@@ -7223,12 +7241,12 @@ function PositionsView({ allowedCollateralTokens, allowedDebtTokens, onBorrow, o
7223
7241
  children: totalDebtUsd
7224
7242
  }
7225
7243
  ),
7226
- debtPositions.length > 0 && /* @__PURE__ */ jsx(
7244
+ sortedDebt.length > 0 && /* @__PURE__ */ jsx(
7227
7245
  "span",
7228
7246
  {
7229
7247
  className: "text-xs",
7230
7248
  style: { color: "var(--compass-color-text-tertiary)" },
7231
- children: debtPositions.map((p) => p.symbol).join(", ")
7249
+ children: sortedDebt.map((p) => p.symbol).join(", ")
7232
7250
  }
7233
7251
  )
7234
7252
  ] }),
@@ -7326,7 +7344,7 @@ function PositionsView({ allowedCollateralTokens, allowedDebtTokens, onBorrow, o
7326
7344
  ]
7327
7345
  }
7328
7346
  ),
7329
- debtPositions.length > 0 ? debtPositions.map((position) => /* @__PURE__ */ jsx(
7347
+ sortedDebt.length > 0 ? sortedDebt.map((position) => /* @__PURE__ */ jsx(
7330
7348
  DebtPositionCard,
7331
7349
  {
7332
7350
  position,
@@ -9112,6 +9130,13 @@ function floorTo2(value, decimals) {
9112
9130
  const factor = Math.pow(10, decimals);
9113
9131
  return Math.floor(value * factor) / factor;
9114
9132
  }
9133
+ function formatAmount3(num) {
9134
+ if (num === 0) return "0.00";
9135
+ const abs = Math.abs(num);
9136
+ const decimals = abs >= 1 ? 2 : Math.max(2, -Math.floor(Math.log10(abs)) + 1);
9137
+ const floored = floorTo2(num, decimals);
9138
+ return floored.toLocaleString(void 0, { minimumFractionDigits: 2, maximumFractionDigits: decimals });
9139
+ }
9115
9140
  function formatUsd2(value) {
9116
9141
  if (value === null) return "$0.00";
9117
9142
  const num = typeof value === "string" ? parseFloat(value) : value;
@@ -9159,7 +9184,7 @@ function CreditAccount({
9159
9184
  const price = tokenPrices[b.tokenSymbol];
9160
9185
  const usdValue = amount > 0 && price != null ? amount * price : 0;
9161
9186
  return { ...b, usdValue };
9162
- }).filter((b) => b.usdValue >= 0.01).sort((a, b) => a.tokenSymbol.localeCompare(b.tokenSymbol));
9187
+ }).filter((b) => b.usdValue >= 0.01).sort((a, b) => b.usdValue - a.usdValue);
9163
9188
  const totalIdleUsd = balancesWithUsd.reduce((sum, b) => sum + b.usdValue, 0);
9164
9189
  const { data: walletBalance } = useQuery({
9165
9190
  queryKey: ["embeddedWalletBalance", chainId, address, "USDC"],
@@ -9551,7 +9576,7 @@ function CreditAccount({
9551
9576
  }
9552
9577
  )
9553
9578
  ] }),
9554
- [...positions.collateralPositions].filter((p) => parseFloat(p.usdValue ?? "0") >= 0.01).sort((a, b) => a.symbol.localeCompare(b.symbol)).map((p) => /* @__PURE__ */ jsxs(
9579
+ [...positions.collateralPositions].filter((p) => parseFloat(p.usdValue ?? "0") >= 0.01).sort((a, b) => parseFloat(b.usdValue ?? "0") - parseFloat(a.usdValue ?? "0")).map((p) => /* @__PURE__ */ jsxs(
9555
9580
  "div",
9556
9581
  {
9557
9582
  className: "flex items-center justify-between",
@@ -9564,10 +9589,7 @@ function CreditAccount({
9564
9589
  children: [
9565
9590
  /* @__PURE__ */ jsx("span", { className: "font-semibold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: p.symbol }),
9566
9591
  /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-end", children: [
9567
- /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", fontFamily: "monospace" }, children: floorTo2(parseFloat(p.amountSupplied ?? "0"), 2).toLocaleString(void 0, {
9568
- minimumFractionDigits: 2,
9569
- maximumFractionDigits: 2
9570
- }) }),
9592
+ /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", fontFamily: "monospace" }, children: formatAmount3(parseFloat(p.amountSupplied ?? "0")) }),
9571
9593
  /* @__PURE__ */ jsx("span", { style: { color: "var(--compass-color-text-tertiary)", fontSize: "0.75rem" }, children: formatUsd2(p.usdValue) })
9572
9594
  ] })
9573
9595
  ]
@@ -9597,7 +9619,7 @@ function CreditAccount({
9597
9619
  }
9598
9620
  )
9599
9621
  ] }),
9600
- [...positions.debtPositions].filter((p) => parseFloat(p.usdValue ?? "0") >= 0.01).sort((a, b) => a.symbol.localeCompare(b.symbol)).map((p) => /* @__PURE__ */ jsxs(
9622
+ [...positions.debtPositions].filter((p) => parseFloat(p.usdValue ?? "0") >= 0.01).sort((a, b) => parseFloat(b.usdValue ?? "0") - parseFloat(a.usdValue ?? "0")).map((p) => /* @__PURE__ */ jsxs(
9601
9623
  "div",
9602
9624
  {
9603
9625
  className: "flex items-center justify-between",
@@ -9610,10 +9632,7 @@ function CreditAccount({
9610
9632
  children: [
9611
9633
  /* @__PURE__ */ jsx("span", { className: "font-semibold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: p.symbol }),
9612
9634
  /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-end", children: [
9613
- /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", fontFamily: "monospace" }, children: floorTo2(parseFloat(p.amountBorrowed ?? "0"), 2).toLocaleString(void 0, {
9614
- minimumFractionDigits: 2,
9615
- maximumFractionDigits: 2
9616
- }) }),
9635
+ /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", fontFamily: "monospace" }, children: formatAmount3(parseFloat(p.amountBorrowed ?? "0")) }),
9617
9636
  /* @__PURE__ */ jsx("span", { style: { color: "var(--compass-color-text-tertiary)", fontSize: "0.75rem" }, children: formatUsd2(p.usdValue) })
9618
9637
  ] })
9619
9638
  ]
@@ -9653,10 +9672,7 @@ function CreditAccount({
9653
9672
  children: [
9654
9673
  /* @__PURE__ */ jsx("span", { className: "font-semibold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: b.tokenSymbol }),
9655
9674
  /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-end", children: [
9656
- /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", fontFamily: "monospace" }, children: floorTo2(parseFloat(b.amount), 2).toLocaleString(void 0, {
9657
- minimumFractionDigits: 2,
9658
- maximumFractionDigits: 2
9659
- }) }),
9675
+ /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem", fontFamily: "monospace" }, children: formatAmount3(parseFloat(b.amount)) }),
9660
9676
  /* @__PURE__ */ jsx("span", { style: { color: "var(--compass-color-text-tertiary)", fontSize: "0.75rem" }, children: formatUsd2(b.usdValue.toString()) })
9661
9677
  ] })
9662
9678
  ]
@@ -11350,10 +11366,12 @@ function floorTo3(value, decimals) {
11350
11366
  const factor = Math.pow(10, decimals);
11351
11367
  return Math.floor(value * factor) / factor;
11352
11368
  }
11353
- function formatAmount2(value) {
11369
+ function formatAmount4(value) {
11354
11370
  const num = typeof value === "string" ? parseFloat(value) : value;
11355
- if (isNaN(num)) return "0";
11356
- return floorTo3(num, 2).toFixed(2);
11371
+ if (isNaN(num) || num === 0) return "0.00";
11372
+ const abs = Math.abs(num);
11373
+ const decimals = abs >= 1 ? 2 : Math.max(2, -Math.floor(Math.log10(abs)) + 1);
11374
+ return floorTo3(num, decimals).toFixed(decimals);
11357
11375
  }
11358
11376
  function RebalancingWidget({
11359
11377
  showChainSwitcher = true,
@@ -11445,7 +11463,7 @@ function RebalancingWidget({
11445
11463
  if (portfolio && portfolio.positions.length > 0 && !hasInitializedTargets && !isLoading) {
11446
11464
  const origAllocs = {};
11447
11465
  setTargets(
11448
- portfolio.positions.map((p) => {
11466
+ [...portfolio.positions].sort((a, b) => b.usdValue - a.usdValue).map((p) => {
11449
11467
  const pct = parseFloat(p.allocationPercent.toFixed(2));
11450
11468
  origAllocs[`${p.venueType}-${p.venueAddress.toLowerCase()}`] = pct;
11451
11469
  return {
@@ -11491,7 +11509,7 @@ function RebalancingWidget({
11491
11509
  if (!portfolio) return;
11492
11510
  const origAllocs = {};
11493
11511
  setTargets(
11494
- portfolio.positions.map((p) => {
11512
+ [...portfolio.positions].sort((a, b) => b.usdValue - a.usdValue).map((p) => {
11495
11513
  const pct = parseFloat(p.allocationPercent.toFixed(2));
11496
11514
  origAllocs[`${p.venueType}-${p.venueAddress.toLowerCase()}`] = pct;
11497
11515
  return {
@@ -12106,7 +12124,7 @@ function RebalancingWidget({
12106
12124
  className: "font-semibold",
12107
12125
  style: { color: "var(--compass-color-primary)", fontSize: "0.8125rem", background: "none", border: "none", padding: 0 },
12108
12126
  children: [
12109
- formatAmount2(earnBalancesQuery),
12127
+ formatAmount4(earnBalancesQuery),
12110
12128
  " ",
12111
12129
  selectedToken
12112
12130
  ]
@@ -12432,7 +12450,7 @@ function RebalancingWidget({
12432
12450
  children: "Available in Account"
12433
12451
  }
12434
12452
  ),
12435
- portfolio.idleBalances.filter((b) => b.balance >= 5e-3).map((b) => /* @__PURE__ */ jsxs(
12453
+ [...portfolio.idleBalances].filter((b) => b.balance >= 5e-3).sort((a, b) => b.usdValue - a.usdValue).map((b) => /* @__PURE__ */ jsxs(
12436
12454
  "div",
12437
12455
  {
12438
12456
  className: "flex items-center justify-between",
@@ -12445,7 +12463,7 @@ function RebalancingWidget({
12445
12463
  children: [
12446
12464
  /* @__PURE__ */ jsx("span", { className: "font-semibold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: b.token }),
12447
12465
  /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-end", children: [
12448
- /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: floorTo3(b.balance, 2).toFixed(2) }),
12466
+ /* @__PURE__ */ jsx("span", { className: "font-bold", style: { color: "var(--compass-color-text)", fontSize: "0.9375rem" }, children: formatAmount4(b.balance) }),
12449
12467
  /* @__PURE__ */ jsx("span", { style: { color: "var(--compass-color-text-tertiary)", fontSize: "0.75rem" }, children: formatUSD(b.usdValue) })
12450
12468
  ] })
12451
12469
  ]