2

On the web3py EthereumTesterProvider blockchain, I tested the contract deployment example at https://web3py.readthedocs.io/en/v5/contracts.html. But I came across 2 errors.

pip config (windows 10) :

  • web3py (5.31.3)
  • eth-tester (0.8.0b3)

Here is the code :

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))

greeter_obj = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)

print(f"{greeter_obj.functions.greet().call() = }")      # <===== Second 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) The first error takes place at the contract deployment: "TypeError: MockBackend.estimate_gas() takes 2 positional arguments but 3 were given." I fixed it by adding a dictionary with some gas but the exemple from Web3.py does not have this parameter. I'd like to know the reason. Did I miss something?

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(

Traceback (most recent call last):
  File "D:\_P\dev\python\blockchain\web3\tester1.py", line 45, in <module>
    tx_hash = greeter_bin.constructor().transact()
  File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_utils\decorators.py", line 18, in _wrapper
    return self.method(obj, *args, **kwargs)

  .............

  File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\providers\eth_tester\middleware.py", line 331, in middleware
    return make_request(method, [filled_transaction] + list(params)[1:])
  File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\middleware\formatting.py", line 94, in middleware
    response = make_request(method, params)
  File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\providers\eth_tester\main.py", line 103, in make_request
    response = delegator(self.ethereum_tester, params)
  File "cytoolz\functoolz.pyx", line 253, in cytoolz.functoolz.curry.__call__
  File "cytoolz\functoolz.pyx", line 249, in cytoolz.functoolz.curry.__call__
  File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\providers\eth_tester\defaults.py", line 66, in call_eth_tester
    return getattr(eth_tester, fn_name)(*fn_args, **fn_kwargs)
  File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\eth_tester\main.py", line 483, in estimate_gas
    raw_gas_estimate = self.backend.estimate_gas(raw_transaction, raw_block_number)
TypeError: MockBackend.estimate_gas() takes 2 positional arguments but 3 were given

2) After fixing the first error by adding {'gas': 123456} in transact(), the second error takes place at greeter_obj.functions.greet().call() : "ValueError: Error expected to be a dict." For this one, I have no clue

Information�: impossible de trouver des fichiers pour le(s) mod�le(s) sp�cifi�(s). 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(

{'blockHash': HexBytes('0xafae7675633fedae22a1f5b9d11066ff78de5947f7b3e2915824823cc65d0e56'),
 'blockNumber': 1,
 'contractAddress': '0xa0Beb7081fDaF3ed157370836A85eeC20CEc9e04',
 'cumulativeGasUsed': 21000,
 'effectiveGasPrice': 1000000000,
 'from': '0xaBbACadABa000000000000000000000000000000',
 'gasUsed': 21000,
 'logs': [],
 'state_root': b'\x00',
 'status': 0,
 'to': '',
 'transactionHash': HexBytes('0x5193460ead56b33c2fa79b490a6c0f4e0d68e07c712d762afcadc5976148bf1a'),
 'transactionIndex': 0,
 'type': '0x2'}

Traceback (most recent call last): File "D:_P\dev\python\blockchain\web3\tester1.py", line 54, in print(f"{greeter_obj.functions.greet().call() = }") File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\contract.py", line 970, in call return call_contract_function( File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\contract.py", line 1525, in call_contract_function return_data = web3.eth.call( File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\module.py", line 57, in caller result = w3.manager.request_blocking(method_str, File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 198, in request_blocking return self.formatted_response(response, File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 170, in formatted_response apply_error_formatters(error_formatters, response) File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 70, in apply_error_formatters formatted_resp = pipe(response, error_formatters) File "cytoolz\functoolz.pyx", line 666, in cytoolz.functoolz.pipe File "cytoolz\functoolz.pyx", line 641, in cytoolz.functoolz.c_pipe File "C:\Users\Gilles\AppData\Local\Programs\Python\Python310\lib\site-packages\web3_utils\method_formatters.py", line 555, in raise_solidity_error_on_revert raise ValueError('Error expected to be a dict') ValueError: Error expected to be a dict

Please note the status of the deployment transaction: 'status': 0 It failed but a contractAddress was returned !

As far as the UserWarning is concerned, I also tried unsuccessfully MockBackend (though it's the default backend):

from eth_tester import MockBackend
w3 = Web3(Web3.EthereumTesterProvider(MockBackend()))

Lastly, I tried to install py-evm "pip install py-evm" to try PyEVMBackend backend, but the installation failed at pyethash dependency:

"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
u2gilles
  • 6,888
  • 7
  • 51
  • 75
  • Please edit your question and include full tracebacks, so people do not need to guess what errors you are having. – Mikko Ohtamaa Jan 29 '23 at 10:03
  • I had the exact same problem as you. I was able to install py-evm by first installing pytash. You need to follow all steps outlined [here](https://github.com/ethereum/ethash/issues/131). Make sure you turn the version to 0.1.27. Then you can install py-evm and can use PyEVMBackend. Unfortunately it has the exact same issue meaning status is still 0. Please let me know if you figured this out. – user127776 Mar 02 '23 at 02:29
  • Nevermind it worked, I think my gas limit was not enough. – user127776 Mar 02 '23 at 02:36
  • Furthermore the the default backend started to work too. I think after the installation above it is better to re-install web3 and 'web3[tester]', I think pyethash issue is related to MockBackend() not working properly. – user127776 Mar 02 '23 at 02:50

0 Answers0