tornado-relayer/utils.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-07-18 14:43:26 +02:00
const fetch = require('node-fetch')
2019-09-19 17:21:57 +02:00
const { isHexStrict, hexToNumberString } = require('web3-utils')
2019-09-19 21:56:45 +02:00
const { gasOracleUrls, ethdaiAddress, mixers } = require('./config')
2019-09-19 17:21:57 +02:00
const oracleABI = require('./abis/ETHDAIOracle.json')
2019-07-18 14:43:26 +02: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 17:21:57 +02: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-07-18 14:43:26 +02:00
function isValidProof(proof) {
// validator expects `websnarkUtils.toSolidityInput(proof)` output
2019-10-06 09:55:24 +02:00
if (!(proof.proof && proof.publicSignals)) {
return { valid: false, reason: 'One of inputs is empty. There must be proof and publicSignals' }
2019-07-18 14:43:26 +02:00
}
Object.keys(proof).forEach(key => {
if (!Array.isArray(proof[key])) {
return { valid: false, reason: `Corrupted ${key}` }
}
})
2019-10-06 09:55:24 +02:00
if (proof.proof.length !== 8) {
return { valid: false, reason: 'Corrupted proof' }
2019-07-18 14:43:26 +02:00
}
2019-10-06 09:55:24 +02:00
if (proof.publicSignals.length !== 6) {
2019-07-18 14:43:26 +02:00
return { valid: false, reason: 'Corrupted publicSignals' }
}
for (let [key, input] of Object.entries(proof)) {
for (let i = 0; i < input.length; i++ ) {
if (!isHexStrict(input[i]) || input[i].length !== 66) {
return { valid: false, reason: `Corrupted ${key}` }
}
}
}
return { valid: true }
}
2019-09-19 21:56:45 +02: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 18:43:24 +02:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
2019-09-19 21:56:45 +02:00
module.exports = { fetchGasPrice, isValidProof, sleep, fetchDAIprice, isKnownContract }