Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
1、安装 Web3.py2 `; `; g3 a- i& f) R! J
* `- H6 o+ @2 A: C# `
  1. pip install web3.py
复制代码

8 q! }9 y  ~4 G* d* E. [. E% C- e! D; k7 |# n
2、注册 Infura 获得节点服务& Q9 o" I1 d0 e
使用邮箱注册 Infura 账户后,创建一个项目,即可获得以太坊节点服务,进入设置即可看到链接的URL
% J! ~7 [; w8 E- r; @7 d 04ab0f6855e74cb6b49a35b1f466da1f.png
. S3 r6 }9 J" D
3 A# F7 i7 x( J. x1 D# [可以选择主网测试网等,会有两个链接,一个是使用HTTPS的一个是使用WebSocket,按你的需求选择一个就行了,注意:Infura 个人免费请求次数,是每天有十万个请求。
5 T" j# A! x& W' B) V& i
1 C* \8 \+ x6 D! m; y) f: \3、代码示例 1 }6 q1 ^5 k) J7 c
  1. 2 x. I5 \# @8 {9 }* g
  2. from web3 import Web3' x& A, ?( @1 r
  3. import json( y& ^, O  j7 S, s2 R5 L
  4. import time
    . N+ z' W# _$ ^" k6 A4 W) ~
  5. import os( s! V  R. J- j* r4 f1 V/ S
  6. import logging
    * X$ z9 x5 f5 N, }2 ?6 c
  7. from django.conf import settings
    ; A, G0 m# d6 y0 X$ ?
  8. from decimal import Decimal  N/ h2 g4 E+ H! t$ d' B+ B) K
  9. . l; d+ c! r+ d  ~; r+ a

  10. % ]! W" ?( C1 N6 X) l. m) @; W& n
  11. class PayEthOrToken(object):5 N$ T5 ~8 q# Z) W/ R. ?0 K5 w
  12.     " A) g; \, g6 k) `  @5 J7 A/ J
  13.     def __init__(self):. F4 K6 v( Y8 w0 @
  14.         # 设置web3* a) t0 F% d4 M) c2 X! |
  15.         self.web3 = Web3(Web3.HTTPProvider('your infura http url'))# q% X  @$ t; O* @; B& q  e6 X% j8 C
  16.         # token合约地址
    $ s$ b7 i1 P. I. h7 M& r- R2 ^
  17.         self.contract_address = 'your contract address'7 }: d' @  G% Z& ~% f; i
  18.         # 主钱包地址
    & X$ Y! k. ~/ d- \! J
  19.         self.wallet = 'your wallet address'
    6 s# m" S( {' ]/ F
  20.         # 钱包的私钥5 ]5 F6 E+ R" P
  21.         self.wallet_key = 'your wallet key'
    8 O3 C* w1 W/ A4 d
  22.         # 合约的abi test.json 是eth的abi json文件,可以在eth区块链浏览器上获得
    0 A0 u  H2 k6 n# V, q1 R
  23.         with open('test.json', 'r') as f:
    % e! t; }8 N' M* O: V: f" O# ]
  24.             self.abi = json.loads(f.read())0 `% U5 T5 x- [6 D( |( e! x
  25.         # 生成合约
    1 X* `: e4 X4 ^0 E
  26.         self.contract = self.web3.eth.contract(address=self.contract_address, abi=self.abi)# n; u7 x0 K1 W: g- Y7 z
  27.         # 代币简写
    9 u1 T2 d8 E, q& R
  28.         self.token_name = 'USDT'
    & r; q. L9 E% o1 X3 i4 M& o
  29.         6 i8 @6 D+ v: C, C8 }
  30.     def transfer_usdt(self, to, value):4 @. r$ I4 ~# ^  s
  31.         '''进行代币转账' {( A, o* c4 H# I+ s9 y( f6 P
  32.         args:
    $ d: h6 i' s* S" A
  33.             to str:接收代币的地址, }0 r4 O6 b$ m5 |0 F
  34.             value str/int:代币数量,以ether为单位,可以是字符串和int类型0 `8 F3 S* [, i0 K
  35.         returns:/ ^4 O# y1 W. t, H* ]. R) V9 D
  36.             (str, str):返回交易哈希,以及异常信息% v( ?; Y! s8 a- K' [
  37.         '''( c6 A: a6 s% q5 {# S- j2 _6 ^. O' _* g
  38.         try:# m8 j1 ^) s9 ^! k
  39.             token_balance = self.web3.fromWei(self.contract.functions.balanceOf(self.wallet).call(), 'ether')/ _. `  E* T* C8 k3 \
  40.             # 如果代币不足返回异常  |/ L- R; l0 W' B  F
  41.             if Decimal(token_balance) < Decimal(value):
    3 O4 f, R  R$ x( d$ b5 b+ {- r2 R: h
  42.                 return None, 'Platform USDT token is insufficient, please try again later'
    , L' t" U1 Y' q, u# m0 u+ T
  43.             # 进行转账代币
    % u4 z2 N) j# L6 S/ y, g
  44.             nonce = self.web3.eth.get_transaction_count(self.wallet); s0 P, W; p0 C6 K' t
  45.             tx = {) S( U, x! d; U: _
  46.                 'from': self.wallet,
    ( |" U, |6 S9 B' [* Z* M
  47.                 'nonce': nonce,3 ]. M" n8 _. Q$ T( l
  48.                 'gas': 100000,' S2 E3 ^' o" C" r, K% Q7 @4 E, B
  49.                 'gasPrice': self.web3.toWei('50', 'gwei'),
    / Q1 B" ]; S" Y6 _- k6 m
  50.                 'chainId': 1
    # }* |  T& `( b# c( ]9 U
  51.             }
    # e2 j0 Z5 {, x
  52.             to = Web3.toChecksumAddress(to)
      H7 ]2 T- o; v' {9 {7 N
  53.             txn = self.contract.functions.transfer(to, self.web3.toWei(value, 'ether')).buildTransaction(tx)  T4 e% `( q3 m2 k
  54.             signed_txn = self.web3.eth.account.sign_transaction(txn, private_key=self.wallet_key)
    0 _5 a' t% L+ L' g9 f
  55.             tx_hash = self.web3.eth.send_raw_transaction(signed_txn.rawTransaction), W! L& i6 |) b' s0 X
  56.             return self.web3.toHex(tx_hash), 'pay success'
    4 Q$ [, G1 B3 O1 K7 ~6 E0 n% V
  57.         except Exception as e:
    - Z/ H. ~7 I7 [( T2 `3 r# c1 H
  58.             logging.error(f'转账{self.token_name}代币时发生异常:{e}')
    ! z2 Q- H/ x* q7 n- k
  59.             logging.exception(e)
    ( u" z/ T; q1 C. P
  60.             return None, str(e)
    + X( I2 b8 C4 g
  61.     9 g" {) C( U# r* r
  62.     def transfer_eth(self, to, value):
    : ~" q5 G  m' `2 m
  63.         '''进行eth转账
    8 B) j2 x) o: U! X1 U
  64.         args:+ u9 O/ i( `$ v7 k" Y
  65.             to str:接收以太坊的地址
    5 O, C3 @+ K! |+ ~
  66.             value str/int:数量,以ether为单位,可以是字符串和int类型- w$ H/ {; x7 ]$ C7 }+ I) F
  67.         returns:( p' x: V0 U- L6 G9 {
  68.             str:返回交易哈希
    6 V1 {3 `& I) H+ N! W  [9 \; P
  69.         '''
    7 r7 B8 r2 V3 o) F
  70.         try:8 d" X& V# y: f6 y$ T0 ^
  71.             token_balance = self.web3.fromWei(self.web3.eth.get_balance(self.wallet), 'ether')
    & N; P3 o. R( r7 x4 ~  N4 L
  72.             # 如果代币不足返回异常9 W8 K6 E! v+ g" q* U9 `" y
  73.             if Decimal(token_balance) < Decimal(value):
    . N' Q! {+ d9 M% K5 e
  74.                 return None, 'Platform ETH token is insufficient, please try again later'
    - T; I6 l5 I* _' Y! o5 U7 e1 g
  75.             # 获取 nonce,这个是交易计数9 T6 V; d2 `' O3 i
  76.             to = Web3.toChecksumAddress(to)* p) M4 J& u4 I; W
  77.             nonce = self.web3.eth.get_transaction_count(self.wallet)
    ( r: n: R; Y3 g7 i( C
  78.             tx = {
    ; F/ ~+ U. E) n5 d1 d4 x% x+ ]
  79.                 'nonce': nonce,
    0 `( F- ^2 }- G9 H- c
  80.                 'to': to,* K& f% w3 a$ H
  81.                 'gas': 100000,
    5 j* u+ |' r" ?
  82.                 'gasPrice': self.web3.toWei('50', 'gwei'),5 G: A! l. U% n) U& t/ R6 a
  83.                 'value': self.web3.toWei(value, 'ether'),
    4 S3 V/ I9 u8 A+ {% N! y
  84.                 'chainId': 15 @, u2 L( Y' x( Q& q7 [9 ~
  85.             }- C# l! E2 c5 D; ?  `7 V8 M
  86.             # 签名交易* |% ^' A3 i6 q4 T+ Y) z) K# M# N
  87.             signed_tx = self.web3.eth.account.sign_transaction(tx, self.wallet_key)
      E; v5 P% X3 B8 I2 b; H
  88.             tx_hash = self.web3.eth.send_raw_transaction(signed_tx.rawTransaction)( C! m. Q& K: x" _% i* j8 I' b
  89.             return self.web3.toHex(tx_hash), 'pay success'
    2 l3 b8 ^0 f/ z% N! K
  90.         except Exception as e:4 o+ D$ R( M% X) R* X0 g- P) j
  91.             logging.error(f'转账eth时发生异常:{e}')6 P! ?0 A' L9 J2 L; {, C# i
  92.             logging.exception(e)
    : p# \2 k9 \6 }3 S# y4 E
  93.             return None, str(e)+ w: G$ Q2 E' _3 \
复制代码
8 G6 i: k/ l( F, }% B; G8 G( X
标签: Python 代币转账
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

常德小学生 初中生
  • 粉丝

    0

  • 关注

    0

  • 主题

    13