cleanup and formatting code

This commit is contained in:
smart_ex 2022-07-08 10:20:17 +10:00
parent 591c2d94f3
commit d32ff07fa8
7 changed files with 26 additions and 36 deletions

@ -1,6 +1,5 @@
NET_ID=1 NET_ID=1
HTTP_RPC_URL=https://mainnet.infura.io HTTP_RPC_URL=https://mainnet.infura.io
WS_RPC_URL=wss://mainnet.infura.io/ws/v3/
# ORACLE_RPC_URL should always point to the mainnet # ORACLE_RPC_URL should always point to the mainnet
ORACLE_RPC_URL=https://mainnet.infura.io ORACLE_RPC_URL=https://mainnet.infura.io
REDIS_URL=redis://127.0.0.1:6379 REDIS_URL=redis://127.0.0.1:6379
@ -14,8 +13,7 @@ APP_PORT=8000
PRIVATE_KEY= PRIVATE_KEY=
# 0.05 means 0.05% # 0.05 means 0.05%
REGULAR_TORNADO_WITHDRAW_FEE=0.05 REGULAR_TORNADO_WITHDRAW_FEE=0.05
MINING_SERVICE_FEE=0.05 REWARD_ACCOUNT=0x...
REWARD_ACCOUNT=
CONFIRMATIONS=4 CONFIRMATIONS=4
# in GWEI # in GWEI

@ -8,8 +8,9 @@ export const priceWorker = async () => {
const healthService = getHealthService(); const healthService = getHealthService();
const price = new PriceQueueHelper(); const price = new PriceQueueHelper();
console.log(price.queue.name, 'worker started');
price.scheduler.on('stalled', (jobId, prev) => console.log({ jobId, prev })); price.scheduler.on('stalled', (jobId, prev) => console.log({ jobId, prev }));
console.log('price worker', price.queue.name);
price.worker.on('active', () => console.log('worker active')); price.worker.on('active', () => console.log('worker active'));
price.worker.on('completed', async (job, result) => { price.worker.on('completed', async (job, result) => {
console.log(`Job ${job.name} completed with result: `, result); console.log(`Job ${job.name} completed with result: `, result);
@ -22,6 +23,7 @@ export const relayerWorker = async () => {
const relayer = new RelayerQueueHelper(); const relayer = new RelayerQueueHelper();
const healthService = getHealthService(); const healthService = getHealthService();
console.log(relayer.queue.name, 'worker started'); console.log(relayer.queue.name, 'worker started');
relayer.worker.on('completed', (job, result) => { relayer.worker.on('completed', (job, result) => {
console.log(`Job ${job.id} completed with result: `, result); console.log(`Job ${job.id} completed with result: `, result);
}); });
@ -33,7 +35,6 @@ export const relayerWorker = async () => {
}; };
export const healthWorker = async () => { export const healthWorker = async () => {
console.log('health worker starting');
await configService.init(); await configService.init();
const health = new HealthQueueHelper(); const health = new HealthQueueHelper();
console.log(health.queue.name, 'worker started'); console.log(health.queue.name, 'worker started');

@ -12,7 +12,7 @@ import {
tornadoGoerliProxy, tornadoGoerliProxy,
tornToken, tornToken,
} from '../config'; } from '../config';
import { Token } from '../types'; import { ChainIds, Token } from '../types';
import { getProvider, getTornadoProxyContract, getTornadoProxyLightContract, getTornTokenContract } from '../modules/contracts'; import { getProvider, getTornadoProxyContract, getTornadoProxyLightContract, getTornTokenContract } from '../modules/contracts';
import { resolve } from '../modules'; import { resolve } from '../modules';
import { ERC20Abi, ProxyLightABI, TornadoProxyABI } from '../contracts'; import { ERC20Abi, ProxyLightABI, TornadoProxyABI } from '../contracts';
@ -56,7 +56,7 @@ export class ConfigService {
constructor(private store: RedisStore) { constructor(private store: RedisStore) {
this.netIdKey = `netId${this.netId}`; this.netIdKey = `netId${this.netId}`;
this.queueName = `relayer_${this.netId}`; this.queueName = `relayer_${this.netId}`;
this.isLightMode = ![1, 5].includes(netId); this.isLightMode = ![ChainIds.ethereum, ChainIds.goerli].includes(netId);
this.host = host; this.host = host;
this.instances = instances[this.netIdKey]; this.instances = instances[this.netIdKey];
this.provider = getProvider(false); this.provider = getProvider(false);
@ -85,7 +85,6 @@ export class ConfigService {
private _fillInstanceMap() { private _fillInstanceMap() {
if (!this.instances) throw new Error('config mismatch, check your environment variables'); if (!this.instances) throw new Error('config mismatch, check your environment variables');
// TODO
for (const [currency, { instanceAddress, symbol, decimals }] of Object.entries(this.instances)) { for (const [currency, { instanceAddress, symbol, decimals }] of Object.entries(this.instances)) {
for (const [amount, address] of Object.entries(instanceAddress)) { for (const [amount, address] of Object.entries(instanceAddress)) {
if (address) if (address)
@ -123,13 +122,10 @@ export class ConfigService {
} else { } else {
this._proxyAddress = tornadoGoerliProxy; this._proxyAddress = tornadoGoerliProxy;
this.nativeCurrency = 'eth'; this.nativeCurrency = 'eth';
if (this.netId === 1) { if (this.netId === ChainIds.ethereum) {
this._proxyAddress = await resolve(torn.tornadoRouter.address); this._proxyAddress = await resolve(torn.tornadoRouter.address);
} }
this._proxyContract = getTornadoProxyContract(this._proxyAddress); this._proxyContract = getTornadoProxyContract(this._proxyAddress);
}
// TODO get instances from registry
this.tokens = [tornToken, ...Object.values(torn.instances['netId1'])] this.tokens = [tornToken, ...Object.values(torn.instances['netId1'])]
.map<Token>( .map<Token>(
(el) => (el) =>
@ -140,6 +136,9 @@ export class ConfigService {
}, },
) )
.filter(Boolean); .filter(Boolean);
}
// TODO get instances from registry
console.log( console.log(
'Configuration completed\n', 'Configuration completed\n',
`-- netId: ${this.netId}\n`, `-- netId: ${this.netId}\n`,
@ -156,9 +155,9 @@ export class ConfigService {
async clearRedisState() { async clearRedisState() {
const queueKeys = (await this.store.client.keys('bull:*')).filter((s) => s.indexOf('relayer') === -1); const queueKeys = (await this.store.client.keys('bull:*')).filter((s) => s.indexOf('relayer') === -1);
const errorKeys = await this.store.client.keys('errors:*'); const errorKeys = await this.store.client.keys('errors:*');
// const alertKeys = await this.store.client.keys('alerts:*'); const alertKeys = await this.store.client.keys('alerts:*');
const keys = [...queueKeys, ...errorKeys]; const keys = [...queueKeys, ...errorKeys, ...alertKeys];
if (keys.length) await this.store.client.del([...queueKeys, ...errorKeys]); if (keys.length) await this.store.client.del(keys);
} }
getInstance(address: string) { getInstance(address: string) {

@ -80,10 +80,10 @@ export class HealthService {
} }
async saveError(e, jobId?: string) { async saveError(e, jobId?: string) {
await this.store.client.zadd('errors:code', 'INCR', 1, e?.code || 'RUNTIME_ERROR'); await this.store.client.zadd('errors:code', 'INCR', 1, e.code || 'RUNTIME_ERROR');
await this.store.client.zadd('errors:log', 'INCR', 1, e.message); await this.store.client.zadd('errors:log', 'INCR', 1, e.message);
if (e?.code === 'REVERTED' || e?.code === 'SEND_ERROR') { if (e.code === 'REVERTED' || e.code === 'SEND_ERROR') {
const jobUrl = `${this.config.host}/v1/jobs/${jobId}`; const jobUrl = `${this.config.host}/v1/jobs/${jobId}`;
await this.pushAlert({ await this.pushAlert({
message: `${e.message} \n ${jobUrl}`, message: `${e.message} \n ${jobUrl}`,

@ -38,15 +38,6 @@ export class JobService {
return await this.relayer.queue.getJobCountByTypes('active', 'waiting', 'delayed'); return await this.relayer.queue.getJobCountByTypes('active', 'waiting', 'delayed');
} }
private async _clearSchedulerJobs() {
try {
const jobs = await Promise.all([this.price.queue.getJobs(), this.health.queue.getJobs()]);
await Promise.all(jobs.flat().map((job) => job?.remove()));
} catch (e) {
console.log(e);
}
}
async setupRepeatableJobs() { async setupRepeatableJobs() {
if (!this.config.isLightMode) { if (!this.config.isLightMode) {
await this.price.addRepeatable(this.config.tokens); await this.price.addRepeatable(this.config.tokens);

@ -51,15 +51,15 @@ export class PriceService {
} }
async getPrice(currency: string) { async getPrice(currency: string) {
return await this.store.client.hget('prices', currency); return this.store.client.hget('prices', currency);
} }
async getPrices() { async getPrices() {
return await this.store.client.hgetall('prices'); return this.store.client.hgetall('prices');
} }
async savePrices(prices: Record<string, string>) { async savePrices(prices: Record<string, string>) {
return await this.store.client.hset('prices', prices); return this.store.client.hset('prices', prices);
} }
} }

@ -60,6 +60,7 @@ export class TxService {
provider: this.provider, provider: this.provider,
}); });
this.oracle = new GasPriceOracle(gasPriceOracleConfig); this.oracle = new GasPriceOracle(gasPriceOracleConfig);
switch (netId) { switch (netId) {
case ChainIds.ethereum: case ChainIds.ethereum:
case ChainIds.goerli: case ChainIds.goerli: