tovarish-relayer/lib/config.js

76 lines
3.3 KiB
JavaScript
Raw Normal View History

2024-11-13 04:42:13 +03:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.version = void 0;
exports.getPrivateKey = getPrivateKey;
exports.getRewardAccount = getRewardAccount;
exports.getRelayerConfig = getRelayerConfig;
const path_1 = __importDefault(require("path"));
const process_1 = __importDefault(require("process"));
const os_1 = __importDefault(require("os"));
require("dotenv/config");
const ethers_1 = require("ethers");
const core_1 = require("@tornado/core");
const package_json_1 = __importDefault(require("../package.json"));
exports.version = `${package_json_1.default.name} ${package_json_1.default.version}`;
function getPrivateKey() {
const privateKey = process_1.default.env.PRIVATE_KEY;
if (!privateKey || !(0, ethers_1.isHexString)(privateKey, 32)) {
throw new Error('Invalid private key, make sure it contains 0x prefix!');
}
return privateKey;
}
function getRewardAccount() {
return (0, ethers_1.computeAddress)(getPrivateKey());
}
function getRelayerConfig() {
const enabledNetworks = process_1.default.env.ENABLED_NETWORKS
? process_1.default.env.ENABLED_NETWORKS.replaceAll(' ', '')
.split(',')
.map((n) => Number(n))
.filter((n) => core_1.enabledChains.includes(n))
: core_1.enabledChains;
const rpcUrls = enabledNetworks.reduce((acc, netId) => {
// If we have custom RPC url (like as 1_RPC from ENV)
if (process_1.default.env[`${netId}_RPC`]) {
acc[netId] = process_1.default.env[`${netId}_RPC`] || '';
}
else {
acc[netId] = Object.values((0, core_1.getConfig)(netId).rpcUrls)[0]?.url;
}
return acc;
}, {});
const txRpcUrls = enabledNetworks.reduce((acc, netId) => {
// If we have custom RPC url (like as 1_RPC from ENV)
if (process_1.default.env[`${netId}_TX_RPC`]) {
acc[netId] = process_1.default.env[`${netId}_TX_RPC`] || '';
}
else {
acc[netId] = rpcUrls[netId];
}
return acc;
}, {});
const STATIC_DIR = process_1.default.env.CACHE_DIR || path_1.default.join(__dirname, '../static');
const USER_DIR = process_1.default.env.USER_DIR || './data';
return {
host: process_1.default.env.HOST || '0.0.0.0',
port: Number(process_1.default.env.PORT || 3000),
workers: Number(process_1.default.env.WORKERS || os_1.default.cpus().length),
reverseProxy: process_1.default.env.REVERSE_PROXY === 'true',
logLevel: process_1.default.env.LOG_LEVEL || undefined,
rewardAccount: getRewardAccount(),
serviceFee: Number(process_1.default.env.SERVICE_FEE || 0.5),
clearInterval: Number(process_1.default.env.CLEAR_INTERVAL || 86400),
enabledNetworks,
rpcUrls,
txRpcUrls,
merkleWorkerPath: path_1.default.join(STATIC_DIR, './merkleTreeWorker.js'),
cacheDir: path_1.default.join(STATIC_DIR, './events'),
userEventsDir: path_1.default.join(USER_DIR, './events'),
userTreeDir: path_1.default.join(USER_DIR, './trees'),
2024-11-23 19:05:28 +03:00
syncInterval: Number(process_1.default.env.SYNC_INTERVAL || 120),
2024-11-13 04:42:13 +03:00
};
}