Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 220c3a6

Browse files
Aniket GuptaAniket Gupta
authored andcommitted
Adds parsing to all three libraries
1 parent 09989ff commit 220c3a6

File tree

3 files changed

+128
-22
lines changed

3 files changed

+128
-22
lines changed

token-balances-from-alchemyWeb3.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
22

3-
//Replace with your API Key
3+
//Replace with your Alchemy API Key:
44
const apiKey = "demo";
55

66
// Initialize an alchemy-web3 instance:
77
const web3 = createAlchemyWeb3(
88
`https://eth-mainnet.alchemyapi.io/v2/${apiKey}`,
99
);
1010

11-
//Feel free to switch this wallet address with another address
11+
// Replace with the wallet address you want to query:
1212
const ownerAddress = "0x00000000219ab540356cbb839cbe05303d7705fa";
13-
14-
//The below token contract address corresponds to USDT
13+
/*
14+
Replace with the token contract address you want to query:
15+
The below address Corresponds to USDT
16+
*/
1517
const tokenContractAddresses = ["0xdAC17F958D2ee523a2206206994597C13D831ec7"];
1618

19+
/*
20+
** Fetching the token Balance with Alchemy's getTokenBalances API
21+
*/
1722
const data = await web3.alchemy.getTokenBalances( ownerAddress, tokenContractAddresses);
18-
19-
console.log("Token balance for Address");
20-
console.log(data);
23+
console.log("Response Object for getTokenBalances\n", data)
24+
25+
/*
26+
** Fetching the metadata for the token with Alchemy's getTokenMetadata API
27+
*/
28+
const metadata = await web3.alchemy.getTokenMetadata( tokenContractAddresses[0]);
29+
30+
//Forming the name of the token that comprises of the Name and the Symbol of the token
31+
const tokenName = metadata.name + "(" + metadata.symbol + ")";
32+
33+
/* Calculating the tokenBalance in USD. The "decimals" field in the token metadata on line 21 tells us
34+
how many digits at the end of the tokenBalance in Line 17 are to the right of the decimal.
35+
so we divide the Full tokenBalance with 10 to the power of the decimal value of the token
36+
*/
37+
const tokenBalance = "$"+ data["tokenBalances"][0]["tokenBalance"]/Math.pow(10, metadata.decimals)
38+
console.log("Token balance for", tokenName, "is", tokenBalance);

token-balances-from-axios.js

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ import axios from 'axios';
22

33
// Replace with your Alchemy API key:
44
const apiKey = "demo";
5-
65
const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
7-
86
// Replace with the wallet address you want to query:
97
const ownerAddr = "0x00000000219ab540356cbb839cbe05303d7705fa";
108

11-
// Replace with the token contract address you want to query:
9+
/*
10+
Replace with the token contract address you want to query:
11+
the below address Corresponds to the token Tether (USDT)
12+
*/
1213
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
1314

14-
var data = JSON.stringify({
15+
/*
16+
** Fetching the token Balance with Alchemy's getTokenBalances API
17+
*/
18+
var dataParams = JSON.stringify({
1519
"jsonrpc": "2.0",
1620
"method": "alchemy_getTokenBalances",
1721
"params": [
@@ -23,21 +27,61 @@ var data = JSON.stringify({
2327
"id": 42
2428
});
2529

26-
var config = {
30+
var dataConfig = {
2731
method: 'post',
2832
url: baseURL,
2933
headers: {
3034
'Content-Type': 'application/json'
3135
},
32-
data : data
36+
data : dataParams
3337
};
3438

35-
axios(config)
39+
var data, metadata;
40+
41+
await axios(dataConfig)
3642
.then(function (response) {
3743
//This line converts the tokenBalance values from hex to decimal
3844
response.data["result"]["tokenBalances"][0]["tokenBalance"] = parseInt(response.data["result"]["tokenBalances"][0]["tokenBalance"], 16);
39-
console.log("Token balance for address\n", JSON.stringify(response.data.result, null, 2))
45+
data = response.data.result;
46+
console.log("Response Object for getTokenBalances\n", data)
47+
return true;
4048
})
4149
.catch(function (error) {
4250
console.log(error);
43-
});
51+
});
52+
53+
/*
54+
** Fetching the metadata for the token with Alchemy's getTokenMetadata API
55+
*/
56+
var metadataParams = JSON.stringify({
57+
"jsonrpc": "2.0",
58+
"method": "alchemy_getTokenMetadata",
59+
"params": [
60+
`${tokenAddr}`
61+
],
62+
"id": 42
63+
});
64+
65+
var metadataConfig = {
66+
method: 'post',
67+
url: baseURL,
68+
headers: {
69+
'Content-Type': 'application/json'
70+
},
71+
data : metadataParams
72+
};
73+
74+
axios(metadataConfig)
75+
.then(function (response) {
76+
metadata = response.data.result;
77+
78+
//Forming the name of the token that comprises of the Name and the Symbol of the token
79+
const tokenName = metadata.name + "(" + metadata.symbol + ")";
80+
81+
/* Calculating the tokenBalance in USD. The "decimals" field in the token metadata on line 21 tells us
82+
how many digits at the end of the tokenBalance in Line 17 are to the right of the decimal.
83+
so we divide the Full tokenBalance with 10 to the power of the decimal value of the token
84+
*/
85+
const tokenBalance = "$"+ data["tokenBalances"][0]["tokenBalance"]/Math.pow(10, metadata.decimals)
86+
console.log("Token balance for", tokenName, "is", tokenBalance);
87+
})

token-balances-from-fetch.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import fetch from 'node-fetch';
22

33
// Replace with your Alchemy API key:
44
const apiKey = "demo";
5-
65
const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
76

87
// Replace with the wallet address you want to query:
98
const ownerAddr = "0x00000000219ab540356cbb839cbe05303d7705fa";
10-
11-
// Replace with the token contract address you want to query:
9+
/*
10+
Replace with the token contract address you want to query:
11+
The below address Corresponds to USDT
12+
*/
1213
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
1314

1415
var raw = JSON.stringify({
@@ -32,12 +33,55 @@ var requestOptions = {
3233
redirect: 'follow'
3334
};
3435

35-
// Make the request and print the formatted response:
36-
fetch(fetchURL, requestOptions)
36+
var data, metadata;
37+
38+
/*
39+
** Fetching the token Balance with Alchemy's getTokenBalances API
40+
*/
41+
await fetch(fetchURL, requestOptions)
3742
.then(response => response.json())
3843
.then(response => {
3944
//This line converts the tokenBalance values from hex to decimal
4045
response["result"]["tokenBalances"][0]["tokenBalance"] = parseInt(response["result"]["tokenBalances"][0]["tokenBalance"], 16);
41-
console.log("Token balance for address\n", JSON.stringify(response.result, null, 2))
46+
data = response.result;
47+
console.log("Response Object for getTokenBalances\n", data)
4248
})
4349
.catch(error => console.log('error', error));
50+
51+
var metadataRaw = JSON.stringify({
52+
"jsonrpc": "2.0",
53+
"method": "alchemy_getTokenMetadata",
54+
"headers": {
55+
"Content-Type": "application/json"
56+
},
57+
"params": [
58+
`${tokenAddr}`
59+
],
60+
"id": 42
61+
});
62+
63+
var metadataRequestOptions = {
64+
method: 'POST',
65+
body: metadataRaw,
66+
redirect: 'follow'
67+
};
68+
69+
/*
70+
** Fetching the metadata for the token with Alchemy's getTokenMetadata API
71+
*/
72+
fetch(fetchURL, metadataRequestOptions)
73+
.then(response => response.json())
74+
.then(response => {
75+
metadata = response.result;
76+
77+
//Forming the name of the token that comprises of the Name and the Symbol of the token
78+
const tokenName = metadata.name + "(" + metadata.symbol + ")";
79+
80+
/* Calculating the tokenBalance in USD. The "decimals" field in the token metadata on line 21 tells us
81+
how many digits at the end of the tokenBalance in Line 17 are to the right of the decimal.
82+
so we divide the Full tokenBalance with 10 to the power of the decimal value of the token
83+
*/
84+
const tokenBalance = "$"+ data["tokenBalances"][0]["tokenBalance"]/Math.pow(10, metadata.decimals)
85+
console.log("Token balance for", tokenName, "is", tokenBalance);
86+
})
87+
.catch(error => console.log('error', error));

0 commit comments

Comments
 (0)