Web3.py

無法在 Fantom 網路上使用 Web3.py 發送_raw_transaction

  • November 4, 2021

我目前正試圖在 Fantom 主網上簡單地將 FTM 從一個錢包發送到另一個錢包,儘管遇到如下所示的值錯誤“rlp:預期列表”。

ftm = Web3(Web3.HTTPProvider('https://rpcapi.fantom.network'))

# Check if connected correctly
print(ftm.isConnected())

from_address = ftm.toChecksumAddress(wallet)
value = 0.1
to_address = ftm.toChecksumAddress('to_address')
nonce = ftm.eth.getTransactionCount(from_address)

transaction = {
   'from': from_address,
   'to': to_address,
   'value': ftm.toWei(value, "ether"),
   'gas': 2000000,
   'maxFeePerGas': 2000000000,
   'maxPriorityFeePerGas': 1000000000,
   'nonce': nonce,
   'chainId': 250}  # FTM

signed = ftm.eth.account.sign_transaction(transaction, pk)
res = ftm.eth.send_raw_transaction(signed.rawTransaction)
txhash = ftm.toHex(ftm.sha3(signed.rawTransaction))
print(txhash)

ValueError                                Traceback (most recent call last)
<ipython-input-4-361fbe8285cf> in <module>
    24 
    25 signed = ftm.eth.account.sign_transaction(transaction, pk)
---> 26 res = ftm.eth.send_raw_transaction(signed.rawTransaction)
    27 txhash = ftm.toHex(ftm.sha3(signed.rawTransaction))
    28 print(txhash)

~\anaconda3\lib\site-packages\web3\eth.py in send_raw_transaction(self, transaction)
   696 
   697     def send_raw_transaction(self, transaction: Union[HexStr, bytes]) -> HexBytes:
--> 698         return self._send_raw_transaction(transaction)
   699 
   700     def sign_munger(

~\anaconda3\lib\site-packages\web3\module.py in caller(*args, **kwargs)
    55             return LogFilter(eth_module=module, filter_id=err.filter_id)
    56         result_formatters, error_formatters, null_result_formatters = response_formatters
---> 57         result = w3.manager.request_blocking(method_str,
    58                                              params,
    59                                              error_formatters,

~\anaconda3\lib\site-packages\web3\manager.py in request_blocking(self, method, params, error_formatters, null_result_formatters)
   185         """
   186         response = self._make_request(method, params)
--> 187         return self.formatted_response(response,
   188                                        params,
   189                                        error_formatters,

~\anaconda3\lib\site-packages\web3\manager.py in formatted_response(self, response, params, error_formatters, null_result_formatters)
   166         if "error" in response:
   167             apply_error_formatters(error_formatters, response)
--> 168             raise ValueError(response["error"])
   169         elif response['result'] in NULL_RESPONSES:
   170             # null_result_formatters raise either a BlockNotFound

ValueError: {'code': -32000, 'message': 'rlp: expected List'}

完全相同的程式碼在 Eth 上的 Ropstein 測試網上完美執行(為 ropstein 更新了 RPC 和鏈 id),在 Fantom 上沒有運氣,有人能夠解決這個問題嗎?

我相信這是 EIP-1559 的問題,Ropsten 支持但 Fantom 不支持。Fantom 沒有優先費用,只有傳統的 iirc。

所以這部分交易:

‘maxFeePerGas’:2000000000,‘maxPriorityFeePerGas’:1000000000,

可能應該替換為(或您想要的任何其他值):

‘gasPrice’: ftm.toWei(‘200’, ‘gwei’),

引用自:https://ethereum.stackexchange.com/questions/112848