miningFee check

This commit is contained in:
Alexey 2020-10-06 21:55:03 +03:00
parent ed3fceaadc
commit fd8b5dcbb4
2 changed files with 16 additions and 10 deletions

View File

@ -26,7 +26,7 @@
"gas-price-oracle": "^0.1.5", "gas-price-oracle": "^0.1.5",
"ioredis": "^4.14.1", "ioredis": "^4.14.1",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"tornado-cash-anonymity-mining": "git+https://github.com/tornadocash/tornado-anonymity-mining.git#e3ae8b98b14eb7fee7cb8c93221920a20b1e0cd8", "tornado-cash-anonymity-mining": "git+https://github.com/tornadocash/tornado-anonymity-mining.git#dd4b996b89580fad986604e507a7e56ad64a3f71",
"tx-manager": "git+https://github.com/tornadocash/tx-manager.git#df118be318b007e597e157504d105dfa3e6322a1", "tx-manager": "git+https://github.com/tornadocash/tx-manager.git#df118be318b007e597e157504d105dfa3e6322a1",
"uuid": "^8.3.0", "uuid": "^8.3.0",
"web3": "^1.3.0", "web3": "^1.3.0",

View File

@ -4,6 +4,7 @@ const { toBN, toWei, fromWei } = require('web3-utils')
const MerkleTree = require('fixed-merkle-tree') const MerkleTree = require('fixed-merkle-tree')
const Redis = require('ioredis') const Redis = require('ioredis')
const { GasPriceOracle } = require('gas-price-oracle') const { GasPriceOracle } = require('gas-price-oracle')
const { Utils } = require('tornado-cash-anonymity-mining')
const tornadoABI = require('../abis/tornadoABI.json') const tornadoABI = require('../abis/tornadoABI.json')
const miningABI = require('../abis/mining.abi.json') const miningABI = require('../abis/mining.abi.json')
@ -21,7 +22,7 @@ const {
gasLimits, gasLimits,
instances, instances,
tornadoServiceFee, tornadoServiceFee,
miningServiceFee miningServiceFee,
} = require('./config') } = require('./config')
const { TxManager } = require('tx-manager') const { TxManager } = require('tx-manager')
const { Controller } = require('tornado-cash-anonymity-mining') const { Controller } = require('tornado-cash-anonymity-mining')
@ -32,6 +33,7 @@ let currentJob
let tree let tree
let txManager let txManager
let controller let controller
let swap
const redis = new Redis(redisUrl) const redis = new Redis(redisUrl)
const redisSubscribe = new Redis(redisUrl) const redisSubscribe = new Redis(redisUrl)
const gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl }) const gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl })
@ -73,6 +75,7 @@ async function fetchTree() {
async function start() { async function start() {
web3 = new Web3(rpcUrl) web3 = new Web3(rpcUrl)
txManager = new TxManager({ privateKey, rpcUrl }) txManager = new TxManager({ privateKey, rpcUrl })
swap = new web3.eth.Contract(swapABI, swapAddress)
redisSubscribe.subscribe('treeUpdate', fetchTree) redisSubscribe.subscribe('treeUpdate', fetchTree)
await fetchTree() await fetchTree()
const provingKeys = { const provingKeys = {
@ -130,29 +133,32 @@ async function checkTornadoFee({ args, contract }) {
} }
async function checkMiningFee({ args }) { async function checkMiningFee({ args }) {
const swap = new web3.eth.Contract(swapABI, swapAddress)
const tornAmount = await swap.methods.getExpectedReturn(args.fee).call()
const { fast } = await gasPriceOracle.gasPrices() const { fast } = await gasPriceOracle.gasPrices()
const ethPrice = await redis.hget('prices', 'torn') const ethPrice = await redis.hget('prices', 'torn')
const expense = toBN(toWei(fast.toString(), 'gwei')).mul(toBN(gasLimits[args.type])) const expense = toBN(toWei(fast.toString(), 'gwei')).mul(toBN(gasLimits[args.type]))
const expenseInTorn = expense.mul(toBN(1e18)).div(toBN(ethPrice))
// todo make aggregator for ethPrices and rewardSwap data
const balance = await swap.virtualTornBalance()
const poolWeight = await swap.poolWeight()
const expenseInPoints = Utils.reverseTornadoFormula({ balance, tokens: expenseInTorn, poolWeight })
/* eslint-disable */ /* eslint-disable */
const feePercent = const serviceFeePercent =
args.type === jobType.MINING_REWARD args.type === jobType.MINING_REWARD
? 0 ? 0
: toBN(tornAmount) : toBN(args.amount)
.mul(toBN(miningServiceFee * 1e10)) .mul(toBN(miningServiceFee * 1e10))
.div(toBN(1e10 * 100)) .div(toBN(1e10 * 100))
/* eslint-enable */ /* eslint-enable */
let desiredFee = expense.mul(toBN(1e18)).div(toBN(ethPrice)).add(feePercent) const desiredFee = expenseInPoints.add(serviceFeePercent) // in points
console.log( console.log(
'sent fee, desired fee, feePercent', 'sent fee, desired fee, serviceFeePercent',
fromWei(args.fee.toString()), fromWei(args.fee.toString()),
fromWei(desiredFee.toString()), fromWei(desiredFee.toString()),
fromWei(feePercent.toString()), fromWei(serviceFeePercent.toString()),
) )
if (toBN(tornAmount).lt(desiredFee)) { if (toBN(args.fee).lt(desiredFee)) {
throw new Error('Provided fee is not enough. Probably it is a Gas Price spike, try to resubmit.') throw new Error('Provided fee is not enough. Probably it is a Gas Price spike, try to resubmit.')
} }
} }