Skip to content

Commit

Permalink
add using collector API to get correct transactionIndex for eth_getTr…
Browse files Browse the repository at this point in the history
…ansactionByHash and eth_getTransactionReceipt
  • Loading branch information
tonititi committed May 9, 2024
1 parent 3d06c61 commit 432d07c
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,29 @@ export const methods = {
// result found, skipping querying from archiver, validator and explorer.
success = true
retry = 100
// start to query getBlock to get transactionIndex
if (result) {
try {
// getBlock
const txBlockNumber = result.blockNumber
const blockResp = await collectorAPI.getBlock(txBlockNumber, 'hex_num', true)
if (blockResp) {
const txIndex = blockResp.transactions.findIndex((tx) => tx === txHash)
if (txIndex !== -1) {
result.transactionIndex = '0x' + txIndex.toString(16)
}
}
} catch (e) {
if (verbose)
console.log(
'try to get transactionIndex using collector but fail. Return default transactionIndex'
)
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
callback(null, result)
countSuccessResponse(api_name, 'success', 'collector')
return
}
}
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
callback(null, result)
countSuccessResponse(api_name, 'success', 'collector')
Expand Down Expand Up @@ -2506,6 +2529,29 @@ export const methods = {
} else if (!isErr(result)) {
// result found, skipping querying from archiver, validator and explorer.
result = extractTransactionReceiptObject(result)
// start to query getBlock to get transactionIndex
if (result) {
try {
// getBlock
const txBlockNumber = result.blockNumber
const blockResp = await collectorAPI.getBlock(txBlockNumber, 'hex_num', true)
if (blockResp) {
const txIndex = blockResp.transactions.findIndex((tx) => tx === txHash)
if (txIndex !== -1) {
result.transactionIndex = '0x' + txIndex.toString(16)
}
}
} catch (e) {
if (verbose)
console.log(
'try to get transactionIndex using collector but fail. Return default transactionIndex'
)
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
callback(null, result)
countSuccessResponse(api_name, 'success', 'collector')
return
}
}
logEventEmitter.emit('fn_end', ticket, { success: true }, performance.now())
callback(null, result)
countSuccessResponse(api_name, 'success', 'collector')
Expand Down Expand Up @@ -3632,7 +3678,7 @@ export const methods = {
countFailedResponse(api_name, 'Invalid address')
return
}

if (!isValidAddress(callObj.from)) {
if (verbose) console.log('Invalid params: `from` is not valid address', callObj.from)
callback({ code: -32000, message: 'Invalid params: `from` is not valid address' }, null)
Expand Down

0 comments on commit 432d07c

Please sign in to comment.