forked from hari-hara-sudharsan/Blockchain-TokenCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (127 loc) · 3.81 KB
/
Copy pathindex.js
File metadata and controls
151 lines (127 loc) · 3.81 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
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
// ------------------------
// SAFE-MINT INDEXER PHASE 2
// Full upgrade: caching, safe writeDB, robust routes, referrals
// ------------------------
import fs from "fs";
import path from "path";
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
const app = express();
app.use(express.json({ limit: "2mb" }));
app.use(cors());
// ----------------------------
// DB FILE (Windows safe path)
// ----------------------------
const DB_PATH = path.join(process.cwd(), "db.json");
function readDB() {
try {
if (!fs.existsSync(DB_PATH)) {
return { tokens: [] };
}
const raw = fs.readFileSync(DB_PATH, "utf8");
return JSON.parse(raw || "{}");
} catch (err) {
console.error("DB READ ERROR:", err);
return { tokens: [] };
}
}
function writeDB(data) {
try {
fs.writeFileSync(DB_PATH, JSON.stringify(data, null, 2), {
encoding: "utf-8",
flag: "w"
});
} catch (err) {
console.error("DB WRITE ERROR:", err);
}
}
// ----------------------------
// In-memory cache for tokens
// ----------------------------
let tokensCache = null;
let tokensCacheTs = 0;
function getCachedTokens() {
if (tokensCache && (Date.now() - tokensCacheTs < 5000)) {
return tokensCache;
}
const db = readDB();
tokensCache = db.tokens || [];
tokensCacheTs = Date.now();
return tokensCache;
}
// ----------------------------
// POST /tokens → Save or update token
// ----------------------------
app.post("/tokens", (req, res) => {
try {
const token = req.body;
if (!token || !token.tokenAddress) {
return res.status(400).json({ error: "missing tokenAddress" });
}
const db = readDB();
if (!db.tokens) db.tokens = [];
const addr = token.tokenAddress.toLowerCase();
let existing = db.tokens.find(t => (t.tokenAddress || "").toLowerCase() === addr);
if (!existing) {
db.tokens.push(token);
writeDB(db);
tokensCache = null; // reset cache
console.log("Saved token:", token.tokenAddress);
return res.json({ ok: true, saved: token.tokenAddress });
} else {
Object.assign(existing, token);
writeDB(db);
tokensCache = null;
return res.json({ ok: true, updated: token.tokenAddress });
}
} catch (err) {
console.error("POST /tokens failed:", err);
res.status(500).json({ error: "Save failed" });
}
});
// ----------------------------
// GET /tokens → return cached list
// ----------------------------
app.get("/tokens", (req, res) => {
return res.json(getCachedTokens());
});
// ----------------------------
// GET /token/:address → single token lookup
// ----------------------------
app.get("/token/:address", (req, res) => {
const addr = req.params.address.toLowerCase();
const tokens = getCachedTokens();
const token = tokens.find(t => (t.tokenAddress || "").toLowerCase() === addr);
if (!token) return res.status(404).json({ error: "Token not found" });
res.json(token);
});
// ----------------------------
// REFERRAL ENDPOINT (Phase 2)
// ----------------------------
app.post("/refer/:token", (req, res) => {
try {
const addr = req.params.token.toLowerCase();
const db = readDB();
const token = db.tokens.find(t => (t.tokenAddress || "").toLowerCase() === addr);
if (!token) {
return res.status(404).json({ error: "Token not found" });
}
token.referrals = (token.referrals || 0) + 1;
writeDB(db);
tokensCache = null;
res.json({ ok: true, referrals: token.referrals });
} catch (err) {
console.error("/refer error:", err);
res.status(500).json({ error: "referral failed" });
}
});
// ----------------------------
// SERVER START
// ----------------------------
const PORT = 4000;
app.listen(PORT, () => {
console.log(`Phase-2 Indexer running on port ${PORT}`);
});