如何使用python进行eth批量转账,薅羊毛必备!

这是一个向Python开发人员介绍Web3.py(一个区块链/以太坊库)的基础知识的教程。我们知道在以太坊链上创建钱包是没有成本的,这就意味着我们可以通过以太坊寻找到许多获利的机会,比如通过多个钱包领取空投,参加IDO等等。那如果我们参加活动需要1000个钱包帐号,如果一个个手工操作的话,这样不仅会导致过程很繁琐可能最后的人工成本比收益高使我们错过很多的机会。目前有好几个平台提供在线网页端批量转账(个人不建议使用,毕竟转账是需要提供钱包密钥的会导致密钥泄露丢失财产)

所以,本次教程我将通过Web3.py与以太坊合约进行交互并完成交易。

一、什么是Web3.py

    Web3.py是连接以太坊的python库,它常见于去中心化应用程序 (dapps) 中,用于帮助发送交易、与智能合约交互、读取块数据以及各种其他用例。它的API从web3.js中派生而来,如果你用过web3.js,你会对它的API很熟悉。

一、初始化你的 Web3 实例,连接到对应的链RPC,比如eth,bsc,heco,okex等等。这里我写了一篇文章,收藏了各个链的rpc(rpc节点大全

from web3 import Web3my_provider = Web3(HTTPProvider("https://bsc-dataseed1.binance.org/"))

二、以太坊转账等上链操作,对于以太坊来说都是一笔交易,发送交易关键参数如下。

(1)、nonce – 这是账号的一个交易计数。

(2)、to – 目标账户。

(3)、value – 要发送的eth,bnb金额(根据链区分)。这个值必须以十六进制表示,单位必须是wei。我们可以使用Web3.js工具w3.toWei()转换单位。

(4)、gasLimit – 交易能消耗Gas的上限。像这样的基本交易总是要花费460869单位的Gas。

(5)、gasPrice – Gas价格,这里是 5Gwei。​​​​​​​

txn_dict = {            'to': self.to_address,            'value': self.w3.toWei(amount_in_wei, 'wei'),            'gas': 460869,            'gasPrice': self.w3.toWei('5', 'gwei'),            'nonce': nonce,            'chainId': 56}

三、交易关键方法

(1)、初始化您的合同对象

(2)、建立交易

(3)、使用signTransaction()签署交易

(4)、使用sendRawTransaction()广播事务

四、这里是我写好的一个在bsc链上批量转账BNB的demo 。one  -> many

privateJson.json格式:​​​​​​​

 {        "privateKey": "XXXXXXXXXXXXXXXXX",        "address": "XXXXXXXXXXXXXXXXXXXXXXXX"    }

total.json格式:​​​​​​​

[{        "privateKey": "XXXXXXXXXXXXXXXXX",        "address": "XXXXXXXXXXXXXXXXXXXXXXXX"    }]
​​​
import timefrom web3 import Web3, HTTPProviderimport json

class BSC:    def __init__(self, wallet_address, wallet_private_key, to_address='输入你默认转账的接收地址'):        self.w3 = Web3(HTTPProvider("https://bsc-dataseed1.binance.org/"))        self.wallet_address = wallet_address        self.wallet_private_key = wallet_private_key        self.to_address = to_address    def send_ether_to_contract_BNB(self, amount_in_ether):        if len(self.to_address) <= 0:            print("接收为空!请检查")            return        amount_in_wei = self.w3.toWei(amount_in_ether, 'ether');        nonce = self.w3.eth.getTransactionCount(self.wallet_address)        txn_dict = {            'to': self.to_address,            'value': self.w3.toWei(amount_in_wei, 'wei'),            'gas': 460869,            'gasPrice': self.w3.toWei('5', 'gwei'),            'nonce': nonce,            'chainId': 56        }        signed_txn = self.w3.eth.account.signTransaction(txn_dict, self.wallet_private_key)        txn_hash = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)        txn_receipt = None        count = 0        while txn_receipt is None and (count < 30):            try:                txn_receipt = self.w3.eth.getTransactionReceipt(txn_hash)                print(txn_receipt)            except:                pass            time.sleep(5)        if txn_receipt is None:            return {'status': 'failed', 'error': 'timeout'}        return {'status': 'added', 'txn_receipt': txn_receipt}single_transfer_amount = 0.1def run():    with open("total.json", 'r') as f:        readJson = json.loads(f.read())    wallet_private_key = readJson[0]["privateKey"]    wallet_address = readJson[0]["address"]    print("本次转出钱包地址:" + wallet_address)    for addressArr in readJson:        to_address = addressArr["address"]        print("本次转入钱包地址:" + to_address)        bsc = BSC(wallet_address, wallet_private_key, to_address)        bnb_account = float(bsc.w3.fromWei(bsc.w3.eth.getBalance(to_address),"ether"))        bsc.send_ether_to_contract_BNB(single_transfer_amount)run()

五、这里是我写好的一个在bsc链上批量回转的转账BNB的demo 。 many  -> one

import timefrom web3 import Web3, HTTPProviderimport json

class BSC:    def __init__(self, wallet_address, wallet_private_key, to_address='输入你默认转账的接收地址'):        self.w3 = Web3(HTTPProvider("https://bsc-dataseed1.binance.org/"))        self.wallet_address = wallet_address        self.wallet_private_key = wallet_private_key        self.to_address = to_address
    def send_ether_to_contract_BNB(self, amount_in_ether):        if len(self.to_address) <= 0:            print("接收为空!请检查")            return        amount_in_wei = self.w3.toWei(amount_in_ether, 'ether');        nonce = self.w3.eth.getTransactionCount(self.wallet_address)        txn_dict = {            'to': self.to_address,            'value': self.w3.toWei(amount_in_wei, 'wei'),            'gas': 460869,            'gasPrice': self.w3.toWei('5', 'gwei'),            'nonce': nonce,            'chainId': 56        }        signed_txn = self.w3.eth.account.signTransaction(txn_dict, self.wallet_private_key)        txn_hash = self.w3.eth.sendRawTransaction(signed_txn.rawTransaction)        txn_receipt = None        count = 0        while txn_receipt is None and (count < 30):            try:                txn_receipt = self.w3.eth.getTransactionReceipt(txn_hash)                print(txn_receipt)            except:                pass            time.sleep(5)        if txn_receipt is None:            return {'status': 'failed', 'error': 'timeout'}        return {'status': 'added', 'txn_receipt': txn_receipt}def run():    to_address = "输入你默认转账的接收地址"    with open("test_total.json", 'r') as f:        readJson = json.loads(f.read())    for addressArr in readJson:        wallet_private_key = addressArr["privateKey"]        wallet_address = addressArr["address"]        print("本次转出钱包地址:" + wallet_address)        print("本次转入钱包地址:" + to_address)        bsc = BSC(wallet_address, wallet_private_key, to_address)        bnb_account = float(bsc.w3.fromWei(bsc.w3.eth.getBalance(wallet_address),"ether"))        bsc.send_ether_to_contract_BNB(bnb_account-0.005)
run()

使用成功案例:参与某个平台 ido活动,成本20W 美元将币均分到100个号上,并使用全部号参与活动(因为单个帐号是有参与额度限制),最终获利1W美元

Python经验分享

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

Python学习路线

这里把Python常用的技术点做了整理,有各个领域的知识点汇总,可以按照上面的知识点找对应的学习资源。
在这里插入图片描述

学习软件

Python常用的开发软件,会给大家节省很多时间。
在这里插入图片描述

学习视频

编程学习一定要多多看视频,书籍和视频结合起来学习才能事半功倍。
在这里插入图片描述

100道练习题

在这里插入图片描述

实战案例

光学理论是没用的,学习编程切忌纸上谈兵,一定要动手实操,将自己学到的知识运用到实际当中。
在这里插入图片描述
最后祝大家天天进步!!

上面这份完整版的Python全套学习资料已经上传至CSDN官方,朋友如果需要可以直接微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】。

在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员二飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值