Getting Transaction status via Moralis endpoint

Hello, thanks for your help. I’m trying to get the confirmation status for transactions in python and I’m not sure which endpoint to use. I tried the /{address} endpoint while calling “receipt_status” and it doesn’t work. Bscscan api works but it keeps confirming failed transactions as though they were successful. Idk if I’m missing something here…pls advise… code and error below.
Code:

txHash = "Insert any txn hash"
chain = "bsc"

checkStatus = 'https://deep-index.moralis.io/api/v2/' + txHash + '?chain=' + chain

statusResponse = requests.request("GET", checkStatus, headers=config.moralisAPI)

txStatus = statusResponse.json()['result']['receipt_status']
print(txStatus) ```

Error:

C:/Users/Owner/Documents/BlockchainPy/Scripts/python.exe c:/Users/Owner/Documents/BlockchainPy/MyCodes/scratchTest.py
Traceback (most recent call last):
File “c:/Users/Owner/Documents/BlockchainPy/MyCodes/scratchTest.py”, line 27, in
statusResponse = requests.request(“GET”, checkStatus, headers=config.moralisAPI)
File “C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\requests\api.py”, line 61, in request
return session.request(method=method, url=url, **kwargs)
File “C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\requests\sessions.py”, line 528, in request
prep = self.prepare_request(req)
File “C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\requests\sessions.py”, line 456, in prepare_request
p.prepare(
File “C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\requests\models.py”, line 317, in prepare
self.prepare_headers(headers)
File “C:\Users\Owner\Documents\BlockchainPy\lib\site-packages\requests\models.py”, line 449, in prepare_headers
for header in headers.items():
AttributeError: ‘set’ object has no attribute ‘items’
(BlockchainPy)```

Thanks in advance!

Hi,
Can you also provide the transaction ids that you tried?

Hello there, I just picked random txn hash to test it out. Here’s one 0xfb4b9200a7f3faf717db5cdb137acc66f94023bb2b8ef5e2b96799b09d7761d2

Here’s a failed txn as well…
0xf10d04c6ea144fe60347c5bea2cb6aa32db8bc39e6d14ff051a84225af513178

you can use speedy nodes for that, for example:

curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xf10d04c6ea144fe60347c5bea2cb6aa32db8bc39e6d14ff051a84225af513178"],"id":1}' https://speedy-nodes-nyc.moralis.io/<your_speedy_node_id_here>/bsc/mainnet/archive

{"jsonrpc":"2.0","id":1,"result":{"blockHash":"0x9256392d482de86f79000873f61e0f967a331c9a819415aea3f773f6c2d536ea","blockNumber":"0x9b74bd","contractAddress":null,"cumulativeGasUsed":"0x20717cf","from":"0x93400f40deeef4696e4d447ade2eeff232491ed1","gasUsed":"0x57bd","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0x0","to":"0x16729528002f37922b79795c732d28feee682698","transactionHash":"0xf10d04c6ea144fe60347c5bea2cb6aa32db8bc39e6d14ff051a84225af513178","transactionIndex":"0xe1","type":"0x0"}}

note that you’ll have you use your specific speedy node where I added https://speedy-nodes-nyc.moralis.io/<your_speedy_node_id_here>/bsc/mainnet/archive

Nice! I just pasted it in my editor (see screenshot). What steps do I need to take to get rid of the undefined variables (noob question here lol)?

I don’t understand your question.
Here is a python example for what you want to do:

import requests

post_data = '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0xf10d04c6ea144fe60347c5bea2cb6aa32db8bc39e6d14ff051a84225af513178"],"id":1}'
speedy_node = 'https://speedy-nodes-nyc.moralis.io/<!!!!!!your_id_here>/bsc/mainnet/archive'

headers = {
    "Content-Type": "application/json"
    }
statusResponse = requests.request("POST", speedy_node, headers=headers, data=post_data)


data = statusResponse.json()
print(data)
print("result=", data['result']['status'])

1 Like

Great! this is exactly what I need. Sorry for the confusion, I’m fairly new to coding and wasn’t sure on how to apply it. Thanks for help sir.

So I applied it as suggested and I’m getting a Keyerror: ‘result’ below. Any ideas what could be wrong?

Code:

from web3 import Web3
import requests
import json


wss = 'wss://speedy-nodes-nyc.moralis.io/dae030584ff490fb9c9a0c9b/bsc/mainnet/ws'
web3 = Web3(Web3.WebsocketProvider(wss))


txHash = "0xf10d04c6ea144fe60347c5bea2cb6aa32db8bc39e6d14ff051a84225af513178"

chain = "bsc"

speedy_node = 'https://speedy-nodes-nyc.moralis.io/dae030584ff490fb9c9a0c9b/' + chain + '/mainnet/archive'

post_data = '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":[txHash],"id":1}'

headers = {
    "Content-Type": "application/json"
    }
statusResponse = requests.request("POST", speedy_node, headers=headers, data=post_data)

data = statusResponse.json()

print(data)
print("result=", data['result']['status'])

Error:

(BlockchainPy)
[email protected] MINGW64 ~/Documents/BlockchainPy
$ C:/Users/Owner/Documents/BlockchainPy/Scripts/python.exe c:/Users/Owner/Documents/BlockchainPy/MyCodes/scratchTest.py
{'jsonrpc': '2.0', 'id': None, 'error': {'code': -32700, 'message': 'parse error'}}
Traceback (most recent call last):
  File "c:/Users/Owner/Documents/BlockchainPy/MyCodes/scratchTest.py", line 27, in <module>
    print("result=", data['result']['status'])
KeyError: 'result'

you need to insert there that txHash value

Yes!!! that worked. Thank you so much! Just one last set of questions pls…

So this script is just a piece of a larger script where multiple transactions will run. Is there a way to get around not having to hardcode the ‘txHash’ like we just did so that it can freely be interchangeable?

Second question: Is there a difference in the router ABIs across chains (bsc, eth and polygon) ? I was wondering if I can just use one of them and reference other chains to it.

Thanks again

for first question:

you could use:
post_data = '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":[%s],"id":1}' % txHash

for second question: I don’t understand what you mean by router ABIs.

I had the same format for the first suggestion earlier. That’s what returned the KeyError: ‘result’ initially. The second suggestion returned same KeyError: ‘result’. Are they working on your end?

Sorry, I was referring to the corresponding ABIs of Uniswap, PancakeSwap and QuickSwap router addresses. These guys…

uniswapRouterAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
pancakeswapRouterAddress = "0x10ED43C718714eb63d5aA57B78B54704E256024E" 
quickswapRouterAddress = "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff"

I was wondering if their corresponding ABIs are similar or the same? That way I can just use one of them and don’t have to reference each one specifically.

In python if you have

a = "5";
b = "34%s6" % a;
print(b);

then you will get 3456 printed

Related to those ABIs, I don’t know if they are identical, but you can check if they are identical or not, you save each one in a file and then you compare the contents of the files to see if are identical or not.
You can compute the md5 hash of each file to do the comparison for example.

Oh I see, the problem was, I had [%s] instead of ["%s"] . Now it works. Thank you!

Great suggestion, I’ll try that and that should suffice. Thanks again.

1 Like