fix: remove node config & use nest configuration

This commit is contained in:
nikdementev 2021-07-14 14:20:59 +03:00
parent 0ce449620a
commit 20d71a731a
No known key found for this signature in database
GPG Key ID: 769B05D57CF16FE2
11 changed files with 96 additions and 84 deletions

@ -1,10 +1,10 @@
{ {
"name": "new-relayer", "name": "new-relayer",
"version": "0.0.1", "version": "0.0.1",
"description": "Relayer for Tornado.cash privacy solution. https://tornado.cash", "description": "",
"author": "tornado.cash", "author": "",
"private": true, "private": true,
"license": "MIT", "license": "UNLICENSED",
"scripts": { "scripts": {
"compile": "typechain --target ethers-v5 --out-dir ./src/artifacts './src/abi/*.json'", "compile": "typechain --target ethers-v5 --out-dir ./src/artifacts './src/abi/*.json'",
"prebuild": "rimraf dist", "prebuild": "rimraf dist",
@ -27,13 +27,14 @@
"@nestjs/common": "^8.0.0", "@nestjs/common": "^8.0.0",
"@nestjs/config": "^1.0.0", "@nestjs/config": "^1.0.0",
"@nestjs/core": "^8.0.0", "@nestjs/core": "^8.0.0",
"@nestjs/microservices": "^8.0.2",
"@nestjs/platform-express": "^8.0.0", "@nestjs/platform-express": "^8.0.0",
"ajv": "^8.6.1", "ajv": "^8.6.1",
"bull": "^3.22.11", "bull": "^3.22.11",
"class-validator": "^0.13.1", "class-validator": "^0.13.1",
"config": "^3.3.6",
"ethers": "^5.4.1", "ethers": "^5.4.1",
"gas-price-oracle": "^0.3.3", "gas-price-oracle": "^0.3.3",
"redis": "^3.1.2",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rxjs": "^7.2.0", "rxjs": "^7.2.0",
@ -47,7 +48,6 @@
"@nestjs/testing": "^8.0.0", "@nestjs/testing": "^8.0.0",
"@typechain/ethers-v5": "^7.0.1", "@typechain/ethers-v5": "^7.0.1",
"@types/bull": "^3.15.2", "@types/bull": "^3.15.2",
"@types/config": "^0.0.39",
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"@types/jest": "^26.0.24", "@types/jest": "^26.0.24",
"@types/node": "^16.0.0", "@types/node": "^16.0.0",

@ -1,9 +1,8 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { baseConfig } from './config'; import { baseConfig } from '@/config';
import { QueueModule } from './modules'; import { QueueModule, StatusModule } from '@/modules';
import { CommunicationsModule } from './communication';
@Module({ @Module({
imports: [ imports: [
@ -12,7 +11,7 @@ import { CommunicationsModule } from './communication';
isGlobal: true, isGlobal: true,
}), }),
QueueModule, QueueModule,
CommunicationsModule, StatusModule,
], ],
}) })
export class AppModule {} export class AppModule {}

17
src/config/bull.config.ts Normal file

@ -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,
},
}));

@ -1,18 +1,12 @@
export const baseConfig = () => ({ export const baseConfig = () => ({
port: parseInt(process.env.PORT, 10) || 8080, port: parseInt(process.env.PORT, 10) || 8080,
bull: { txManager: {
redis: { privateKey: '',
host: 'localhost', rpcUrl: '',
port: 6379, config: {
}, CONFIRMATIONS: '',
settings: { MAX_GAS_PRICE: '',
lockDuration: 300000, THROW_ON_REVERT: false,
lockRenewTime: 30000,
stalledInterval: 30000,
maxStalledCount: 3,
guardInterval: 5000,
retryProcessDelay: 5000,
drainDelay: 5,
}, },
}, },
}); });

@ -1 +1,2 @@
export * from './configuration'; export * from './configuration';
export * from './bull.config';

@ -1 +1,2 @@
export * from './queue'; export * from './queue';
export * from './status';

@ -1,20 +1,14 @@
import { BullModule } from '@nestjs/bull'; import { BullModule } from '@nestjs/bull';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import config from 'config';
import { AdvancedSettings } from 'bull';
import { RedisOptions } from 'ioredis';
import { WithdrawalProcessor } from './withdrawal.processor'; import { WithdrawalProcessor } from './withdrawal.processor';
const redis = config.get<RedisOptions>('bull.redis'); import bullConfig from '@/config/bull.config';
const settings = config.get<AdvancedSettings>('bull.settings');
@Module({ @Module({
imports: [ imports: [
BullModule.registerQueue({ BullModule.registerQueue({
redis, ...bullConfig(),
settings,
name: 'withdrawal', name: 'withdrawal',
}), }),
], ],

@ -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 { Job, Queue } from 'bull';
import { BigNumber } from 'ethers'; import { BigNumber } from 'ethers';
import { TxManager } from 'tx-manager'; 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 { getGasPrice } from '@/services';
import { toChecksumAddress, toWei } from '@/utilities'; import { getTornadoPool } from '@/contracts';
import { BaseProcessor } from './base.processor'; import { BaseProcessor } from './base.processor';
export interface Withdrawal { export interface Withdrawal {
args: string[]; args: string[];
proof: string;
amount: string;
txHash: string; txHash: string;
status: string; status: string;
contract: string; contract: string;
@ -19,11 +23,11 @@ export interface Withdrawal {
} }
@Injectable() @Injectable()
@Processor('job') @Processor('withdrawal')
export class WithdrawalProcessor extends BaseProcessor<Withdrawal> { export class WithdrawalProcessor extends BaseProcessor<Withdrawal> {
constructor( constructor(
private configService: ConfigService,
@InjectQueue('withdrawal') public withdrawalQueue: Queue, @InjectQueue('withdrawal') public withdrawalQueue: Queue,
private configService: ConfigService,
) { ) {
super(); super();
this.queueName = 'withdrawal'; this.queueName = 'withdrawal';
@ -35,22 +39,20 @@ export class WithdrawalProcessor extends BaseProcessor<Withdrawal> {
try { try {
await job.isActive(); 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) { } catch (err) {
await job.moveToFailed(err, true); await job.moveToFailed(err, true);
} }
} }
async submitTx(job: Job<Withdrawal>) { async submitTx(job: Job<Withdrawal>) {
const txManager = new TxManager({ const txManager = new TxManager(this.configService.get('txManager'));
privateKey: '',
rpcUrl: '',
config: { CONFIRMATIONS: '', MAX_GAS_PRICE: '', THROW_ON_REVERT: false },
});
const tx = await txManager.createTx(await getTxObject(job)); const prepareTx = await this.prepareTransaction(job.data);
const tx = await txManager.createTx(prepareTx);
try { try {
const receipt = await tx const receipt = await tx
@ -86,27 +88,27 @@ export class WithdrawalProcessor extends BaseProcessor<Withdrawal> {
} }
} }
getInstance(address) { async prepareTransaction({ proof, args, amount }) {
const id = this.configService.get('network.id'); const contract = getTornadoPool(1);
const instances = this.configService.get(`instances.${id}`);
for (const currency of Object.keys(instances)) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment
const { instanceAddress, decimals } = instances[currency]; // @ts-ignore
const data = contract.interface.encodeFunctionData('transaction', [
proof,
...args,
]);
for (const amount of Object.keys(instanceAddress)) { const gasLimit = this.configService.get<number>('gasLimits');
const contract = instances[currency].instanceAddress[amount];
if (toChecksumAddress(contract) === toChecksumAddress(address)) { return {
return { currency, amount, decimals }; data,
} gasLimit,
} value: amount,
} to: contract.address,
return null; };
} }
async checkFee({ fee, contract }) { async checkFee({ fee, amount }) {
const { amount } = this.getInstance(contract);
const gasLimit = this.configService.get<number>('gasLimits'); const gasLimit = this.configService.get<number>('gasLimits');
const { fast } = await getGasPrice(1); const { fast } = await getGasPrice(1);

@ -1,16 +1,7 @@
import { Queue } from 'bull';
import { InjectQueue } from '@nestjs/bull';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
class StatusService { class StatusService {
constructor(
private configService: ConfigService,
@InjectQueue('withdrawal') private withdrawalQueue: Queue,
) {}
async status(): Promise<Health> { async status(): Promise<Health> {
return { return {
status: '', status: '',

@ -8,6 +8,7 @@
"experimentalDecorators": true, "experimentalDecorators": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"target": "es2017", "target": "es2017",
"resolveJsonModule": true,
"sourceMap": true, "sourceMap": true,
"outDir": "./dist", "outDir": "./dist",
"baseUrl": "./", "baseUrl": "./",

@ -1003,6 +1003,15 @@
tslib "2.3.0" tslib "2.3.0"
uuid "8.3.2" 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": "@nestjs/platform-express@^8.0.0":
version "8.0.0" version "8.0.0"
resolved "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-8.0.0.tgz#76099221413d2ced0afc9ce81083b68742ea9016" resolved "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-8.0.0.tgz#76099221413d2ced0afc9ce81083b68742ea9016"
@ -1155,11 +1164,6 @@
dependencies: dependencies:
"@types/ioredis" "*" "@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@*": "@types/connect@*":
version "3.4.35" version "3.4.35"
resolved "https://registry.npmjs.org/@types/connect/-/connect-3.4.35.tgz#5fcf6ae445e4021d1fc2219a4873cc73a3bb2ad1" 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" readable-stream "^2.2.2"
typedarray "^0.0.6" 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: consola@^2.15.0:
version "2.15.3" version "2.15.3"
resolved "https://registry.npmjs.org/consola/-/consola-2.15.3.tgz#2e11f98d6a4be71ff72e0bdf07bd23e12cb61550" 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" resolved "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=
denque@^1.1.0: denque@^1.1.0, denque@^1.5.0:
version "1.5.0" version "1.5.0"
resolved "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de" resolved "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz#773de0686ff2d8ec2ff92914316a47b73b1c73de"
integrity sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ== 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" resolved "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz#ae7bcb3656ab77a73ba5c49bf654f38e6b6860e2"
integrity sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug== 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: json-stable-stringify-without-jsonify@^1.0.1:
version "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" resolved "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
integrity sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE= 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" version "2.2.0"
resolved "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3" resolved "https://registry.npmjs.org/json5/-/json5-2.2.0.tgz#2dfefe720c6ba525d9ebd909950f0515316c89a3"
integrity sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA== integrity sha512-f+8cldu7X/y7RAJurMEJmdoKXGB/X550w2Nr3tTbezL6RwEE/iMcm+tZnXeoZtKuOq6ft8+CqzEkrIgx1fPoQA==
@ -5119,7 +5121,7 @@ rechoir@^0.6.2:
dependencies: dependencies:
resolve "^1.1.6" resolve "^1.1.6"
redis-commands@1.7.0: redis-commands@1.7.0, redis-commands@^1.7.0:
version "1.7.0" version "1.7.0"
resolved "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89" resolved "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz#15a6fea2d58281e27b1cd1acfb4b293e278c3a89"
integrity sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ== integrity sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==
@ -5136,6 +5138,16 @@ redis-parser@^3.0.0:
dependencies: dependencies:
redis-errors "^1.0.0" 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: reflect-metadata@^0.1.13:
version "0.1.13" version "0.1.13"
resolved "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08" resolved "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"