@humanode-hyperlane/metrics 0.0.0-stage → 0.2.8-humanode.1

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.
Files changed (53) hide show
  1. package/LICENSE.md +193 -0
  2. package/dist/balance.d.ts +93 -0
  3. package/dist/balance.d.ts.map +1 -0
  4. package/dist/balance.js +235 -0
  5. package/dist/balance.js.map +1 -0
  6. package/dist/gauges.d.ts +41 -0
  7. package/dist/gauges.d.ts.map +1 -0
  8. package/dist/gauges.js +100 -0
  9. package/dist/gauges.js.map +1 -0
  10. package/dist/index.d.ts +8 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +13 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/index.test.d.ts +2 -0
  15. package/dist/index.test.d.ts.map +1 -0
  16. package/dist/index.test.js +45 -0
  17. package/dist/index.test.js.map +1 -0
  18. package/dist/pushgateway.d.ts +23 -0
  19. package/dist/pushgateway.d.ts.map +1 -0
  20. package/dist/pushgateway.js +54 -0
  21. package/dist/pushgateway.js.map +1 -0
  22. package/dist/server.d.ts +15 -0
  23. package/dist/server.d.ts.map +1 -0
  24. package/dist/server.js +63 -0
  25. package/dist/server.js.map +1 -0
  26. package/dist/server.test.d.ts +2 -0
  27. package/dist/server.test.d.ts.map +1 -0
  28. package/dist/server.test.js +23 -0
  29. package/dist/server.test.js.map +1 -0
  30. package/dist/types.d.ts +64 -0
  31. package/dist/types.d.ts.map +1 -0
  32. package/dist/types.js +2 -0
  33. package/dist/types.js.map +1 -0
  34. package/dist/update.d.ts +51 -0
  35. package/dist/update.d.ts.map +1 -0
  36. package/dist/update.js +163 -0
  37. package/dist/update.js.map +1 -0
  38. package/dist/utils.d.ts +7 -0
  39. package/dist/utils.d.ts.map +1 -0
  40. package/dist/utils.js +9 -0
  41. package/dist/utils.js.map +1 -0
  42. package/package.json +52 -3
  43. package/src/balance.ts +380 -0
  44. package/src/gauges.ts +136 -0
  45. package/src/index.test.ts +52 -0
  46. package/src/index.ts +53 -0
  47. package/src/pushgateway.ts +66 -0
  48. package/src/server.test.ts +32 -0
  49. package/src/server.ts +76 -0
  50. package/src/types.ts +74 -0
  51. package/src/update.ts +239 -0
  52. package/src/utils.ts +10 -0
  53. package/README.md +0 -3
package/src/update.ts ADDED
@@ -0,0 +1,239 @@
1
+ import type { Logger } from 'pino';
2
+
3
+ import {
4
+ type ChainName,
5
+ type Token,
6
+ TokenStandard,
7
+ type WarpCore,
8
+ } from '@humanode-hyperlane/sdk';
9
+ import { type Address, isNullish } from '@humanode-hyperlane/utils';
10
+
11
+ import type { WarpMetricsGauges } from './gauges.js';
12
+ import type {
13
+ NativeWalletBalance,
14
+ WarpRouteBalance,
15
+ WarpRouteMetricLabels,
16
+ XERC20Limit,
17
+ } from './types.js';
18
+
19
+ /**
20
+ * Updates token balance metrics for a warp route token.
21
+ *
22
+ * @param gauges - The warp metrics gauges to update
23
+ * @param warpCore - The WarpCore instance for the route
24
+ * @param token - The token to update metrics for
25
+ * @param balanceInfo - The balance information for the token
26
+ * @param warpRouteId - The warp route identifier
27
+ * @param logger - The logger instance
28
+ */
29
+ export function updateTokenBalanceMetrics(
30
+ gauges: WarpMetricsGauges,
31
+ warpCore: WarpCore,
32
+ token: Token,
33
+ balanceInfo: WarpRouteBalance,
34
+ warpRouteId: string,
35
+ logger: Logger,
36
+ ): void {
37
+ const allChains = warpCore.getTokenChains().sort();
38
+ const relatedChains = allChains.filter(
39
+ (chainName) => chainName !== token.chainName,
40
+ );
41
+
42
+ const metrics: WarpRouteMetricLabels = {
43
+ chain_name: token.chainName,
44
+ token_address: balanceInfo.tokenAddress,
45
+ token_name: token.name,
46
+ wallet_address:
47
+ // the balance for an EvmHypERC20 token is returned as the total supply of the xERC20 token,
48
+ // therefore we set the wallet address to the token address,
49
+ // we follow the same pattern or synthetic tokens
50
+ token.standard !== TokenStandard.EvmHypXERC20
51
+ ? token.addressOrDenom
52
+ : balanceInfo.tokenAddress,
53
+ token_standard:
54
+ // as we are reporting the total supply for clarity we report the standard as xERC20
55
+ token.standard !== TokenStandard.EvmHypXERC20 ? token.standard : 'xERC20',
56
+ warp_route_id: warpRouteId,
57
+ // TODO: consider deprecating this label given that we have the value at risk metric
58
+ related_chain_names: relatedChains.join(','),
59
+ };
60
+
61
+ gauges.warpRouteTokenBalance.labels({ ...metrics }).set(balanceInfo.balance);
62
+ logger.info(
63
+ {
64
+ labels: metrics,
65
+ balance: balanceInfo.balance,
66
+ },
67
+ 'Wallet balance updated for token',
68
+ );
69
+
70
+ if (!isNullish(balanceInfo.valueUSD)) {
71
+ // TODO: consider deprecating this metric in favor of the value at risk metric
72
+ gauges.warpRouteCollateralValue
73
+ .labels({ ...metrics })
74
+ .set(balanceInfo.valueUSD);
75
+ logger.info(
76
+ {
77
+ labels: metrics,
78
+ valueUSD: balanceInfo.valueUSD,
79
+ },
80
+ 'Wallet value updated for token',
81
+ );
82
+
83
+ for (const chainName of allChains) {
84
+ const labels = {
85
+ chain_name: chainName,
86
+ collateral_chain_name: metrics.chain_name,
87
+ token_address: metrics.token_address,
88
+ token_name: metrics.token_name,
89
+ collateral_token_standard: metrics.token_standard,
90
+ warp_route_id: metrics.warp_route_id,
91
+ };
92
+
93
+ gauges.warpRouteValueAtRisk
94
+ .labels({ ...labels })
95
+ .set(balanceInfo.valueUSD);
96
+ logger.info(
97
+ {
98
+ labels,
99
+ valueUSD: balanceInfo.valueUSD,
100
+ },
101
+ `Value at risk on ${chainName} updated for token`,
102
+ );
103
+ }
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Updates managed lockbox balance metrics.
109
+ *
110
+ * @param gauges - The warp metrics gauges to update
111
+ * @param warpCore - The WarpCore instance for the route
112
+ * @param chainName - The chain name where the lockbox is deployed
113
+ * @param tokenName - The name of the collateral token
114
+ * @param tokenAddress - The address of the collateral token
115
+ * @param lockBoxAddress - The address of the managed lockbox
116
+ * @param balanceInfo - The balance information for the lockbox
117
+ * @param warpRouteId - The warp route identifier
118
+ * @param logger - The logger instance
119
+ */
120
+ export function updateManagedLockboxBalanceMetrics(
121
+ gauges: WarpMetricsGauges,
122
+ warpCore: WarpCore,
123
+ chainName: ChainName,
124
+ tokenName: string,
125
+ tokenAddress: string,
126
+ lockBoxAddress: string,
127
+ balanceInfo: WarpRouteBalance,
128
+ warpRouteId: string,
129
+ logger: Logger,
130
+ ): void {
131
+ const metrics: WarpRouteMetricLabels = {
132
+ chain_name: chainName,
133
+ token_address: tokenAddress,
134
+ token_name: tokenName,
135
+ wallet_address: lockBoxAddress,
136
+ token_standard: 'EvmManagedLockbox', // TODO: we should eventually add a new TokenStandard for this
137
+ warp_route_id: warpRouteId,
138
+ related_chain_names: warpCore
139
+ .getTokenChains()
140
+ .filter((_chainName) => _chainName !== chainName)
141
+ .sort()
142
+ .join(','),
143
+ };
144
+
145
+ gauges.warpRouteTokenBalance.labels({ ...metrics }).set(balanceInfo.balance);
146
+ logger.info(
147
+ {
148
+ labels: metrics,
149
+ balance: balanceInfo.balance,
150
+ },
151
+ 'ManagedLockbox collateral balance updated',
152
+ );
153
+
154
+ if (!isNullish(balanceInfo.valueUSD)) {
155
+ gauges.warpRouteCollateralValue
156
+ .labels({ ...metrics })
157
+ .set(balanceInfo.valueUSD);
158
+ logger.info(
159
+ {
160
+ labels: metrics,
161
+ valueUSD: balanceInfo.valueUSD,
162
+ },
163
+ 'ManagedLockbox value updated for token',
164
+ );
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Updates native wallet balance metrics.
170
+ *
171
+ * @param gauges - The warp metrics gauges to update
172
+ * @param balance - The native wallet balance information
173
+ * @param logger - The logger instance
174
+ */
175
+ export function updateNativeWalletBalanceMetrics(
176
+ gauges: WarpMetricsGauges,
177
+ balance: NativeWalletBalance,
178
+ logger: Logger,
179
+ ): void {
180
+ gauges.walletBalanceGauge
181
+ .labels({
182
+ chain: balance.chain,
183
+ wallet_address: balance.walletAddress,
184
+ wallet_name: balance.walletName,
185
+ token_address: 'native',
186
+ token_symbol: 'Native',
187
+ token_name: 'Native',
188
+ })
189
+ .set(balance.balance);
190
+ logger.info('Native wallet balance updated', {
191
+ balanceInfo: balance,
192
+ });
193
+ }
194
+
195
+ /**
196
+ * Updates xERC20 limits metrics.
197
+ *
198
+ * @param gauges - The warp metrics gauges to update
199
+ * @param token - The token to update metrics for
200
+ * @param limits - The xERC20 limits
201
+ * @param bridgeAddress - The address of the bridge
202
+ * @param bridgeLabel - A label for the bridge (e.g., token standard)
203
+ * @param xERC20Address - The address of the xERC20 token
204
+ * @param logger - The logger instance
205
+ */
206
+ export function updateXERC20LimitsMetrics(
207
+ gauges: WarpMetricsGauges,
208
+ token: Token,
209
+ limits: XERC20Limit,
210
+ bridgeAddress: Address,
211
+ bridgeLabel: string,
212
+ xERC20Address: Address,
213
+ logger: Logger,
214
+ ): void {
215
+ const labels = {
216
+ chain_name: token.chainName,
217
+ token_name: token.name,
218
+ bridge_address: bridgeAddress,
219
+ token_address: xERC20Address,
220
+ bridge_label: bridgeLabel,
221
+ };
222
+
223
+ for (const [limitType, limit] of Object.entries(limits)) {
224
+ gauges.xERC20LimitsGauge
225
+ .labels({
226
+ ...labels,
227
+ limit_type: limitType,
228
+ })
229
+ .set(limit);
230
+ }
231
+
232
+ logger.info(
233
+ {
234
+ ...labels,
235
+ limits,
236
+ },
237
+ 'xERC20 limits updated for bridge on token',
238
+ );
239
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,10 @@
1
+ import type { Token } from '@humanode-hyperlane/sdk';
2
+
3
+ /**
4
+ * Formats a bigint value to a number using the token's decimal precision.
5
+ * Returns 0 if the result is non-finite (NaN, Infinity) to avoid Prometheus dropping the metric.
6
+ */
7
+ export function formatBigInt(token: Token, num: bigint): number {
8
+ const result = Number(token.amount(num).getDecimalFormattedAmount());
9
+ return Number.isFinite(result) ? result : 0;
10
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.