This question arose from answering Transaction Status.
In the following example, I am sending gas of 21,000 (the amount required for a regular transaction). In this situation gas == gasUsed
, and so cannot be used to determine if the contract has thrown an exception?
> eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(1.2345, "ether"), gas: 21000})
"0xc7c63b67747c0c825229ce3d36d226423adb8cab6bebe12b6d5001e0dc3f79b3"
> eth.getTransaction("0xc7c63b67747c0c825229ce3d36d226423adb8cab6bebe12b6d5001e0dc3f79b3")
{
blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: null,
from: "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
gas: 21000,
gasPrice: 20000000000,
hash: "0xc7c63b67747c0c825229ce3d36d226423adb8cab6bebe12b6d5001e0dc3f79b3",
input: "0x",
nonce: 55,
to: "0x4d5bbe7fbc80933ffa90ece988a764e41ee6d018",
transactionIndex: null,
value: 1234500000000000000
}
> eth.getTransactionReceipt("0xc7c63b67747c0c825229ce3d36d226423adb8cab6bebe12b6d5001e0dc3f79b3")
{
blockHash: "0xf0af8236ceec7ad1839d67c9934ab062a8d95fa1f88b06139f97dbdfbd1cd842",
blockNumber: 2234,
contractAddress: null,
cumulativeGasUsed: 21000,
from: "0xa7857047907d53a2e494d5f311b4b586dc6a96d2",
gasUsed: 21000,
logs: [],
root: "3280f47a0de1149ad5c5fda421faaf95f303da8a77e83c8ec6ac2b3d8ca27abc",
to: "0x4d5bbe7fbc80933ffa90ece988a764e41ee6d018",
transactionHash: "0xc7c63b67747c0c825229ce3d36d226423adb8cab6bebe12b6d5001e0dc3f79b3",
transactionIndex: 0
}
A transaction can be set with the gas being exactly the same as the gas that will be used, using web3.eth.estimateGas function to firstly estimate the required gas.
So, how can the transaction status from a thrown error be detected when gas can be exactly the same as the gasUsed for a successful transaction?
EDIT 15/06/2016 – Explaining differences from How do I know when I've run out of gas programmatically?
In this question, we already KNOW how to detect that we have run out of gas programmatically. And we already know that this method to detect an error transaction is not reliable, as gas == gasUsed
is a valid condition for a non-error transaction.
What this question is asking is what reliable alternatives are there to detect an error transaction when the "run out of gas" situation is not reliable?
Best Answer
Summary
I have sent a test transaction (#1) to firstly find out how much gas a successful smart contract transaction uses. From the results of this transaction, the gas required for a successful transaction is 26747.
I then used this 26747 gas amount in my going-to-be-successful transaction (#2). And
gasUsed == gas
.I then use this same 26747 gas amount in my going-to-be-UNSUCCESSFUL transaction (#3). And
gasUsed == gas
.The only reliable way I have found so far to easily check if a smart contract transaction is successful is to use
debug.traceTransaction
and check the last error message. If this is""
then no error occurred. If this is"Out of gas"
or"invalid jump destination (PUSH1) 2"
, then an error occurred.And here's a short bit of code to determine the status of your transaction.
The return value above will be
""
if there are no errors, or"Out of gas"
if you run out of gas.UPDATE 29/07/2016 - the
geth --fast
blockchain download does not contain the information fordebug.traceTransaction(...)
to display.Details
I'm using the following example for a smart contract that throws an exception if _value < 12345:
I flattened the source to:
I compiled and inserted the contract into the blockchain:
I sent a transaction that won't cause an error to determine the gas required:
The gas required is 26747.
I sent a transaction with
value=123456
that will NOT fail, andgas==gasUsed
:I'm using
debug.traceTransaction
to confirm that this transaction has not encountered an error.And in this situation
gas(26747) == gasUsed(26747)
I then sent a transaction with
value=123
that failed, andgas==gasUsed
:So the only easy reliable way I've found to determine if a smart contract transaction has succeeded or failed is to use
debug.traceTransaction
.Here's the output of
debug.traceTransaction
for the last transaction that failed.And here's a short script to check if the transaction passed or failed: