mirror of
https://github.com/tornadocash/tornado-relayer.git
synced 2025-08-15 01:25:33 -04:00
refactor + coingecko
This commit is contained in:
parent
db35a0cbcc
commit
b5609c30c5
10 changed files with 3399 additions and 124 deletions
79
src/utils.js
Normal file
79
src/utils.js
Normal file
|
@ -0,0 +1,79 @@
|
|||
const { isHexStrict, toBN, toWei } = require('web3-utils')
|
||||
const { mixers } = require('../config')
|
||||
|
||||
function isValidProof(proof) {
|
||||
// validator expects `websnarkUtils.toSolidityInput(proof)` output
|
||||
|
||||
if (!(proof)) {
|
||||
return { valid: false, reason: 'The proof is empty.' }
|
||||
}
|
||||
|
||||
if (!isHexStrict(proof) || proof.length !== 2 + 2 * 8 * 32) {
|
||||
return { valid: false, reason: 'Corrupted proof' }
|
||||
}
|
||||
|
||||
return { valid: true }
|
||||
}
|
||||
|
||||
function isValidArgs(args) {
|
||||
|
||||
if (!(args)) {
|
||||
return { valid: false, reason: 'Args are empty' }
|
||||
}
|
||||
|
||||
if (args.length !== 6) {
|
||||
return { valid: false, reason: 'Length of args is lower than 6' }
|
||||
}
|
||||
|
||||
for(let signal of args) {
|
||||
if (!isHexStrict(signal)) {
|
||||
return { valid: false, reason: `Corrupted signal ${signal}` }
|
||||
}
|
||||
}
|
||||
|
||||
if (args[0].length !== 66 ||
|
||||
args[1].length !== 66 ||
|
||||
args[2].length !== 42 ||
|
||||
args[3].length !== 42 ||
|
||||
args[4].length !== 66 ||
|
||||
args[5].length !== 66) {
|
||||
return { valid: false, reason: 'The length one of the signals is incorrect' }
|
||||
}
|
||||
|
||||
return { valid: true }
|
||||
}
|
||||
|
||||
function isKnownContract(contract) {
|
||||
for (let i = 0; i < mixers.length; i++) {
|
||||
if (mixers[i].address === contract) {
|
||||
return { valid: true, currency: mixers[i].currency }
|
||||
}
|
||||
}
|
||||
return { valid: false }
|
||||
}
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
function isEnoughFee({ gas, gasPrices, currency, refund, ethPrices, fee }) {
|
||||
const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN(gas))
|
||||
let desiredFee
|
||||
switch (currency) {
|
||||
case 'eth': {
|
||||
desiredFee = expense
|
||||
break
|
||||
}
|
||||
case 'dai': {
|
||||
desiredFee = expense.add(refund).mul(toBN(ethPrices.dai)).div(toBN(10 ** 18))
|
||||
break
|
||||
}
|
||||
}
|
||||
console.log('desired fee', desiredFee)
|
||||
if (fee.lt(desiredFee)) {
|
||||
return { isEnough: false, reason: 'Not enough fee' }
|
||||
}
|
||||
return { isEnough: true }
|
||||
}
|
||||
|
||||
module.exports = { isValidProof, isValidArgs, sleep, isKnownContract, isEnoughFee }
|
Loading…
Add table
Add a link
Reference in a new issue