簡體   English   中英

使用本地私鑰向web3.py發送ERC20令牌

[英]Send ERC20 token with web3.py using a local private key

我試圖使用本地私鑰使用web3.py在python中發送ERC20令牌。

使用此代碼我可以發送以太:

w3 = Web3(HTTPProvider('https://api.myetherapi.com/eth'))
signed_txn = w3.eth.account.signTransaction(dict(
                nonce=w3.eth.getTransactionCount(from_address),
                gasPrice=w3.eth.gasPrice,
                gas=100000,
                to=to_address,
                value=12345,
                data=b'',
            ),
                private_key,
            )
w3.eth.sendRawTransaction(signed_txn.rawTransaction)

然后我也找到了這個,但是總是得到一個估計氣體的錯誤,在我看來,我不能指定我發送的地址,或者通過某種簽名證明我的地址?

contract = w3.eth.contract(address=address, abi=EIP20_ABI, bytecode=bytecode)
contract.functions.transfer(to_address, 121212).transact()

所以我有JSON abi,​​字節碼,地址和我的私鑰,我能以某種方式從我找到的代碼中構建一個工作腳本嗎?

提前致謝!

總是得到估計氣體的錯誤

如果您的交易失敗,您有時會收到錯誤。 如果您在估算氣體時從“錯誤的地址”發送,則會發生這種情況。 嘗試在estimateGas()指定from地址,如下所示:

acct = w3.eth.account.privateKeyToAccount(private_key)
contract.functions.transfer(to_address, 121212).estimateGas({'from': acct.address})

我可以用我發現的代碼以某種方式構建一個工作腳本嗎?

第二種方法的一個問題是transact()將嘗試使用您的節點對事務進行簽名。 由於您有本地私鑰,因此您需要使用buildTransaction()然后在本地簽名。

請參閱此Web3.py指南 ,該指南通常涉及本地簽署合同事務,但恰好使用ERC20示例。

大綱

  1. 初始化Contract對象
  2. 建立交易
  3. 使用w3.eth.account.signTransaction()對事務進行簽名
  4. 使用sendRawTransaction()廣播事務

>>> from ethtoken.abi import EIP20_ABI
>>> from web3.auto import w3

>>> unicorns = w3.eth.contract(address="0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", abi=EIP20_ABI)

>>> nonce = w3.eth.getTransactionCount('0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E')  

# Build a transaction that invokes this contract's function, called transfer
>>> unicorn_txn = unicorns.functions.transfer(
...     '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
...     1,
... ).buildTransaction({
...     'chainId': 1,
...     'gas': 70000,
...     'gasPrice': w3.toWei('1', 'gwei'),
...     'nonce': nonce,
... })

>>> unicorn_txn
{'value': 0,
 'chainId': 1,
 'gas': 70000,
 'gasPrice': 1000000000,
 'nonce': 0,
 'to': '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
 'data': '0xa9059cbb000000000000000000000000fb6916095ca1df60bb79ce92ce3ea74c37c5d3590000000000000000000000000000000000000000000000000000000000000001'}

>>> private_key = b"\xb2\\}\xb3\x1f\xee\xd9\x12''\xbf\t9\xdcv\x9a\x96VK-\xe4\xc4rm\x03[6\xec\xf1\xe5\xb3d"
>>> signed_txn = w3.eth.account.signTransaction(unicorn_txn, private_key=private_key)
>>> signed_txn.hash
HexBytes('0x4795adc6a719fa64fa21822630c0218c04996e2689ded114b6553cef1ae36618')
>>> signed_txn.rawTransaction
HexBytes('0xf8a980843b9aca008301117094fb6916095ca1df60bb79ce92ce3ea74c37c5d35980b844a9059cbb000000000000000000000000fb6916095ca1df60bb79ce92ce3ea74c37c5d359000000000000000000000000000000000000000000000000000000000000000125a00fb532eea06b8f17d858d82ad61986efd0647124406be65d359e96cac3e004f0a02e5d7ffcfb7a6073a723be38e6733f353cf9367743ae94e2ccd6f1eba37116f4')
>>> signed_txn.r
7104843568152743554992057394334744036860247658813231830421570918634460546288
>>> signed_txn.s
20971591154030974221209741174186570949918731455961098911091818811306894497524
>>> signed_txn.v
37

>>> w3.eth.sendRawTransaction(signed_txn.rawTransaction)  

# When you run sendRawTransaction, you get the same result as the hash of the transaction:
>>> w3.toHex(w3.sha3(signed_txn.rawTransaction))
'0x4795adc6a719fa64fa21822630c0218c04996e2689ded114b6553cef1ae36618'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM