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)
Owner@DESKTOP-LUT5VA2 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