【问题标题】:Why do I get the error "Method eth_maxPriorityFeePerGas not supported" when I try to run Web3py with Ganache?Python Web3 和 Ganache 错误“不支持方法 eth_maxPriorityFeePerGas
【发布时间】:2021-11-21 04:48:37
【问题描述】:

使用 Ganache-cli 运行 Web3.py 会返回此错误:

Method eth_maxPriorityFeePerGas not supported.

但是,在没有 Ganache-cli 的情况下直接使用像 Ropsten 这样的测试网络时,这确实有效。

按照教程运行此代码:

from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv

load_dotenv()

with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()

install_solc("0.6.0")

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
            }
        },
    },
    solc_version="0.6.0",
)

with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)

bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
    "bytecode"
]["object"]

abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]

w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 1337
my_address = "0x3d5CDfCea81141d7C839938095912F594b566Ab8"
private_key = os.getenv("private_key")

SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)

nonce = w3.eth.getTransactionCount(my_address)

transaction = SimpleStorage.constructor().buildTransaction(
    {"chainId": chain_id, "from": my_address, "nonce": nonce}
)

signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)

tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)

我在终端中收到此错误:

Traceback (most recent call last):
  File "C:\Users\Axceus\Desktop\demos\web3_py_simple_storage\deploy.py", line 54, in <module>
    transaction = SimpleStorage.constructor().buildTransaction(
  File "C:\Users\Axceus\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\Axceus\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\contract.py", line 684, in buildTransaction
    return fill_transaction_defaults(self.web3, built_transaction)
  File "cytoolz/functoolz.pyx", line 250, in cytoolz.functoolz.curry.__call__
    return self.func(*args, **kwargs)
  File "C:\Users\Axceus\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\_utils\transactions.py", line 121, in fill_transaction_defaults
    default_val = default_getter(web3, transaction)
  File "C:\Users\Axceus\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\_utils\transactions.py", line 71, in <lambda>
    web3.eth.max_priority_fee + (2 * web3.eth.get_block('latest')['baseFeePerGas'])
  File "C:\Users\Axceus\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\eth.py", line 549, in max_priority_fee
    return self._max_priority_fee()
  File "C:\Users\Axceus\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\Axceus\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 198, in request_blocking
    return self.formatted_response(response,
  File "C:\Users\Axceus\AppData\Local\Programs\Python\Python310\lib\site-packages\web3\manager.py", line 171, in formatted_response
    raise ValueError(response["error"])
ValueError: {'message': 'Method eth_maxPriorityFeePerGas not supported.', 'code': -32000, 'data': {'stack': 'Error: Method eth_maxPriorityFeePerGas not supported.\n    at GethApiDouble.handleRequest (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\subproviders\\geth_api_double.js:70:16)\n    at next (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:136:18)\n    at GethDefaults.handleRequest (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\subproviders\\gethdefaults.js:15:12)\n    at next (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:136:18)\n    at SubscriptionSubprovider.FilterSubprovider.handleRequest (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\subproviders\\filters.js:89:7)\n    at SubscriptionSubprovider.handleRequest (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\subproviders\\subscriptions.js:137:49)\n    at next (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:136:18)\n    at DelayedBlockFilter.handleRequest (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\subproviders\\delayedblockfilter.js:31:3)\n    at next (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:136:18)\n    at RequestFunnel.handleRequest (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\lib\\subproviders\\requestfunnel.js:32:12)\n    at next (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:136:18)\n    at Web3ProviderEngine._handleAsync (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:123:3)\n    at Timeout._onTimeout (C:\\Program Files\\WindowsApps\\GanacheUI_2.5.4.0_x64__5dg5pnz03psnj\\app\\resources\\static\\node\\node_modules\\ganache-core\\node_modules\\web3-provider-engine\\index.js:107:12)\n    at listOnTimeout (internal/timers.js:531:17)\n    at processTimers (internal/timers.js:475:7)', 'name': 'Error'}}

解决方案:

 transaction = SimpleStorage.constructor().buildTransaction( {
    "gasPrice": w3.eth.gas_price, 
    "chainId": chain_id, 
    "from": my_address, 
    "nonce": nonce, 
}

【问题讨论】:

  • 1.修正你帖子的标题,2. 帖子应该关注你的问题,而不是你的挫败感或编程水平,如果你是专家或初学者,你都会得到相同的答案,所以发帖是无用且烦人的。 3. 解释你的代码是关于什么的,你希望得到什么等。另请阅读How to Ask 并查看tour
  • 如果这看起来有点苛刻,让我解释一下。问题标题是获得好答案的关键;你想吸引能够提供帮助的人的注意力。最好的方法是在标题中加入尽可能多的相关信息。
  • 如果我可以,但它不会让我:标题不能包含“VS Code not working with Ganache”
  • 是的,控制 SO 的保姆机器人有时会有点霸道。我的建议是使用措辞直到你得到通过。
  • 我尝试了很多标题,但它总是给我“标题不能包含”似乎很挑剔它会不会让我输入标题,所以当系统不会让我说更具体的内容

标签: python solidity web3 ganache


【解决方案1】:

请使用它来发送您的交易:

 transaction = SimpleStorage.constructor().buildTransaction( {
    "gasPrice": w3.eth.gas_price, 
    "chainId": chain_id, 
    "from": my_address, 
    "nonce": nonce, 
}

【讨论】:

    【解决方案2】:

    默认情况下,web3py 会尝试创建一个EIP-1559 transaction。 为此,它将尝试获取估计的优先费用。

    Ganache 当前稳定版(6.x)不支持 EIP-1559 相关功能,因此 RPC 调用失败。 将 gas price 参数添加到交易中将使 web3py 发送“遗留交易”(即 EIP-1559 之前),因此它不会尝试查询优先费用并简单地硬编码 gas 价格。

    从 Ganache 7.x(撰写本文时为测试版)开始,将支持 EIP-1559 事务,因此 web3py 的默认行为也将起作用。

    【讨论】:

      【解决方案3】:
      from solcx import compile_standard, install_solc
      from web3 import Web3
      import json
      
      with open("simpleStorage.sol", "r") as f:
          simple_storage_file = f.read()
      
      
      # install version of compiler
      install_solc("0.6.0")
      
      compiled_sol = compile_standard(
          {
              "language": "Solidity",
              "sources": {"simpleStorage.sol": {"content": simple_storage_file}},
              "settings": {
                  "outputSelection": {
                      "*": {
                          "*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]
                      }
                  }
              }
          },
          solc_version = "0.6.0"
      )
      
      # print(compiled_sol)
      
      
      
      # save the json to file
      with open("compile_code.json", "w") as f:
          json.dump(compiled_sol, f)
      
      
      # get bytecode for deployment
      bytecode = compiled_sol["contracts"]["simpleStorage.sol"]["SimpleStorage"]["evm"]["bytecode"]["object"]
      
      # get ABI
      abi = compiled_sol["contracts"]["simpleStorage.sol"]["SimpleStorage"]["abi"]
      
      
      # connect to ganache
      
      # setup RPC SERVER / HTTP provider
      w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))
      chain_id = 1337
      my_address = "address here"
      private_key = "private key here"
      
      
      SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
      # print(SimpleStorage)
      
      # nonce >> Number only used once in a cryptographic communication
      nonce = w3.eth.getTransactionCount(my_address)
      # print(nonce)
      
      
      transaction = SimpleStorage.constructor().buildTransaction({
          "gasPrice": w3.eth.gas_price, "chainId": chain_id, "from": my_address, "nonce": nonce
      })
      print(transaction)
      

      【讨论】:

        【解决方案4】:

        我尝试遵循 Daniel 的建议,但由于某种原因,我无法在我的计算机上安装 ganache-cli 7.x。所以我将 web3 版本降级到 5.24.0 并且它可以工作。

        【讨论】:

        猜你喜欢
        • 2022-01-03
        • 2022-01-01
        • 2023-03-18
        • 1970-01-01
        • 2022-08-31
        • 2021-11-28
        • 1970-01-01
        • 2022-01-14
        • 2013-04-17
        相关资源
        最近更新 更多