Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 38b166c

Browse files
authored
txHelper error handling additions (#4199)
* adds a bit more error handling for non-catastrphic error states in transaction handling * fixes null ref * adds isValidHash()
1 parent b84a8c8 commit 38b166c

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

packages/graphql/src/mutations/_txHelper.js

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ export async function checkMetaMask(from) {
4141
// to hang
4242
const isServer = typeof window === 'undefined'
4343

44+
/**
45+
* Check if the hash is a valid transaction hash
46+
* @param hash {string} - A suspected transaction hash
47+
* @returns {boolean} - if hash was a valid transaction hash
48+
*/
49+
function isValidHash(hash) {
50+
return typeof hash === 'string' && [66, 64].includes(hash.length)
51+
}
52+
4453
// Should we try to use the relayer
4554
function useRelayer({ mutation, value }) {
4655
if (isServer) return
@@ -311,6 +320,11 @@ async function handleCallbacks({ callbacks, val }) {
311320
async function handleHash({ hash, from, mutation }) {
312321
debug(`got hash ${hash}`)
313322

323+
if (!isValidHash(hash)) {
324+
console.error(`Received hash: ${hash}`)
325+
throw new Error('handleHash got invalid tx hash!')
326+
}
327+
314328
contracts.transactions[from] = contracts.transactions[from] || []
315329
contracts.transactions[from].unshift({
316330
id: hash,
@@ -441,7 +455,7 @@ async function sendViaRelayer({
441455
throw new Error('No transaction hash from relayer!')
442456
}
443457
const txHash = resp.id
444-
if (typeof txHash !== 'string' || ![66, 64].includes(txHash.length)) {
458+
if (!isValidHash(txHash)) {
445459
throw new Error('Invalid transaction hash returned by relayer!')
446460
}
447461

@@ -465,7 +479,12 @@ async function sendViaRelayer({
465479
let receipt
466480
const responseBlocks = async ({ newBlock }) => {
467481
if (!receipt) {
468-
receipt = await web3.eth.getTransactionReceipt(txHash)
482+
try {
483+
receipt = await web3.eth.getTransactionReceipt(txHash)
484+
} catch (err) {
485+
console.error(err)
486+
return
487+
}
469488
}
470489
/**
471490
* There some potential races going on here, where we can't use === for
@@ -563,10 +582,7 @@ async function sendViaWeb3({
563582
} else if (txHash === null) {
564583
console.error(tx)
565584
throw new Error('Transaction hash returned null. Invalid tx?')
566-
} else if (
567-
typeof txHash !== 'string' ||
568-
![66, 64].includes(txHash.length)
569-
) {
585+
} else if (!isValidHash(txHash)) {
570586
console.error('Invaild hash: ', txHash)
571587
throw new Error('Invalid transaction hash returned by web3!')
572588
}
@@ -582,10 +598,20 @@ async function sendViaWeb3({
582598
})
583599
})
584600
.on('error', function(error) {
601+
/**
602+
* This seems like an internal web3.js error and not caused by our code.
603+
* it should be temporary, so we're just gonna ignore it.
604+
*/
605+
if (error && error.message && error.message.includes('Invalid params')) {
606+
console.error(error.message)
607+
return
608+
}
609+
585610
if (txHash) {
611+
const hashTosend = isValidHash(txHash) ? txHash : null
586612
pubsub.publish('TRANSACTION_UPDATED', {
587613
transactionUpdated: {
588-
id: txHash,
614+
id: hashTosend,
589615
status: 'error',
590616
error,
591617
mutation

0 commit comments

Comments
 (0)