技术问询:如何获取以太坊地址的交易记录列表
获取以太坊指定地址交易记录的几种方法
方法一:通过Web3.js/Web3.py调用eth_getLogs(合约交易/带日志的交易)
这个方法适合查询目标地址参与的合约交易(比如ERC20转账、合约调用),这类交易会生成日志记录,能通过eth_getLogs过滤。
Web3.js示例
const Web3 = require('web3'); const web3 = new Web3('https://your-ethereum-node-url'); // 替换为你的节点地址 async function getContractRelatedTransactions(address) { // 查询地址作为发送方的合约交易日志 const sentLogs = await web3.eth.getLogs({ fromBlock: '0x0', toBlock: 'latest', topics: [null, web3.eth.abi.encodeParameter('address', address)] }); // 查询地址作为接收方的合约交易日志 const receivedLogs = await web3.eth.getLogs({ fromBlock: '0x0', toBlock: 'latest', topics: [null, null, web3.eth.abi.encodeParameter('address', address)] }); // 合并并去重交易哈希 const uniqueTxHashes = [...new Set([ ...sentLogs.map(log => log.transactionHash), ...receivedLogs.map(log => log.transactionHash) ])]; // 获取完整交易详情 const transactions = await Promise.all( uniqueTxHashes.map(hash => web3.eth.getTransaction(hash)) ); return transactions; } // 调用示例 getContractRelatedTransactions('0xYourTargetAddress') .then(txs => console.log(txs)) .catch(err => console.error(err));
Web3.py示例
from web3 import Web3 w3 = Web3(Web3.HTTPProvider('https://your-ethereum-node-url')) def get_contract_related_transactions(address): # 查询发送的合约交易日志 sent_filter = w3.eth.filter({ 'fromBlock': 0, 'toBlock': 'latest', 'topics': [None, w3.to_bytes(hexstr=address)] }) sent_logs = sent_filter.get_all_entries() # 查询接收的合约交易日志 received_filter = w3.eth.filter({ 'fromBlock': 0, 'toBlock': 'latest', 'topics': [None, None, w3.to_bytes(hexstr=address)] }) received_logs = received_filter.get_all_entries() # 去重交易哈希 tx_hashes = list({log['transactionHash'] for log in sent_logs + received_logs}) # 获取完整交易信息 transactions = [w3.eth.get_transaction(hash) for hash in tx_hashes] return transactions # 调用示例 txs = get_contract_related_transactions('0xYourTargetAddress') for tx in txs: print(tx)
方法二:使用节点Trace API(包含普通ETH转账)
如果需要查询所有交易(包括无日志的普通ETH转账),可以使用Geth等节点提供的trace_filter API,但需要节点提前开启trace功能(启动时添加--trace或--api=trace参数)。
Web3.js示例
async function getAllTransactions(address) { const traceFilter = { fromBlock: '0x0', toBlock: 'latest', fromAddress: [address], toAddress: [address] }; // 调用trace_filter接口 const traces = await web3.currentProvider.send({ jsonrpc: '2.0', method: 'trace_filter', params: [traceFilter], id: 1 }); // 提取唯一交易哈希 const uniqueTxHashes = [...new Set(traces.result.map(trace => trace.transactionHash))]; // 获取完整交易详情 const transactions = await Promise.all( uniqueTxHashes.map(hash => web3.eth.getTransaction(hash)) ); return transactions; }
方法三:直接调用RPC接口(curl命令)
查询合约交易日志
# 查询地址作为发送方的合约交易 curl -X POST https://your-ethereum-node-url \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "method": "eth_getLogs", "params": [{ "fromBlock": "0x0", "toBlock": "latest", "topics": [null, "0x'$(echo -n "0xYourTargetAddress" | sed 's/^0x//')'"] }], "id": 1 }' # 查询地址作为接收方的合约交易 curl -X POST https://your-ethereum-node-url \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "method": "eth_getLogs", "params": [{ "fromBlock": "0x0", "toBlock": "latest", "topics": [null, null, "0x'$(echo -n "0xYourTargetAddress" | sed 's/^0x//')'"] }], "id": 1 }'
查询所有交易(含普通转账)
curl -X POST https://your-ethereum-node-url \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "method": "trace_filter", "params": [{ "fromBlock": "0x0", "toBlock": "latest", "fromAddress": ["0xYourTargetAddress"], "toAddress": ["0xYourTargetAddress"] }], "id": 1 }'
关键注意事项
- 公共节点通常有请求频率限制,查询大量历史交易建议搭建本地节点(Geth/OpenEthereum)。
- 避免一次性查询过大的区块范围,建议分批次(比如每次查1000个区块),防止超时或内存溢出。
trace_filter需要节点开启trace功能,默认不开启,启动节点时需添加对应参数。- 普通ETH转账没有日志输出,只能通过Trace API或遍历区块查询(遍历区块效率极低,不推荐)。
内容的提问来源于stack exchange,提问作者Taqveem Ahsan
相关产品推荐
相关产品推荐

