Given transaction hash, i want to fetch Status code ('45' in this case, refer below image) with which transaction has failed using python web3.py. I already tried these methods, they don't seem to contain needed information.
3 Answers
In ethers.js, you just need to be able to get the transaction hash value, you can get the result by the following code, When the status value in the result is 1, the transaction is successful, and the status value is 0, the transaction fails.
const result = await provider.getTransactionReceipt(txHash)
The result should be like this
{
to: '0x55d398326f99059fF775485246999027B3197955',
from: '0x25dce14C8A77D71BFF84346dDb4e87a912d21424',
contractAddress: null,
transactionIndex: 0,
gasUsed: BigNumber { _hex: '0xad22', _isBigNumber: true },
logsBloom: '0x00000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000200000000000000000000000000000020000000000000080000000020000000000000000000000000000000000000000000000000000000000000100000000200000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xa688cb46a56b92de47c3786ff6ba26a0cbb1b5365ccbe1708ee62e9ba555d66f',
transactionHash: '0x3c36c5b2418cb43d1b461a51a2c97fb7126c92677c79a47a3b27d16aab7851e7',
logs: [
{
transactionIndex: 0,
blockNumber: 18265542,
transactionHash: '0x3c36c5b2418cb43d1b461a51a2c97fb7126c92677c79a47a3b27d16aab7851e7',
address: '0x55d398326f99059fF775485246999027B3197955',
topics: [Array],
data: '0x0000000000000012725dd1d243aba0e75fe645cc4873f9e65afe688c928e1f21',
logIndex: 0,
blockHash: '0xa688cb46a56b92de47c3786ff6ba26a0cbb1b5365ccbe1708ee62e9ba555d66f'
}
],
blockNumber: 18265542,
confirmations: 1,
cumulativeGasUsed: BigNumber { _hex: '0xad22', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x02540be400', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}
By the way, this transaction was simulated on tenderly so you can't check it on Blockchain Browser :)
-
I dont want whether transaction was successful or not. (status 0 or 1). I want code with which it failed (in this case 45) Jun 3, 2022 at 9:19
-
I found this code on github, may be helpful to you :) gist.github.com/gluk64/fdea559472d957f1138ed93bcbc6f78a Jun 3, 2022 at 10:29
Let's clear something first which can be misleading. Transaction status code in the term of ETH tx receipts is an integer, 0
for failed, 1
for successful tx's. If I am right this is not exactly what you need in this situation. (You can use the tx receipt to retrieve the error message, or just simply check on BSCScan as you did)
What you are looking for is the result of an error handling function in the Solidity code of the smart contract you are calling. For instance the statement in a require function wasn't true. In a very basic code it is something like this:
function test_function() external {
uint a = 1;
uint b = 2;
require(a == b, "45");
}
In the above case the code would generate the same error you got.
The exact answer for your question is, that you can easily find the reason of the issue if the contract you call is verified (it's source code is public on BSCScan).
- You get the contract code from the BSCScan page of the contract
- Search for the
"45"
text in the code. You will clearly see the full function and can investigate the issue.
This is the easiest solution imo. You can also find the function by signature or decoding the input of the receipt, but that would be an overkill.
In web3.py
tx_hash='0x......................'
tx=web3.eth.getTransaction(tx_hash)
tx= {k:v for k,v in tx.items() if str(type(v))!="<class 'hexbytes.main.HexBytes'>"}
web3.eth.call(tx,tx['blockNumber'])
-
2Can you explain what the code is doing? It seems an overcomplicated way to retrieve a transaction.– Ismael ♦Feb 12 at 2:33
-
the first line it your transaction hash, second line get the data you sent to make the transaction. the third line strips out anything that is a HexBytes object (it fails otherwise). the forth line calls (simulates) the original transaction data at the block when it was originally made. it will return the error message for you. Feb 14 at 12:01