-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalance.js
More file actions
126 lines (110 loc) · 3.09 KB
/
balance.js
File metadata and controls
126 lines (110 loc) · 3.09 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
const { ethers } = require("ethers");
const axios = require("axios");
// Token contract addresses on Ethereum mainnet
const WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const BETH_ADDRESS = "0x0000000000A39bb272e79075ade125fd351887Ac"; // Blur ETH
// NFTTools configuration
const API_ENDPOINT = "https://nfttools.pro";
const API_KEY = "a4eae399-f135-4627-829a-18435bb631ae";
// Minimal ERC20 ABI for balanceOf
const ERC20_ABI = [
"function balanceOf(address owner) view returns (uint256)",
"function decimals() view returns (uint8)",
"function symbol() view returns (string)",
];
async function makeRequest(address) {
// Common headers for all requests
const headers = {
url: "https://ethereum.publicnode.com",
"x-nft-api-key": API_KEY,
"Content-Type": "application/json",
};
// ETH Balance Request
const ethBalanceRequest = {
jsonrpc: "2.0",
method: "eth_getBalance",
params: [address, "latest"],
id: 1,
};
// WETH Balance Request
const wethData = ethers.Interface.from(ERC20_ABI).encodeFunctionData(
"balanceOf",
[address]
);
const wethBalanceRequest = {
jsonrpc: "2.0",
method: "eth_call",
params: [
{
to: WETH_ADDRESS,
data: wethData,
},
"latest",
],
id: 2,
};
// BETH Balance Request
const bethBalanceRequest = {
jsonrpc: "2.0",
method: "eth_call",
params: [
{
to: BETH_ADDRESS,
data: wethData,
},
"latest",
],
id: 3,
};
try {
const responses = await Promise.all([
axios.post(API_ENDPOINT, ethBalanceRequest, { headers }),
axios.post(API_ENDPOINT, wethBalanceRequest, { headers }),
axios.post(API_ENDPOINT, bethBalanceRequest, { headers }),
]);
return {
eth: responses[0].data.result,
weth: responses[1].data.result,
beth: responses[2].data.result,
};
} catch (error) {
console.error("Request error:", error.message);
if (error.response) {
console.error("Response data:", error.response.data);
console.error("Response status:", error.response.status);
}
throw error;
}
}
async function getBalances(walletAddress) {
try {
if (!ethers.isAddress(walletAddress)) {
throw new Error("Invalid Ethereum address");
}
const balances = await makeRequest(walletAddress);
// Convert balances from hex to readable format
const ethBalance = ethers.formatEther(balances.eth);
const wethBalance = ethers.formatEther(balances.weth);
const bethBalance = ethers.formatEther(balances.beth);
// Display balances
console.log(`\nETH Balance: ${ethBalance} ETH`);
console.log(`WETH Balance: ${wethBalance} WETH`);
console.log(`Blur ETH Balance: ${bethBalance} BETH`);
// Calculate total
const totalInEth =
parseFloat(ethBalance) +
parseFloat(wethBalance) +
parseFloat(bethBalance);
console.log(`\nTotal ETH Value: ${totalInEth.toFixed(4)} ETH`);
} catch (error) {
console.error("Error:", error.message);
}
}
// Check if wallet address is provided as command line argument
const walletAddress = process.argv[2];
if (!walletAddress) {
console.log("Please provide a wallet address as argument:");
console.log("node script.js 0x...");
} else {
getBalances(walletAddress);
}