From efc024f7c04e780092e6f8de59948ea32873d359 Mon Sep 17 00:00:00 2001 From: maxim055 Date: Mon, 24 Nov 2025 15:23:17 +0400 Subject: [PATCH] Fix blockchain fee calculation. --- csharp/src/Common/Common.csproj | 1 + .../Common/Extensions/Web3ClientExtensions.cs | 17 +++++++++++++++++ .../Helpers/EthereumEIP1559FeeEstimator.cs | 9 +++++---- .../src/Workflow.Solana/Workflow.Solana.csproj | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 csharp/src/Common/Extensions/Web3ClientExtensions.cs diff --git a/csharp/src/Common/Common.csproj b/csharp/src/Common/Common.csproj index f07e35f5..e39fffaf 100644 --- a/csharp/src/Common/Common.csproj +++ b/csharp/src/Common/Common.csproj @@ -6,6 +6,7 @@ + diff --git a/csharp/src/Common/Extensions/Web3ClientExtensions.cs b/csharp/src/Common/Extensions/Web3ClientExtensions.cs new file mode 100644 index 00000000..6d7a2f62 --- /dev/null +++ b/csharp/src/Common/Extensions/Web3ClientExtensions.cs @@ -0,0 +1,17 @@ +using Nethereum.Hex.HexTypes; +using Nethereum.Web3; +using System.Numerics; + +namespace Train.Solver.Common.Extensions; + +public static class Web3ClientExtensions +{ + public static async Task GetMaxPriorityFeePerGasAsync(this IWeb3 web3) + { + var request = new Nethereum.JsonRpc.Client.RpcRequest(Guid.NewGuid().ToString(), "eth_maxPriorityFeePerGas"); + + var response = await web3.Client.SendRequestAsync(request); + + return new HexBigInteger(response); + } +} \ No newline at end of file diff --git a/csharp/src/Workflow.EVM/Helpers/EthereumEIP1559FeeEstimator.cs b/csharp/src/Workflow.EVM/Helpers/EthereumEIP1559FeeEstimator.cs index 23c4c4df..fefa51a7 100644 --- a/csharp/src/Workflow.EVM/Helpers/EthereumEIP1559FeeEstimator.cs +++ b/csharp/src/Workflow.EVM/Helpers/EthereumEIP1559FeeEstimator.cs @@ -16,6 +16,8 @@ public class EthereumEIP1559FeeEstimator(ISmartNodeInvoker smartNodeInvoker) : F public virtual int HighPriorityBlockCount => 5; + private const string MinMaxPriorityFeePerGas = "1"; + public override BigInteger CalculateFee(Block block, Transaction transaction, EVMTransactionReceipt receipt) { return receipt.GasUsed * (block.BaseFeePerGas + transaction.MaxPriorityFeePerGas.Value); @@ -66,15 +68,14 @@ async Task GetFeeAmountAsync( suggestedFees.BaseFee.Value.CompoundInterestRate(MaximumBaseFeeIncreasePerBlock, blockCount); // Node returns 0 but transfer service throws exception in case of 0 - suggestedFees.MaxPriorityFeePerGas += 1; - suggestedFees.MaxPriorityFeePerGas = - suggestedFees.MaxPriorityFeePerGas.Value.PercentageIncrease(feePercentageIncrease); + var maxPriorityFeePerGas = BigInteger.Parse(MinMaxPriorityFeePerGas); + maxPriorityFeePerGas += (await web3.GetMaxPriorityFeePerGasAsync()).PercentageIncrease(feePercentageIncrease); return new Fee( feeCurrency.Symbol, feeCurrency.Decimals, new EIP1559Data( - suggestedFees.MaxPriorityFeePerGas.Value, + maxPriorityFeePerGas, increasedBaseFee, gasLimit)); } diff --git a/csharp/src/Workflow.Solana/Workflow.Solana.csproj b/csharp/src/Workflow.Solana/Workflow.Solana.csproj index 0f1ac094..4a66cc16 100644 --- a/csharp/src/Workflow.Solana/Workflow.Solana.csproj +++ b/csharp/src/Workflow.Solana/Workflow.Solana.csproj @@ -6,7 +6,7 @@ - +