首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >web3py EthereumTesterProvider不工作

web3py EthereumTesterProvider不工作
EN

Ethereum用户
提问于 2023-01-31 08:22:25
回答 1查看 147关注 0票数 1

我在web3py EthereumTesterProvider“区块链”上尝试的所有交互都失败了。

当python方案没有崩溃时,我的事务(部署、setter)状态总是为0 (状态:0)

例如,在https://web3py.readthedocs.io/en/v5/contracts.html中找到的以下代码有3个问题:

代码语言:javascript
复制
from web3 import Web3
from solcx import compile_source
from pprint import pprint

# Solidity source code
    compiled_sol = compile_source(
        
             "pragma solidity ^0.8.17;
        
             contract Greeter {
                 string public greeting;
        
                 constructor() public {
                     greeting = 'Hello';
                 }
        
                 function setGreeting(string memory _greeting) public {
                     greeting = _greeting;
                 }
        
                 function greet() view public returns (string memory) {
                     return greeting;
                 }
             }"
        ,
        output_values=['abi', 'bin']
    )
    
    # retrieve the contract interface
    contract_id, contract_interface = compiled_sol.popitem()
    
    # get bytecode and abi
    bytecode = contract_interface['bin']
    abi = contract_interface['abi']
    
    # web3.py instance
    w3 = Web3(Web3.EthereumTesterProvider())
    
    # set pre-funded account as sender
    w3.eth.default_account = w3.eth.accounts[0]
    
    greeter_bin = w3.eth.contract(abi=abi, bytecode=bytecode)
    
    # Submit the transaction that deploys the contract
    # tx_hash = greeter_bin.constructor().transact()           # <==== first error
    tx_hash = greeter_bin.constructor().transact({'gas': 123456})
    
    # Wait for the transaction to be mined, and get the transaction receipt
    tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
    pprint(dict(tx_receipt))   # <===== second error ('status': 0,)
    
    greeter_obj = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
    
    print(f"{greeter_obj.functions.greet().call() = }")      # <===== third error
    
    
    tx_hash = greeter_obj.functions.setGreeting('Nihao').transact()
    tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
    
    print(f"{greeter_obj.functions.greet().call() = }") 
  1. 第一个错误发生在契约部署中:"TypeError: MockBackend.estimate_gas()接受两个位置参数,但给出了3个。“我通过添加带有一些气体的字典来修正它,但是Web3.py中的示例没有这个参数。我想知道原因。我错过了什么吗?
  2. 第二个问题是部署事务状态是(' status ':0,)。下面的设置者也有同样的问题。
  3. 第三个问题是"C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py",().call()中的错误:在formatted_response apply_error_formatters(error_formatters,response)文件.call第70行中,在apply_error_formatters formatted_resp =pipe(响应,error_formatters)文件"cytoolz\functoolz.pyx",第666行,cytoolz.functoolz.pipe文件中的"cytoolz\functoolz.pyx",第641行,cytoolz.functoolz.c_pipe文件中的第555行,raise_solidity_error_on_revert raise (‘错误预期是一个dict') ValueError:预期为dict的错误

我亦有以下警告:

代码语言:javascript
复制
C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_tester\backends_init_.py:30: UserWarning: Ethereum Tester: No backend was explicitly set, and no full backends were available. Falling back to the MockBackend which does not support all EVM functionality. Please refer to the eth-tester documentation for information on what backends are available and how to set them. Your py-evm package may need to be updated. warnings.warn(

因此,我尝试安装py-evm“pip install py-evm”来尝试PyEVMBackend后端(而不是MockBackend),但是安装失败了:

代码语言:javascript
复制
"D:\Program\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Users\Gilles\AppData\Local\Programs\Python\Python310\include -IC:\Users\Gilles\AppData\Local\Programs\Python\Python310\Include "-ID:\Program\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.34.31933\include" "-ID:\Program\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22000.0\\cppwinrt" /Tcsrc/libethash/io_win32.c /Fobuild\temp.win-amd64-cpython-310\Release\src/libethash/io_win32.obj -Isrc/ -std=gnu99 -Wall
      clÿ: Ligne de commande warning D9002ÿ: option '-std=gnu99' inconnue ignor‚e
      io_win32.c
      c1: fatal error C1083: Impossible d'ouvrir le fichier sourceÿ: 'src/libethash/io_win32.c'ÿ: No such file or directory
      error: command 'D:\\Program\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> pyethash

希望,如果我能够安装和使用PyEVMBackend,这些问题将得到解决。

你能帮我解决一下聚乙烯的安装问题吗?还是你看到了其他我错过的东西?

我在windows 10上的pip配置是:

  • web3py (5.31.3)
  • eth测试器(0.8.0b3)
EN

回答 1

Ethereum用户

发布于 2023-02-07 09:41:53

我试着按数字回答你的问题。

第一个错误是调用TypeError时的greeter_bin.constructor().transact(),这个错误是通过添加一个带有gas的字典来修正的。造成此错误的原因是,MockBackend所使用的EthereumTesterProvider需要在进行事务时指定gas参数。

第二个问题是事务的状态为0。这可能是由于部署或执行事务时出错所致。MockBackend也可能不正确地处理事务收据的status字段。

第三个问题是greeter_obj.functions.greet().call()中的一个.call(),它是由响应的格式引起的,而不是代码所期望的。这可能是由于MockBackend没有返回响应的正确格式。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/143997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档