Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文
1、安装 Web3.py
3 V& U+ r9 a/ R" y1 A6 q' B) l& i  C; u8 I/ t5 m( T: ]
  1. pip install web3.py
复制代码

4 j" C/ P1 f7 x" [; Y+ H
) c7 u7 T7 Q# Z3 H9 f7 r2、注册 Infura 获得节点服务5 r: D% d6 Y/ }1 A/ C8 N
使用邮箱注册 Infura 账户后,创建一个项目,即可获得以太坊节点服务,进入设置即可看到链接的URL* ^$ J/ m* G( {
04ab0f6855e74cb6b49a35b1f466da1f.png ! |2 L$ [3 G2 D& d% w7 Z: {5 C

8 D. {( N8 Q1 b3 R; o$ ~可以选择主网测试网等,会有两个链接,一个是使用HTTPS的一个是使用WebSocket,按你的需求选择一个就行了,注意:Infura 个人免费请求次数,是每天有十万个请求。* R4 c9 I% p; Y6 @7 w+ g1 i  W
  X- y$ ]' D0 W' f* m! o" D4 j- Y
3、代码示例 : A; e1 O  x6 E( y# W; [0 a
  1. ) {8 v* [+ B' p' v
  2. from web3 import Web3
    2 C: y  k2 m) i1 P% E5 o1 t8 G: V
  3. import json
    , P0 W* L" C5 p/ ~, z( N
  4. import time
    0 d  r; F  |# n+ v/ k  W! W3 m; K, s
  5. import os3 J; r. v# k+ V' w1 p6 X8 G
  6. import logging& R4 y8 E, {9 b/ V: E8 V* i
  7. from django.conf import settings
    / N% [6 W9 U* Q) F; Y+ C. L
  8. from decimal import Decimal7 J- a+ s5 O8 j

  9. 2 ~- U6 c7 E+ b0 S" A6 {5 x
  10. 0 c+ z- j4 r0 Z" w* s. Y
  11. class PayEthOrToken(object):
    % k: `1 e) J' X; s
  12.    
    + z  ]( `1 w4 _/ ?7 x  T
  13.     def __init__(self):
    " S2 c6 Y& D8 ]+ h
  14.         # 设置web3
    + I" J) z3 y9 q& ?; v9 e( M+ b  j
  15.         self.web3 = Web3(Web3.HTTPProvider('your infura http url'))# Q0 ^. o  a2 V- s5 f
  16.         # token合约地址; P3 F+ z7 x, J
  17.         self.contract_address = 'your contract address'  P/ L$ f4 m& E7 m
  18.         # 主钱包地址
    9 h+ S( G6 a6 \- l. r
  19.         self.wallet = 'your wallet address'
    3 d7 c$ P. K  {" N. F6 g" F
  20.         # 钱包的私钥
    + Z) b9 c- {5 U
  21.         self.wallet_key = 'your wallet key'
    ; _' J" c) j( k2 v/ ^! R1 F
  22.         # 合约的abi test.json 是eth的abi json文件,可以在eth区块链浏览器上获得
    . x8 [- e# q5 n1 S
  23.         with open('test.json', 'r') as f:6 x8 b; d1 L+ {  p5 C+ ~+ y
  24.             self.abi = json.loads(f.read())
    ; s1 t, S  ~, P4 \, y% F* M5 x: S4 h
  25.         # 生成合约8 P. `! \2 B7 `6 z" |$ D; {! }
  26.         self.contract = self.web3.eth.contract(address=self.contract_address, abi=self.abi)9 S! z9 ^1 T! }8 }! z& w
  27.         # 代币简写' ~' M6 k9 q; f) @9 Z* r
  28.         self.token_name = 'USDT'. \4 f) c8 s& J0 y7 P  J
  29.         
    3 f. [( m9 a" b5 U0 t8 h
  30.     def transfer_usdt(self, to, value):
    , c" o7 V6 l6 j
  31.         '''进行代币转账* K9 N& ?6 m: g/ B+ I
  32.         args:$ P8 M+ e0 e, I+ N  l
  33.             to str:接收代币的地址) {2 N- m' z' c. a
  34.             value str/int:代币数量,以ether为单位,可以是字符串和int类型
    % e& C7 d8 ^) |4 I1 }
  35.         returns:1 j0 U/ W' k4 _8 U, B& y% N2 b
  36.             (str, str):返回交易哈希,以及异常信息
    ) S, Q- D( [. Y" ~
  37.         '''# w# m/ j; ^6 ~
  38.         try:. O; t/ t4 \& [- L9 v; I
  39.             token_balance = self.web3.fromWei(self.contract.functions.balanceOf(self.wallet).call(), 'ether')# g! @9 e4 i% O4 J
  40.             # 如果代币不足返回异常
    + h& t% M8 u& k$ Q
  41.             if Decimal(token_balance) < Decimal(value):
    / v/ |8 y4 n# \$ b0 b6 W
  42.                 return None, 'Platform USDT token is insufficient, please try again later'& z& \1 Q* V9 `1 M" A; }
  43.             # 进行转账代币
    6 |& t1 Z. e$ |9 o5 R. V- ]. D
  44.             nonce = self.web3.eth.get_transaction_count(self.wallet)
      r* Z& }  v% U# O& U
  45.             tx = {, {: m/ f* t- f
  46.                 'from': self.wallet,( l0 Y: @. {0 Y8 E2 Q' Q
  47.                 'nonce': nonce,
    - V7 U' _3 s5 ~+ e# ]3 |/ g: q
  48.                 'gas': 100000,
    4 o* C3 R, R2 t# N* ]& D; y
  49.                 'gasPrice': self.web3.toWei('50', 'gwei'),
    ( I+ h) ^* K7 v5 j; t  F: A
  50.                 'chainId': 1- N; F, {& X: b- @0 v8 M7 k0 K+ F
  51.             }
    , d! T8 k9 g  u* @/ M3 _! ~! g' ?
  52.             to = Web3.toChecksumAddress(to)
    8 X& T7 z: O" C3 V! w  t0 h1 W0 B
  53.             txn = self.contract.functions.transfer(to, self.web3.toWei(value, 'ether')).buildTransaction(tx)4 Z2 e0 c; k. F$ D- y
  54.             signed_txn = self.web3.eth.account.sign_transaction(txn, private_key=self.wallet_key)
    3 A- @( B& f" g
  55.             tx_hash = self.web3.eth.send_raw_transaction(signed_txn.rawTransaction)$ A, y  C; I- m# c2 h& v; C
  56.             return self.web3.toHex(tx_hash), 'pay success'! F% e+ k0 ?% C8 a. V1 \- B
  57.         except Exception as e:5 X( E. Q, x8 v, p
  58.             logging.error(f'转账{self.token_name}代币时发生异常:{e}')+ \! c- L! N4 Z6 s, P# H
  59.             logging.exception(e)
    : d+ `5 t" o+ x: y" G3 A8 @
  60.             return None, str(e)3 T/ [4 j# o+ e$ @
  61.    
    0 ?! |8 _9 s, V9 M& B/ s% e' F$ g
  62.     def transfer_eth(self, to, value):& |$ z8 p# I( x$ \- y
  63.         '''进行eth转账
    ' ]# c1 N7 v( }0 }6 l# D5 t
  64.         args:
    5 F3 R/ A5 v1 J; ~
  65.             to str:接收以太坊的地址: z! X6 z# w% P1 j% `$ S
  66.             value str/int:数量,以ether为单位,可以是字符串和int类型
    . C; W% E1 V7 `8 j$ y8 L
  67.         returns:
    8 ]" ]3 w# s: _& k5 x+ C$ M* x
  68.             str:返回交易哈希+ N! l/ y! _" p* d8 K+ C" S4 G
  69.         '''/ m$ p# V( m, ]3 ?
  70.         try:. v! x3 c* Z) R/ |! s4 d5 q* e0 r
  71.             token_balance = self.web3.fromWei(self.web3.eth.get_balance(self.wallet), 'ether')
      F, ]# C2 b  `) o
  72.             # 如果代币不足返回异常
    % }, r2 Z( Y+ o4 G5 u
  73.             if Decimal(token_balance) < Decimal(value):) O7 h0 l" B, T: P& S$ H
  74.                 return None, 'Platform ETH token is insufficient, please try again later'# K8 L2 k, p, [5 I! f
  75.             # 获取 nonce,这个是交易计数& H' ^- S; Z, {% d
  76.             to = Web3.toChecksumAddress(to)& |9 E9 d. j+ |) V' N
  77.             nonce = self.web3.eth.get_transaction_count(self.wallet)
    + F$ W1 d5 h3 z% G/ o
  78.             tx = {$ V5 H# h0 Z4 z+ T/ B
  79.                 'nonce': nonce,
    $ ]  ?: C- Q1 x. U
  80.                 'to': to,
    ; \" }0 i# U! ?  R' e
  81.                 'gas': 100000,
    ! Z5 ]& e) v! V: ]
  82.                 'gasPrice': self.web3.toWei('50', 'gwei'),
    2 ]4 r! u; D" T, Y
  83.                 'value': self.web3.toWei(value, 'ether'),3 {: j- ?5 |0 C2 \
  84.                 'chainId': 1
    4 U7 ]) h: i5 X) M( ^3 H8 I7 w
  85.             }
    : e5 L- Z8 m* F+ T: L# s0 h* z
  86.             # 签名交易) U% E7 p9 C/ W# u; ~& g
  87.             signed_tx = self.web3.eth.account.sign_transaction(tx, self.wallet_key)
    ! _7 J5 G( m* O, c) Y
  88.             tx_hash = self.web3.eth.send_raw_transaction(signed_tx.rawTransaction)( O! l4 d$ c5 A6 x5 H- @" \8 @
  89.             return self.web3.toHex(tx_hash), 'pay success') }4 u, C. }/ Q
  90.         except Exception as e:3 R  e% b" a1 n% s1 f
  91.             logging.error(f'转账eth时发生异常:{e}')0 {: y8 g0 K0 W2 t7 O2 q9 A+ O
  92.             logging.exception(e)6 k' o2 |2 i. T0 W2 E
  93.             return None, str(e)4 ]: l, }( J0 U
复制代码
8 v9 l0 t8 _) M, }/ y- m
标签: Python 代币转账
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

常德小学生 初中生
  • 粉丝

    0

  • 关注

    0

  • 主题

    13