首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用web3.py获取本地电子钱包的所有交易列表?

如何使用web3.py获取本地电子钱包的所有交易列表?
EN

Ethereum用户
提问于 2018-06-22 14:59:44
回答 1查看 3.8K关注 0票数 6

我已经开发了django项目,该项目使用web3.py在本地使用that钱包。我想知道我钱包的交易记录。

但我没有办法。请帮帮我。

EN

回答 1

Ethereum用户

发布于 2018-09-15 13:58:39

下面的脚本获取块并将事务筛选到给定地址。你可以修改它以适应你的需要。

代码语言:javascript
运行
复制
#!/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__()
票数 3
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/51863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档