【问题标题】:Send ERC20 token with web3.py using a local private key使用本地私钥通过 web3.py 发送 ERC20 令牌
【发布时间】:2018-11-18 18:05:06
【问题描述】:

我正在尝试使用本地私钥在 python 中通过 web3.py 发送 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)

然后我也发现了这个,但总是收到estimateGas错误,在我看来,我无法像这样指定我发送的地址或通过某种签名证明它是我的地址?

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

所以我有 JSON abi、字节码、地址和我的私钥,我可以用我找到的代码构建一个工作脚本吗?

提前致谢!

【问题讨论】:

    标签: python token ethereum web3 erc20


    【解决方案1】:

    estimateGas 总是出错

    如果您的交易失败,有时您会遇到错误。如果您在估算 gas 时“从”错误的地址发送,就会发生这种情况。尝试在estimateGas() 中指定from 地址,如下所示:

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

    我可以用我找到的代码以某种方式构建一个工作脚本吗?

    第二种方法的一个问题是transact() 将尝试使用您的节点来签署交易。由于您有本地私钥,因此您需要使用 buildTransaction() 在本地签名。

    this Web3.py guide,一般是关于本地签署合约交易,但碰巧用了一个ERC20的例子。

    大纲

    1. 初始化您的合同对象
    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'
    

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 2018-10-01
      • 2019-12-01
      • 2022-06-14
      • 2021-08-16
      • 2023-03-31
      • 2021-12-24
      • 2022-06-25
      • 2018-06-17
      相关资源
      最近更新 更多