From 6d91c75ec2d59ad4a0ece98bab7b23a8fa9c2b5f Mon Sep 17 00:00:00 2001 From: cryptooda Date: Fri, 26 Sep 2025 17:16:41 +0700 Subject: [PATCH] Fix position gas fee --- .../Bots/TradingBotBase.cs | 7 +++--- .../Handlers/OpenPositionCommandHandler.cs | 24 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Managing.Application/Bots/TradingBotBase.cs b/src/Managing.Application/Bots/TradingBotBase.cs index 1ae0059e..754d9f92 100644 --- a/src/Managing.Application/Bots/TradingBotBase.cs +++ b/src/Managing.Application/Bots/TradingBotBase.cs @@ -427,11 +427,12 @@ public class TradingBotBase : ITradingBot } else { - if (!internalPosition.Status.Equals(PositionStatus.New)) + // No position on the broker, the position have been closed by the exchange + if (internalPosition.Status.Equals(PositionStatus.Filled)) { - internalPosition.Status = PositionStatus.Filled; + internalPosition.Status = PositionStatus.Finished; - // Update Open trade status when position becomes Filled + // Update Open trade status when position becomes Finished if (internalPosition.Open != null) { internalPosition.Open.SetStatus(TradeStatus.Filled); diff --git a/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs b/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs index d8655a91..f98e1d9f 100644 --- a/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs +++ b/src/Managing.Application/Trading/Handlers/OpenPositionCommandHandler.cs @@ -46,16 +46,24 @@ namespace Managing.Application.Trading.Handlers // Gas fee check for EVM exchanges decimal gasFeeUsd = 0; - if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2) + if (!request.IsForPaperTrading) { - gasFeeUsd = await exchangeService.GetFee(account); - if (gasFeeUsd > Constants.GMX.Config.MaximumGasFeeUsd) + if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2) { - throw new InsufficientFundsException( - $"Gas fee too high for position opening: {gasFeeUsd:F2} USD (threshold: {Constants.GMX.Config.MaximumGasFeeUsd} USD). Position opening cancelled.", - InsufficientFundsType.InsufficientEth); + gasFeeUsd = await exchangeService.GetFee(account); + if (gasFeeUsd > Constants.GMX.Config.MaximumGasFeeUsd) + { + throw new InsufficientFundsException( + $"Gas fee too high for position opening: {gasFeeUsd:F2} USD (threshold: {Constants.GMX.Config.MaximumGasFeeUsd} USD). Position opening cancelled.", + InsufficientFundsType.InsufficientEth); + } } } + else + { + gasFeeUsd = Constants.GMX.Config.GasFeePerTransaction; + } + var price = request.IsForPaperTrading && request.Price.HasValue ? request.Price.Value @@ -87,7 +95,7 @@ namespace Managing.Application.Trading.Handlers // Calculate and set fees for the position var positionSizeUsd = (position.Open.Price * position.Open.Quantity) * position.Open.Leverage; - + // Set gas fees (only for EVM exchanges) if (account.Exchange == TradingExchanges.Evm || account.Exchange == TradingExchanges.GmxV2) { @@ -97,7 +105,7 @@ namespace Managing.Application.Trading.Handlers { position.GasFees = TradingHelpers.CalculateOpeningGasFees(); } - + // Set UI fees for opening position.UiFees = TradingHelpers.CalculateOpeningUiFees(positionSizeUsd);