-
-
Save gluk64/fdea559472d957f1138ed93bcbc6f78a to your computer and use it in GitHub Desktop.
| // This is universal, works with Infura -- set provider accordingly | |
| const ethers = require('ethers') | |
| //const provider = ethers.getDefaultProvider('rinkeby') | |
| const provider = new ethers.providers.JsonRpcProvider(process.env.WEB3_URL) | |
| function hex_to_ascii(str1) { | |
| var hex = str1.toString(); | |
| var str = ''; | |
| for (var n = 0; n < hex.length; n += 2) { | |
| str += String.fromCharCode(parseInt(hex.substr(n, 2), 16)); | |
| } | |
| return str; | |
| } | |
| async function reason() { | |
| var args = process.argv.slice(2) | |
| let hash = args[0] | |
| console.log('tx hash:', hash) | |
| console.log('provider:', process.env.WEB3_URL) | |
| let tx = await provider.getTransaction(hash) | |
| if (!tx) { | |
| console.log('tx not found') | |
| } else { | |
| let code = await provider.call(tx, tx.blockNumber) | |
| let reason = hex_to_ascii(code.substr(138)) | |
| console.log('revert reason:', reason) | |
| } | |
| } | |
| reason() |
| #!/bin/bash | |
| # This is for geth | |
| # Fetching revert reason -- https://ethereum.stackexchange.com/questions/48383/how-to-receive-revert-reason-for-past-transactions | |
| if [ -z "$1" ] | |
| then | |
| echo "Usage: revert-reason <TX_HASH>" | |
| exit | |
| fi | |
| TX=$1 | |
| SCRIPT=" tx = eth.getTransaction( \"$TX\" ); tx.data = tx.input; eth.call(tx, tx.blockNumber)" | |
| geth --exec "$SCRIPT" attach http://localhost:8545 | cut -d '"' -f 2 | cut -c139- | xxd -r -p | |
| echo |
Should this snippet work also with latest 5.4 ethers.js please?
I'm trying it together with typescript and there is type issue between getTransaction() returning TransactionResponse and call() method requesting TransactionRequest.
When I tried to re-type it then I'm still receiving only:
code:'CALL_EXCEPTION'
data:'0x'
reason:'missing revert data in call exception'
Though you can use
ethers.utils.toUtf8String('0x' + code.substr(138));on L27 and not needhex_to_asciiat all
Wow it works!! But is this really all I need?
Should this snippet work also with latest 5.4 ethers.js please?
I'm trying it together with typescript and there is type issue between
getTransaction()returningTransactionResponseandcall()method requestingTransactionRequest.When I tried to re-type it then I'm still receiving only:
code:'CALL_EXCEPTION' data:'0x' reason:'missing revert data in call exception'
same with me
This works for me:
const tx = await provider.getTransaction(txHash)
const response = await provider.call(
{
to: tx.to,
from: tx.from,
nonce: tx.nonce,
gasLimit: tx.gasLimit,
gasPrice: tx.gasPrice,
data: tx.data,
value: tx.value,
chainId: tx.chainId,
type: tx.type ?? undefined,
accessList: tx.accessList,
},
tx.blockNumber,
)
// This should be your error code
const firstFourBytes = response.slice(0, 10)
const tx = await provider.getTransaction(txHash)
const response = await provider.call(
{
to: tx.to,
from: tx.from,
nonce: tx.nonce,
gasLimit: tx.gasLimit,
gasPrice: tx.gasPrice,
data: tx.data,
value: tx.value,
chainId: tx.chainId,
type: tx.type ?? undefined,
accessList: tx.accessList,
},
tx.blockNumber,
)
const reason = ethers.utils.toUtf8String('0x' + response.substring(138))


The bash script gives an error