Python3 使用Web3.py查询以太坊账户余额

Python017

Python3 使用Web3.py查询以太坊账户余额,第1张

from web3 import Web3

def QuerryBalanceETH(accounts):

    w3 = Web3(Web3.HTTPProvider('https://mainnet. infura .io/v3/ {此处设置自己托管账户ID} '))

    #accounts = w3.eth.accounts

    balance = w3.eth.getBalance(accounts,'latest')#latest表示使用区块链中最后一个块的状态,也就是最后的余额

    print('balance@latest =>{0}'.format(balance))

    return balance

1、什么是Infura?

专业一点讲,Infura是一种IaaS(Infrastructure as a Service)产品,目的是为了降低访问以太坊数据的门槛。

通俗一点讲,Infura就是一个可以让你的dApp快速接入以太坊的平台,不需要本地运行以太坊节点。

从程序员的角度讲,Infura就是一个Web3 Provider,背后是负载均衡的API节点集群。使用它的好处就是,你永远不必担心连接的节点失效的问题,Infura会管理好这一切。

除此之外,Infura还可以很方便地接入IPFS,这是另外一个话题,这里就不讨论了。

最后,也是非常重要的一点:Infura目前是免费的。

2、如何使用Infura?

使用Infura首先需要注册一个账户,访问官网 https://infura.io ,点击注册并提供一个邮箱,会收到一封邮件,点击邮件中的链接激活就可以了,然后你就会看到下面的界面:

点击右上角的黑色按钮,创建新项目,就可以生成你专属的Project ID了(左边的红框)。

参考文章: https://blog.csdn.net/TurkeyCock/article/details/85103434

import pickle

import time

import os

def cost(fname):

    '用于记录花费'

    cost_time = time.strftime('%Y-%m-%d')

    try:#异常处理机制

        cost_deposit = int(input('花销金额:'))

        cost_mark = input('花销说明:')

    except ValueError:

        print('无效的金额')

        return # 函数的return类似于循环的break,return提前结束函数。

    except (KeyboardInterrupt, EOFError):

        print('\nbye-bye')

        exit(1)

    # 在文件中取出所有的收支记录

    with open(fname,'rb') as fobj:

        records = pickle.load(fobj)

    # 计算最新余额

    balance = records[-1][-2] - cost_deposit

    # 构建最新一笔收入

    record = [cost_time,0,cost_deposit,balance,cost_mark]

    # 将收入追加到收支列表中

    records.append(record)

    # 将最新收支情况写入文件

    with open(fname,'wb') as fobj:

        pickle.dump(records,fobj)

def save(fname):

    save_time = time.strftime('%Y-%m-%d')

    try:

        save_deposit = int(input('收入金额:'))

        save_mark = input('收入说明:')

    except ValueError:

        print('无效的金额')

        return

    except (KeyboardInterrupt,EOFError):

        print('bye-bye')

        exit(1)

    with open(fname, 'rb') as fobj:

        records = pickle.load(fobj)

    balance = records[-1][-2] +  save_deposit

    record = [save_time,save_deposit,0,balance, save_mark]

    records.append(record)

    with open(fname, 'wb') as fobj:

        pickle.dump(records, fobj)

def query(fname):

    '用于查账'

    # 打印表头

    print(f'{"date":<15}{"save":<8}{"cost":<8}{"balance":<12}{"mark":<50}')

    with open(fname,'rb') as fobj:

        records = pickle.load(fobj)

    for date,cost,save,balance,mark in records:

        print(f'{date:<15}{cost:<8}{save:<8}{balance:<12}{mark:<50}')

def menu():

    funcs = {'0':cost,'1':save,'2':query}

    prompt = '''(0)开销

(1)收入

(2)查询

(3)退出

请选择(0/1/2/3):'''

    fname = 'account.data'

    if not os.path.exists(fname):

        init_data = [[time.strftime('%Y-%m-%d'),0,0,10000,'init_data']]

        with open(fname,'wb') as fobj:

            pickle.dump(init_data,fobj)

    while 1:

        try:

            choice = input(prompt).strip()

        except(KeyboardInterrupt,EOFError):

            choice = '3'

        if choice not in ['0','1','2','3']:

            print('无效输入,重试')

            continue

        if choice == '3':

            print('\nbye_bye')

            break

        funcs[choice](fname)

if __name__ == '__main__':

    menu()

python2.7.12可以实现

python2.7.12模拟与银行ATM功能,主要功能如下:1,登录查询余额2,转账功能3,提现功能4,流水查询5,密码修改6,还款功能7,退出(切换账号)。