-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
201 lines (180 loc) · 5.08 KB
/
index.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
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const http = require("http");
const express = require("express");
const { createServer } = require("vite");
const reactRefresh = require("@vitejs/plugin-react-refresh").default;
const ReplitDB = require("@replit/database");
const db = new ReplitDB();
async function init(output) {
const app = express();
const server = http.createServer(app);
const vite = await createServer({
plugins: [reactRefresh()],
server: {
middlewareMode: "ssr",
hmr: {
clientPort: 443,
server: server,
},
},
configFile: false,
});
app.get("/", async (req, res) => {
const url = req.originalUrl;
const template = await vite.transformIndexHtml(
req.originalUrl,
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NFT Contract Template</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono&family=IBM+Plex+Sans:wght@400;500&display=swap" rel="stylesheet">
</head>
<body>
<div id="root">Loading...</div>
<script type="module" src="/tools/ui.jsx"></script>
</body>
</html>
`
);
res.end(template);
});
let first = true;
app.get("/compile", async (req, res) => {
res.json(compile()).end();
});
app.get("/watch/:mtime", async (req, res) => {
const reqMtime = parseInt(req.params.mtime);
const fileMtime = fs.statSync("contract.sol").mtime.getTime();
if (reqMtime < fileMtime) {
compile().then(
(output) => res.json({ changed: true, output, mtime: fileMtime }),
(error) => console.error(error)
);
return;
}
var watcher = fs.watch("contract.sol", "utf-8", (event, filename) => {
clearTimeout(timeout);
watcher.close();
compile().then(
(output) =>
res.json({
changed: true,
output,
mtime: fs.statSync("contract.sol").mtime.getTime(),
}),
(error) => console.error(error)
);
});
var timeout = setTimeout(() => {
watcher.close();
res.json({ changed: false }).end();
}, 30000);
req.on("close", () => {
clearTimeout(timeout);
watcher.close();
});
});
app.get("/contracts", async (req, res) => {
const contracts = await db.get("contracts").catch((err) => null);
res.json(contracts || []).end();
});
app.post("/contracts", express.json(), async (req, res) => {
if (req.body) {
await db.set("contracts", req.body);
}
res.json({ success: true }).end();
});
app.use(vite.middlewares);
server.listen(3000, () => console.log("Ready"));
}
// solcjs is less efficient but more hackable,
// e.g. can specify import resolver that behaves like commonjs
async function compile() {
const solc = require("solc");
const sources = {};
for (const src of fs.readdirSync(path.join(__dirname, ".."))) {
if (src.endsWith(".sol")) {
sources[src] = {
content: fs.readFileSync(src, "utf8"),
};
}
}
const input = {
language: "Solidity",
sources,
settings: {
outputSelection: {
"*": {
"*": ["abi", "evm.bytecode.object"],
},
},
},
};
function findImports(path) {
try {
const file = fs.readFileSync(require.resolve(path), "utf8");
input.sources[path] = { content: file };
return { contents: file };
} catch (error) {
return { error };
}
}
console.log("Compiling", Object.keys(sources).join(", "));
const output = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
);
console.log("Compiled");
return output;
}
// solc is more efficient but it's a binary we can't
// flexibly interact with
function compile2() {
return new Promise((resolve, reject) => {
const proc = spawn("solc", [
"@=node_modules/@",
"--allow-paths",
'""',
"--combined-json",
"abi,bin",
"contract.sol",
]);
let stdout = "";
let stderr = "";
proc.stdout.on("data", (data) => (stdout += data));
proc.stderr.on("data", (data) => (stderr += data));
proc.on("close", (code) => {
if (code === 0) {
resolve(toJS(JSON.parse(stdout)));
} else {
resolve({ errors: [{ formattedMessage: stderr }] });
}
});
});
}
// Change it to the format ui is expecting
// It's legacy from solcjs and we should change the UI
function toJS(output) {
const contracts = {};
for (const [name, data] of Object.entries(output.contracts)) {
const [file, contract] = name.split(":");
contracts[file] = contracts[file] || {};
contracts[file][contract] = {
abi: data.abi,
evm: {
bytecode: {
object: data.bin,
},
},
};
}
return { contracts };
}
init();
console.log("Done!");