-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
204 lines (179 loc) · 5.67 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const express = require("express");
const cors = require("cors");
const sequelise = require("./config/db");
const memberRouter = require("./routes/member");
const validatorRouter = require("./routes/validator");
// const swaggerJSDoc = require('swagger-jsdoc');
// const swaggerUi = require('swagger-ui-express');
const Web3 = require("web3");
const fs = require("fs");
const artifactJson = "build/contracts/CarbonCreditToken.json";
const artifact = JSON.parse(fs.readFileSync(artifactJson));
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var CarbonCreditToken;
var accounts;
var networkId;
var contractAddress;
const initModels = require("./models/init-models");
const models = initModels(sequelise);
const {Sequelize} = require("sequelize");
const statuses = require("./utils/statuses");
const PORT = process.env.PORT || 3001;
const HOST = "0.0.0.0";
// const swaggerDefinition = {
// openapi: '3.0.0',
// info: {
// title: 'Carbon Credit API',
// version: '1.0.0',
// description: 'Carbon Credit Market place',
// license: {
// name: 'Licensed Under MIT',
// url: '',
// },
// contact: {
// name: 'Carbon Credit',
// url: '',
// },
// },
// servers: [
// {
// url: 'http://localhost:3001',
// description: 'dev',
// },
// ],
// };
//
// const swaggerOptions = {
// swaggerDefinition,
// apis: ['./routes/*.js'],
// };
//
// const swaggerSpecs = swaggerJSDoc(swaggerOptions);
const app = express();
app.use(express.static("public"));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cors());
app.get("/", (req, res) => {
res.send("Welcome the API is running");
});
// app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
app.use("/member", memberRouter);
app.use("/validator", validatorRouter);
app.get("/api/validator", async (req, res) => {
const balance = await CarbonCreditToken.methods.balanceOf(accounts[0]).call();
const total = await CarbonCreditToken.methods.totalSupply().call();
res.send({
totalsupply: total / 10 ** 18,
balance: balance / 10 ** 18,
wallet: accounts[0],
});
});
app.get("/api/totalsupply", async (req, res) => {
try {
const val = await CarbonCreditToken.methods.totalSupply().call();
res.send({balance: val / 10 ** 18});
} catch (e) {
console.log(e.message)
}
});
app.get("/api/balance", async (req, res) => {
try {
const val = await CarbonCreditToken.methods
.balanceOf(req.body.address)
.call();
res.send({balance: val / 10 ** 18});
} catch (e) {
console.log(e.message)
}
});
app.post("/api/myaccount", async (req, res) => {
try {
const member = await models.RegisteredMembers.findOne({
where: {pk: req.body.memberid},
});
const val = await CarbonCreditToken.methods
.balanceOf(member.walletaddress)
.call();
res.send({
balance: val / 10 ** 18,
membertype: member.membertype,
projectid: member.projectid,
taxid: member.taxid
});
} catch (e) {
res.status(400).send({error: e, message: "Unexpected error occurred 😤"});
}
});
app.post("/api/transfer", async (req, res) => {
try {
// console.log(req.body.address);
const gasPrice = await web3.eth.getGasPrice();
const tokenTransferResult = await CarbonCreditToken.methods
.transfer(
req.body.walletaddress,
web3.utils.toWei(req.body.amount.toString(), "ETHER")
)
.send({
from: accounts[0],
gasPrice,
});
// console.log(req.body.memberid);
await models.CreditRequests.update(
{
status: statuses.APPROVED,
},
{
where: {
memberid: req.body.memberid,
pk: req.body.code
},
}
);
res.status(200).json("done");
} catch (e) {
console.log(e)
res.status(400).json({message: e});
}
});
app.post("/api/mint", async (req, res) => {
try {
await CarbonCreditToken.methods
.mint(BigInt(req.body.amount * 10 ** 18))
.send({from: accounts[0]});
const val = await CarbonCreditToken.methods.totalSupply().call();
res.send({balance: val});
} catch (e) {
console.log(e.message)
res.status(400).json({message: " "})
}
});
sequelise
.authenticate()
.then(async () => {
console.log("Database connected...");
accounts = await web3.eth.getAccounts();
networkId = await web3.eth.net.getId();
contractAddress = artifact.networks[networkId].address;
// console.log(`address ${contractAddress}`);
CarbonCreditToken = new web3.eth.Contract(artifact.abi, contractAddress, {
from: accounts[0],
});
})
.catch((err) => {
console.log("Error connecting to database: " + err);
});
sequelise
.sync()
.then(() => {
app.listen(PORT, HOST);
console.log(`App running on http://${HOST}:${PORT}`);
})
.catch((err) => console.log("Error synching models: " + err));
module.exports = app;
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });