encifher-health 1.0.0

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 (2) hide show
  1. package/index.js +178 -0
  2. package/package.json +15 -0
package/index.js ADDED
@@ -0,0 +1,178 @@
1
+ const fs = require('fs');
2
+ const { ethers } = require('ethers');
3
+ const { TEEClient, PlaintextType } = require('@encifher-js/core');
4
+
5
+ const defaultConfig = {
6
+ network: "baseSepolia",
7
+ eusdc: "0x6Bc109e6187b98B279BbbdBDdc59021dc59A5058",
8
+ usdc: "0x7A8cb5C885597398F7745c0403094705a4AEb1Df",
9
+ eusdcWrapper: "0xC97100472255aB79b7858139056f2cbA37f67252",
10
+ orderManager: "0x2602931AF6848e7e657b2840c1E8507C112c6129",
11
+ faucet: "0x2381aa52cFB6eB752d72F4A611ec43CC718ed206",
12
+ customAddress: "0x42ed70a51d0f4ff03ac3085c3021fc79c332f5a0"
13
+ };
14
+ let config = defaultConfig;
15
+ const args = process.argv.slice(2);
16
+ let customAddressArg = null;
17
+ if (args.includes('--custom')) {
18
+ const customIndex = args.indexOf('--custom') + 1;
19
+ if (customIndex < args.length) {
20
+ customAddressArg = args[customIndex];
21
+ } else {
22
+ console.error('No custom address provided after --custom flag');
23
+ process.exit(1);
24
+ }
25
+ }
26
+ let configFilePath = args.find(arg => !arg.startsWith('--'));
27
+ if (configFilePath) {
28
+ if (fs.existsSync(configFilePath)) {
29
+ try {
30
+ const configFile = fs.readFileSync(configFilePath, 'utf-8');
31
+ config = JSON.parse(configFile);
32
+ } catch (error) {
33
+ console.error('Error reading or parsing config file:', error);
34
+ process.exit(1);
35
+ }
36
+ } else {
37
+ console.error(`Config file not found at: ${configFilePath}. Using default config.`);
38
+ }
39
+ }
40
+ if (customAddressArg) {
41
+ config.customAddress = customAddressArg;
42
+ }
43
+ const { eusdc, usdc, eusdcWrapper, orderManager, faucet, customAddress } = config;
44
+ const networks = {
45
+ baseSepolia: 'https://sepolia.base.org',
46
+ }
47
+ const network = networks[config.network];
48
+
49
+ class ColoredLogger {
50
+ static colorText(text, colorCode) {
51
+ const colors = {
52
+ reset: '\x1b[0m',
53
+ red: '\x1b[31m',
54
+ green: '\x1b[32m',
55
+ yellow: '\x1b[33m',
56
+ cyan: '\x1b[36m',
57
+ orange: '\x1b[38;5;196m',
58
+ };
59
+ const color = colors[colorCode] || colors.reset;
60
+ return color + text + colors.reset;
61
+ }
62
+
63
+ static log(message, color) {
64
+ const coloredMessage = this.colorText(message, color);
65
+ console.log(coloredMessage);
66
+ }
67
+ }
68
+
69
+ async function requestDecrypt(handle) {
70
+ try {
71
+ const response = await fetch('https://decrypt.rpc.encifher.io/decrypt', {
72
+ method: 'POST',
73
+ headers: {
74
+ 'Content-Type': 'application/json'
75
+ },
76
+ body: JSON.stringify({ handle: BigInt(handle).toString() })
77
+ });
78
+ const result = await response.json();
79
+ return result;
80
+ } catch (error) {
81
+ return "error";
82
+ }
83
+ }
84
+
85
+ async function logBalance(address, onlyToken = true) {
86
+ if (!address) {
87
+ ColoredLogger.log("no address given. Skipping...", 'red')
88
+ return;
89
+ }
90
+ console.log('Address:', address)
91
+ const provider = new ethers.JsonRpcProvider(network);
92
+ try {
93
+ if (!onlyToken) {
94
+ const balanceWei = await provider.getBalance(address);
95
+ const balanceEther = (BigInt(balanceWei).toString() / "1e18").toString();
96
+ ColoredLogger.log(`ETH Balance: ${balanceEther} ETH`, 'orange');
97
+ }
98
+ const eusdcContract = new ethers.Contract(
99
+ eusdc,
100
+ [
101
+ "function balanceOf(address) view returns (uint256)"
102
+ ],
103
+ provider
104
+ );
105
+ const balanceEusdc = await eusdcContract.balanceOf(address);
106
+ ColoredLogger.log(`eUSDC balance handle: ${BigInt(balanceEusdc).toString()}`, 'cyan');
107
+ const decrypted = await requestDecrypt(balanceEusdc);
108
+ if (decrypted === "error") {
109
+ ColoredLogger.log(`Decrypted balance: error`, 'red');
110
+ return;
111
+ }
112
+ const formatted = (decrypted?.toString() / "1e6");
113
+ ColoredLogger.log(`Decrypted balance: ${formatted} eusdc`, 'green');
114
+ } catch (error) {
115
+ console.error("Error fetching balance:", error);
116
+ }
117
+ }
118
+
119
+ async function logUsdcBalance(address) {
120
+ if (!address) {
121
+ ColoredLogger.log("no address given. Skipping...", 'red')
122
+ return;
123
+ }
124
+ console.log('Address:', address)
125
+ const provider = new ethers.JsonRpcProvider(network);
126
+ try {
127
+ const usdcContract = new ethers.Contract(
128
+ usdc,
129
+ [
130
+ "function balanceOf(address) view returns (uint256)"
131
+ ],
132
+ provider
133
+ );
134
+ const balanceUsdc = await usdcContract.balanceOf(address);
135
+ const formatted = (balanceUsdc?.toString() / "1e6");
136
+ ColoredLogger.log(`Decrypted balance: ${formatted} usdc`, 'green');
137
+ } catch (error) {
138
+ console.error("Error fetching balance:", error);
139
+ }
140
+ }
141
+
142
+ async function pingPong() {
143
+ const client = new TEEClient();
144
+ const handle = await client.encrypt(1, PlaintextType.uint32)
145
+ if (!handle) {
146
+ ColoredLogger.log("Encryption server down", 'red');
147
+ return;
148
+ }
149
+ const decrypted = await requestDecrypt('0x' + Buffer.from(handle).toString('hex'));
150
+ if (decrypted === "error") {
151
+ ColoredLogger.log("Decryption server down", 'red');
152
+ return;
153
+ }
154
+ ColoredLogger.log(`Encryption server is healthy`, 'green');
155
+ ColoredLogger.log(`Decryption server is healthy`, 'green');
156
+ }
157
+
158
+ async function main() {
159
+ try {
160
+ console.log('\n------------------------------------');
161
+ ColoredLogger.log('Faucet health', 'yellow')
162
+ await logBalance(faucet, false);
163
+ ColoredLogger.log('\nOrder manager health', 'yellow')
164
+ await logBalance(orderManager);
165
+ ColoredLogger.log('\nCustom address health', 'yellow')
166
+ await logBalance(customAddress, false);
167
+ ColoredLogger.log('\nEUSDC wrapper health', 'yellow')
168
+ await logUsdcBalance(eusdcWrapper);
169
+ ColoredLogger.log('\nCoprocessor health', 'yellow')
170
+ await pingPong();
171
+ console.log('------------------------------------');
172
+ console.log('\n');
173
+ } catch (error) {
174
+ console.error(error);
175
+ }
176
+ }
177
+
178
+ main();
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "encifher-health",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "start": "node index.js"
7
+ },
8
+ "author": "human",
9
+ "license": "ISC",
10
+ "description": "",
11
+ "dependencies": {
12
+ "@encifher-js/core": "^1.1.1",
13
+ "ethers": "^6.13.4"
14
+ }
15
+ }