This repository was archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathmetrics.ts
More file actions
347 lines (314 loc) · 10.3 KB
/
metrics.ts
File metadata and controls
347 lines (314 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import {
HydratedProviders,
ERC20Abi,
ChainInfo,
TransactionReason,
IVectorChainReader,
jsonifyError,
} from "@connext/vector-types";
import {
hydrateProviders,
getChainInfo,
getAssetName,
getMainnetEquivalent,
getExchangeRateInEth,
calculateExchangeWad,
parseProviders,
} from "@connext/vector-utils";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { AddressZero } from "@ethersproject/constants";
import { Contract } from "@ethersproject/contracts";
import { formatEther, formatUnits } from "@ethersproject/units";
import { Wallet } from "@ethersproject/wallet";
import { getAddress } from "ethers/lib/utils";
import { BaseLogger } from "pino";
import { Counter, Gauge } from "prom-client";
import { getConfig } from "./config";
const config = getConfig();
//////////////////////////
///// Router Metrics /////
/////////////////////////
//////////////////////////
///// Helpers/Utils
export const wallet = Wallet.fromMnemonic(config.mnemonic);
export const signerAddress = wallet.address;
export const hydrated: HydratedProviders = hydrateProviders(parseProviders(config.chainProviders));
export const rebalancedTokens: {
[chainId: string]: {
[assetId: string]: {
contract: Contract;
decimals?: number;
};
};
} = {};
Object.entries(hydrated).forEach(async ([chainId, provider]) => {
rebalancedTokens[chainId] = {};
const assets = config.rebalanceProfiles
.filter((prof) => prof.chainId.toString() === chainId && prof.assetId !== AddressZero)
.map((p) => getAddress(p.assetId));
assets.forEach((asset) => {
rebalancedTokens[chainId][asset] = {
contract: new Contract(asset, ERC20Abi, provider),
decimals: undefined,
};
});
});
export const getDecimals = async (chainId: string, _assetId: string): Promise<number> => {
const assetId = getAddress(_assetId);
if (assetId === AddressZero) {
return 18;
}
const { decimals: _decimals, contract: _contract } = rebalancedTokens[chainId][assetId] ?? {};
if (_decimals) {
return _decimals;
}
if (!_contract && !hydrated[parseInt(chainId)]) {
// no provider for that chain, cannot retrieve decimals,
// use default of 18
return 18;
}
const contract = _contract ?? new Contract(assetId, ERC20Abi, hydrated[parseInt(chainId)]);
let decimals = 18;
try {
decimals = await contract.decimals();
} catch (e) {
// default to 18
}
rebalancedTokens[chainId][assetId] = { decimals, contract };
return decimals;
};
export const parseBalanceToNumber = async (
toFormat: BigNumberish,
chainId: string,
assetId: string,
): Promise<number> => {
if (assetId === AddressZero) {
return parseFloat(formatEther(toFormat));
}
const decimals = await getDecimals(chainId, assetId);
return parseFloat(formatUnits(toFormat, decimals));
};
export const incrementGasCosts = async (
gasUsed: string,
chainId: number,
reason: TransactionReason,
ethReader: IVectorChainReader,
logger: BaseLogger,
): Promise<void> => {
// Increment the gas cost without any normalizing
gasConsumed.inc({ chainId, reason }, await parseBalanceToNumber(gasUsed, chainId.toString(), AddressZero));
// Normalize the gas amounts to mainnet eth prices
const mainnetEquivalent = getMainnetEquivalent(chainId, AddressZero);
if (mainnetEquivalent.isError) {
logger.warn(
{ error: mainnetEquivalent.getError()!.message, chainId, assetId: AddressZero },
"No mainnet equivalent, cannot normalize",
);
return;
}
// Get exchange rate (token : eth)
const rate = await getExchangeRateInEth(mainnetEquivalent.getValue(), logger);
if (rate.isError) {
logger.warn({ error: rate.getError()?.message }, "Failed to get exchange rate");
return;
}
// Normalize to eth cost
const gasPrice = await ethReader.getGasPrice(chainId);
if (gasPrice.isError) {
logger.warn({ ...jsonifyError(gasPrice.getError()!), chainId }, "Failed to get gasPrice");
return;
}
const ethFee = calculateExchangeWad(
gasPrice.getValue().mul(gasUsed),
await getDecimals("1", mainnetEquivalent.getValue()),
rate.getValue().toString(),
18,
);
// Increment fees in eth
mainnetGasCost.inc({ chainId, reason }, await parseBalanceToNumber(ethFee, "1", AddressZero));
logger.debug(
{
gasUsed,
chainId,
reason,
mainnetEquivalent: mainnetEquivalent.getValue(),
gasPrice: gasPrice.getValue().toString(),
ethFee: ethFee.toString(),
},
"Incremented gas fees",
);
};
export const incrementFees = async (
feeAmount: string,
feeAssetId: string,
feeChainId: number,
logger: BaseLogger,
): Promise<void> => {
// First increment fees in native asset
feesCollected.inc(
{
chainId: feeChainId,
assetId: feeAssetId,
},
await parseBalanceToNumber(feeAmount, feeChainId.toString(), feeAssetId),
);
// Get the mainnet equivalent
const mainnetEquivalent = getMainnetEquivalent(feeChainId, feeAssetId);
if (mainnetEquivalent.isError) {
logger.warn(
{ error: mainnetEquivalent.getError()!.message, assetId: feeAssetId, chainId: feeChainId },
"No mainnet equivalent, cannot normalize",
);
return;
}
// Get exchange rate (token : eth)
const rate = await getExchangeRateInEth(mainnetEquivalent.getValue(), logger);
if (rate.isError) {
logger.warn({ error: rate.getError()?.message }, "Failed to get exchange rate");
return;
}
// Get equivalent eth amount
const ethFee = calculateExchangeWad(
BigNumber.from(feeAmount),
await getDecimals("1", mainnetEquivalent.getValue()),
rate.getValue().toString(),
18,
);
mainnetFeesCollectedInEth.inc(
{ chainId: feeChainId, assetId: feeAssetId },
await parseBalanceToNumber(ethFee, "1", AddressZero),
);
logger.debug(
{
feeAmount,
chainId: feeChainId,
assetId: feeAssetId,
mainnetEquivalent: mainnetEquivalent.getValue(),
ethFee: ethFee.toString(),
},
"Incremented collected fees",
);
};
//////////////////////////
///// Onchain liquidity
export const onchainLiquidity = new Gauge({
name: "router_onchain_liquidity",
help: "router_onchain_liquidity_help",
labelNames: ["chainName", "chainId", "assetName", "assetId"] as const,
async collect() {
await Promise.all(
Object.entries(hydrated).map(async ([chainId, provider]) => {
// base asset
const balance = await provider.getBalance(signerAddress);
const chainInfo: ChainInfo = await getChainInfo(Number(chainId));
const baseAssetName: string = getAssetName(Number(chainId), AddressZero);
this.set(
{ chainName: chainInfo?.name ?? chainId, chainId, assetName: baseAssetName, assetId: AddressZero },
parseFloat(formatEther(balance)),
);
// tokens
await Promise.all(
Object.entries(rebalancedTokens[chainId] ?? {}).map(async ([assetId, config]) => {
const balance = await config.contract.balanceOf(signerAddress);
const assetName: string = getAssetName(Number(chainId), assetId);
const toSet = await parseBalanceToNumber(balance, chainId, assetId);
this.set({ chainName: chainInfo?.name ?? chainId, chainId, assetName, assetId }, toSet);
}),
);
}),
);
},
});
//////////////////////////
///// Offchain liquidity
export const offchainLiquidity = new Gauge({
name: "router_offchain_liquidity",
help: "router_offchain_liquidity_help",
labelNames: ["assetId", "chainId"] as const,
});
//////////////////////////
///// Channel metrics
export const openChannels = new Counter({
name: "router_channels",
help: "router_channels_help",
labelNames: ["chainId"] as const,
});
//////////////////////////
///// Transfer metrics
// Track number of times a transfer attempt was made
export const attemptedTransfer = new Counter({
name: "router_transfer_attempt",
help: "router_transfer_attempt_help",
labelNames: ["assetId", "chainId"] as const,
});
// Track successful transfers
export const successfulTransfer = new Counter({
name: "router_transfer_successful",
help: "router_transfer_successful_help",
labelNames: ["assetId", "chainId"] as const,
});
// Track failing forwards
export const failedTransfer = new Counter({
name: "router_transfer_failed",
help: "router_transfer_failed_help",
labelNames: ["assetId", "chainId"] as const,
});
// Track volume from receiver side
export const forwardedTransferVolume = new Counter({
name: "router_transfer_volume",
help: "router_transfer_volume_help",
labelNames: ["assetId", "chainId"] as const,
});
// Track size of forwarded transfers
export const forwardedTransferSize = new Gauge({
name: "router_transfer_size",
help: "router_transfer_size_help",
labelNames: ["assetId", "chainId"] as const,
});
// Track fees charged on transfers
export const feesCollected = new Counter({
name: "router_fees",
help: "router_fees_help",
labelNames: ["assetId", "chainId"] as const,
});
// Track fees charged on transfers in eth where possible
// NOTE: it is only possible to calculate fees when one of
// the sides of the swap touches mainnet. Otherwise, we cannot
// get the token:eth exchange rate to normalize the fees.
export const mainnetFeesCollectedInEth = new Counter({
name: "router_mainnet_eth_fees",
help: "router_mainnet_eth_fees_help",
labelNames: ["assetId", "chainId"] as const,
});
//////////////////////////
///// Transaction metrics
// Track on chain transactions attempts
export const transactionAttempt = new Counter({
name: "router_transaction_attempt",
help: "router_transaction_attempt_help",
labelNames: ["reason", "chainId"] as const,
});
// Track successful on chain transactions
export const transactionSuccess = new Counter({
name: "router_transaction_success",
help: "router_transaction_success_help",
labelNames: ["reason", "chainId"] as const,
});
// Track failed on chain transactions
export const transactionFailed = new Counter({
name: "router_transaction_failed",
help: "router_transaction_failed_help",
labelNames: ["reason", "chainId"] as const,
});
// Track gas consumed
export const gasConsumed = new Counter({
name: "router_gas_consumed",
help: "router_gas_consumed_help",
labelNames: ["reason", "chainId"] as const,
});
// Track gas consumed on mainnet in eth
export const mainnetGasCost = new Counter({
name: "router_mainnet_gas_cost",
help: "router_mainnet_gas_cost_help",
labelNames: ["reason", "chainId"] as const,
});