1

有没有办法按地址过滤交易?我试图通过将地址与事务字典键值对('from','to')匹配来做到这一点,但这需要太长时间。有没有办法更快地做到这一点?例如,分析最后 5000 个区块需要 1 个多小时。

from web3 import Web3
from datetime import datetime

# Defining url for collecting ETH transactions
infura_url = "https://mainnet.infura.io/v3/xxxxxxxxxxxx"

# Making connections with infra url
web3 = Web3(Web3.HTTPProvider(infura_url))
print("Connection is successful:", web3.isConnected())


# Wallet Address
account = "0xaa7a9ca87d3694b5755f213b5d04094b8d0f0a6f"
# Checking the balance for that address (in WEIs)
try:
    print("Input Address:", account)
    balance = web3.eth.getBalance(account)
except:
    # If its not a lowercase()
    account = Web3.toChecksumAddress(account)
    print("Input Address:", account)
    balance = web3.eth.getBalance(account)

# Print balance in WEIs
print("Balance in WEIs:", balance)

# Converting account balance to ETH
balanceWEI = web3.fromWei(balance, "ether")
print("Balance in ETH:", balanceWEI)


# Defining start block and latest block
start_block = web3.eth.blockNumber-5000
end_block = web3.eth.blockNumber


for block_num in range(start_block, end_block):

    current_block = block_num

    # Get block with specific number with all transactions
    block = web3.eth.getBlock(block_num, full_transactions=True)

    list_of_block_transactions = block.transactions
    for transaction in list_of_block_transactions:

        to_account = transaction['to']
        from_account = transaction['from']

        if to_account == account:
            print("To account:", to_account)         
            to_match = True
        else:
            to_match = False            


        if from_account == account:
            print("From account:", from_account)
            from_account = True
            
        else:
            from_account = False            

    
        if to_match == True or from_account == True:
            print("Found Transaction with HASH:", transaction['hash'])
            print("Found Transaction with HASH-HEX:", transaction['hash'].hex())
            print("Found Transaction with value:", transaction['value']) # this value is always 0
            print("Found Transaction with gas:", transaction['gas']) # this value is more than 0
            
            print()

例如,如果我的地址是:

# Wallet Address
account = "0xaa7a9ca87d3694b5755f213b5d04094b8d0f0a6f"

如果存在与该地址的交易(从该地址或到该地址的交易),我如何才能获得仅块?

4

0 回答 0