Update tornado-oracles & calculate and check relayer & user-provided fee correctly

This commit is contained in:
Theo 2023-09-11 20:22:20 -07:00
parent 67c0782794
commit 531567d8b2
3 changed files with 29 additions and 35 deletions

View File

@ -20,7 +20,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@tornado/tornado-config": "^2.0.0", "@tornado/tornado-config": "^2.0.0",
"@tornado/tornado-oracles": "^2.1.0", "@tornado/tornado-oracles": "^3.3.0",
"ajv": "^6.12.5", "ajv": "^6.12.5",
"async-mutex": "^0.2.4", "async-mutex": "^0.2.4",
"bull": "^3.12.1", "bull": "^3.12.1",

View File

@ -10,7 +10,6 @@ const { queue } = require('./queue')
const { const {
poseidonHash2, poseidonHash2,
getInstance, getInstance,
fromDecimals,
sleep, sleep,
toBN, toBN,
toWei, toWei,
@ -119,39 +118,31 @@ function checkFee({ data }, gasInfo) {
return checkMiningFee(data) return checkMiningFee(data)
} }
async function checkTornadoFee({ args, contract }, { gasLimit, gasPrice }) { async function checkTornadoFee({ args, contract }, tx) {
const { currency, amount, decimals } = getInstance(contract) 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 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( console.log(
'sent fee, desired fee, feePercent', 'user-provided fee, desired fee',
fromWei(fee.toString()), fromWei(userProvidedFee.toString()),
fromWei(desiredFee.toString()), fromWei(toBN(totalWithdrawalFee).toString()),
fromWei(feePercent.toString()),
) )
if (fee.lt(desiredFee)) { if (userProvidedFee.lt(toBN(totalWithdrawalFee))) {
throw new RelayerError( throw new RelayerError(
'Provided fee is not enough. Probably it is a Gas Price spike, try to resubmit.', 'Provided fee is not enough. Probably it is a Gas Price spike, try to resubmit.',
0, 0,
@ -228,9 +219,12 @@ async function getTxObject({ data }) {
to: contract._address, to: contract._address,
data: calldata, 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 { } else {
const method = data.type === jobType.MINING_REWARD ? 'reward' : 'withdraw' const method = data.type === jobType.MINING_REWARD ? 'reward' : 'withdraw'
const calldata = minerContract.methods[method](data.proof, data.args).encodeABI() const calldata = minerContract.methods[method](data.proof, data.args).encodeABI()

View File

@ -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" 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== integrity sha512-7EkpWNfEm34VEOrbLnPpvd/aUJYnA1L+6/qx2fZ/AfmuJFkjSZ18Z4jvVGNY7ktKIhTu3/Tbze+9l3eNueCNIA==
"@tornado/tornado-oracles@^2.1.0": "@tornado/tornado-oracles@^3.3.0":
version "2.1.0" version "3.3.0"
resolved "https://git.tornado.ws/api/packages/tornado-packages/npm/%40tornado%2Ftornado-oracles/-/2.1.0/tornado-oracles-2.1.0.tgz#2aa0d8c9288992e6d194d4bb28acb37c2035c453" 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-Y6FPAGnCvHLWzUnNYgGoOv+X7KY3CF02rRSawataYaLyl+v2ivh7RYZZZ3G/B5hXf+pD3IFeCdm4PDnTNyNe1g== integrity sha512-OBJ+TmygY6VMcYJCPxSOAzPDZpmF4WhRVhjgTEhqw+hg70WW9L4b3DC1B1P/4Gmar2lzi6BnTI4ckkDy/xsHQQ==
dependencies: dependencies:
"@tornado/gas-price-oracle" "^0.5.3" "@tornado/gas-price-oracle" "^0.5.3"
"@tornado/tornado-config" "^2.0.0" "@tornado/tornado-config" "^2.0.0"