1

How can I get current gas price? I want to calculate the gas fee per transaction. I need to multiply Gas Price by _estimatedGas. I looked at the Gas price on https://bscscan.com/ it's 5 Gwei. Can I get this value through the code?

        var _estimatedGas = new HexBigInteger(new BigInteger(35000));
        try
        {
            _estimatedGas = func.EstimateGasAsync(
                account.Address,
                new HexBigInteger(new BigInteger(_transactionValue)),
                new HexBigInteger(new BigInteger(_transactionValue)),
                inputParameters).GetAwaiter().GetResult();

            taskManager.Logger($"_estimatedGas: {_estimatedGas} \n", 7);
            
        }
        catch (Exception e)
        {
            taskManager.Logger($"Error can't get GAS amount {e.Message} \n \n", 7);
            return;
        }
        

        taskManager.Logger($"amountMin: {((BigInteger)amountMin-_estimatedGas)} _estimatedGas.Value: {_estimatedGas.Value} _transactionValue: {_transactionValue}. \n \n", 7);
        inputParameters[0] = new HexBigInteger((BigInteger)amountMin-_estimatedGas);

2 Answers 2

0

Seems Nethereum doesn't have such function built-in. You'll have to get gasPrice from some API like EthGasStation

0

I was facing the same issue some time ago.

Before showing the code, keep in mind that:

Fee = GasPrice * Gas

The GasPrice is the same for all network parties. What changes per transaction is the amount of Gas. E.g.: Deploying a smart contract will probably demand much more Gas than sending Ether to another wallet/account.

According to the current Nethereum version, you can use the following:

Estimating Gas for sending Ether:

Account senderAccount = new(privateKey);
Web3 web3 = new(senderAccount, "https://infura.your-provider-here.com");

await web3.Eth.GetEtherTransferService().EstimateGasAsync(destinationAddress, etherAmount);

Getting GasPrice:

Account senderAccount = new(privateKey);
Web3 web3 = new(senderAccount, "https://infura.your-provider-here.com");

await web3.Eth.GasPrice.SendRequestAsync();

Also, you should know that to make sure your transaction will be mined and processed you can, for example, increase the Gas or GasPrice in a percentage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.