mirror of
https://github.com/tornadocash/tornado-pool-relayer
synced 2024-02-02 15:04:09 +01:00
fix: withdrawal processor
This commit is contained in:
parent
20d71a731a
commit
268dd097b7
3
example.env
Normal file
3
example.env
Normal file
@ -0,0 +1,3 @@
|
||||
RPC_URL=
|
||||
PRIVATE_KEY=
|
||||
SERVICE_FEE=0.05
|
@ -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,
|
||||
});
|
||||
|
@ -1,2 +1,4 @@
|
||||
export * from './configuration';
|
||||
|
||||
export * from './bull.config';
|
||||
export * from './txManager.config';
|
||||
|
11
src/config/txManager.config.ts
Normal file
11
src/config/txManager.config.ts
Normal file
@ -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,
|
||||
},
|
||||
}));
|
13
src/main.ts
13
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<NestExpressApplication>(AppModule);
|
||||
const configService = app.get(ConfigService);
|
||||
await app.listen(configService.get('port'));
|
||||
try {
|
||||
const app = await NestFactory.create<NestExpressApplication>(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));
|
||||
|
@ -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<Withdrawal> {
|
||||
|
||||
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<Withdrawal> {
|
||||
}
|
||||
|
||||
async submitTx(job: Job<Withdrawal>) {
|
||||
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<Withdrawal> {
|
||||
}
|
||||
|
||||
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<Withdrawal> {
|
||||
...args,
|
||||
]);
|
||||
|
||||
const gasLimit = this.configService.get<number>('gasLimits');
|
||||
const gasLimit = this.configService.get<number>('gasLimit');
|
||||
|
||||
return {
|
||||
data,
|
||||
gasLimit,
|
||||
value: amount,
|
||||
value: BigNumber.from(0)._hex,
|
||||
to: contract.address,
|
||||
};
|
||||
}
|
||||
|
||||
async checkFee({ fee, amount }) {
|
||||
const gasLimit = this.configService.get<number>('gasLimits');
|
||||
const gasLimit = this.configService.get<number>('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<number>('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.',
|
||||
);
|
||||
|
@ -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<string> {
|
||||
return this.service.main();
|
||||
}
|
||||
|
||||
@Post('/withdrawal')
|
||||
async withdrawal(_, @Body() { body }: any): Promise<string> {
|
||||
console.log('body', body)
|
||||
|
||||
return await this.service.withdrawal(JSON.parse(body))
|
||||
}
|
||||
}
|
||||
|
@ -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 {}
|
||||
|
@ -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<Health> {
|
||||
return {
|
||||
status: '',
|
||||
@ -12,6 +16,12 @@ class StatusService {
|
||||
main(): string {
|
||||
return `This is <a href=https://tornado.cash>tornado.cash</a> Relayer service. Check the <a href=/status>/status</a> for settings`;
|
||||
}
|
||||
|
||||
async withdrawal(data): Promise<string> {
|
||||
const job = await this.withdrawalQueue.add(data)
|
||||
|
||||
return String(job.id);
|
||||
}
|
||||
}
|
||||
|
||||
export { StatusService };
|
||||
|
Loading…
Reference in New Issue
Block a user