Fix status IsFinished/IsOpen/IsForMetrics + use redis for markets on gmx.tsx instead of inmemory cache

This commit is contained in:
2025-10-08 12:13:04 +07:00
parent 67065469a6
commit 86dd6849ea
9 changed files with 209 additions and 86 deletions

View File

@@ -211,7 +211,7 @@ public class AgentGrain : Grain, IAgentGrain
usdcWalletValue += usdcBalance; usdcWalletValue += usdcBalance;
} }
foreach (var position in positions.Where(p => !p.IsFinished())) foreach (var position in positions.Where(p => p.IsOpen()))
{ {
var positionUsd = position.Open.Price * position.Open.Quantity; var positionUsd = position.Open.Price * position.Open.Quantity;
var realized = position.ProfitAndLoss?.Realized ?? 0; var realized = position.ProfitAndLoss?.Realized ?? 0;

View File

@@ -925,14 +925,14 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
if (_tradingBot == null) if (_tradingBot == null)
{ {
// For non-running bots, check grain state positions // For non-running bots, check grain state positions
var hasOpenPositions = _state.State.Positions?.Values.Any(p => !p.IsFinished()) ?? false; var hasOpenPositions = _state.State.Positions?.Values.Any(p => p.IsOpen()) ?? false;
_logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from grain state)", _logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from grain state)",
this.GetPrimaryKey(), hasOpenPositions); this.GetPrimaryKey(), hasOpenPositions);
return Task.FromResult(hasOpenPositions); return Task.FromResult(hasOpenPositions);
} }
// For running bots, check live positions // For running bots, check live positions
var hasLiveOpenPositions = _tradingBot.Positions?.Values.Any(p => !p.IsFinished()) ?? false; var hasLiveOpenPositions = _tradingBot.Positions?.Values.Any(p => p.IsOpen()) ?? false;
_logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from live data)", _logger.LogDebug("Bot {GrainId} has open positions: {HasOpenPositions} (from live data)",
this.GetPrimaryKey(), hasLiveOpenPositions); this.GetPrimaryKey(), hasLiveOpenPositions);
return Task.FromResult(hasLiveOpenPositions); return Task.FromResult(hasLiveOpenPositions);
@@ -958,13 +958,13 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
_scopeFactory, _scopeFactory,
async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId)); async tradingService => await tradingService.GetPositionsByInitiatorIdentifierAsync(botId));
var hasOpenPositions = positions?.Any(p => !p.IsFinished()) ?? false; var hasOpenPositions = positions?.Any(p => p.IsOpen()) ?? false;
_logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}", _logger.LogDebug("Bot {GrainId} has open positions in database: {HasOpenPositions}",
botId, hasOpenPositions); botId, hasOpenPositions);
if (hasOpenPositions) if (hasOpenPositions)
{ {
var openPositions = positions?.Where(p => !p.IsFinished()).ToList() ?? new List<Position>(); var openPositions = positions?.Where(p => p.IsOpen()).ToList() ?? new List<Position>();
_logger.LogWarning( _logger.LogWarning(
"Bot {GrainId} cannot be stopped - has {Count} open positions in database: {Positions}", "Bot {GrainId} cannot be stopped - has {Count} open positions in database: {Positions}",
botId, openPositions.Count, string.Join(", ", openPositions.Select(p => p.Identifier))); botId, openPositions.Count, string.Join(", ", openPositions.Select(p => p.Identifier)));

View File

@@ -193,7 +193,7 @@ public class TradingBotBase : ITradingBot
public async Task UpdateSignals(HashSet<Candle> candles = null) public async Task UpdateSignals(HashSet<Candle> candles = null)
{ {
// If position open and not flipped, do not update signals // If position open and not flipped, do not update signals
if (!Config.FlipPosition && Positions.Any(p => !p.Value.IsFinished())) return; if (!Config.FlipPosition && Positions.Any(p => p.Value.IsOpen())) return;
// Check if we're in cooldown period for any direction // Check if we're in cooldown period for any direction
if (await IsInCooldownPeriodAsync()) if (await IsInCooldownPeriodAsync())
@@ -277,7 +277,7 @@ public class TradingBotBase : ITradingBot
private async Task ManagePositions() private async Task ManagePositions()
{ {
// First, process all existing positions that are not finished // First, process all existing positions that are not finished
foreach (var position in Positions.Values.Where(p => !p.IsFinished())) foreach (var position in Positions.Values.Where(p => p.IsOpen()))
{ {
var signalForPosition = Signals[position.SignalIdentifier]; var signalForPosition = Signals[position.SignalIdentifier];
if (signalForPosition == null) if (signalForPosition == null)
@@ -306,6 +306,31 @@ public class TradingBotBase : ITradingBot
await UpdatePosition(signalForPosition, position); await UpdatePosition(signalForPosition, position);
} }
// Second, process all finished positions to ensure they are updated in the database
// This should be removed in the future, when we have a better way to handle positions
foreach (var position in Positions.Values.Where(p => p.IsFinished()))
{
try
{
var positionInDatabase = await ServiceScopeHelpers.WithScopedService<ITradingService, Position>(_scopeFactory, async tradingService =>
{
return await tradingService.GetPositionByIdentifierAsync(position.Identifier);
});
if (positionInDatabase != null && positionInDatabase.Status != position.Status)
{
await UpdatePositionDatabase(position);
await LogInformation(
$"💾 Database Update\nPosition: `{position.Identifier}`\nStatus: `{position.Status}`\nUpdated in database");
}
}
catch (Exception ex)
{
await LogWarning($"Failed to update finished position {position.Identifier} in database: {ex.Message}");
}
}
// Then, open positions for signals waiting for a position open // Then, open positions for signals waiting for a position open
// But first, check if we already have a position for any of these signals // But first, check if we already have a position for any of these signals
var signalsWaitingForPosition = Signals.Values.Where(s => s.Status == SignalStatus.WaitingForPosition); var signalsWaitingForPosition = Signals.Values.Where(s => s.Status == SignalStatus.WaitingForPosition);
@@ -830,7 +855,7 @@ public class TradingBotBase : ITradingBot
// Check for any existing open position (not finished) for this ticker // Check for any existing open position (not finished) for this ticker
var openedPosition = var openedPosition =
Positions.Values.FirstOrDefault(p => !p.IsFinished() && p.SignalIdentifier != signal.Identifier); Positions.Values.FirstOrDefault(p => p.IsOpen() && p.SignalIdentifier != signal.Identifier);
decimal lastPrice = await ServiceScopeHelpers.WithScopedService<IExchangeService, decimal>(_scopeFactory, decimal lastPrice = await ServiceScopeHelpers.WithScopedService<IExchangeService, decimal>(_scopeFactory,
async exchangeService => async exchangeService =>
@@ -1708,8 +1733,8 @@ public class TradingBotBase : ITradingBot
public int GetWinRate() public int GetWinRate()
{ {
var succeededPositions = Positions.Values.Where(p => p.IsFinished()).Count(p => p.ProfitAndLoss?.Realized > 0); var succeededPositions = Positions.Values.Where(p => p.IsValidForMetrics()).Count(p => p.IsInProfit());
var total = Positions.Values.Where(p => p.IsFinished()).Count(); var total = Positions.Values.Where(p => p.IsValidForMetrics()).Count();
if (total == 0) if (total == 0)
return 0; return 0;

View File

@@ -484,7 +484,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
{ {
// Check if position was active at this hour // Check if position was active at this hour
var wasActiveAtThisHour = position.Date <= hourDateTime && var wasActiveAtThisHour = position.Date <= hourDateTime &&
(!position.IsFinished() || (position.IsOpen() ||
(position.StopLoss.Status == TradeStatus.Filled && (position.StopLoss.Status == TradeStatus.Filled &&
position.StopLoss.Date > hourDateTime) || position.StopLoss.Date > hourDateTime) ||
(position.TakeProfit1.Status == TradeStatus.Filled && (position.TakeProfit1.Status == TradeStatus.Filled &&
@@ -527,7 +527,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
} }
// Add closing volume if position was closed on or before this day // Add closing volume if position was closed on or before this day
if (position.IsFinished()) if (position.IsValidForMetrics())
{ {
if (position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate) if (position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate)
{ {
@@ -559,7 +559,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
} }
// Calculate CUMULATIVE fees and PnL for positions closed on or before this day // Calculate CUMULATIVE fees and PnL for positions closed on or before this day
var wasClosedOnOrBeforeThisDay = position.IsFinished() && ( var wasClosedOnOrBeforeThisDay = position.IsValidForMetrics() && (
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate) || (position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate) ||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date <= targetDate) || (position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date <= targetDate) ||
(position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled && (position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled &&

View File

@@ -301,7 +301,7 @@ namespace Managing.Application.ManageBot
// Calculate ROI based on total investment // Calculate ROI based on total investment
var totalInvestment = botData.Positions.Values var totalInvestment = botData.Positions.Values
.Where(p => p.IsFinished()) .Where(p => p.IsValidForMetrics())
.Sum(p => p.Open.Quantity * p.Open.Price); .Sum(p => p.Open.Quantity * p.Open.Price);
var netPnl = pnl - fees; var netPnl = pnl - fees;
var roi = totalInvestment > 0 ? (netPnl / totalInvestment) * 100 : 0; var roi = totalInvestment > 0 ? (netPnl / totalInvestment) * 100 : 0;

View File

@@ -564,7 +564,7 @@ public static class TradingBox
foreach (var position in positions.Values) foreach (var position in positions.Values)
{ {
// Only count positions that were opened or closed within the last 24 hours // Only count positions that were opened or closed within the last 24 hours
if (position.IsFinished() && if (position.IsValidForMetrics() &&
(position.Open.Date >= cutoff || (position.Open.Date >= cutoff ||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date >= cutoff) || (position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date >= cutoff) ||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date >= cutoff) || (position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date >= cutoff) ||
@@ -595,7 +595,7 @@ public static class TradingBox
if (timeFilter == "Total") if (timeFilter == "Total")
{ {
return positions return positions
.Where(p => p.IsFinished() && p.ProfitAndLoss != null) .Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null)
.Sum(p => p.ProfitAndLoss.Realized); .Sum(p => p.ProfitAndLoss.Realized);
} }
@@ -623,7 +623,7 @@ public static class TradingBox
// Include positions that were closed within the time range // Include positions that were closed within the time range
return positions return positions
.Where(p => p.IsFinished() && p.ProfitAndLoss != null && .Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null &&
(p.Date >= cutoffDate || (p.Date >= cutoffDate ||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) || (p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) || (p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||
@@ -673,8 +673,8 @@ public static class TradingBox
// Filter positions in the time range // Filter positions in the time range
var filteredPositions = timeFilter == "Total" var filteredPositions = timeFilter == "Total"
? positions.Where(p => p.IsFinished() && p.ProfitAndLoss != null) ? positions.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null)
: positions.Where(p => p.IsFinished() && p.ProfitAndLoss != null && : positions.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null &&
(p.Date >= cutoffDate || (p.Date >= cutoffDate ||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) || (p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) || (p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||
@@ -729,8 +729,8 @@ public static class TradingBox
// Filter positions in the time range // Filter positions in the time range
var filteredPositions = timeFilter == "Total" var filteredPositions = timeFilter == "Total"
? positions.Where(p => p.IsFinished()) ? positions.Where(p => p.IsValidForMetrics())
: positions.Where(p => p.IsFinished() && : positions.Where(p => p.IsValidForMetrics() &&
(p.Date >= cutoffDate || (p.Date >= cutoffDate ||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) || (p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) || (p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||

View File

@@ -137,20 +137,26 @@ public static class TradingHelpers
// Check which closing trade was executed (StopLoss, TakeProfit1, or TakeProfit2) // Check which closing trade was executed (StopLoss, TakeProfit1, or TakeProfit2)
if (position.StopLoss?.Status == TradeStatus.Filled) if (position.StopLoss?.Status == TradeStatus.Filled)
{ {
var stopLossPositionSizeUsd = (position.StopLoss.Price * position.StopLoss.Quantity) * position.StopLoss.Leverage; var stopLossPositionSizeUsd =
var uiFeeClose = stopLossPositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via StopLoss (position.StopLoss.Price * position.StopLoss.Quantity) * position.StopLoss.Leverage;
var uiFeeClose =
stopLossPositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via StopLoss
uiFees += uiFeeClose; uiFees += uiFeeClose;
} }
else if (position.TakeProfit1?.Status == TradeStatus.Filled) else if (position.TakeProfit1?.Status == TradeStatus.Filled)
{ {
var takeProfit1PositionSizeUsd = (position.TakeProfit1.Price * position.TakeProfit1.Quantity) * position.TakeProfit1.Leverage; var takeProfit1PositionSizeUsd = (position.TakeProfit1.Price * position.TakeProfit1.Quantity) *
var uiFeeClose = takeProfit1PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit1 position.TakeProfit1.Leverage;
var uiFeeClose =
takeProfit1PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit1
uiFees += uiFeeClose; uiFees += uiFeeClose;
} }
else if (position.TakeProfit2?.Status == TradeStatus.Filled) else if (position.TakeProfit2?.Status == TradeStatus.Filled)
{ {
var takeProfit2PositionSizeUsd = (position.TakeProfit2.Price * position.TakeProfit2.Quantity) * position.TakeProfit2.Leverage; var takeProfit2PositionSizeUsd = (position.TakeProfit2.Price * position.TakeProfit2.Quantity) *
var uiFeeClose = takeProfit2PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit2 position.TakeProfit2.Leverage;
var uiFeeClose =
takeProfit2PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit2
uiFees += uiFeeClose; uiFees += uiFeeClose;
} }
@@ -190,7 +196,7 @@ public static class TradingHelpers
return Constants.GMX.Config.GasFeePerTransaction; return Constants.GMX.Config.GasFeePerTransaction;
} }
/// <summary> /// <summary>
/// Calculates the total volume for a position based on its status and filled trades /// Calculates the total volume for a position based on its status and filled trades
/// </summary> /// </summary>
/// <param name="position">The position to calculate volume for</param> /// <param name="position">The position to calculate volume for</param>
@@ -201,7 +207,7 @@ public static class TradingHelpers
var totalVolume = position.Open.Price * position.Open.Quantity * position.Open.Leverage; var totalVolume = position.Open.Price * position.Open.Quantity * position.Open.Leverage;
// For closed positions, add volume from filled closing trades // For closed positions, add volume from filled closing trades
if (position.IsFinished()) if (position.IsValidForMetrics())
{ {
// Add Stop Loss volume if filled // Add Stop Loss volume if filled
if (position.StopLoss?.Status == TradeStatus.Filled) if (position.StopLoss?.Status == TradeStatus.Filled)
@@ -212,13 +218,15 @@ public static class TradingHelpers
// Add Take Profit 1 volume if filled // Add Take Profit 1 volume if filled
if (position.TakeProfit1?.Status == TradeStatus.Filled) if (position.TakeProfit1?.Status == TradeStatus.Filled)
{ {
totalVolume += position.TakeProfit1.Price * position.TakeProfit1.Quantity * position.TakeProfit1.Leverage; totalVolume += position.TakeProfit1.Price * position.TakeProfit1.Quantity *
position.TakeProfit1.Leverage;
} }
// Add Take Profit 2 volume if filled // Add Take Profit 2 volume if filled
if (position.TakeProfit2?.Status == TradeStatus.Filled) if (position.TakeProfit2?.Status == TradeStatus.Filled)
{ {
totalVolume += position.TakeProfit2.Price * position.TakeProfit2.Quantity * position.TakeProfit2.Leverage; totalVolume += position.TakeProfit2.Price * position.TakeProfit2.Quantity *
position.TakeProfit2.Leverage;
} }
} }

View File

@@ -77,16 +77,46 @@ namespace Managing.Domain.Trades
[Required] [Required]
public Guid InitiatorIdentifier { get; set; } public Guid InitiatorIdentifier { get; set; }
/// <summary>
/// Return true if position is finished even if the position was canceled or rejected
/// </summary>
/// <returns></returns>
public bool IsFinished() public bool IsFinished()
{ {
return Status switch return Status switch
{ {
PositionStatus.Finished => true, PositionStatus.Finished => true,
PositionStatus.Canceled => true,
PositionStatus.Rejected => true,
PositionStatus.Flipped => true, PositionStatus.Flipped => true,
_ => false _ => false
}; };
} }
public bool IsInProfit()
{
if (ProfitAndLoss?.Net == null)
{
return false;
}
return ProfitAndLoss.Net > 0;
}
public bool IsOpen()
{
return Status switch
{
PositionStatus.Filled => true,
_ => false
};
}
/// <summary>
/// Return true if position is valid for metrics calculation (PnL, WinRate, etc.)
/// Only positions with status Filled, Finished or Flipped are considered valid
/// </summary>
/// <returns></returns>
public bool IsValidForMetrics() public bool IsValidForMetrics()
{ {
return Status switch return Status switch
@@ -94,7 +124,6 @@ namespace Managing.Domain.Trades
PositionStatus.Filled => true, PositionStatus.Filled => true,
PositionStatus.Finished => true, PositionStatus.Finished => true,
PositionStatus.Flipped => true, PositionStatus.Flipped => true,
PositionStatus.Updating => true,
_ => false _ => false
}; };
} }

View File

@@ -2,6 +2,7 @@ import fp from 'fastify-plugin'
import {FastifyReply, FastifyRequest} from 'fastify' import {FastifyReply, FastifyRequest} from 'fastify'
import {z} from 'zod' import {z} from 'zod'
import {GmxSdk} from '../../generated/gmxsdk/index.js' import {GmxSdk} from '../../generated/gmxsdk/index.js'
import {createClient} from 'redis'
import {arbitrum} from 'viem/chains'; import {arbitrum} from 'viem/chains';
import {getTokenBySymbol} from '../../generated/gmxsdk/configs/tokens.js'; import {getTokenBySymbol} from '../../generated/gmxsdk/configs/tokens.js';
@@ -50,32 +51,11 @@ interface CacheEntry {
timestamp: number; timestamp: number;
} }
const CACHE_TTL = 30 * 60 * 1000; // 30 minutes in milliseconds const CACHE_TTL = 30 * 60; // 30 minutes in seconds (Redis TTL)
const marketsCache = new Map<string, CacheEntry>();
const MAX_CACHE_SIZE = 5; // Limit cache size to prevent memory issues
const OPERATION_TIMEOUT = 30000; // 30 seconds timeout for operations const OPERATION_TIMEOUT = 30000; // 30 seconds timeout for operations
const MEMORY_WARNING_THRESHOLD = 0.8; // Warn when memory usage exceeds 80%
const MAX_GAS_FEE_USD = 1.5; // Maximum gas fee in USD (1 USDC) const MAX_GAS_FEE_USD = 1.5; // Maximum gas fee in USD (1 USDC)
// Memory monitoring function
function checkMemoryUsage() {
const used = process.memoryUsage();
const total = used.heapTotal;
const usage = used.heapUsed / total;
if (usage > MEMORY_WARNING_THRESHOLD) {
console.warn(`⚠️ High memory usage detected: ${(usage * 100).toFixed(1)}% (${Math.round(used.heapUsed / 1024 / 1024)}MB / ${Math.round(total / 1024 / 1024)}MB)`);
// Clear cache if memory usage is too high
if (usage > 0.9) {
console.warn(`🧹 Clearing markets cache due to high memory usage`);
marketsCache.clear();
pendingRequests.clear();
}
}
}
/** /**
* Checks if the user has sufficient ETH balance for gas fees * Checks if the user has sufficient ETH balance for gas fees
* @param sdk The GMX SDK client * @param sdk The GMX SDK client
@@ -278,20 +258,37 @@ async function executeWithFallback<T>(
// Add a promise cache to prevent concurrent calls to the same endpoint // Add a promise cache to prevent concurrent calls to the same endpoint
const pendingRequests = new Map<string, Promise<{ marketsInfoData: MarketsInfoData; tokensData: TokensData }>>(); const pendingRequests = new Map<string, Promise<{ marketsInfoData: MarketsInfoData; tokensData: TokensData }>>();
async function getMarketsInfoWithCache(sdk: GmxSdk): Promise<{ marketsInfoData: MarketsInfoData; tokensData: TokensData }> { // Redis client helper function
// Check memory usage before proceeding async function getRedisClient() {
checkMemoryUsage(); const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
const redisPassword = process.env.REDIS_PASSWORD;
const redisConfig: any = {
url: redisUrl,
socket: {
connectTimeout: 2000, // 2 second connection timeout
reconnectStrategy: false // Don't retry on health check
}
};
if (redisPassword) {
redisConfig.password = redisPassword;
}
const redisClient = createClient(redisConfig);
// Suppress error logs to avoid spam
redisClient.on('error', () => {
// Silently ignore errors
});
await redisClient.connect();
return redisClient;
}
async function getMarketsInfoWithCache(sdk: GmxSdk): Promise<{ marketsInfoData: MarketsInfoData; tokensData: TokensData }> {
const cacheKey = `markets_${sdk.chainId}`; const cacheKey = `markets_${sdk.chainId}`;
const now = Date.now(); const now = Date.now();
const cached = marketsCache.get(cacheKey);
if (cached && (now - cached.timestamp) < CACHE_TTL) {
if (!cached.data.marketsInfoData || !cached.data.tokensData) {
throw new Error("Invalid cached data: missing markets or tokens info");
}
return cached.data as { marketsInfoData: MarketsInfoData; tokensData: TokensData };
}
// Check if there's already a pending request for this chain // Check if there's already a pending request for this chain
if (pendingRequests.has(cacheKey)) { if (pendingRequests.has(cacheKey)) {
@@ -300,7 +297,39 @@ async function getMarketsInfoWithCache(sdk: GmxSdk): Promise<{ marketsInfoData:
// Create a new request and cache the promise // Create a new request and cache the promise
const requestPromise = (async () => { const requestPromise = (async () => {
let redisClient = null;
try { try {
// Try to get data from Redis cache first
try {
redisClient = await getRedisClient();
const cachedData = await redisClient.get(cacheKey);
if (cachedData) {
const cached: CacheEntry = JSON.parse(cachedData);
const ageMinutes = (now - cached.timestamp) / (1000 * 60);
if (ageMinutes < CACHE_TTL / 60) { // Convert seconds to minutes for comparison
if (!cached.data.marketsInfoData || !cached.data.tokensData) {
throw new Error("Invalid cached data: missing markets or tokens info");
}
console.log(`✅ Markets info retrieved from Redis cache for chain ${sdk.chainId}`);
return cached.data as { marketsInfoData: MarketsInfoData; tokensData: TokensData };
}
}
} catch (redisError) {
console.warn(`⚠️ Redis cache unavailable, fetching fresh data: ${redisError instanceof Error ? redisError.message : 'Unknown error'}`);
} finally {
if (redisClient) {
try {
await redisClient.disconnect();
} catch (disconnectError) {
// Silently ignore disconnect errors
}
redisClient = null;
}
}
// Fetch fresh data from GMX
console.log(`🔄 Fetching markets info for chain ${sdk.chainId}...`); console.log(`🔄 Fetching markets info for chain ${sdk.chainId}...`);
const data = await withTimeout(sdk.markets.getMarketsInfo(), OPERATION_TIMEOUT); const data = await withTimeout(sdk.markets.getMarketsInfo(), OPERATION_TIMEOUT);
@@ -308,34 +337,64 @@ async function getMarketsInfoWithCache(sdk: GmxSdk): Promise<{ marketsInfoData:
throw new Error("Invalid response from GMX: missing markets or tokens info"); throw new Error("Invalid response from GMX: missing markets or tokens info");
} }
// Implement cache size limit to prevent memory issues // Try to cache the data in Redis
if (marketsCache.size >= MAX_CACHE_SIZE) { try {
// Remove the oldest entry if (!redisClient) {
const oldestKey = marketsCache.keys().next().value; redisClient = await getRedisClient();
marketsCache.delete(oldestKey); }
const cacheEntry: CacheEntry = {
data: data as { marketsInfoData: MarketsInfoData; tokensData: TokensData },
timestamp: now
};
await redisClient.setEx(cacheKey, CACHE_TTL, JSON.stringify(cacheEntry));
console.log(`✅ Markets info cached in Redis for chain ${sdk.chainId}`);
} catch (redisError) {
console.warn(`⚠️ Failed to cache data in Redis: ${redisError instanceof Error ? redisError.message : 'Unknown error'}`);
} finally {
if (redisClient) {
try {
await redisClient.disconnect();
} catch (disconnectError) {
// Silently ignore disconnect errors
}
}
} }
marketsCache.set(cacheKey, {
data: data as { marketsInfoData: MarketsInfoData; tokensData: TokensData },
timestamp: now
});
console.log(`✅ Markets info cached for chain ${sdk.chainId}`);
return data as { marketsInfoData: MarketsInfoData; tokensData: TokensData }; return data as { marketsInfoData: MarketsInfoData; tokensData: TokensData };
} catch (error) { } catch (error) {
console.error(`❌ Failed to fetch markets info for chain ${sdk.chainId}:`, error); console.error(`❌ Failed to fetch markets info for chain ${sdk.chainId}:`, error);
// If RPC is failing, return empty data to prevent memory issues // If RPC is failing, return empty data
const emptyData = { const emptyData = {
marketsInfoData: {} as MarketsInfoData, marketsInfoData: {} as MarketsInfoData,
tokensData: {} as TokensData tokensData: {} as TokensData
}; };
// Cache the empty data for a shorter time to allow retries // Try to cache the empty data for a shorter time to allow retries
marketsCache.set(cacheKey, { try {
data: emptyData, if (!redisClient) {
timestamp: now - (CACHE_TTL - 60000) // Cache for only 1 minute redisClient = await getRedisClient();
}); }
const cacheEntry: CacheEntry = {
data: emptyData,
timestamp: now - (CACHE_TTL * 1000 - 60000) // Cache for only 1 minute
};
await redisClient.setEx(cacheKey, 60, JSON.stringify(cacheEntry)); // 1 minute TTL
} catch (redisError) {
console.warn(`⚠️ Failed to cache empty data in Redis: ${redisError instanceof Error ? redisError.message : 'Unknown error'}`);
} finally {
if (redisClient) {
try {
await redisClient.disconnect();
} catch (disconnectError) {
// Silently ignore disconnect errors
}
}
}
console.log(`⚠️ Returning empty markets data for chain ${sdk.chainId} due to RPC failure`); console.log(`⚠️ Returning empty markets data for chain ${sdk.chainId} due to RPC failure`);
return emptyData; return emptyData;
@@ -1978,12 +2037,14 @@ export const claimGmxFundingFeesImpl = async (
const marketAddresses: string[] = []; const marketAddresses: string[] = [];
const tokenAddresses: string[] = []; const tokenAddresses: string[] = [];
// Get markets info data once for all iterations
const { marketsInfoData } = await getMarketsInfoWithCache(sdk);
// Build arrays of markets and tokens that have claimable amounts // Build arrays of markets and tokens that have claimable amounts
Object.entries(claimableFundingData).forEach(([marketAddress, data]) => { Object.entries(claimableFundingData).forEach(([marketAddress, data]) => {
if (data.claimableFundingAmountLong > 0) { if (data.claimableFundingAmountLong > 0) {
marketAddresses.push(marketAddress); marketAddresses.push(marketAddress);
// Get the market info to find the long token address // Get the market info to find the long token address
const { marketsInfoData } = marketsCache.get(`markets_${sdk.chainId}`)?.data || {};
if (marketsInfoData?.[marketAddress]) { if (marketsInfoData?.[marketAddress]) {
tokenAddresses.push(marketsInfoData[marketAddress].longToken.address); tokenAddresses.push(marketsInfoData[marketAddress].longToken.address);
} }
@@ -1992,7 +2053,6 @@ export const claimGmxFundingFeesImpl = async (
if (data.claimableFundingAmountShort > 0) { if (data.claimableFundingAmountShort > 0) {
marketAddresses.push(marketAddress); marketAddresses.push(marketAddress);
// Get the market info to find the short token address // Get the market info to find the short token address
const { marketsInfoData } = marketsCache.get(`markets_${sdk.chainId}`)?.data || {};
if (marketsInfoData?.[marketAddress]) { if (marketsInfoData?.[marketAddress]) {
tokenAddresses.push(marketsInfoData[marketAddress].shortToken.address); tokenAddresses.push(marketsInfoData[marketAddress].shortToken.address);
} }
@@ -2256,10 +2316,11 @@ export const claimGmxUiFeesImpl = async (
const marketAddresses: string[] = []; const marketAddresses: string[] = [];
const tokenAddresses: string[] = []; const tokenAddresses: string[] = [];
// Get markets info data once for all iterations
const { marketsInfoData } = await getMarketsInfoWithCache(sdk);
// Build arrays of markets and tokens that have claimable amounts // Build arrays of markets and tokens that have claimable amounts
Object.entries(claimableUiFeeData).forEach(([marketAddress, data]) => { Object.entries(claimableUiFeeData).forEach(([marketAddress, data]) => {
const { marketsInfoData } = marketsCache.get(`markets_${sdk.chainId}`)?.data || {};
if (!marketsInfoData?.[marketAddress]) { if (!marketsInfoData?.[marketAddress]) {
return; return;
} }