-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (77 loc) · 2.91 KB
/
index.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
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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const Web3 = require('web3')
const Token = require("./src/blockchain/abis/Token.json");
const service = require("./src/service");
const balanceRouter = require("./src/routes/balance.router");
const transactionRouter = require("./src/routes/transaction.router");
// const path = require("path");
const { tokenAddress, privateKey } = require("./src/config");
require('dotenv').config(/*{
path: path.resolve(__dirname, `${process.env.NODE_ENV}.env`)
}*/);
const pinataSDK = require('@pinata/sdk');
const pinata = pinataSDK(process.env.PINATA_API_KEY, process.env.PINATA_API_SECRET_KEY);
pinata.testAuthentication().then((result) => {
service.pinata = pinata;
console.log({...result, sucess: true});
}).catch((err) => {
//handle error here
console.log({...err, sucess: false});
});
const app = express();
var corsOptions = {
origin: "*"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
(async () => {
try{
const web3 = new Web3(new Web3.providers.HttpProvider('https://data-seed-prebsc-1-s1.binance.org:8545'));
// const web3 = new Web3(new Web3.providers.HttpProvider(`https://rinkeby.infura.io/v3/21e6bcc402cc4195aaffb188e72f758a`));
// const web3 = new Web3(new Web3.providers.HttpProvider(`https://rpc-mumbai.matic.today`));
// const web3 = new Web3(new Web3.providers.HttpProvider(`https://rpc-mainnet.matic.network`));
const account = web3.eth.accounts.privateKeyToAccount(privateKey)
service.web3 = web3;
service.account = account;
service.tokenContract = new web3.eth.Contract(Token.abi, tokenAddress);
service.pinata = pinata;
console.log(service.account.address)
} catch (evt) {
console.log(evt);
}
})();
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Origin', '*');
// another common pattern
// res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT');
res.setHeader(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
);
if (req.method === 'OPTIONS') {
res.status(200).end()
return;
}
// Pass to next layer of middleware
next();
});
app.get("/", (req, res) => {
return res.send("Welcome!");
});
app.get("/api/check", (req, res) => {
return res.send("Server is started!");
});
balanceRouter(app);
transactionRouter(app);
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});