Skip to content

Instantly share code, notes, and snippets.

@gluk64 gluk64/reason.js
Last active Nov 17, 2019

Embed
What would you like to do?
// 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
@justinjmoses

This comment has been minimized.

Copy link

justinjmoses commented May 18, 2019

Awesome - just what I was looking for!

@justinjmoses

This comment has been minimized.

Copy link

justinjmoses commented May 18, 2019

Though you can use ethers.utils.toUtf8String('0x' + code.substr(138)); on L27 and not need hex_to_ascii at all

@amaurer

This comment has been minimized.

Copy link

amaurer commented May 30, 2019

Thanks for putting this together. I forked and made a few mods including the above recommendation.

@justinjmoses

This comment has been minimized.

Copy link

justinjmoses commented May 30, 2019

Nice. I bundled this up with some other features into https://github.com/justinjmoses/eth-reveal/

@msigwart

This comment has been minimized.

Copy link

msigwart commented Jul 16, 2019

Thanks, this is quite useful! I also forked and added a Go version of the script: https://gist.github.com/msigwart/d3e374a64c8718f8ac5ec04b5093597f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.