From 3f9b0ed49904dc1cd3af92fec31c81d4b55df7a4 Mon Sep 17 00:00:00 2001 From: Alexey Date: Tue, 23 Jul 2019 19:07:33 +0300 Subject: [PATCH] error handing --- .env.example | 2 +- config.js | 2 +- index.js | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.env.example b/.env.example index 90d859c..e7cef8f 100644 --- a/.env.example +++ b/.env.example @@ -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 \ No newline at end of file diff --git a/config.js b/config.js index 88f2130..0348498 100644 --- a/config.js +++ b/config.js @@ -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/'] } \ No newline at end of file diff --git a/index.js b/index.js index dc079f6..812cf14 100644 --- a/index.js +++ b/index.js @@ -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.' }) } })