ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python 使用 Web3.py 进行 ETH 和代币转账

2022-01-31 20:30:14  阅读:722  来源: 互联网

标签:tx wallet Python 代币 py web3 eth self


1、安装 Web3.py

pip install web3.py

2、注册 Infura 获得节点服务
使用邮箱注册 Infura 账户后,创建一个项目,即可获得以太坊节点服务,进入设置即可看到链接的URL
在这里插入图片描述
可以选择主网测试网等,会有两个链接,一个是使用HTTPS的一个是使用WebSocket,按你的需求选择一个就行了,注意:Infura 个人免费请求次数,是每天有十万个请求。
3、代码示例

from web3 import Web3
import json
import time
import os
import logging
from django.conf import settings
from decimal import Decimal


class PayEthOrToken(object):
    
    def __init__(self):
        # 设置web3
        self.web3 = Web3(Web3.HTTPProvider('your infura http url'))
        # token合约地址
        self.contract_address = 'your contract address'
        # 主钱包地址
        self.wallet = 'your wallet address'
        # 钱包的私钥
        self.wallet_key = 'your wallet key'
        # 合约的abi test.json 是eth的abi json文件,可以在eth区块链浏览器上获得
        with open('test.json', 'r') as f:
            self.abi = json.loads(f.read())
        # 生成合约
        self.contract = self.web3.eth.contract(address=self.contract_address, abi=self.abi)
        # 代币简写
        self.token_name = 'USDT'
        
    def transfer_usdt(self, to, value):
        '''进行代币转账
        args:
            to str:接收代币的地址
            value str/int:代币数量,以ether为单位,可以是字符串和int类型
        returns:
            (str, str):返回交易哈希,以及异常信息
        '''
        try:
            token_balance = self.web3.fromWei(self.contract.functions.balanceOf(self.wallet).call(), 'ether')
            # 如果代币不足返回异常
            if Decimal(token_balance) < Decimal(value):
                return None, 'Platform USDT token is insufficient, please try again later'
            # 进行转账代币
            nonce = self.web3.eth.get_transaction_count(self.wallet)
            tx = {
                'from': self.wallet,
                'nonce': nonce,
                'gas': 100000,
                'gasPrice': self.web3.toWei('50', 'gwei'),
                'chainId': 1
            }
            to = Web3.toChecksumAddress(to)
            txn = self.contract.functions.transfer(to, self.web3.toWei(value, 'ether')).buildTransaction(tx)
            signed_txn = self.web3.eth.account.sign_transaction(txn, private_key=self.wallet_key)
            tx_hash = self.web3.eth.send_raw_transaction(signed_txn.rawTransaction)
            return self.web3.toHex(tx_hash), 'pay success'
        except Exception as e:
            logging.error(f'转账{self.token_name}代币时发生异常:{e}')
            logging.exception(e)
            return None, str(e)
    
    def transfer_eth(self, to, value):
        '''进行eth转账
        args:
            to str:接收以太坊的地址
            value str/int:数量,以ether为单位,可以是字符串和int类型
        returns:
            str:返回交易哈希
        '''
        try:
            token_balance = self.web3.fromWei(self.web3.eth.get_balance(self.wallet), 'ether')
            # 如果代币不足返回异常
            if Decimal(token_balance) < Decimal(value):
                return None, 'Platform ETH token is insufficient, please try again later'
            # 获取 nonce,这个是交易计数
            to = Web3.toChecksumAddress(to)
            nonce = self.web3.eth.get_transaction_count(self.wallet)
            tx = {
                'nonce': nonce,
                'to': to,
                'gas': 100000,
                'gasPrice': self.web3.toWei('50', 'gwei'),
                'value': self.web3.toWei(value, 'ether'),
                'chainId': 1
            }
            # 签名交易
            signed_tx = self.web3.eth.account.sign_transaction(tx, self.wallet_key)
            tx_hash = self.web3.eth.send_raw_transaction(signed_tx.rawTransaction)
            return self.web3.toHex(tx_hash), 'pay success'
        except Exception as e:
            logging.error(f'转账eth时发生异常:{e}')
            logging.exception(e)
            return None, str(e)

标签:tx,wallet,Python,代币,py,web3,eth,self
来源: https://blog.csdn.net/haeasringnar/article/details/122761580

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有