我在web3py EthereumTesterProvider“区块链”上尝试的所有交互都失败了。
当python方案没有崩溃时,我的事务(部署、setter)状态总是为0 (状态:0)
例如,在https://web3py.readthedocs.io/en/v5/contracts.html中找到的以下代码有3个问题:
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() = }") 我亦有以下警告:
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),但是安装失败了:
"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配置是:
发布于 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没有返回响应的正确格式。
https://ethereum.stackexchange.com/questions/143997
复制相似问题