-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtradeserver.js
More file actions
34 lines (29 loc) · 1.13 KB
/
tradeserver.js
File metadata and controls
34 lines (29 loc) · 1.13 KB
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
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const Web3 = require("web3");
const fs = require("fs");
const cors = require("cors");
dotenv.config();
app.use(cors());
app.use(express.json());
app.use(express.static(__dirname)); // Serve HTML
const web3 = new Web3(new Web3.providers.HttpProvider(process.env.INFURA_URL));
const contractABI = JSON.parse(fs.readFileSync("./TradeContractABI.json"));
const contract = new web3.eth.Contract(contractABI, process.env.CONTRACT_ADDRESS);
const privateKey = process.env.PRIVATE_KEY;
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
app.post("/mint", async (req, res) => {
const { to, amount } = req.body;
try {
const tx = contract.methods.mint(to, amount);
const gas = await tx.estimateGas({ from: account.address });
const result = await tx.send({ from: account.address, gas });
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));