Getting Transactions of EOA in real time in python

Im trying to monitor EOA for the latest trasnactions and than send a notification.

I have this python code here:
from web3 import Web3
from web3.middleware import geth_poa_middleware
import time


web3 = Web3(Web3.HTTPProvider('bsc node/'))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)

monitor = web3.toChecksumAddress('EOA to monitor')


def getBlock():
    getLatest = web3.eth.get_block('latest')
    print(web3.eth.get_block_number())
    latest = getLatest.transactions
  
    for i in latest:
        txHash = Web3.toJSON(i).strip('"')
        tx = web3.eth.get_transaction(txHash)
        print(tx['from'])
        if tx['from'] == monitor:
            print('new tx sent')

while True:
    getBlock()
    time.sleep(1)

This code kinda works but the problem I have is that, it takes to long to process the transactions in a block, which makes the script skip blocks, hence I miss transactions.
Is there a better way to monitor accounts and the transactions in real time using python?

https://admin.moralis.io/web3apis

there is an api endpoint there, first of them, that returns the data from a block number, you could use that API endpoint to get the information from a block

you can make REST http requests to that API endpoint

you could also use other API endpoints if they help you more

I do get all of that info with my script already, the problem is that i need to process all the transactions in the block and this takes to long.

how do you identify the transactions that you are interested in?

When I run the script, I get the latestBlock, based on the latest block I can get all the transactions that are in this block, than I get all the transactionHashes of every transaction in this block.
Based on txHash I can get the txInfo, like to, from, value, gas etc.
And than I just check if any transaction matches my from address(address I want to monitor)
but the problem is that there are to many transactions in a block and it takes to long to process all that data.
And when Im done with processing 1 block, I keep missing a bunc of blocks.

Even in your web3API example there are about 250 tx included in 1 block and python isnt that quick, therefore I keep missing blocks and when I miss blocks I might miss transactions from my β€˜from’ address

I tried filter in web3py, but this works only for contract events, so I think its actually impossible to monitor transactions of an address in real time.
I hope Im wrong and i know I must be wrong, but I cant think of a solution

does this API endpoint help you?

/{address}
getTransactions

Get native transactions ordered by block number in descending order.

I dont find the getTransaction endpoint anywhere
nvm I found the endpoint, but dont I have to run this every second?

you will have to run it from time to time

we are working on a separate API for events and transactions, it is not yet ready

Its not really what I have in mind, but I can definitly make it work

thanks for your help, I will do some testing now

Just 1 more thing, how do I send the headers

I do this but it doesnt seem to work


headers = {

'X-API-Key: apai key',

'accept: application/json'

}

address = 'EOA'

url = 'https://deep-index.moralis.io/api/v2/'+ address +'?chain=bsc'

response = requests.get(url, headers=headers)

print(response.json())

Error I get
AttributeError: 'set' object has no attribute 'items'

you can test it directly on that web3api interface, for address you have to specify the exact address in hex format

headers is a dict, you have to separate the key from the value, not to have a list of strings

got it, thanks, I didnt see that, Im already so tired

header={β€˜X-API-KEY’:β€˜API key’}
is the correct format

1 Like