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

View File

@ -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",

View File

@ -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 {}

17
src/config/bull.config.ts Normal file
View 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,
},
}));

View File

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

View File

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

View File

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

View File

@ -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<RedisOptions>('bull.redis');
const settings = config.get<AdvancedSettings>('bull.settings');
import bullConfig from '@/config/bull.config';
@Module({
imports: [
BullModule.registerQueue({
redis,
settings,
...bullConfig(),
name: 'withdrawal',
}),
],

View File

@ -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<Withdrawal> {
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<Withdrawal> {
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<Withdrawal>) {
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<Withdrawal> {
}
}
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<number>('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<number>('gasLimits');
const { fast } = await getGasPrice(1);

View File

@ -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<Health> {
return {
status: '',

View File

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

View File

@ -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"