Jus*_*Cho 3 python blockchain solidity ganache web3py
我正在 YouTube 上学习freeCodeCamp课程,在运行该程序时发现了一个错误。当我试图在 Ganache 中建立一个交易时。我在 Ganache 日志中看到也有活动。第一次发帖,请告诉我还应该提供哪些信息!
\n课程:Solidity、区块链和智能合约课程 \xe2\x80\x93 初学者到专家 Python 教程
\ntransaction = SimpleStorage.constructor().buildTransaction(\n {"chainId": chain_id, "from": my_address, "nonce": nonce}\n)\nRun Code Online (Sandbox Code Playgroud)\n终端返回的错误:
\ntransaction = SimpleStorage.constructor().buildTransaction(\n {"chainId": chain_id, "from": my_address, "nonce": nonce}\n)\nRun Code Online (Sandbox Code Playgroud)\n我使用的完整代码如下:
\nfrom solcx import compile_standard, install_solc\nimport json\nfrom web3 import Web3\n\nwith open("./SimpleStorage.sol", "r") as file:\n simple_storage_file = file.read()\n\n# Compile Our Solidity\nprint("Installing...")\ninstall_solc("0.6.0")\n\ncompiled_sol = compile_standard(\n {\n "language": "Solidity",\n "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},\n "settings": {\n "outputSelection": {\n "*": {\n "*": [\n "abi",\n "metadata",\n "evm.bytecode",\n "evm.bytecode.sourceMap",\n ]\n }\n }\n },\n },\n solc_version="0.6.0",\n)\n\nwith open("compiled_code.json", "w") as file:\n json.dump(compiled_sol, file)\n\n# get bytecode\nbytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][\n "bytecode"\n]["object"]\n\n# get abi\nabi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]\n\n# for connecting to ganache\nw3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))\nchain_id = 5777\nmy_address = "0xf2580f5ddfFb89e69A12a7CbCa8CA175Df4cBe08"\nprivate_key = "0x937073896304af4297e6bbb3a3c623689d48388aa8595058752fafb522b31a13"\n\n# Create the contract in python\nSimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)\nprint(SimpleStorage)\n\n# Get the latest transaction\nnonce = w3.eth.getTransactionCount(my_address)\nprint(nonce)\n\n# 1. Build a transaction\n# 2. Sign a transaction\n# 3. Send a transaction\ntransaction = SimpleStorage.constructor().buildTransaction(\n {"chainId": chain_id, "from": my_address, "nonce": nonce}\n)\nprint(transaction)\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
从web3.py 5.25.0 开始,我们现在需要将 GasPrice 添加到与本地Ganache链的交易中。添加"gasPrice": w3.eth.gas_price应该可以解决您在交易中的问题。
完整示例:
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1509 次 |
| 最近记录: |