Fix status IsFinished/IsOpen/IsForMetrics + use redis for markets on gmx.tsx instead of inmemory cache
This commit is contained in:
@@ -211,7 +211,7 @@ public class AgentGrain : Grain, IAgentGrain
|
||||
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 realized = position.ProfitAndLoss?.Realized ?? 0;
|
||||
|
||||
@@ -925,14 +925,14 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
if (_tradingBot == null)
|
||||
{
|
||||
// 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)",
|
||||
this.GetPrimaryKey(), hasOpenPositions);
|
||||
return Task.FromResult(hasOpenPositions);
|
||||
}
|
||||
|
||||
// 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)",
|
||||
this.GetPrimaryKey(), hasLiveOpenPositions);
|
||||
return Task.FromResult(hasLiveOpenPositions);
|
||||
@@ -958,13 +958,13 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
|
||||
_scopeFactory,
|
||||
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}",
|
||||
botId, 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(
|
||||
"Bot {GrainId} cannot be stopped - has {Count} open positions in database: {Positions}",
|
||||
botId, openPositions.Count, string.Join(", ", openPositions.Select(p => p.Identifier)));
|
||||
|
||||
@@ -193,7 +193,7 @@ public class TradingBotBase : ITradingBot
|
||||
public async Task UpdateSignals(HashSet<Candle> candles = null)
|
||||
{
|
||||
// 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
|
||||
if (await IsInCooldownPeriodAsync())
|
||||
@@ -277,7 +277,7 @@ public class TradingBotBase : ITradingBot
|
||||
private async Task ManagePositions()
|
||||
{
|
||||
// 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];
|
||||
if (signalForPosition == null)
|
||||
@@ -306,6 +306,31 @@ public class TradingBotBase : ITradingBot
|
||||
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
|
||||
// But first, check if we already have a position for any of these signals
|
||||
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
|
||||
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,
|
||||
async exchangeService =>
|
||||
@@ -1708,8 +1733,8 @@ public class TradingBotBase : ITradingBot
|
||||
|
||||
public int GetWinRate()
|
||||
{
|
||||
var succeededPositions = Positions.Values.Where(p => p.IsFinished()).Count(p => p.ProfitAndLoss?.Realized > 0);
|
||||
var total = Positions.Values.Where(p => p.IsFinished()).Count();
|
||||
var succeededPositions = Positions.Values.Where(p => p.IsValidForMetrics()).Count(p => p.IsInProfit());
|
||||
var total = Positions.Values.Where(p => p.IsValidForMetrics()).Count();
|
||||
|
||||
if (total == 0)
|
||||
return 0;
|
||||
|
||||
@@ -484,7 +484,7 @@ public class PlatformSummaryGrain : Grain, IPlatformSummaryGrain, IRemindable
|
||||
{
|
||||
// Check if position was active at this hour
|
||||
var wasActiveAtThisHour = position.Date <= hourDateTime &&
|
||||
(!position.IsFinished() ||
|
||||
(position.IsOpen() ||
|
||||
(position.StopLoss.Status == TradeStatus.Filled &&
|
||||
position.StopLoss.Date > hourDateTime) ||
|
||||
(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
|
||||
if (position.IsFinished())
|
||||
if (position.IsValidForMetrics())
|
||||
{
|
||||
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
|
||||
var wasClosedOnOrBeforeThisDay = position.IsFinished() && (
|
||||
var wasClosedOnOrBeforeThisDay = position.IsValidForMetrics() && (
|
||||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date.Date <= targetDate) ||
|
||||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date.Date <= targetDate) ||
|
||||
(position.TakeProfit2 != null && position.TakeProfit2.Status == TradeStatus.Filled &&
|
||||
|
||||
@@ -301,7 +301,7 @@ namespace Managing.Application.ManageBot
|
||||
|
||||
// Calculate ROI based on total investment
|
||||
var totalInvestment = botData.Positions.Values
|
||||
.Where(p => p.IsFinished())
|
||||
.Where(p => p.IsValidForMetrics())
|
||||
.Sum(p => p.Open.Quantity * p.Open.Price);
|
||||
var netPnl = pnl - fees;
|
||||
var roi = totalInvestment > 0 ? (netPnl / totalInvestment) * 100 : 0;
|
||||
|
||||
@@ -564,7 +564,7 @@ public static class TradingBox
|
||||
foreach (var position in positions.Values)
|
||||
{
|
||||
// Only count positions that were opened or closed within the last 24 hours
|
||||
if (position.IsFinished() &&
|
||||
if (position.IsValidForMetrics() &&
|
||||
(position.Open.Date >= cutoff ||
|
||||
(position.StopLoss.Status == TradeStatus.Filled && position.StopLoss.Date >= cutoff) ||
|
||||
(position.TakeProfit1.Status == TradeStatus.Filled && position.TakeProfit1.Date >= cutoff) ||
|
||||
@@ -595,7 +595,7 @@ public static class TradingBox
|
||||
if (timeFilter == "Total")
|
||||
{
|
||||
return positions
|
||||
.Where(p => p.IsFinished() && p.ProfitAndLoss != null)
|
||||
.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null)
|
||||
.Sum(p => p.ProfitAndLoss.Realized);
|
||||
}
|
||||
|
||||
@@ -623,7 +623,7 @@ public static class TradingBox
|
||||
|
||||
// Include positions that were closed within the time range
|
||||
return positions
|
||||
.Where(p => p.IsFinished() && p.ProfitAndLoss != null &&
|
||||
.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null &&
|
||||
(p.Date >= cutoffDate ||
|
||||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.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
|
||||
var filteredPositions = timeFilter == "Total"
|
||||
? positions.Where(p => p.IsFinished() && p.ProfitAndLoss != null)
|
||||
: positions.Where(p => p.IsFinished() && p.ProfitAndLoss != null &&
|
||||
? positions.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null)
|
||||
: positions.Where(p => p.IsValidForMetrics() && p.ProfitAndLoss != null &&
|
||||
(p.Date >= cutoffDate ||
|
||||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.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
|
||||
var filteredPositions = timeFilter == "Total"
|
||||
? positions.Where(p => p.IsFinished())
|
||||
: positions.Where(p => p.IsFinished() &&
|
||||
? positions.Where(p => p.IsValidForMetrics())
|
||||
: positions.Where(p => p.IsValidForMetrics() &&
|
||||
(p.Date >= cutoffDate ||
|
||||
(p.StopLoss.Status == TradeStatus.Filled && p.StopLoss.Date >= cutoffDate) ||
|
||||
(p.TakeProfit1.Status == TradeStatus.Filled && p.TakeProfit1.Date >= cutoffDate) ||
|
||||
|
||||
@@ -137,20 +137,26 @@ public static class TradingHelpers
|
||||
// Check which closing trade was executed (StopLoss, TakeProfit1, or TakeProfit2)
|
||||
if (position.StopLoss?.Status == TradeStatus.Filled)
|
||||
{
|
||||
var stopLossPositionSizeUsd = (position.StopLoss.Price * position.StopLoss.Quantity) * position.StopLoss.Leverage;
|
||||
var uiFeeClose = stopLossPositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via StopLoss
|
||||
var stopLossPositionSizeUsd =
|
||||
(position.StopLoss.Price * position.StopLoss.Quantity) * position.StopLoss.Leverage;
|
||||
var uiFeeClose =
|
||||
stopLossPositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via StopLoss
|
||||
uiFees += uiFeeClose;
|
||||
}
|
||||
else if (position.TakeProfit1?.Status == TradeStatus.Filled)
|
||||
{
|
||||
var takeProfit1PositionSizeUsd = (position.TakeProfit1.Price * position.TakeProfit1.Quantity) * position.TakeProfit1.Leverage;
|
||||
var uiFeeClose = takeProfit1PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit1
|
||||
var takeProfit1PositionSizeUsd = (position.TakeProfit1.Price * position.TakeProfit1.Quantity) *
|
||||
position.TakeProfit1.Leverage;
|
||||
var uiFeeClose =
|
||||
takeProfit1PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit1
|
||||
uiFees += uiFeeClose;
|
||||
}
|
||||
else if (position.TakeProfit2?.Status == TradeStatus.Filled)
|
||||
{
|
||||
var takeProfit2PositionSizeUsd = (position.TakeProfit2.Price * position.TakeProfit2.Quantity) * position.TakeProfit2.Leverage;
|
||||
var uiFeeClose = takeProfit2PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit2
|
||||
var takeProfit2PositionSizeUsd = (position.TakeProfit2.Price * position.TakeProfit2.Quantity) *
|
||||
position.TakeProfit2.Leverage;
|
||||
var uiFeeClose =
|
||||
takeProfit2PositionSizeUsd * Constants.GMX.Config.UiFeeRate; // Fee paid on closing via TakeProfit2
|
||||
uiFees += uiFeeClose;
|
||||
}
|
||||
|
||||
@@ -201,7 +207,7 @@ public static class TradingHelpers
|
||||
var totalVolume = position.Open.Price * position.Open.Quantity * position.Open.Leverage;
|
||||
|
||||
// For closed positions, add volume from filled closing trades
|
||||
if (position.IsFinished())
|
||||
if (position.IsValidForMetrics())
|
||||
{
|
||||
// Add Stop Loss volume if filled
|
||||
if (position.StopLoss?.Status == TradeStatus.Filled)
|
||||
@@ -212,13 +218,15 @@ public static class TradingHelpers
|
||||
// Add Take Profit 1 volume if 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
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,16 +77,46 @@ namespace Managing.Domain.Trades
|
||||
[Required]
|
||||
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()
|
||||
{
|
||||
return Status switch
|
||||
{
|
||||
PositionStatus.Finished => true,
|
||||
PositionStatus.Canceled => true,
|
||||
PositionStatus.Rejected => true,
|
||||
PositionStatus.Flipped => true,
|
||||
_ => 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()
|
||||
{
|
||||
return Status switch
|
||||
@@ -94,7 +124,6 @@ namespace Managing.Domain.Trades
|
||||
PositionStatus.Filled => true,
|
||||
PositionStatus.Finished => true,
|
||||
PositionStatus.Flipped => true,
|
||||
PositionStatus.Updating => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import fp from 'fastify-plugin'
|
||||
import {FastifyReply, FastifyRequest} from 'fastify'
|
||||
import {z} from 'zod'
|
||||
import {GmxSdk} from '../../generated/gmxsdk/index.js'
|
||||
import {createClient} from 'redis'
|
||||
|
||||
import {arbitrum} from 'viem/chains';
|
||||
import {getTokenBySymbol} from '../../generated/gmxsdk/configs/tokens.js';
|
||||
@@ -50,32 +51,11 @@ interface CacheEntry {
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
const CACHE_TTL = 30 * 60 * 1000; // 30 minutes in milliseconds
|
||||
const marketsCache = new Map<string, CacheEntry>();
|
||||
const MAX_CACHE_SIZE = 5; // Limit cache size to prevent memory issues
|
||||
const CACHE_TTL = 30 * 60; // 30 minutes in seconds (Redis TTL)
|
||||
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)
|
||||
|
||||
// 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
|
||||
* @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
|
||||
const pendingRequests = new Map<string, Promise<{ marketsInfoData: MarketsInfoData; tokensData: TokensData }>>();
|
||||
|
||||
async function getMarketsInfoWithCache(sdk: GmxSdk): Promise<{ marketsInfoData: MarketsInfoData; tokensData: TokensData }> {
|
||||
// Check memory usage before proceeding
|
||||
checkMemoryUsage();
|
||||
// Redis client helper function
|
||||
async function getRedisClient() {
|
||||
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 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
|
||||
if (pendingRequests.has(cacheKey)) {
|
||||
@@ -300,7 +297,39 @@ async function getMarketsInfoWithCache(sdk: GmxSdk): Promise<{ marketsInfoData:
|
||||
|
||||
// Create a new request and cache the promise
|
||||
const requestPromise = (async () => {
|
||||
let redisClient = null;
|
||||
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}...`);
|
||||
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");
|
||||
}
|
||||
|
||||
// Implement cache size limit to prevent memory issues
|
||||
if (marketsCache.size >= MAX_CACHE_SIZE) {
|
||||
// Remove the oldest entry
|
||||
const oldestKey = marketsCache.keys().next().value;
|
||||
marketsCache.delete(oldestKey);
|
||||
// Try to cache the data in Redis
|
||||
try {
|
||||
if (!redisClient) {
|
||||
redisClient = await getRedisClient();
|
||||
}
|
||||
|
||||
marketsCache.set(cacheKey, {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ Markets info cached for chain ${sdk.chainId}`);
|
||||
return data as { marketsInfoData: MarketsInfoData; tokensData: TokensData };
|
||||
} catch (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 = {
|
||||
marketsInfoData: {} as MarketsInfoData,
|
||||
tokensData: {} as TokensData
|
||||
};
|
||||
|
||||
// Cache the empty data for a shorter time to allow retries
|
||||
marketsCache.set(cacheKey, {
|
||||
// Try to cache the empty data for a shorter time to allow retries
|
||||
try {
|
||||
if (!redisClient) {
|
||||
redisClient = await getRedisClient();
|
||||
}
|
||||
|
||||
const cacheEntry: CacheEntry = {
|
||||
data: emptyData,
|
||||
timestamp: now - (CACHE_TTL - 60000) // Cache for only 1 minute
|
||||
});
|
||||
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`);
|
||||
return emptyData;
|
||||
@@ -1978,12 +2037,14 @@ export const claimGmxFundingFeesImpl = async (
|
||||
const marketAddresses: 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
|
||||
Object.entries(claimableFundingData).forEach(([marketAddress, data]) => {
|
||||
if (data.claimableFundingAmountLong > 0) {
|
||||
marketAddresses.push(marketAddress);
|
||||
// Get the market info to find the long token address
|
||||
const { marketsInfoData } = marketsCache.get(`markets_${sdk.chainId}`)?.data || {};
|
||||
if (marketsInfoData?.[marketAddress]) {
|
||||
tokenAddresses.push(marketsInfoData[marketAddress].longToken.address);
|
||||
}
|
||||
@@ -1992,7 +2053,6 @@ export const claimGmxFundingFeesImpl = async (
|
||||
if (data.claimableFundingAmountShort > 0) {
|
||||
marketAddresses.push(marketAddress);
|
||||
// Get the market info to find the short token address
|
||||
const { marketsInfoData } = marketsCache.get(`markets_${sdk.chainId}`)?.data || {};
|
||||
if (marketsInfoData?.[marketAddress]) {
|
||||
tokenAddresses.push(marketsInfoData[marketAddress].shortToken.address);
|
||||
}
|
||||
@@ -2256,10 +2316,11 @@ export const claimGmxUiFeesImpl = async (
|
||||
const marketAddresses: 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
|
||||
Object.entries(claimableUiFeeData).forEach(([marketAddress, data]) => {
|
||||
const { marketsInfoData } = marketsCache.get(`markets_${sdk.chainId}`)?.data || {};
|
||||
|
||||
if (!marketsInfoData?.[marketAddress]) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user