Search code examples
c#.net-corenugetblockchainnethereum

Nethereum - the method eth_feeHistory does not exist or is not available


I have a function that "write" on the blockchain (private network on a virtual machine), using Nethereum. I was forced to upgrade from version 3.8.0 to version 4.5.0 Before the update everything was working fine, but now, when i call the SendTransactionAndWaitForReceiptAsync function, the following exception is raised.

Nethereum.JsonRpc.Client.RpcResponseException: the method eth_feeHistory does not exist/is not available: eth_feeHistory
   at Nethereum.JsonRpc.Client.ClientBase.HandleRpcError(RpcResponseMessage response, String reqMsg)
   at Nethereum.JsonRpc.Client.ClientBase.SendInnerRequestAsync[T](RpcRequestMessage reqMsg, String route)
   at Nethereum.JsonRpc.Client.ClientBase.SendRequestAsync[T](RpcRequest request, String route)
   at Nethereum.RPC.Fee1559Suggestions.TimePreferenceFeeSuggestionStrategy.SuggestFeesAsync()
   at Nethereum.RPC.Fee1559Suggestions.TimePreferenceFeeSuggestionStrategy.SuggestFeeAsync(Nullable`1 maxPriorityFeePerGas)
   at Nethereum.RPC.TransactionManagers.TransactionManagerBase.SetTransactionFeesOrPricingAsync(TransactionInput transaction)
   at Nethereum.Web3.Accounts.AccountSignerTransactionManager.SignTransactionRetrievingNextNonceAsync(TransactionInput transaction)
   at Nethereum.Web3.Accounts.AccountSignerTransactionManager.SignAndSendTransactionAsync(TransactionInput transaction)
   at Nethereum.RPC.TransactionReceipts.TransactionReceiptPollingService.SendRequestAndWaitForReceiptAsync(Func`1 transactionFunction, CancellationTokenSource tokenSource)
   at Project.BlockchainAdapter.BlockchainInteractionAdapter.Write(String privateKey, String contractAddress, String url, String smartContractLocation, String functionName, Object[] inputParameters, Int32 transactionValue, Int32 chainId)

That is the code:

public void Write(
      string privateKey,
      string contractAddress,
      string url,
      string smartContractLocation,
      string functionName,
      object[] inputParameters,
      int transactionValue = 0,
      int chainId = (int)Nethereum.Signer.Chain.Ropsten)
    {
      var function = GetEthFunction(privateKey, contractAddress, url, smartContractLocation, functionName, out Account account, chainId);

      var _transactionValue = new HexBigInteger(new BigInteger(transactionValue));
      var _estimatedGas = new HexBigInteger(new BigInteger(35000));
      try
      {
        _estimatedGas = function.EstimateGasAsync(
            account.Address,
            new HexBigInteger(new BigInteger(transactionValue)),
            new HexBigInteger(new BigInteger(transactionValue)),
            inputParameters).GetAwaiter().GetResult();
      }
      catch
      {
        // Intentionally left blank
      }

      var receipt = function.SendTransactionAndWaitForReceiptAsync(
            account.Address,
            _estimatedGas,
            _transactionValue,
            null,
            inputParameters).GetAwaiter().GetResult();
      if (!receipt.Status.Value.Equals(1))
      {
        throw new OperationCanceledException($"Unable to complete transaction. Transaction hash: {receipt.TransactionHash}.");
      }
    }
  }

private Function GetEthFunction(string privateKey,
      string contractAddress, string url, string smartContractLocation, string functionName, out Account account,
      int chainId)
    {
      account = new Account(privateKey, chainId);
      var web3 = new Web3(account, url);

      string abi = null;
      using (StreamReader file = File.OpenText($@"{smartContractLocation}"))
      {
        abi = file.ReadToEnd();
      }
      var contract = web3.Eth.GetContract(abi, contractAddress);
      return contract.GetFunction(functionName);
    }

How can I fix this? Thank you.


Solution

  • A solution that worked for me is to disable EIP1559 transactions. This is very useful if you're using a non EIP1559 enabled chain, most notably BSC. Here is the code to do that:

    Web3.TransactionManager.UseLegacyAsDefault = true;
    

    Hope this helps anyone in the future with the same issue. (please note that the web3 class must be instanced here and is not static)