fix: service fee

This commit is contained in:
Danil Kovtonyuk 2021-09-27 19:10:19 +10:00 committed by 0xZick
parent 67455a744c
commit dd24249359
8 changed files with 57 additions and 23 deletions

View File

@ -5,8 +5,11 @@ LETSENCRYPT_HOST=
# server settings # server settings
PORT=8000 PORT=8000
#commision for service # commission for service
SERVICE_FEE=0.05 # transfer fee is a fixed value in ether, 0.01 means 0.01 ether
TRANSFER_SERVICE_FEE=0.01
# withdrawal fee is a percentage of the amount, 0.05 means 0.05%
WITHDRAWAL_SERVICE_FEE=0.05
REWARD_ADDRESS= REWARD_ADDRESS=
# tx-manager settings # tx-manager settings

View File

@ -1,4 +1,5 @@
import { Wallet } from 'ethers'; import { Wallet } from 'ethers';
import { toWei } from '@/utilities';
import { NETWORKS_INFO } from '@/constants'; import { NETWORKS_INFO } from '@/constants';
import { version } from '../../package.json'; import { version } from '../../package.json';
@ -8,7 +9,10 @@ export const baseConfig = () => ({
version, version,
port: process.env.PORT, port: process.env.PORT,
chainId: process.env.CHAIN_ID, chainId: process.env.CHAIN_ID,
serviceFee: process.env.SERVICE_FEE, serviceFee: {
transfer: toWei(process.env.TRANSFER_SERVICE_FEE).toString(),
withdrawal: Number(process.env.WITHDRAWAL_SERVICE_FEE),
},
rewardAddress: process.env.REWARD_ADDRESS, rewardAddress: process.env.REWARD_ADDRESS,
address: new Wallet(process.env.PRIVATE_KEY).address, address: new Wallet(process.env.PRIVATE_KEY).address,
gasLimit: NETWORKS_INFO[process.env.CHAIN_ID].gasLimit, gasLimit: NETWORKS_INFO[process.env.CHAIN_ID].gasLimit,

View File

@ -3,11 +3,11 @@ import { ChainId } from '@/types';
const NETWORKS_INFO: { [chainId in ChainId] } = { const NETWORKS_INFO: { [chainId in ChainId] } = {
[ChainId.MAINNET]: { [ChainId.MAINNET]: {
gasLimit: BigNumber.from(600000), gasLimit: BigNumber.from(400000),
minimumBalance: '0.5', minimumBalance: '0.5',
}, },
[ChainId.GOERLI]: { [ChainId.GOERLI]: {
gasLimit: BigNumber.from(600000), gasLimit: BigNumber.from(400000),
minimumBalance: '0.5', minimumBalance: '0.5',
}, },
[ChainId.OPTIMISM]: { [ChainId.OPTIMISM]: {
@ -20,6 +20,8 @@ const numbers = {
ZERO: 0, ZERO: 0,
ONE: 1, ONE: 1,
TWO: 2, TWO: 2,
TEN: 10,
ONE_HUNDRED: 100,
SECOND: 1000, SECOND: 1000,
ETH_DECIMALS: 18, ETH_DECIMALS: 18,
MERKLE_TREE_HEIGHT: 23, MERKLE_TREE_HEIGHT: 23,

View File

@ -21,9 +21,9 @@ class ApiService {
return { return {
health, health,
version, version,
serviceFee,
rewardAddress, rewardAddress,
chainId: Number(chainId), chainId: Number(chainId),
serviceFee: Number(serviceFee),
}; };
} }

View File

@ -26,9 +26,6 @@ const arrayType = { type: 'array', items: bytes32Type };
const transactionSchema = { const transactionSchema = {
type: 'object', type: 'object',
properties: { properties: {
amount: {
type: 'string',
},
extData: { extData: {
type: 'object', type: 'object',
properties: { properties: {
@ -55,7 +52,7 @@ const transactionSchema = {
}, },
}, },
additionalProperties: false, additionalProperties: false,
required: ['extData', 'args', 'amount'], required: ['extData', 'args'],
}; };
const validateTornadoTransaction = ajv.compile(transactionSchema); const validateTornadoTransaction = ajv.compile(transactionSchema);

View File

@ -3,10 +3,15 @@ type Health = {
error: string; error: string;
}; };
type ServiceFee = {
transfer: string;
withdrawal: number;
};
type Status = { type Status = {
health: Health; health: Health;
rewardAddress: string;
version: string;
serviceFee: number;
chainId: number; chainId: number;
version: string;
rewardAddress: string;
serviceFee: ServiceFee;
}; };

View File

@ -7,7 +7,8 @@ import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { InjectQueue, Process, Processor } from '@nestjs/bull'; import { InjectQueue, Process, Processor } from '@nestjs/bull';
import { toWei } from '@/utilities'; import { numbers } from '@/constants';
import { toWei, getToIntegerMultiplier } from '@/utilities';
import { GasPriceService, ProviderService } from '@/services'; import { GasPriceService, ProviderService } from '@/services';
import txMangerConfig from '@/config/txManager.config'; import txMangerConfig from '@/config/txManager.config';
@ -37,7 +38,6 @@ export type ArgsProof = {
export interface Transaction { export interface Transaction {
extData: ExtData; extData: ExtData;
args: ArgsProof; args: ArgsProof;
amount: string;
txHash: string; txHash: string;
status: string; status: string;
confirmations: number; confirmations: number;
@ -62,9 +62,9 @@ export class TransactionProcessor extends BaseProcessor<Transaction> {
try { try {
await job.isActive(); await job.isActive();
const { extData, amount } = job.data; const { extData } = job.data;
await this.checkFee({ fee: extData.fee, amount }); await this.checkFee({ fee: extData.fee, externalAmount: extData.extAmount });
await this.submitTx(job); await this.submitTx(job);
} catch (err) { } catch (err) {
await job.moveToFailed(err, true); await job.moveToFailed(err, true);
@ -140,16 +140,29 @@ export class TransactionProcessor extends BaseProcessor<Transaction> {
}; };
} }
async checkFee({ fee, amount }) { getServiceFee(externalAmount) {
const { gasLimit, serviceFee } = this.configService.get('base'); const amount = BigNumber.from(externalAmount);
const { serviceFee } = this.configService.get('base');
// for withdrawals the amount is negative
if (amount.isNegative()) {
const integerMultiplier = getToIntegerMultiplier(serviceFee.withdrawal);
return BigNumber.from(amount)
.mul(serviceFee.withdrawal * integerMultiplier)
.div(numbers.ONE_HUNDRED * integerMultiplier);
}
return serviceFee.transfer;
}
async checkFee({ fee, externalAmount }) {
const { gasLimit } = this.configService.get('base');
const { fast } = await this.gasPriceService.getGasPrice(); const { fast } = await this.gasPriceService.getGasPrice();
const expense = BigNumber.from(toWei(fast.toString(), 'gwei')).mul(gasLimit); const expense = BigNumber.from(toWei(fast.toString(), 'gwei')).mul(gasLimit);
const feePercent = BigNumber.from(amount) const feePercent = this.getServiceFee(externalAmount);
.mul(serviceFee * 1e10)
.div(100 * 1e10);
const desiredFee = expense.add(feePercent); const desiredFee = expense.add(feePercent);

View File

@ -10,7 +10,7 @@ export function toChecksumAddress(value: string): string {
return utils.getAddress(value); return utils.getAddress(value);
} }
export function toWei(value: string, uintName = 'wei') { export function toWei(value: string, uintName = 'ether') {
return utils.parseUnits(value, uintName); return utils.parseUnits(value, uintName);
} }
@ -25,3 +25,13 @@ export function numberToHex(value: number) {
export function fromWei(balance: BigNumberish) { export function fromWei(balance: BigNumberish) {
return utils.formatUnits(balance, numbers.ETH_DECIMALS); return utils.formatUnits(balance, numbers.ETH_DECIMALS);
} }
export function getToIntegerMultiplier(value: number | string): number {
const [, decimals] = String(value).split('.');
if (!decimals) {
return numbers.ZERO;
}
return Math.pow(numbers.TEN, decimals.length);
}