Skip to content

Commit 48239bc

Browse files
committed
move start as index.ts
1 parent c0830ac commit 48239bc

File tree

2 files changed

+100
-106
lines changed

2 files changed

+100
-106
lines changed

bin/start.ts

Lines changed: 0 additions & 100 deletions
This file was deleted.

index.ts

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,100 @@
1-
export { eth_sendRawTransaction } from "./src/eth_sendRawTransaction.js";
2-
export { eth_gasPrice } from "./src/eth_gasPrice.js";
3-
export { start } from "./bin/start.js";
4-
export { claim } from "./bin/claim.js";
5-
export { open } from "./bin/open.js";
6-
export { powerup } from "./bin/powerup.js";
1+
import { JSONRPCServer } from "json-rpc-2.0";
2+
import { Session } from "@wharfkit/session";
3+
import { DEFAULT_HOSTNAME, DEFAULT_PORT, HOSTNAME, LOCK_GAS_PRICE, PORT,PROMETHEUS_PORT, DEFAULT_PROMETHEUS_PORT, createSession, METRICS_DISABLED, DEFAULT_METRICS_DISABLED, DEFAULT_VERBOSE, VERBOSE } from "./src/config.js";
4+
import { logger } from "./src/logger.js";
5+
import { DefaultOptions } from "./bin/cli.js";
6+
import { eth_sendRawTransaction } from "./src/eth_sendRawTransaction.js";
7+
import { eth_gasPrice } from "./src/eth_gasPrice.js";
8+
import * as prometheus from "./src/prometheus.js"
9+
10+
export interface StartOptions extends DefaultOptions {
11+
port?: number;
12+
metricsListenPort?: number;
13+
hostname?: string;
14+
verbose?: boolean;
15+
lockGasPrice?: string;
16+
metricsDisabled?: boolean;
17+
}
18+
19+
export default function (options: StartOptions) {
20+
const port = options.port ?? PORT ?? DEFAULT_PORT;
21+
const hostname = options.hostname ?? HOSTNAME;
22+
const metricsDisabled = options.metricsDisabled ?? METRICS_DISABLED ?? DEFAULT_METRICS_DISABLED;
23+
const metricsListenPort = options.metricsListenPort ?? PROMETHEUS_PORT ?? DEFAULT_PROMETHEUS_PORT;
24+
const lockGasPrice = options.lockGasPrice ?? LOCK_GAS_PRICE;
25+
const verbose = options.verbose ?? VERBOSE ?? DEFAULT_VERBOSE;
26+
27+
// create Wharfkit session
28+
const session = createSession(options);
29+
const server = new JSONRPCServer();
30+
31+
// enable logging if verbose enabled
32+
if (verbose) {
33+
logger.settings.type = "json";
34+
console.log(banner(session, port, hostname, metricsListenPort, metricsDisabled));
35+
}
36+
37+
server.addMethod("eth_sendRawTransaction", async params => {
38+
prometheus.sendRawTransaction.requests?.inc();
39+
const result = await eth_sendRawTransaction(session, params)
40+
prometheus.sendRawTransaction.success?.inc();
41+
return result;
42+
});
43+
server.addMethod("eth_gasPrice", async () => {
44+
prometheus.gasPrice.requests?.inc();
45+
const result = eth_gasPrice(session, lockGasPrice)
46+
prometheus.gasPrice.success?.inc();
47+
return result;
48+
});
49+
50+
if ( !options.metricsDisabled ) {
51+
prometheus.listen(metricsListenPort, hostname);
52+
}
53+
54+
return Bun.serve({
55+
port,
56+
hostname,
57+
development: true,
58+
fetch: async (request: Request) => {
59+
const url = new URL(request.url);
60+
if ( request.method == "GET" ) {
61+
if ( url.pathname == "/" ) return new Response(banner(session, port, hostname, metricsListenPort, metricsDisabled));
62+
const info = await session.client.v1.chain.get_info();
63+
return toJSON(info.toJSON());
64+
}
65+
const jsonRPCRequest = await request.json<any>();
66+
if ( !jsonRPCRequest ) return new Response("invalid request", {status: 400})
67+
// server.receive takes a JSON-RPC request and returns a promise of a JSON-RPC response.
68+
// It can also receive an array of requests, in which case it may return an array of responses.
69+
// Alternatively, you can use server.receiveJSON, which takes JSON string as is (in this case req.body).
70+
const jsonRPCResponse = await server.receive(jsonRPCRequest)
71+
if ( jsonRPCResponse ) return new Response(JSON.stringify(jsonRPCResponse), { headers: { 'Content-Type': 'application/json' } });
72+
// If response is absent, it was a JSON-RPC notification method.
73+
// Respond with no content status (204).
74+
return new Response("no content", {status: 204})
75+
}
76+
})
77+
}
78+
79+
function toJSON(obj: any, status: number = 200) {
80+
const headers = { 'Content-Type': 'application/json' };
81+
const body = JSON.stringify(obj);
82+
return new Response(body, { status, headers });
83+
}
84+
85+
function banner( session: Session, port: number, hostname?: string, metricsListenPort?: number, metricsDisabled?: boolean ) {
86+
let text = `
87+
88+
███████╗ ██████╗ ███████╗ ███████╗██╗ ██╗███╗ ███╗
89+
██╔════╝██╔═══██╗██╔════╝ ██╔════╝██║ ██║████╗ ████║
90+
█████╗ ██║ ██║███████╗ █████╗ ██║ ██║██╔████╔██║
91+
██╔══╝ ██║ ██║╚════██║ ██╔══╝ ╚██╗ ██╔╝██║╚██╔╝██║
92+
███████╗╚██████╔╝███████║ ███████╗ ╚████╔╝ ██║ ╚═╝ ██║
93+
╚══════╝ ╚═════╝ ╚══════╝ ╚══════╝ ╚═══╝ ╚═╝ ╚═╝
94+
`
95+
text += ` EOS EVM Miner listening @ ${hostname ?? DEFAULT_HOSTNAME}:${port.toString()}\n`
96+
if ( !metricsDisabled ) text += ` Prometheus metrics listening @ ${hostname ?? DEFAULT_HOSTNAME}:${metricsListenPort?.toString()}\n`;
97+
text += ` Your miner account is ${session.actor.toString()}\n`;
98+
text += ` ${session.walletPlugin.metadata.publicKey}\n`
99+
return text;
100+
}

0 commit comments

Comments
 (0)