error handing

This commit is contained in:
Alexey 2019-07-23 19:07:33 +03:00
parent 1462313b14
commit 3f9b0ed499
3 changed files with 11 additions and 10 deletions

View File

@ -1,6 +1,6 @@
NET_ID=42
RPC_URL=https://kovan.infura.io/v3/a3f4d001c1fc4a359ea70dd27fd9cb51
PRIVATE_KEY=
MIXER_ADDRESS=0xa01dCfABF44674f2f60e064e9F4B1300a87753c8
MIXER_ADDRESS=0x5375776Da34366b9A4C8a44F6303B44b5Ee2F07a
# in wei
DESIRED_FEE=10000000000000000

View File

@ -6,6 +6,6 @@ module.exports = {
privateKey: process.env.PRIVATE_KEY,
mixerAddress: process.env.MIXER_ADDRESS,
desiredFee: process.env.DESIRED_FEE || 10000000000000000, // 0.01 ETH
defaultGasPrice: 1,
defaultGasPrice: 2,
gasOracleUrls: ['https://www.etherchain.org/api/gasPriceOracle', 'https://gasprice.poa.network/']
}

View File

@ -10,7 +10,7 @@ app.use(function(req, res, next) {
next()
})
const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice, desiredFee } = require('./config')
const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice } = require('./config')
const { fetchGasPrice, isValidProof } = require('./utils')
const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 })
@ -31,27 +31,28 @@ app.post('/relay', async (req, resp) => {
const { valid , reason } = isValidProof(req.body)
if (!valid) {
console.log('Proof is invalid:', reason)
return resp.status(400).send('Proof is invalid')
return resp.status(400).json({ error: 'Proof is invalid' })
}
let { pi_a, pi_b, pi_c, publicSignals } = req.body
const fee = toBN(publicSignals[3])
if (fee.lt(toBN(desiredFee))) {
const desiredFee = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000'))
if (fee.lt(desiredFee)) {
console.log('Fee is too low')
return resp.status(400).send('Fee is too low')
return resp.status(400).json({ error: 'Fee is too low. Try to resend.' })
}
try {
const nullifier = publicSignals[1]
const isSpent = await mixer.methods.isSpent(nullifier).call()
if (isSpent) {
throw new Error('The note has been spent')
return resp.status(400).json({ error: 'The note has been spent.' })
}
const root = publicSignals[0]
const isKnownRoot = await mixer.methods.isKnownRoot(root).call()
if (!isKnownRoot) {
throw new Error('The merkle root is too old or invalid')
return resp.status(400).json({ error: 'The merkle root is too old or invalid.' })
}
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({
@ -63,11 +64,11 @@ app.post('/relay', async (req, resp) => {
resp.json({ txHash: hash })
}).on('error', function(e){
console.log(e)
resp.status(400).send('Proof is malformed')
return resp.status(400).json({ error: 'Proof is malformed.' })
})
} catch (e) {
console.log(e)
resp.status(400).send('Proof is malformed or spent')
return resp.status(400).json({ error: 'Proof is malformed or spent.' })
}
})