Skip to content

Instantly share code, notes, and snippets.

@amaurer
Forked from gluk64/reason.js
Last active May 30, 2019 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaurer/ec968b4fe33fc06abc1b826a368356ad to your computer and use it in GitHub Desktop.
Save amaurer/ec968b4fe33fc06abc1b826a368356ad to your computer and use it in GitHub Desktop.
const path = require("path");
require("dotenv").config({ path: path.resolve(process.cwd(), ".env") });
const infuraRPC = `${process.env.INFURA_URL}${ process.env.INFURA_KEY }`;
const ethers = require("ethers");
const provider = new ethers.providers.JsonRpcProvider(infuraRPC);
const args = process.argv.slice(2);
const hash = (args.length >= 1)? args[0] : process.env.HASH;
console.log("tx hash:", hash);
console.log("provider:", infuraRPC);
(async function reason() {
let tx = await provider.getTransaction(hash);
if (!tx) {
console.log("tx not found");
} else {
let code = await provider.call(tx, tx.blockNumber);
let reasonResponse = ethers.utils.toUtf8String("0x" + code.substr(138));
console.log("revert reason:", reasonResponse);
}
})();
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment