-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
restroom.mjs
209 lines (186 loc) · 5.83 KB
/
restroom.mjs
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
202
203
204
205
206
207
208
import db from "@restroom-mw/db";
import ethereum from "@restroom-mw/ethereum";
import fabric from "@restroom-mw/fabric";
import files from "@restroom-mw/files";
import git from "@restroom-mw/git";
import logger from "@restroom-mw/logger";
import influxdb from "@restroom-mw/influxdb";
import rrhttp from "@restroom-mw/http";
import rrredis from "@restroom-mw/redis";
import timestamp from "@restroom-mw/timestamp";
import ui from "@restroom-mw/ui";
import zencode from "@restroom-mw/core";
import WebSocket from 'ws';
import axios from 'axios';
import bodyParser from "body-parser";
import chalk from "chalk";
import dotenv from "dotenv";
import express from "express";
import fs from 'fs';
import http from "http";
import morgan from "morgan";
import readdirp from 'readdirp';
import winston from "winston";
dotenv.config();
const L = new winston.createLogger({
level: 'debug',
transports: [
new winston.transports.File({
filename: './access.log',
level: 'debug'
}),
],
exitOnError: false
});
const HTTP_PORT = parseInt(process.env.HTTP_PORT || "80", 10);
const LOCAL_PORT = parseInt(process.env.LOCAL_PORT || "3000", 10);
const HOST = process.env.HOST || "0.0.0.0";
const ZENCODE_DIR = process.env.ZENCODE_DIR;
const OPENAPI = JSON.parse(process.env.OPENAPI || true);
const YML_EXT = process.env.YML_EXT || "yaml";
const CHAIN_EXT = process.env.CHAIN_EXT || "chain";
const SUBSCRIPTION_FILE = process.env.SUBSCRIPTION_FILE || "secrets/subscriptions.json";
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(morgan("dev"));
app.set("json spaces", 2);
app.use(db.default);
app.use(fabric.default);
app.use(files.default);
app.use(logger.default);
app.use(influxdb.default);
app.use(rrhttp.default);
app.use(rrredis.default);
app.use(ethereum.default);
app.use(timestamp.default);
app.use(git.default);
/*
* Load current L1 blockchains database
*/
import {
readFile
} from 'fs/promises';
let subscriptions = null
try {
subscriptions = JSON.parse(
await readFile(
new URL(SUBSCRIPTION_FILE,
import.meta.url)
)
).oracleActions;
Object.keys(subscriptions).forEach(
key => subscriptions[key].name = key);
} catch(e) {
L.warn("NO subscriptions loaded")
}
/*
* Subscribe to ETH node
*/
function subscribeEth(blockchain) {
try {
const ws = new WebSocket(blockchain.subscription);
ws.onopen = function () {
const id = Math.floor(Math.random() * 65536);
let subscriptionId = null;
ws.send(JSON.stringify({
id,
jsonrpc: "2.0",
method: "eth_subscribe",
params: [
"logs",
blockchain.listen
]
}));
const processMsg = function (evt) {
let msg = JSON.parse(evt.data)
console.log(msg)
if (msg.method == "eth_subscription" &&
msg.params && msg.params.subscription == subscriptionId) {
msg['endpoint'] = blockchain.rpc;
Object.assign(msg, {
blockchain
})
console.log(msg.params.result);
axios.post(blockchain.trigger, {
data: msg.params.result
})
.then(function (data) {
L.info(`ETH_RESTROOM ${JSON.stringify(data.data)}`);
}).catch(function (e) {
L.warn(`ETH_RESTROOM_ERROR ${e.response}`)
});
}
}
ws.onmessage = function (e) {
const msg = JSON.parse(e.data);
console.log(msg)
if (msg.result && msg.id == id) {
subscriptionId = msg.result
// from now on messages will be processed as blocks
ws.onmessage = processMsg;
}
}
ws.onclose = function () {
Log.warn("ETH_CLOSE")
}
}
} catch (e) {
L.error(`ETH_WS_ERROR ${e}`);
process.exit(-1);
}
}
const subscribeFn = {
ethereum: subscribeEth
}
function dispatchSubscriptions() {
if(subscriptions == null) {
return;
}
Object.keys(subscriptions).forEach(
key => {
try {
const blockchain = subscriptions[key]
const fn = subscribeFn[blockchain['blockchain']];
if (!fn) {
L.log("UNKNOWN_SUBSCRIPTION " + key);
return
}
fn(blockchain);
} catch (e) {
console.warn(e)
}
});
}
app.get("/apis", async (req, res) => {
let files = await readdirp.promise(ZENCODE_DIR, { fileFilter: '*.zen|*.yaml|*.yml' })
let getEndpoint = (file) => file.path.replace('.zen', '').replace(`.${YML_EXT}`, `.${CHAIN_EXT}`);
let endpoints = files.map(file => `/api/${getEndpoint(file)}`);
res.json(endpoints)
});
if (OPENAPI) {
app.use("/docs", ui.default({ path: ZENCODE_DIR }));
}
app.use("/api/*", zencode.default);
const contracts = fs.readdirSync(ZENCODE_DIR);
if (contracts.length > 0) {
const httpServer = http.createServer(app);
httpServer.listen(LOCAL_PORT, "0.0.0.0", () => {
console.log(`🚻 Restroom started on http://${chalk.bold.blue(HOST)}:${HTTP_PORT}`);
console.log(`📁 the ZENCODE directory is: ${chalk.magenta.underline(ZENCODE_DIR)} \n`);
if (OPENAPI) {
console.log(`To see the OpenApi interface head your browser to: ${chalk.bold.blue.underline('http://' + HOST + ':' + HTTP_PORT + '/docs')}`);
console.log(`To disable OpenApi, run ${chalk.bold('OPENAPI=0 yarn start')}`);
} else {
console.log(`⚠️ The OpenApi is not enabled! NO UI IS SERVED. To enable it run run ${chalk.bold('OPENAPI=1 yarn start')}`);
}
console.log("\nExposing");
readdirp(ZENCODE_DIR, { fileFilter: '*.zen|*.yaml|*.yml' }).on('data', (c) => {
const endpoint = `/api/${c.path.replace('.zen', '')}`
console.log(`\t${chalk.bold.green(endpoint)}`);
});
dispatchSubscriptions();
});
} else {
console.log(`🚨 The ${chalk.magenta.underline(ZENCODE_DIR)} folder is empty, please add some ZENCODE smart contract before running Restroom`);
}