diff --git a/example.env b/example.env new file mode 100644 index 0000000..5e43c8e --- /dev/null +++ b/example.env @@ -0,0 +1,3 @@ +RPC_URL= +PRIVATE_KEY= +SERVICE_FEE=0.05 diff --git a/src/config/configuration.ts b/src/config/configuration.ts index a56af6f..7bb505f 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -1,12 +1,5 @@ export const baseConfig = () => ({ port: parseInt(process.env.PORT, 10) || 8080, - txManager: { - privateKey: '', - rpcUrl: '', - config: { - CONFIRMATIONS: '', - MAX_GAS_PRICE: '', - THROW_ON_REVERT: false, - }, - }, + gasLimit: 400000, + fee: process.env.SERVICE_FEE, }); diff --git a/src/config/index.ts b/src/config/index.ts index 4771920..f2cdfeb 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,2 +1,4 @@ export * from './configuration'; + export * from './bull.config'; +export * from './txManager.config'; diff --git a/src/config/txManager.config.ts b/src/config/txManager.config.ts new file mode 100644 index 0000000..c383496 --- /dev/null +++ b/src/config/txManager.config.ts @@ -0,0 +1,11 @@ +import { registerAs } from '@nestjs/config'; + +export default registerAs('txManager', () => ({ + privateKey: process.env.PRIVATE_KEY, + rpcUrl: process.env.RPC_URL, + config: { + CONFIRMATIONS: '4', + MAX_GAS_PRICE: '100', + THROW_ON_REVERT: false, + }, +})); diff --git a/src/main.ts b/src/main.ts index 6495dfe..c50cd67 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,11 +5,14 @@ import { NestExpressApplication } from '@nestjs/platform-express'; import { AppModule } from './app.module'; async function bootstrap() { - const app = await NestFactory.create(AppModule); - const configService = app.get(ConfigService); - await app.listen(configService.get('port')); + try { + const app = await NestFactory.create(AppModule, { cors: true }); + + const configService = app.get(ConfigService); + await app.listen(configService.get('port')); + } catch (err) { + console.log('err', err.message) + } } bootstrap() - .then((result) => console.log('result', result)) - .catch((e) => console.log('error', e.message)); diff --git a/src/modules/queue/withdrawal.processor.ts b/src/modules/queue/withdrawal.processor.ts index 5910404..0f9cc62 100644 --- a/src/modules/queue/withdrawal.processor.ts +++ b/src/modules/queue/withdrawal.processor.ts @@ -9,6 +9,7 @@ import { InjectQueue, Process, Processor } from '@nestjs/bull'; import { toWei } from '@/utilities'; import { getGasPrice } from '@/services'; import { getTornadoPool } from '@/contracts'; +import txMangerConfig from '@/config/txManager.config'; import { BaseProcessor } from './base.processor'; @@ -18,7 +19,6 @@ export interface Withdrawal { amount: string; txHash: string; status: string; - contract: string; confirmations: number; } @@ -41,7 +41,7 @@ export class WithdrawalProcessor extends BaseProcessor { const { args, amount } = job.data; - await this.checkFee({ fee: args[4], amount }); + await this.checkFee({ fee: args[6], amount }); await this.submitTx(job); } catch (err) { await job.moveToFailed(err, true); @@ -49,7 +49,7 @@ export class WithdrawalProcessor extends BaseProcessor { } async submitTx(job: Job) { - const txManager = new TxManager(this.configService.get('txManager')); + const txManager = new TxManager(txMangerConfig()); const prepareTx = await this.prepareTransaction(job.data); const tx = await txManager.createTx(prepareTx); @@ -89,7 +89,7 @@ export class WithdrawalProcessor extends BaseProcessor { } async prepareTransaction({ proof, args, amount }) { - const contract = getTornadoPool(1); + const contract = getTornadoPool(5); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -98,34 +98,31 @@ export class WithdrawalProcessor extends BaseProcessor { ...args, ]); - const gasLimit = this.configService.get('gasLimits'); + const gasLimit = this.configService.get('gasLimit'); return { data, gasLimit, - value: amount, + value: BigNumber.from(0)._hex, to: contract.address, }; } async checkFee({ fee, amount }) { - const gasLimit = this.configService.get('gasLimits'); + const gasLimit = this.configService.get('gasLimit'); - const { fast } = await getGasPrice(1); + const { fast } = await getGasPrice(5); const expense = BigNumber.from(toWei(fast.toString(), 'gwei')).mul( gasLimit, ); const serviceFee = this.configService.get('fee'); - - const feePercent = BigNumber.from(toWei(amount)) - .mul(toWei(serviceFee.toString())) - .div(100); + const feePercent = BigNumber.from(amount).mul(serviceFee * 1e10).div(100 * 1e10) const desiredFee = expense.add(feePercent); - if (fee.lt(desiredFee)) { + if (BigNumber.from(fee).lt(desiredFee)) { throw new Error( 'Provided fee is not enough. Probably it is a Gas Price spike, try to resubmit.', ); diff --git a/src/modules/status/stat.controller.ts b/src/modules/status/stat.controller.ts index a2d5703..41c64c4 100644 --- a/src/modules/status/stat.controller.ts +++ b/src/modules/status/stat.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Body, Get, Post } from '@nestjs/common'; import { StatusService } from './stat.service'; @@ -15,4 +15,11 @@ export class StatusController { async main(): Promise { return this.service.main(); } + + @Post('/withdrawal') + async withdrawal(_, @Body() { body }: any): Promise { + console.log('body', body) + + return await this.service.withdrawal(JSON.parse(body)) + } } diff --git a/src/modules/status/stat.module.ts b/src/modules/status/stat.module.ts index e2cd19b..02deb22 100644 --- a/src/modules/status/stat.module.ts +++ b/src/modules/status/stat.module.ts @@ -4,9 +4,12 @@ import { ConfigModule } from '@nestjs/config'; import { StatusService } from './stat.service'; import { StatusController } from './stat.controller'; +import { QueueModule } from '@/modules'; + @Module({ - imports: [ConfigModule], + imports: [ConfigModule, QueueModule], providers: [StatusService], controllers: [StatusController], + exports: [], }) export class StatusModule {} diff --git a/src/modules/status/stat.service.ts b/src/modules/status/stat.service.ts index a086e24..f43342d 100644 --- a/src/modules/status/stat.service.ts +++ b/src/modules/status/stat.service.ts @@ -1,7 +1,11 @@ import { Injectable } from '@nestjs/common'; +import { Queue } from 'bull'; +import { InjectQueue } from '@nestjs/bull'; @Injectable() class StatusService { + constructor(@InjectQueue('withdrawal') private withdrawalQueue: Queue) {} + async status(): Promise { return { status: '', @@ -12,6 +16,12 @@ class StatusService { main(): string { return `This is tornado.cash Relayer service. Check the /status for settings`; } + + async withdrawal(data): Promise { + const job = await this.withdrawalQueue.add(data) + + return String(job.id); + } } export { StatusService };