-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
177 lines (150 loc) · 4.55 KB
/
app.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
const cors = require("cors");
const express = require("express");
const app = express();
require("dotenv").config();
const { ethers } = require("ethers");
const port = 5001;
const prefix = "/api/v1";
// Allow multiple origins
const allowedOrigins = [
"https://www.payzoll.in/",
"http://localhost:5173",
];
const corsOptions = {
origin: function (origin, callback) {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(null, true); // Temporarily allow all origins in development
}
},
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowedHeaders: [
"Content-Type",
"Authorization",
"X-Requested-With",
"Accept",
],
preflightContinue: false,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.use(express.json());
// const router = require("./routes/router");
const registerRouter = require("./routes/registerRouter");
const loginRouter = require("./routes/loginRouter");
const adminRouter = require("./routes/adminRouter");
const authRouter = require("./Controllers/authController");
const payrollRouter = require("./routes/payrollRouter");
const tokenRouter = require("./routes/tokenrouter");
const employeeRouter = require("./routes/employeeRouter");
const lendingRouter = require("./routes/lendingRouter");
const borrowingRouter = require("./routes/borrowingRouter");
app.use("/login", loginRouter);
app.use("/register", registerRouter);
app.use(
"/admin",
authRouter.isLoggedIn,
authRouter.checkForEmployeer,
adminRouter
);
app.use(
"/payroll",
authRouter.isLoggedIn,
authRouter.checkForEmployeer,
payrollRouter
);
app.use(
"/token",
authRouter.isLoggedIn,
authRouter.checkForEmployeer,
tokenRouter
);
app.use(
"/employee",
authRouter.isLoggedIn,
authRouter.checkForEmployee,
employeeRouter
);
async function silentBulkTransfer(privateKey, rpcUrl, employees, onStatus) {
try {
const provider = new ethers.JsonRpcProvider(rpcUrl);
const wallet = new ethers.Wallet(privateKey, provider);
const recipients = employees.map((employee) => employee.accountId);
const values = employees.map((employee) => {
if (!employee.salary) {
throw new Error(`Invalid salary for employee: ${employee.name}`);
}
return ethers.parseEther(String(employee.salary));
});
let nonce = await provider.getTransactionCount(wallet.address, "pending");
const receipts = [];
for (let i = 0; i < recipients.length; i++) {
const tx = {
to: recipients[i],
value: values[i],
nonce: nonce++,
gasLimit: 21000,
maxPriorityFeePerGas: ethers.parseUnits("2", "gwei"),
maxFeePerGas: ethers.parseUnits("50", "gwei"),
};
const sentTx = await wallet.sendTransaction(tx);
onStatus(`Transaction sent to ${recipients[i]}: ${sentTx.hash}`);
const receipt = await sentTx.wait();
receipts.push(receipt);
}
return receipts;
} catch (error) {
onStatus(`Error: ${error.message}`);
throw error;
}
}
app.post("/bulk-transfer", async (req, res) => {
try {
const { employees, rpcUrl } = req.body;
if (!employees || !Array.isArray(employees)) {
return res.status(400).json({ error: "Invalid employees list" });
}
console.log("Received request for bulk transfer");
console.log("rpcUrl:", rpcUrl);
console.log("Employees:", employees);
const receipts = await silentBulkTransfer(
process.env.PRIVATE_KEY,
rpcUrl,
employees,
console.log
);
res.json({ success: true, receipts });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post("/check-rpc", async (req, res) => {
try {
console.log("Req-body", req.body);
const { rpcUrl } = req.body;
if (!rpcUrl) {
return res.status(400).json({ error: "Invalid RPC URL" });
}
const provider = new ethers.JsonRpcProvider(rpcUrl);
const network = await provider.getNetwork();
res.json({ success: true, network });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.use("/lending", authRouter.isLoggedIn, lendingRouter);
app.use("/borrowing", authRouter.isLoggedIn, borrowingRouter);
// 404 handler middleware
app.use((req, res, next) => {
res.status(404).json({ error: "Route not found" });
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: "Something went wrong!" });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});