This commit is contained in:
Sewmina 2024-08-28 22:28:33 +05:30
commit f5f7ffcdfb
4 changed files with 1818 additions and 0 deletions

130
.gitignore vendored Normal file
View File

@ -0,0 +1,130 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

68
index.js Normal file
View File

@ -0,0 +1,68 @@
const express = require('express');
const { ethers } = require('ethers');
const app = express();
app.use(express.json());
const PORT = process.env.PORT || 2015;
// Connect to an Ethereum provider
const provider = new ethers.JsonRpcProvider(process.env.INFURA_URL || 'https://mainnet.infura.io/v3/25c9f1810f234c278a4f13736a897836');
// ERC-20 token contract address
const tokenAddress = '0x22b6c31c2beb8f2d0d5373146eed41ab9ede3caf';
// ERC-20 token ABI (Application Binary Interface)
const tokenABI = [
// Only the part of the ABI we need: balanceOf and decimals
"function balanceOf(address owner) view returns (uint256)",
"function decimals() view returns (uint8)"
];
// Create a contract instance
const tokenContract = new ethers.Contract(tokenAddress, tokenABI, provider);
app.listen(PORT, () => {
console.log("started listening on localhost:" + PORT);
});
app.get("/status", (request, response) => {
const status = {
"Status": "Running"
};
response.send(status);
});
/* ------------------------------------------------------------------------------- */
app.get("/getBalance", async (request, response) => {
try {
const { address } = request.query;
if (!address) {
return response.status(400).json({ error: "Missing wallet address in the query parameters" });
}
// Check if the provided address is a valid Ethereum address
if (!ethers.isAddress(address)) {
return response.status(400).json({ error: "Invalid Ethereum address" });
}
// Get the token balance of the wallet address
const balanceRaw = await tokenContract.balanceOf(address);
// Get the number of decimals used by the token
const decimals = await tokenContract.decimals();
// Convert the balance to a human-readable format
const balance = ethers.formatUnits(balanceRaw, decimals);
response.json({ address, tokenAddress, balance});
} catch (error) {
console.error("Error fetching token balance:", error);
response.status(500).json({ error: "Failed to fetch token balance", trace: error });
}
});

1603
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "web3test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"ethers": "^6.13.2",
"express": "^4.19.2",
"web3": "^4.11.1"
}
}