Skip to content

Commit

Permalink
Improve web3 and transaction error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
patitonar committed Jun 27, 2018
1 parent 1158581 commit fd5c1b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ async function main({ msg, ackMsg, nackMsg, sendToQueue }) {
nonce++
console.log(`Tx generated ${txHash} for event Tx ${job.transactionReference}`)
} catch (e) {
console.error(e)
console.error(e.message)
console.error(`Tx Failed for event Tx ${job.transactionReference}`)
failedTx.push(job)

Expand Down
34 changes: 27 additions & 7 deletions src/tx/web3.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// eslint-disable-next-line consistent-return
async function getNonce(web3, address) {
return web3.eth.getTransactionCount(address)
try {
return await web3.eth.getTransactionCount(address)
} catch (e) {
throw new Error(`Nonce cannot be obtained`)
}
}

function getBlockNumber(web3) {
return web3.eth.getBlockNumber()
async function getBlockNumber(web3) {
try {
return await web3.eth.getBlockNumber()
} catch (e) {
throw new Error(`Block Number cannot be obtained`)
}
}

async function getChainId(web3) {
Expand All @@ -15,13 +22,26 @@ async function getChainId(web3) {
}
}

function getRequiredBlockConfirmations(contract) {
return contract.methods.requiredBlockConfirmations().call()
async function getRequiredBlockConfirmations(contract) {
try {
return await contract.methods.requiredBlockConfirmations().call()
} catch (e) {
throw new Error(`Required block confirmations cannot be obtained`)
}
}

async function getEvents({ contract, event, fromBlock, toBlock }) {
try {
return await contract.getPastEvents(event, { fromBlock, toBlock })
} catch (e) {
throw new Error(`${event} events cannot be obtained`)
}
}

module.exports = {
getNonce,
getBlockNumber,
getChainId,
getRequiredBlockConfirmations
getRequiredBlockConfirmations,
getEvents
}
6 changes: 4 additions & 2 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const processDeposits = require('./events/processDeposits')
const processCollectedSignatures = require('./events/processCollectedSignatures')
const processWithdraw = require('./events/processWithdraw')
const { redis } = require('./services/redisClient')
const { getRequiredBlockConfirmations } = require('./tx/web3')
const { getRequiredBlockConfirmations, getEvents } = require('./tx/web3')
const { checkHTTPS } = require('./utils/utils')

if (process.argv.length < 3) {
Expand Down Expand Up @@ -95,7 +95,9 @@ async function main({ sendToQueue }) {
if (lastBlockToProcess <= lastProcessedBlock) {
return
}
const events = await bridgeContract.getPastEvents(config.event, {
const events = await getEvents({
contract: bridgeContract,
event: config.event,
fromBlock: lastProcessedBlock + 1,
toBlock: lastBlockToProcess
})
Expand Down

0 comments on commit fd5c1b8

Please sign in to comment.