tornado-relayer/utils.js

120 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-07-18 08:43:26 -04:00
const fetch = require('node-fetch')
2019-11-15 04:01:59 -05:00
const { isHexStrict, hexToNumberString, toBN, toWei } = require('web3-utils')
2019-09-19 15:56:45 -04:00
const { gasOracleUrls, ethdaiAddress, mixers } = require('./config')
2019-09-19 11:21:57 -04:00
const oracleABI = require('./abis/ETHDAIOracle.json')
2019-07-18 08:43:26 -04:00
async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) {
oracleIndex = (oracleIndex + 1) % gasOracleUrls.length
try {
const response = await fetch(gasOracleUrls[oracleIndex])
if (response.status === 200) {
const json = await response.json()
if (json.slow) {
gasPrices.low = Number(json.slow)
}
if (json.safeLow) {
gasPrices.low = Number(json.safeLow)
}
if (json.standard) {
gasPrices.standard = Number(json.standard)
}
if (json.fast) {
gasPrices.fast = Number(json.fast)
}
} else {
throw Error('Fetch gasPrice failed')
}
setTimeout(() => fetchGasPrice({ gasPrices, oracleIndex }), 15000)
} catch (e) {
setTimeout(() => fetchGasPrice({ gasPrices, oracleIndex }), 15000)
}
}
2019-09-19 11:21:57 -04:00
async function fetchDAIprice({ ethPriceInDai, web3 }) {
try {
const ethDaiInstance = web3.eth.Contract(oracleABI, ethdaiAddress)
const price = await ethDaiInstance.methods.getMedianPrice().call()
ethPriceInDai = hexToNumberString(price)
setTimeout(() => fetchDAIprice({ ethPriceInDai, web3 }), 1000 * 30)
} catch(e) {
setTimeout(() => fetchDAIprice({ ethPriceInDai, web3 }), 1000 * 30)
}
}
2019-11-14 08:24:01 -05:00
function isValidProof(proof) {
2019-07-18 08:43:26 -04:00
// validator expects `websnarkUtils.toSolidityInput(proof)` output
2019-11-14 08:24:01 -05:00
if (!(proof)) {
return { valid: false, reason: 'The proof is empty.' }
2019-07-18 08:43:26 -04:00
}
2019-11-14 08:24:01 -05:00
if (!isHexStrict(proof) || proof.length !== 2 + 2 * 8 * 32) {
2019-10-06 03:55:24 -04:00
return { valid: false, reason: 'Corrupted proof' }
2019-07-18 08:43:26 -04:00
}
2019-11-14 08:24:01 -05:00
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' }
2019-07-18 08:43:26 -04:00
}
2019-11-14 08:24:01 -05:00
for(let signal of args) {
2019-11-07 20:25:43 -05:00
if (!isHexStrict(signal)) {
2019-11-14 08:24:01 -05:00
return { valid: false, reason: `Corrupted signal ${signal}` }
2019-07-18 08:43:26 -04:00
}
}
2019-11-07 20:25:43 -05:00
2019-11-14 08:24:01 -05:00
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' }
2019-11-07 20:25:43 -05:00
}
2019-07-18 08:43:26 -04:00
return { valid: true }
}
2019-09-19 15:56:45 -04:00
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 }
}
2019-07-23 12:43:24 -04:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
2019-11-15 04:01:59 -05:00
function isEnoughFee({ gas, gasPrices, currency, refund, ethPriceInDai, 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(ethPriceInDai)).div(toBN(10 ** 18))
break
}
}
if (fee.lt(desiredFee)) {
return { isEnough: false, reason: 'Not enough fee' }
}
return { isEnough: true }
}
module.exports = { fetchGasPrice, isValidProof, isValidArgs, sleep, fetchDAIprice, isKnownContract, isEnoughFee }