From 20d71a731a5818d2bba1b71b0d87566ae4e2a6ff Mon Sep 17 00:00:00 2001 From: nikdementev Date: Wed, 14 Jul 2021 14:20:59 +0300 Subject: [PATCH] fix: remove node config & use nest configuration --- package.json | 10 ++-- src/app.module.ts | 7 ++- src/config/bull.config.ts | 17 +++++++ src/config/configuration.ts | 20 +++----- src/config/index.ts | 1 + src/modules/index.ts | 1 + src/modules/queue/queue.module.ts | 10 +--- src/modules/queue/withdrawal.processor.ts | 62 ++++++++++++----------- src/modules/status/stat.service.ts | 9 ---- tsconfig.json | 1 + yarn.lock | 42 +++++++++------ 11 files changed, 96 insertions(+), 84 deletions(-) create mode 100644 src/config/bull.config.ts diff --git a/package.json b/package.json index 1f9a9fc..32581bb 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "new-relayer", "version": "0.0.1", - "description": "Relayer for Tornado.cash privacy solution. https://tornado.cash", - "author": "tornado.cash", + "description": "", + "author": "", "private": true, - "license": "MIT", + "license": "UNLICENSED", "scripts": { "compile": "typechain --target ethers-v5 --out-dir ./src/artifacts './src/abi/*.json'", "prebuild": "rimraf dist", @@ -27,13 +27,14 @@ "@nestjs/common": "^8.0.0", "@nestjs/config": "^1.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/microservices": "^8.0.2", "@nestjs/platform-express": "^8.0.0", "ajv": "^8.6.1", "bull": "^3.22.11", "class-validator": "^0.13.1", - "config": "^3.3.6", "ethers": "^5.4.1", "gas-price-oracle": "^0.3.3", + "redis": "^3.1.2", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.2.0", @@ -47,7 +48,6 @@ "@nestjs/testing": "^8.0.0", "@typechain/ethers-v5": "^7.0.1", "@types/bull": "^3.15.2", - "@types/config": "^0.0.39", "@types/express": "^4.17.13", "@types/jest": "^26.0.24", "@types/node": "^16.0.0", diff --git a/src/app.module.ts b/src/app.module.ts index 386ba06..a51171a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,8 @@ import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; -import { baseConfig } from './config'; -import { QueueModule } from './modules'; -import { CommunicationsModule } from './communication'; +import { baseConfig } from '@/config'; +import { QueueModule, StatusModule } from '@/modules'; @Module({ imports: [ @@ -12,7 +11,7 @@ import { CommunicationsModule } from './communication'; isGlobal: true, }), QueueModule, - CommunicationsModule, + StatusModule, ], }) export class AppModule {} diff --git a/src/config/bull.config.ts b/src/config/bull.config.ts new file mode 100644 index 0000000..37a5cb4 --- /dev/null +++ b/src/config/bull.config.ts @@ -0,0 +1,17 @@ +import { registerAs } from '@nestjs/config'; + +export default registerAs('bull', () => ({ + redis: { + host: 'localhost', + port: 6379, + }, + settings: { + lockDuration: 300000, + lockRenewTime: 30000, + stalledInterval: 30000, + maxStalledCount: 3, + guardInterval: 5000, + retryProcessDelay: 5000, + drainDelay: 5, + }, +})); diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 1aaf81e..a56af6f 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -1,18 +1,12 @@ export const baseConfig = () => ({ port: parseInt(process.env.PORT, 10) || 8080, - bull: { - redis: { - host: 'localhost', - port: 6379, - }, - settings: { - lockDuration: 300000, - lockRenewTime: 30000, - stalledInterval: 30000, - maxStalledCount: 3, - guardInterval: 5000, - retryProcessDelay: 5000, - drainDelay: 5, + txManager: { + privateKey: '', + rpcUrl: '', + config: { + CONFIRMATIONS: '', + MAX_GAS_PRICE: '', + THROW_ON_REVERT: false, }, }, }); diff --git a/src/config/index.ts b/src/config/index.ts index 75a1114..4771920 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1 +1,2 @@ export * from './configuration'; +export * from './bull.config'; diff --git a/src/modules/index.ts b/src/modules/index.ts index cadd6a9..2193d5d 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1 +1,2 @@ export * from './queue'; +export * from './status'; diff --git a/src/modules/queue/queue.module.ts b/src/modules/queue/queue.module.ts index 280480d..7f8d7e8 100644 --- a/src/modules/queue/queue.module.ts +++ b/src/modules/queue/queue.module.ts @@ -1,20 +1,14 @@ import { BullModule } from '@nestjs/bull'; import { Module } from '@nestjs/common'; -import config from 'config'; -import { AdvancedSettings } from 'bull'; - -import { RedisOptions } from 'ioredis'; import { WithdrawalProcessor } from './withdrawal.processor'; -const redis = config.get('bull.redis'); -const settings = config.get('bull.settings'); +import bullConfig from '@/config/bull.config'; @Module({ imports: [ BullModule.registerQueue({ - redis, - settings, + ...bullConfig(), name: 'withdrawal', }), ], diff --git a/src/modules/queue/withdrawal.processor.ts b/src/modules/queue/withdrawal.processor.ts index 0874ffa..5910404 100644 --- a/src/modules/queue/withdrawal.processor.ts +++ b/src/modules/queue/withdrawal.processor.ts @@ -1,17 +1,21 @@ -import { InjectQueue, Process, Processor } from '@nestjs/bull'; -import { Injectable } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; import { Job, Queue } from 'bull'; import { BigNumber } from 'ethers'; import { TxManager } from 'tx-manager'; +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { InjectQueue, Process, Processor } from '@nestjs/bull'; + +import { toWei } from '@/utilities'; import { getGasPrice } from '@/services'; -import { toChecksumAddress, toWei } from '@/utilities'; +import { getTornadoPool } from '@/contracts'; import { BaseProcessor } from './base.processor'; export interface Withdrawal { args: string[]; + proof: string; + amount: string; txHash: string; status: string; contract: string; @@ -19,11 +23,11 @@ export interface Withdrawal { } @Injectable() -@Processor('job') +@Processor('withdrawal') export class WithdrawalProcessor extends BaseProcessor { constructor( - private configService: ConfigService, @InjectQueue('withdrawal') public withdrawalQueue: Queue, + private configService: ConfigService, ) { super(); this.queueName = 'withdrawal'; @@ -35,22 +39,20 @@ export class WithdrawalProcessor extends BaseProcessor { try { await job.isActive(); - const { args, contract } = job.data; + const { args, amount } = job.data; - await this.checkFee({ contract, fee: args[4] }); + await this.checkFee({ fee: args[4], amount }); + await this.submitTx(job); } catch (err) { await job.moveToFailed(err, true); } } async submitTx(job: Job) { - const txManager = new TxManager({ - privateKey: '', - rpcUrl: '', - config: { CONFIRMATIONS: '', MAX_GAS_PRICE: '', THROW_ON_REVERT: false }, - }); + const txManager = new TxManager(this.configService.get('txManager')); - const tx = await txManager.createTx(await getTxObject(job)); + const prepareTx = await this.prepareTransaction(job.data); + const tx = await txManager.createTx(prepareTx); try { const receipt = await tx @@ -86,27 +88,27 @@ export class WithdrawalProcessor extends BaseProcessor { } } - getInstance(address) { - const id = this.configService.get('network.id'); - const instances = this.configService.get(`instances.${id}`); + async prepareTransaction({ proof, args, amount }) { + const contract = getTornadoPool(1); - for (const currency of Object.keys(instances)) { - const { instanceAddress, decimals } = instances[currency]; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + const data = contract.interface.encodeFunctionData('transaction', [ + proof, + ...args, + ]); - for (const amount of Object.keys(instanceAddress)) { - const contract = instances[currency].instanceAddress[amount]; + const gasLimit = this.configService.get('gasLimits'); - if (toChecksumAddress(contract) === toChecksumAddress(address)) { - return { currency, amount, decimals }; - } - } - } - return null; + return { + data, + gasLimit, + value: amount, + to: contract.address, + }; } - async checkFee({ fee, contract }) { - const { amount } = this.getInstance(contract); - + async checkFee({ fee, amount }) { const gasLimit = this.configService.get('gasLimits'); const { fast } = await getGasPrice(1); diff --git a/src/modules/status/stat.service.ts b/src/modules/status/stat.service.ts index 51a2867..a086e24 100644 --- a/src/modules/status/stat.service.ts +++ b/src/modules/status/stat.service.ts @@ -1,16 +1,7 @@ -import { Queue } from 'bull'; - -import { InjectQueue } from '@nestjs/bull'; import { Injectable } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; @Injectable() class StatusService { - constructor( - private configService: ConfigService, - @InjectQueue('withdrawal') private withdrawalQueue: Queue, - ) {} - async status(): Promise { return { status: '', diff --git a/tsconfig.json b/tsconfig.json index d4482fd..1a9670b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", + "resolveJsonModule": true, "sourceMap": true, "outDir": "./dist", "baseUrl": "./", diff --git a/yarn.lock b/yarn.lock index 6fb98a8..7bfead1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1003,6 +1003,15 @@ tslib "2.3.0" uuid "8.3.2" +"@nestjs/microservices@^8.0.2": + version "8.0.2" + resolved "https://registry.npmjs.org/@nestjs/microservices/-/microservices-8.0.2.tgz#565eaaef5b1a40f12261baf5981d310003255a39" + integrity sha512-sdADLaRlMXqLZph1yv4/gXTsFKzF9XvqAQXj42Rm7ZNWfTZp26bYGybaftRLhLsBJazk514aQXNANcQ5J9ZWPg== + dependencies: + iterare "1.2.1" + json-socket "0.3.0" + tslib "2.3.0" + "@nestjs/platform-express@^8.0.0": version "8.0.0" resolved "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-8.0.0.tgz#76099221413d2ced0afc9ce81083b68742ea9016" @@ -1155,11 +1164,6 @@ dependencies: "@types/ioredis" "*" -"@types/config@^0.0.39": - version "0.0.39" - resolved "https://registry.npmjs.org/@types/config/-/config-0.0.39.tgz#aad18ceb9439329adc3d4c6b91a908a72c715612" - integrity sha512-EBHj9lSIyw62vwqCwkeJXjiV6C2m2o+RJZlRWLkHduGYiNBoMXcY6AhSLqjQQ+uPdrPYrOMYvVa41zjo00LbFQ== - "@types/connect@*": version "3.4.35" resolved "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz#5fcf6ae445e4021d1fc2219a4873cc73a3bb2ad1" @@ -2244,13 +2248,6 @@ concat-stream@^1.5.2: readable-stream "^2.2.2" typedarray "^0.0.6" -config@^3.3.6: - version "3.3.6" - resolved "https://registry.npmjs.org/config/-/config-3.3.6.tgz#b87799db7399cc34988f55379b5f43465b1b065c" - integrity sha512-Hj5916C5HFawjYJat1epbyY2PlAgLpBtDUlr0MxGLgo3p5+7kylyvnRY18PqJHgnNWXcdd0eWDemT7eYWuFgwg== - dependencies: - json5 "^2.1.1" - consola@^2.15.0: version "2.15.3" resolved "https://registry.npmjs.org/consola/-/consola-2.15.3.tgz#2e11f98d6a4be71ff72e0bdf07bd23e12cb61550" @@ -2439,7 +2436,7 @@ delayed-stream@~1.0.0: resolved "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= -denque@^1.1.0: +denque@^1.1.0, denque@^1.5.0: version "1.5.0" resolved "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de" integrity sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ== @@ -4246,12 +4243,17 @@ json-schema-traverse@^1.0.0: resolved "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2" integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug== +json-socket@0.3.0: + version "0.3.0" + resolved "https://registry.npmjs.org/json-socket/-/json-socket-0.3.0.tgz#f4b953c685bb8e8bd0b72438f5208d9a0799ae07" + integrity sha512-jc8ZbUnYIWdxERFWQKVgwSLkGSe+kyzvmYxwNaRgx/c8NNyuHes4UHnPM3LUrAFXUx1BhNJ94n1h/KCRlbvV0g== + json-stable-stringify-without-jsonify@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651" integrity sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE= -json5@2.x, json5@^2.1.1, json5@^2.1.2, json5@^2.2.0: +json5@2.x, json5@^2.1.2, json5@^2.2.0: version "2.2.0" resolved "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3" integrity sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA== @@ -5119,7 +5121,7 @@ rechoir@^0.6.2: dependencies: resolve "^1.1.6" -redis-commands@1.7.0: +redis-commands@1.7.0, redis-commands@^1.7.0: version "1.7.0" resolved "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89" integrity sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ== @@ -5136,6 +5138,16 @@ redis-parser@^3.0.0: dependencies: redis-errors "^1.0.0" +redis@^3.1.2: + version "3.1.2" + resolved "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz#766851117e80653d23e0ed536254677ab647638c" + integrity sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw== + dependencies: + denque "^1.5.0" + redis-commands "^1.7.0" + redis-errors "^1.2.0" + redis-parser "^3.0.0" + reflect-metadata@^0.1.13: version "0.1.13" resolved "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"