Repository navigation
Expand file tree
/
Copy pathcheck-balances.js
More file actions
159 lines (142 loc) · 4.82 KB
/
Copy pathcheck-balances.js
File metadata and controls
159 lines (142 loc) · 4.82 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
/**
* FOC DevNet Balance Checker
*
* This example demonstrates how to use ethers.js to check balances
* of user accounts on the DevNet using the exported devnet-info.json.
*
* Usage:
* node check-balances.js [path-to-devnet-info.json]
*
* If no path is provided, defaults to ~/.foc-devnet/state/latest/devnet-info.json
*/
import { readFileSync, existsSync } from "fs";
import { homedir } from "os";
import { join } from "path";
import { createPublicClient, http, parseAbi } from "viem";
import { formatUnits } from "viem";
// ERC20 ABI (minimal for balanceOf)
const ERC20_ABI = parseAbi([
"function balanceOf(address owner) view returns (uint256)",
"function decimals() view returns (uint8)",
"function symbol() view returns (string)",
]);
/**
* Load the devnet-info.json file.
* @param {string} filePath - Path to the devnet-info.json file
* @returns {object} The parsed DevNet info
*/
function loadDevnetInfo(filePath) {
if (!existsSync(filePath)) {
throw new Error(`DevNet info file not found: ${filePath}`);
}
const content = readFileSync(filePath, "utf8");
return JSON.parse(content);
}
/**
* Format wei to ether with specified decimals.
* @param {bigint} wei - Amount in wei
* @param {number} decimals - Decimal places
* @returns {string} Formatted amount
*/
function formatBalance(wei, decimals = 18) {
return formatUnits(wei, decimals);
}
/**
* Check native FIL balance for an address.
* @param {object} client - Viem public client
* @param {string} address - Address to check
* @returns {Promise<string>} Balance in FIL
*/
async function checkNativeBalance(client, address) {
const balance = await client.getBalance({ address });
return formatBalance(balance);
}
/**
* Check ERC20 token balance for an address.
* @param {object} client - Viem public client
* @param {string} tokenAddress - Token contract address
* @param {string} userAddress - User address to check
* @returns {Promise<{balance: string, symbol: string}>} Balance and symbol
*/
async function checkTokenBalance(client, tokenAddress, userAddress) {
const [balance, decimals, symbol] = await Promise.all([
client.readContract({
address: tokenAddress,
abi: ERC20_ABI,
functionName: "balanceOf",
args: [userAddress],
}),
client.readContract({
address: tokenAddress,
abi: ERC20_ABI,
functionName: "decimals",
}),
client.readContract({
address: tokenAddress,
abi: ERC20_ABI,
functionName: "symbol",
}),
]);
return {
balance: formatBalance(balance, decimals),
symbol,
};
}
// Main execution
async function main() {
// Determine file path
const defaultPath = join(
homedir(),
".foc-devnet",
"state",
"latest",
"devnet-info.json"
);
const filePath = process.argv[2] || defaultPath;
console.log(`Loading DevNet info from: ${filePath}\n`);
try {
const { info } = loadDevnetInfo(filePath);
// Connect to the Lotus RPC
const client = createPublicClient({
transport: http(info.lotus.host_rpc_url),
});
console.log(`Connected to: ${info.lotus.host_rpc_url}`);
// Check if network is accessible
const blockNumber = await client.getBlockNumber();
console.log(`Current block: ${blockNumber}\n`);
console.log("═══════════════════════════════════════════════════════════");
console.log(" Account Balances");
console.log("═══════════════════════════════════════════════════════════\n");
// Check balances for all users
for (const user of info.users) {
console.log(`${user.name} (${user.evm_addr}):`);
// Check native FIL balance
const filBalance = await checkNativeBalance(client, user.evm_addr);
console.log(` Native FIL: ${filBalance} tFIL`);
// Check MockUSDFC balance
if (info.contracts.mockusdfc_addr) {
try {
const { balance, symbol } = await checkTokenBalance(
client,
info.contracts.mockusdfc_addr,
user.evm_addr
);
console.log(` ${symbol}: ${balance}`);
} catch (e) {
console.log(` MockUSDFC: Error - ${e.message}`);
}
}
console.log();
}
console.log("═══════════════════════════════════════════════════════════\n");
} catch (error) {
console.error(`Error: ${error.message}`);
if (error.code === "ECONNREFUSED") {
console.error(
"Could not connect to the DevNet. Make sure foc-devnet is running."
);
}
process.exit(1);
}
}
main();