From 531567d8b271a6965300ea1ec29d90760fd1083d Mon Sep 17 00:00:00 2001 From: Theo Date: Mon, 11 Sep 2023 20:22:20 -0700 Subject: [PATCH] Update tornado-oracles & calculate and check relayer & user-provided fee correctly --- package.json | 2 +- src/worker.js | 54 +++++++++++++++++++++++---------------------------- yarn.lock | 8 ++++---- 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 61d0879..25773ad 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "license": "MIT", "dependencies": { "@tornado/tornado-config": "^2.0.0", - "@tornado/tornado-oracles": "^2.1.0", + "@tornado/tornado-oracles": "^3.3.0", "ajv": "^6.12.5", "async-mutex": "^0.2.4", "bull": "^3.12.1", diff --git a/src/worker.js b/src/worker.js index 4421c5b..bd1b217 100644 --- a/src/worker.js +++ b/src/worker.js @@ -10,7 +10,6 @@ const { queue } = require('./queue') const { poseidonHash2, getInstance, - fromDecimals, sleep, toBN, toWei, @@ -119,39 +118,31 @@ function checkFee({ data }, gasInfo) { return checkMiningFee(data) } -async function checkTornadoFee({ args, contract }, { gasLimit, gasPrice }) { +async function checkTornadoFee({ args, contract }, tx) { const { currency, amount, decimals } = getInstance(contract) - const [fee, refund] = [args[4], args[5]].map(toBN) + const [userProvidedFee, refund] = [args[4], args[5]].map(toBN) + const { gasLimit, gasPrice } = tx const ethPrice = await redis.hget('prices', currency) - const expense = toBN(gasPrice).mul(toBN(gasLimit)) + const totalWithdrawalFee = await feeOracle.calculateWithdrawalFeeViaRelayer({ + tx, + txType: 'relayer_withdrawal', + amount, + currency, + decimals, + refundInEth: refund.toString(), + predefinedGasLimit: gasLimit, + predefinedGasPrice: gasPrice, + tokenPriceInEth: ethPrice, + relayerFeePercent: tornadoServiceFee, + }) - const feePercent = toBN(fromDecimals(amount, decimals)) - .mul(toBN(parseInt(tornadoServiceFee * 1e10))) - .div(toBN(1e10 * 100)) - - let desiredFee - switch (currency) { - case 'eth': { - desiredFee = expense.add(feePercent) - break - } - default: { - desiredFee = expense - .add(refund) - .mul(toBN(10 ** decimals)) - .div(toBN(ethPrice)) - desiredFee = desiredFee.add(feePercent) - break - } - } console.log( - 'sent fee, desired fee, feePercent', - fromWei(fee.toString()), - fromWei(desiredFee.toString()), - fromWei(feePercent.toString()), + 'user-provided fee, desired fee', + fromWei(userProvidedFee.toString()), + fromWei(toBN(totalWithdrawalFee).toString()), ) - if (fee.lt(desiredFee)) { + if (userProvidedFee.lt(toBN(totalWithdrawalFee))) { throw new RelayerError( 'Provided fee is not enough. Probably it is a Gas Price spike, try to resubmit.', 0, @@ -228,9 +219,12 @@ async function getTxObject({ data }) { to: contract._address, data: calldata, } - const { gasLimit, gasPrice } = await feeOracle.getGasParams(incompleteTx, 'relayer_withdrawal') + const { gasLimit, gasPrice } = await feeOracle.getGasParams({ + tx: incompleteTx, + txType: 'relayer_withdrawal', + }) - return Object.assign(incompleteTx, { gasLimit, gasPrice }) + return { ...incompleteTx, gasLimit, gasPrice } } else { const method = data.type === jobType.MINING_REWARD ? 'reward' : 'withdraw' const calldata = minerContract.methods[method](data.proof, data.args).encodeABI() diff --git a/yarn.lock b/yarn.lock index 5f0db33..d8608ee 100644 --- a/yarn.lock +++ b/yarn.lock @@ -461,10 +461,10 @@ resolved "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-config/-/2.0.0/tornado-config-2.0.0.tgz#52bbc179ecb2385f71b4d56e060b68e7dd6fb8b4" integrity sha512-7EkpWNfEm34VEOrbLnPpvd/aUJYnA1L+6/qx2fZ/AfmuJFkjSZ18Z4jvVGNY7ktKIhTu3/Tbze+9l3eNueCNIA== -"@tornado/tornado-oracles@^2.1.0": - version "2.1.0" - resolved "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/2.1.0/tornado-oracles-2.1.0.tgz#2aa0d8c9288992e6d194d4bb28acb37c2035c453" - integrity sha512-Y6FPAGnCvHLWzUnNYgGoOv+X7KY3CF02rRSawataYaLyl+v2ivh7RYZZZ3G/B5hXf+pD3IFeCdm4PDnTNyNe1g== +"@tornado/tornado-oracles@^3.3.0": + version "3.3.0" + resolved "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/3.3.0/tornado-oracles-3.3.0.tgz#6358f896f399904ed8deb9b8f4253e77732bd227" + integrity sha512-OBJ+TmygY6VMcYJCPxSOAzPDZpmF4WhRVhjgTEhqw+hg70WW9L4b3DC1B1P/4Gmar2lzi6BnTI4ckkDy/xsHQQ== dependencies: "@tornado/gas-price-oracle" "^0.5.3" "@tornado/tornado-config" "^2.0.0"