-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (29 loc) · 1.18 KB
/
server.js
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
const express = require('express');
const dotenv = require('dotenv').config();
const ethers = require('ethers');
const app = express();
const PORT = 5000;
app.get('/totalsupply', async (req, res)=>{
// todo use ethers to connect to an infura rpc endpoint to query the ERC20 Token contract "0x60e683c6514edd5f758a55b6f393bebbafaa8d5e" totalSupply method
try {
const provider = new ethers.InfuraProvider('mainnet', process.env.INFURA_API_KEY);
const contractAddress = '0x60e683c6514edd5f758a55b6f393bebbafaa8d5e';
const abi = [
'function totalSupply() public view returns (uint256)'
];
const contract = new ethers.Contract(contractAddress, abi, provider);
const totalSupply = await contract.totalSupply();
// change the return type to json and return in the format { result: totalSupply }
res.json({ result: totalSupply.toString() });
} catch (error) {
console.log(error);
res.status(500).send('Internal Server Error');
}
});
app.listen(PORT, (error) =>{
if(!error)
console.log("Server is Successfully Running and App is listening on port "+ PORT);
else
console.log("Error occurred, server can't start", error);
}
);