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

Commit ee52103

Browse files
committed
Adds code for non parsed files
1 parent 638c932 commit ee52103

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

token-balances-from-alchemyWeb3.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
2+
3+
//Replace with your Alchemy API Key:
4+
const apiKey = "demo";
5+
6+
// Initialize an alchemy-web3 instance:
7+
const web3 = createAlchemyWeb3(
8+
`https://eth-mainnet.alchemyapi.io/v2/${apiKey}`,
9+
);
10+
11+
// Replace with the wallet address you want to query:
12+
const ownerAddress = "0x00000000219ab540356cbb839cbe05303d7705fa";
13+
/*
14+
Replace with the token contract address you want to query:
15+
The below address Corresponds to USDT
16+
*/
17+
const tokenContractAddresses = ["0xdAC17F958D2ee523a2206206994597C13D831ec7"];
18+
19+
/*
20+
** Fetching the token Balance with Alchemy's getTokenBalances API
21+
*/
22+
const data = await web3.alchemy.getTokenBalances( ownerAddress, tokenContractAddresses);
23+
console.log("Response Object for getTokenBalances\n", data)

token-balances-from-axios.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import axios from 'axios';
2+
3+
// Replace with your Alchemy API key:
4+
const apiKey = "demo";
5+
const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
6+
// Replace with the wallet address you want to query:
7+
const ownerAddr = "0x00000000219ab540356cbb839cbe05303d7705fa";
8+
9+
/*
10+
Replace with the token contract address you want to query:
11+
the below address Corresponds to the token Tether (USDT)
12+
*/
13+
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
14+
15+
/*
16+
** Fetching the token Balance with Alchemy's getTokenBalances API
17+
*/
18+
var dataParams = JSON.stringify({
19+
"jsonrpc": "2.0",
20+
"method": "alchemy_getTokenBalances",
21+
"params": [
22+
`${ownerAddr}`,
23+
[
24+
`${tokenAddr}`
25+
]
26+
],
27+
"id": 42
28+
});
29+
30+
var dataConfig = {
31+
method: 'post',
32+
url: baseURL,
33+
headers: {
34+
'Content-Type': 'application/json'
35+
},
36+
data : dataParams
37+
};
38+
39+
var data;
40+
41+
await axios(dataConfig)
42+
.then(function (response) {
43+
//This line converts the tokenBalance values from hex to decimal
44+
response.data["result"]["tokenBalances"][0]["tokenBalance"] = parseInt(response.data["result"]["tokenBalances"][0]["tokenBalance"], 16);
45+
data = response.data.result;
46+
console.log("Response Object for getTokenBalances\n", data)
47+
return true;
48+
})
49+
.catch(function (error) {
50+
console.log(error);
51+
});

token-balances-from-fetch.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import fetch from 'node-fetch';
2+
3+
// Replace with your Alchemy API key:
4+
const apiKey = "demo";
5+
const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
6+
7+
// Replace with the wallet address you want to query:
8+
const ownerAddr = "0x00000000219ab540356cbb839cbe05303d7705fa";
9+
/*
10+
Replace with the token contract address you want to query:
11+
The below address Corresponds to USDT
12+
*/
13+
const tokenAddr = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
14+
15+
var raw = JSON.stringify({
16+
"jsonrpc": "2.0",
17+
"method": "alchemy_getTokenBalances",
18+
"headers": {
19+
"Content-Type": "application/json"
20+
},
21+
"params": [
22+
`${ownerAddr}`,
23+
[
24+
`${tokenAddr}`,
25+
]
26+
],
27+
"id": 42
28+
});
29+
30+
var requestOptions = {
31+
method: 'POST',
32+
body: raw,
33+
redirect: 'follow'
34+
};
35+
36+
var data;
37+
38+
/*
39+
** Fetching the token Balance with Alchemy's getTokenBalances API
40+
*/
41+
await fetch(fetchURL, requestOptions)
42+
.then(response => response.json())
43+
.then(response => {
44+
//This line converts the tokenBalance values from hex to decimal
45+
response["result"]["tokenBalances"][0]["tokenBalance"] = parseInt(response["result"]["tokenBalances"][0]["tokenBalance"], 16);
46+
data = response.result;
47+
console.log("Response Object for getTokenBalances\n", data)
48+
})
49+
.catch(error => console.log('error', error));

0 commit comments

Comments
 (0)