-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
71 lines (59 loc) · 2.09 KB
/
server.ts
File metadata and controls
71 lines (59 loc) · 2.09 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
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
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// Mock External Crop Data for Oracle
const externalCropData = {
"BATCH_A1": { status: "Verified", quality: "Grade A", yield: 5000 },
"BATCH_B2": { status: "Growing", quality: "Pending", yield: 2000 },
};
// Oracle Adapter API Endpoints
app.get("/api/oracle/verify/:batchId", (req, res) => {
const { batchId } = req.params;
const data = externalCropData[batchId as keyof typeof externalCropData];
if (data) {
res.json({
success: true,
data,
message: `Oracle verification for ${batchId} retrieved from satellite data.`
});
} else {
res.status(404).json({ success: false, message: "Batch not found in external registry." });
}
});
// Simulated trigger to on-chain release
app.post("/api/oracle/trigger-payment", (req, res) => {
const { batchId, milestoneIdx } = req.body;
// In a real scenario, this is where you'd sign a Soroban transaction
// using a private key stored safely in environment variables.
console.log(`ORACLE: Triggering on-chain payment for ${batchId}, milestone ${milestoneIdx}`);
res.json({
success: true,
txId: `soroban_tx_mock_${Math.random().toString(36).substring(7)}`,
status: "Triggered"
});
});
app.get("/api/health", (req, res) => {
res.json({ status: "ok", service: "AgroChain Oracle Adapter" });
});
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`AgroChain Server running on http://localhost:${PORT}`);
});
}
startServer();