Search code examples
pythonethereumweb3py

Ensuring a transaction is confirmed using Web3.py


I'm writing some python Web3 application on blockchain, and I want to be absolutely sure my transaction is stable in the blockchain.

By going through the Python Web3 library I wondered that no embedded function allow to check how many replications of the block chain have written my transaction.

I'm looking for something like:

Count_replica = web3.eth.count_replica('0x_my_transaction_hash')

The method will return an integer corresponding on how many nodes have my transaction hash


Solution

  • I finally went with a Solution hope this one is a not that so bad.

    I first went to the Eth Mainnet list provider Then i took a sample of the most stable RPC server the the best Latency and the overall best score. then simply write a validation function that check on those nodes if my transaction have been written. i take the bett that if my transaction have been written on at least 10 RPC nodes it would be stable.

    my code looks like

    from web3 import Web3    
    ListOfValidationNodes=[
            "https://eth.llamarpc.com",
            "https://endpoints.omniatech.io/v1/eth/mainnet/public",
            "https://virginia.rpc.blxrbdn.com",
            "https://uk.rpc.blxrbdn.com",
            "https://ethereum.publicnode.com",
            "https://endpoints.omniatech.io/v1/eth/mainnet/public",
            "https://eth-mainnet.public.blastapi.io",
            "https://eth-mainnet-public.unifra.io",
            "https://ethereum.blockpi.network/v1/rpc/public"
            ]
        ListOfValidationNodesConnect=[Web3(Web3.HTTPProvider(i)) for i in ListOfValidationNodes ]
        
        
        
        def MultiNodeValidation(tx_hash):
            connList=[]
            for index,i in  enumerate(ListOfValidationNodesConnect):
                try:
                    transact = dict(i.eth.get_transaction(tx_hash))
                    connList.append({"nodes":ListOfValidationNodes[index],"valid":True,"BlockNum":transact['blockNumber']})
                    
                except:
                    # transaction hash was not found on the nodes and return an error
                    connList.append({"nodes":ListOfValidationNodes[index],"valid":False,"BlockNum":None})
            return len([x for x in connList if x['valid']==True])==len(connList),connList
    

    Would appreciate some feedback :)