@0xmonaco/react 0.7.1 → 0.7.2

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.
@@ -4,15 +4,74 @@ import { useMonacoSDK } from "../useMonaco";
4
4
  * Update an AccountBalance with data from a WebSocket balance event
5
5
  */
6
6
  function updateBalanceFromEvent(balance, event) {
7
+ const totalBalance = totalBalanceFromEvent(balance, event);
7
8
  return {
8
9
  ...balance,
9
10
  available_balance: event.data.available,
10
11
  available_balance_raw: event.data.availableRaw,
11
12
  locked_balance: event.data.locked,
12
13
  locked_balance_raw: event.data.lockedRaw,
13
- total_balance: event.data.total,
14
+ total_balance: totalBalance,
14
15
  };
15
16
  }
17
+ function parseDecimal(value) {
18
+ const match = value.trim().match(/^(-?)(\d+)(?:\.(\d+))?$/);
19
+ if (!match)
20
+ return null;
21
+ const [, sign, whole, fractional = ""] = match;
22
+ const digits = `${whole}${fractional}`.replace(/^0+(?=\d)/, "");
23
+ const parsed = BigInt(digits || "0");
24
+ return {
25
+ value: sign === "-" ? -parsed : parsed,
26
+ scale: fractional.length,
27
+ };
28
+ }
29
+ function pow10(exponent) {
30
+ return 10n ** BigInt(exponent);
31
+ }
32
+ function toScale(value, scale) {
33
+ return value.value * pow10(scale - value.scale);
34
+ }
35
+ function formatDecimal(value, scale) {
36
+ const sign = value < 0n ? "-" : "";
37
+ const absolute = value < 0n ? -value : value;
38
+ if (scale === 0)
39
+ return `${sign}${absolute.toString()}`;
40
+ const padded = absolute.toString().padStart(scale + 1, "0");
41
+ const whole = padded.slice(0, -scale);
42
+ const fractional = padded.slice(-scale).replace(/0+$/, "");
43
+ return fractional ? `${sign}${whole}.${fractional}` : `${sign}${whole}`;
44
+ }
45
+ function decimalSum(...values) {
46
+ const parsed = values.map(parseDecimal);
47
+ if (parsed.some((value) => value === null))
48
+ return null;
49
+ const decimals = parsed;
50
+ const scale = Math.max(...decimals.map((value) => value.scale));
51
+ const sum = decimals.reduce((total, value) => total + toScale(value, scale), 0n);
52
+ return formatDecimal(sum, scale);
53
+ }
54
+ function decimalDifference(left, ...right) {
55
+ return decimalSum(left, ...right.map((value) => (value.startsWith("-") ? value.slice(1) : `-${value}`)));
56
+ }
57
+ function decimalGreaterThanZero(value) {
58
+ const parsed = parseDecimal(value);
59
+ return parsed ? parsed.value > 0n : false;
60
+ }
61
+ function totalBalanceFromEvent(balance, event) {
62
+ if (event.data.reason === "margin_transfer_in" || event.data.reason === "margin_transfer_out") {
63
+ return event.data.total;
64
+ }
65
+ const eventExtraTotal = decimalDifference(event.data.total, event.data.available, event.data.locked);
66
+ if (eventExtraTotal && decimalGreaterThanZero(eventExtraTotal)) {
67
+ return event.data.total;
68
+ }
69
+ const previousMarginTotal = decimalDifference(balance.total_balance, balance.available_balance, balance.locked_balance);
70
+ if (!previousMarginTotal || !decimalGreaterThanZero(previousMarginTotal)) {
71
+ return event.data.total;
72
+ }
73
+ return decimalSum(event.data.total, previousMarginTotal) ?? event.data.total;
74
+ }
16
75
  /**
17
76
  * Hook for subscribing to real-time user balance updates via WebSocket (authenticated)
18
77
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xmonaco/react",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",