Reputation: 3885
I want to crawl the ETH blockchain and to get transactions that are made to or from a given wallet address.
I wrote a code with a help of web3.py
but I'm getting some strange results.
I'm finding the transactions but the value of the transaction is 0.
How is that possible?
I have head that there are differences between wallet addresses and smart contract addresses, but I can not find how to make a difference between them. Also, how to crawl amount of ERC20 tokens that are in the transaction?
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-3000
end_block = web3.eth.blockNumber
for block_num in range(start_block, end_block):
current_block = block_num
remaining_num_of_blocks = end_block - block_num
progress = round(100*(block_num - start_block) / (end_block - start_block), 2)
#print('Fetching block {}, remaining:{}, progress: {}%'.format(current_block, remaining_num_of_blocks, progress))
# Get block with specific number with all transactions
block = web3.eth.getBlock(block_num, full_transactions=True)
block_time = block.timestamp
block_time = datetime.fromtimestamp(block_time)
print("Time of the processed block:", block_time)
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()
How is this possible, I have crawled the last 3000 blocks, but I have also tried with the last 25000 blocks and the result is the same. This is an example of the transaction that I'm getting.
AttributeDict({'blockHash': HexBytes('0xb8a0c912053a6e89658158a30912fc9974e280b1009967ed0c5658b89324aaeb'), 'blockNumber': 13385037,
'from': '0xC0e32e544A82D9396B0b52d90B484f831f89Fd83',
'gas': 106070,
'gasPrice': 70000000000,
'hash': HexBytes('0x62f0eefbedc7445de511a1d09682f7674152335bce0eec52c8c3f517990e5602'),
'input': '0xa9059cbb000000000000000000000000a1d8d972560c2f8144af871db508f0b0b10a3fbf0000000000000000000000000000000000000000000003ecdc77a726c9df8000',
'nonce': 446,
'r': HexBytes('0x482fde01e1a71ae4328a2ddab169e56fe9be095c9ebd26c2ddd022caedb2924d'),
's': HexBytes('0x7bce8c69d5832dbe59c9742ba53f714be8693383dee895c333855a3f85391c70'),
'to': '0xaA7a9CA87d3694B5755f213B5D04094b8d0F0A6F',
'transactionIndex': 296,
'type': '0x0',
'v': 37,
'value': 0})
Upvotes: 0
Views: 935
Reputation: 83758
Transactions sent to smart contracts do not need value. As you can see from input
it is a smart contract transaction.
Upvotes: 0