I have developed django project that have local ethereum wallet using web3.py. I want to get transaction history of my wallet.
But I have no way. Please help me.
The following script fetches blocks and filters transactions to/from the given address. You can modify it to suit your needs.
#!/usr/bin/python
import argparse
import json
import web3
from web3 import Web3
from hexbytes import HexBytes
# Exports transactions to a JSON file where each line
# contains the data returned from the JSONRPC interface
provider = Web3.HTTPProvider('https://mainnet.infura.io/')
w3 = Web3(provider)
parser = argparse.ArgumentParser()
parser.add_argument('addr', type=str, help='Address to print the transactions for')
parser.add_argument('-o', '--output', type=str, help="Path to the output JSON file", required=True)
parser.add_argument('-s', '--start-block', type=int, help='Start block', default=0)
parser.add_argument('-e', '--end-block', type=int, help='End block', default=w3.eth.blockNumber)
def tx_to_json(tx):
result = {}
for key, val in tx.items():
if isinstance(val, HexBytes):
result[key] = val.hex()
else:
result[key] = val
return json.dumps(result)
def __main__():
args = parser.parse_args()
start_block = args.start_block
end_block = args.end_block
address_lowercase = args.addr.lower()
ofile = open(args.output, 'w')
for idx in range(start_block, end_block):
print('Fetching block %d, remaining: %d, progress: %d%%'%(
idx, (end_block-idx), 100*(idx-start_block)/(end_block-start_block)))
block = w3.eth.getBlock(idx, full_transactions=True)
for tx in block.transactions:
if tx['to']:
to_matches = tx['to'].lower() == address_lowercase
else:
to_matches = False
if tx['from']:
from_matches = tx['from'].lower() == address_lowercase
else:
from_matches = False
if to_matches or from_matches:
print('Found transaction with hash %s'%tx['hash'].hex())
ofile.write(tx_to_json(tx)+'\n')
ofile.flush()
if __name__ == '__main__':
__main__()
To get a list of transactions of an Ethereum wallet address using the Etherscan API, you can follow the steps below:
Go to https://etherscan.io/apis and create an account.
Once you have created an account, generate an API key by going to "MyAPIs" in the top menu.
Select "Create a new API key", choose the "API" checkbox, and give your API key a name.
Once you've generated your API key, you can use it to make API requests.
To get a list of transactions for a particular Ethereum wallet address, you can make a GET request to the following endpoint:
https://api.etherscan.io/api?module=account&action=txlist&address=<ADDRESS>&startblock=<START_BLOCK>&endblock=<END_BLOCK>&sort=asc&apikey=<API_KEY>
Replace with the Ethereum wallet address you want to query. <START_BLOCK> and <END_BLOCK> are optional parameters that allow you to specify the block range for the transactions you want to retrieve. If you omit these parameters, all transactions will be returned. <API_KEY> should be replaced with the API key you generated earlier.
Here's an example of how you could make this request using cURL:
curl 'https://api.etherscan.io/api?module=account&action=txlist&address=0x742d35Cc6634C0532925a3b844Bc454e4438f44e&startblock=0&endblock=99999999&sort=asc&apikey=<API_KEY>'
This will return a JSON response containing the list of transactions for the specified Ethereum wallet address.