@factorial-finance/blueprint-node
Version:
blueprint-node-plugin
118 lines (116 loc) • 4.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlueprintNode = exports.NodePlugin = void 0;
const aliases_1 = require("./services/aliases");
const blockchain_1 = require("./services/blockchain");
const jsonrpc_1 = require("./handlers/jsonrpc");
const http_1 = require("./server/http");
const arg_1 = __importDefault(require("arg"));
const utils_1 = require("./utils");
class NodePlugin {
runners() {
return [
{
name: "node",
runner: nodeRunner,
help: `Usage: blueprint node [flags]
Starts the Blueprint Node.
Flags:
--port <number> - sets the port number (default: 8545)
--host <string> - sets the host address (default: localhost)
--fork <mainnet|testnet> - forks from the specified network (optional, runs locally without forking if not provided)
--debug - enables debug mode (same as --log-level debug)
--silent - enables silent mode (no logs)
--log-level <debug|info|warn|error|silent> - sets the log level (default: info)`,
},
];
}
}
exports.NodePlugin = NodePlugin;
const nodeRunner = async (args, ui) => {
const localArgs = (0, arg_1.default)({
"--port": Number,
"--host": String,
"--fork": String,
"--debug": Boolean,
"--silent": Boolean,
"--log-level": String,
});
const port = localArgs["--port"] || 8545;
const host = localArgs["--host"] || "localhost";
const fork = localArgs["--fork"] || undefined;
const debug = localArgs["--debug"] || false;
const silent = localArgs["--silent"] || false;
const logLevel = localArgs["--log-level"] || undefined;
// Validate log level
const validLogLevels = ['debug', 'info', 'warn', 'error', 'silent'];
if (logLevel && !validLogLevels.includes(logLevel)) {
throw new Error(`Invalid log level: ${logLevel}. Valid options: ${validLogLevels.join(', ')}`);
}
// Determine log level based on flags (priority: silent > log-level > debug > default)
let finalLogLevel;
if (silent) {
finalLogLevel = 'silent';
}
else if (logLevel) {
finalLogLevel = logLevel;
}
else if (debug) {
finalLogLevel = 'debug';
}
else {
finalLogLevel = 'info';
}
return new Promise((resolve, reject) => {
const node = new BlueprintNode({
port,
host,
fork: fork,
debug,
logLevel: finalLogLevel,
});
node.start().catch(error => {
console.error('❌ Failed to start server:', error);
reject(error);
});
});
};
class BlueprintNode {
constructor(config) {
this.config = {
port: config.port,
host: config.host,
fork: config.fork,
debug: config.debug,
logLevel: config.logLevel,
};
// Configure logger with specified level
const logLevel = config.logLevel || (config.debug ? "debug" : "info");
utils_1.Logger.configure({ level: logLevel });
utils_1.Logger.debug(`Logger configured with level: ${logLevel}`);
this.blockchainService = new blockchain_1.BlockchainService(this.config.fork);
this.aliasService = new aliases_1.AliasService();
this.jsonRPCHandlers = new jsonrpc_1.JSONRPCHandlers(this.blockchainService, this.aliasService);
this.httpServer = new http_1.HTTPServer(this.jsonRPCHandlers.getServer(), this.config);
}
async start() {
try {
// Initialize blockchain service
await this.blockchainService.initialize();
await this.aliasService.initialize();
// Start HTTP server and display all info
await this.httpServer.start();
}
catch (error) {
console.error('❌ Failed to start Blueprint Node:', error);
process.exit(1);
}
}
stop() {
this.httpServer.close();
}
}
exports.BlueprintNode = BlueprintNode;