classic-relayer/index.js

94 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-07-19 13:34:51 -04:00
const { numberToHex, toWei, toHex, toBN } = require('web3-utils')
2019-07-18 10:45:33 -04:00
const Web3 = require('web3')
2019-07-18 08:43:26 -04:00
const express = require('express')
2019-07-18 10:45:33 -04:00
2019-07-18 08:43:26 -04:00
const app = express()
app.use(express.json())
2019-07-24 13:19:00 -04:00
app.use((err, req, res, next) => {
if (err) {
console.log('Invalid Request data')
res.send('Invalid Request data')
} else {
next()
}
})
2019-07-18 12:00:07 -04:00
app.use(function(req, res, next) {
2019-07-19 13:34:51 -04:00
res.header('Access-Control-Allow-Origin', '*')
2019-07-18 14:15:59 -04:00
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
})
2019-07-18 08:43:26 -04:00
2019-07-23 12:07:33 -04:00
const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice } = require('./config')
2019-09-19 11:21:57 -04:00
const { fetchGasPrice, isValidProof, fetchDAIprice } = require('./utils')
2019-07-18 08:43:26 -04:00
const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 })
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey)
web3.eth.accounts.wallet.add('0x' + privateKey)
web3.eth.defaultAccount = account.address
2019-09-19 11:21:57 -04:00
const mixerABI = require('./abis/mixerABI.json')
2019-07-18 08:43:26 -04:00
const mixer = new web3.eth.Contract(mixerABI, mixerAddress)
const gasPrices = { fast: defaultGasPrice }
2019-09-19 11:21:57 -04:00
const ethPriceInDai = toWei('200')
2019-07-18 08:43:26 -04:00
app.get('/', function (req, res) {
// just for testing purposes
2019-07-22 07:22:47 -04:00
res.send(`Tornado mixer relayer. Gas Price is ${JSON.stringify(gasPrices)}. Mixer address is ${mixerAddress}`)
2019-07-18 08:43:26 -04:00
})
app.post('/relay', async (req, resp) => {
2019-09-16 12:22:16 -04:00
console.log(JSON.stringify(req.body, null, 2))
2019-07-18 08:43:26 -04:00
const { valid , reason } = isValidProof(req.body)
if (!valid) {
console.log('Proof is invalid:', reason)
2019-07-23 12:07:33 -04:00
return resp.status(400).json({ error: 'Proof is invalid' })
2019-07-18 08:43:26 -04:00
}
let { pi_a, pi_b, pi_c, publicSignals } = req.body
2019-07-19 13:34:51 -04:00
const fee = toBN(publicSignals[3])
2019-07-23 12:07:33 -04:00
const desiredFee = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000'))
if (fee.lt(desiredFee)) {
2019-07-19 13:34:51 -04:00
console.log('Fee is too low')
2019-07-23 12:07:33 -04:00
return resp.status(400).json({ error: 'Fee is too low. Try to resend.' })
2019-07-19 13:34:51 -04:00
}
2019-07-18 08:43:26 -04:00
try {
const nullifier = publicSignals[1]
const isSpent = await mixer.methods.isSpent(nullifier).call()
if (isSpent) {
2019-07-23 12:07:33 -04:00
return resp.status(400).json({ error: 'The note has been spent.' })
2019-07-18 08:43:26 -04:00
}
2019-07-18 09:30:24 -04:00
const root = publicSignals[0]
const isKnownRoot = await mixer.methods.isKnownRoot(root).call()
if (!isKnownRoot) {
2019-07-23 12:07:33 -04:00
return resp.status(400).json({ error: 'The merkle root is too old or invalid.' })
2019-07-18 09:30:24 -04:00
}
2019-07-18 08:43:26 -04:00
const gas = await mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).estimateGas()
const result = mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).send({
gas: numberToHex(gas + 50000),
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
// TODO: nonce
})
result.once('transactionHash', function(hash){
2019-07-18 12:00:07 -04:00
resp.json({ txHash: hash })
2019-07-18 08:43:26 -04:00
}).on('error', function(e){
console.log(e)
2019-07-23 12:07:33 -04:00
return resp.status(400).json({ error: 'Proof is malformed.' })
2019-07-18 08:43:26 -04:00
})
} catch (e) {
console.log(e)
2019-07-23 12:07:33 -04:00
return resp.status(400).json({ error: 'Proof is malformed or spent.' })
2019-07-18 08:43:26 -04:00
}
})
app.listen(8000)
2019-08-03 16:36:22 -04:00
if (Number(netId) === 1) {
2019-07-18 08:43:26 -04:00
fetchGasPrice({ gasPrices })
2019-09-19 11:21:57 -04:00
fetchDAIprice({ ethPriceInDai, web3 })
2019-07-18 08:43:26 -04:00
console.log('Gas price oracle started.')
}