0

I have written a simply while loop to return the total transaction value of a block, however, it takes almost 30 seconds.

Therefore I am looking to the community for a much faster way of retrieving said info.

Below is my script and I thank you all for taking the time to read - I am very new to blockchain:

from web3 import Web3
import pandas as pd

block_hegiht = 13179360
block = w3.eth.get_block(block_height)
block_tranasctions = (block['transactions'])  
transLen = len(block['transactions'])

count = transLen
transValue_eth_list = []

while count >0:

    count = count - 1
    transId = w3.eth.get_transaction_by_block(block_height,count)
    transValue_wei = transId['value'] # get transaction value in wei
    transValue_eth = w3.fromWei(transValue_wei, 'ether') # convert transaction value from wei to eth
    transValue_eth = pd.to_numeric(transValue_eth) # convert from hex decimal to numeric

    if transValue_eth > 0: # where value of transaction is greater than 0.00 eth

        transValue_eth_list.append(transValue_eth) # append eth transaction value to list
            
block_transValue_eth = sum(transValue_eth_list) # sum of all transactions in block
print(block_transValue_eth)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.