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

1.6k Views Asked by At

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.

3

There are 3 best solutions below

0
On BEST ANSWER

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)

1
On

In Nethereum version 4.5, the logic of the method SendTransactionAndWaitForReceiptAsync has changed from the one implemented in 3.8.0. So, I had to use another function override.

public void Write(...)
{
...
      var transactionInput = new TransactionInput
      {
        From = account.Address,
        Gas = _estimatedGas,
        MaxPriorityFeePerGas = null,
        GasPrice = _transactionValue,
        Type = null
      };
      var receipt = function.SendTransactionAndWaitForReceiptAsync(transactionInput, null, inputParameters).GetAwaiter().GetResult();
...
}

In this way, it will work exactly as the 3.8.0 of Nethereum.

1
On

just use this code and should works web3.TransactionManager.UseLegacyAsDefault = true;