-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
133 lines (106 loc) · 4.17 KB
/
index.ts
File metadata and controls
133 lines (106 loc) · 4.17 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
import { mainnet } from "viem/chains";
import type { IAggregatedMetrics } from "./types.js";
import type { TransactionReceipt } from "viem";
import { BLOCK_WINDOW_ETHEREUM_5_WEEKS, BLOCK_WINDOW_ETHEREUM_10_MINUTES, MIN_SAMPLES } from "../utils/constants.js";
import { getValidEthTransfers, getValidTransactions } from "../utils/rpc.js";
import { getAverageMetrics } from "../utils/utils.js";
import {
FLUIDKEY_CONFIG,
FLUIDKEY_RELAYER_CONTRACT,
SHIELD_ERC20_EVENTS,
TRANSFER_ERC20_EVENTS,
TRANSFER_ETH_EVENTS,
USDC_ERC20_TOKEN_ADDRESS,
} from "./constants.js";
export class Fluidkey {
readonly name = FLUIDKEY_CONFIG.name;
readonly version = FLUIDKEY_CONFIG.version;
async benchmark(): Promise<IAggregatedMetrics> {
const [shieldEthReceipts, shieldErc20Receipts, transferEthReceipts, transferErc20Receipts] = await Promise.all([
this.benchmarkShieldETH(),
this.benchmarkShieldERC20(),
this.benchmarkTransferETH(),
this.benchmarkTransferERC20(),
]);
return {
shieldEth: getAverageMetrics(shieldEthReceipts),
shieldErc20: getAverageMetrics(shieldErc20Receipts),
transferEth: getAverageMetrics(transferEthReceipts),
transferErc20: getAverageMetrics(transferErc20Receipts),
anonymitySetSize: {
eth: shieldEthReceipts.length - transferEthReceipts.length,
...this.benchmarkAnonymitySetSizeERC20(shieldErc20Receipts, transferErc20Receipts),
},
};
}
private async benchmarkShieldETH(): Promise<TransactionReceipt[]> {
const receipts = await getValidEthTransfers({
chain: mainnet,
blockWindow: BLOCK_WINDOW_ETHEREUM_10_MINUTES, // TODO: fetch native ETH txs using block window
});
if (receipts.length < MIN_SAMPLES) {
throw new Error(`${this.name} shield ETH: receipts (${receipts.length}) < MIN_SAMPLES (${MIN_SAMPLES})`);
}
return receipts;
}
private async benchmarkShieldERC20(): Promise<TransactionReceipt[]> {
const receipts = await getValidTransactions({
contractAddress: USDC_ERC20_TOKEN_ADDRESS,
events: SHIELD_ERC20_EVENTS,
chain: mainnet,
blockWindow: BLOCK_WINDOW_ETHEREUM_10_MINUTES, // there are a lot of ERC20 transfers so use a small block window to rate limit
});
if (receipts.length < MIN_SAMPLES) {
throw new Error(`${this.name} shield ERC20: receipts (${receipts.length}) < MIN_SAMPLES (${MIN_SAMPLES})`);
}
return receipts;
}
private async benchmarkTransferETH(): Promise<TransactionReceipt[]> {
const receipts = await getValidTransactions({
contractAddress: FLUIDKEY_RELAYER_CONTRACT,
events: TRANSFER_ETH_EVENTS,
chain: mainnet,
blockWindow: BLOCK_WINDOW_ETHEREUM_5_WEEKS,
});
if (receipts.length < MIN_SAMPLES) {
throw new Error(`${this.name} transfer ETH: receipts (${receipts.length}) < MIN_SAMPLES (${MIN_SAMPLES})`);
}
return receipts;
}
private async benchmarkTransferERC20(): Promise<TransactionReceipt[]> {
const receipts = await getValidTransactions({
contractAddress: FLUIDKEY_RELAYER_CONTRACT,
events: TRANSFER_ERC20_EVENTS,
chain: mainnet,
});
if (receipts.length < MIN_SAMPLES) {
throw new Error(`${this.name} transfer ERC20: receipts (${receipts.length}) < MIN_SAMPLES (${MIN_SAMPLES})`);
}
return receipts;
}
private benchmarkAnonymitySetSizeERC20(
shieldReceipts: TransactionReceipt[],
transferReceipts: TransactionReceipt[],
): Record<string, bigint | number> {
const shieldMap = shieldReceipts.reduce((map, receipt) => {
const address = receipt.to;
if (address) {
const count = map.get(address) ?? 0;
map.set(address, count + 1);
}
return map;
}, new Map<string, number>());
const unshieldMap = transferReceipts.reduce((map, receipt) => {
const address = receipt.to;
if (address) {
const count = map.get(address) ?? 0;
map.set(address, count + 1);
}
return map;
}, new Map<string, number>());
return [...shieldMap.entries()].reduce<Record<string, bigint | number>>((acc, [address, count]) => {
acc[address] = count - (unshieldMap.get(address) ?? 0);
return acc;
}, {});
}
}