Add single time swap + fetch balance cache in AgentGrain

This commit is contained in:
2025-09-21 23:41:27 +07:00
parent 8afe80ca0e
commit 6aad2834a9
5 changed files with 369 additions and 59 deletions

View File

@@ -4,5 +4,91 @@ namespace Managing.Application.Bots.Models
{
public string AgentName { get; set; }
public HashSet<Guid> BotIds { get; set; } = new HashSet<Guid>();
/// <summary>
/// Tracks if a swap operation is currently in progress to prevent multiple simultaneous swaps
/// </summary>
public bool IsSwapInProgress { get; set; } = false;
/// <summary>
/// Timestamp of the last swap operation to implement cooldown period
/// </summary>
public DateTime? LastSwapTime { get; set; } = null;
/// <summary>
/// Cached balance data to reduce external API calls
/// </summary>
public CachedBalanceData? CachedBalanceData { get; set; } = null;
}
/// <summary>
/// Cached balance data to avoid repeated external API calls
/// </summary>
public class CachedBalanceData
{
/// <summary>
/// When the balance was last fetched
/// </summary>
public DateTime LastFetched { get; set; } = DateTime.UtcNow;
/// <summary>
/// The account name this balance data is for
/// </summary>
public string AccountName { get; set; } = string.Empty;
/// <summary>
/// ETH balance in USD
/// </summary>
public decimal EthValueInUsd { get; set; } = 0;
/// <summary>
/// USDC balance value
/// </summary>
public decimal UsdcValue { get; set; } = 0;
/// <summary>
/// Whether the cached data is still valid (less than 1 minute old)
/// </summary>
public bool IsValid => DateTime.UtcNow - LastFetched < TimeSpan.FromMinutes(1.5);
}
/// <summary>
/// Result of a balance check operation
/// </summary>
public class BalanceCheckResult
{
/// <summary>
/// Whether the balance check was successful
/// </summary>
public bool IsSuccessful { get; set; }
/// <summary>
/// The reason for failure if not successful
/// </summary>
public BalanceCheckFailureReason FailureReason { get; set; }
/// <summary>
/// Additional details about the result
/// </summary>
public string Message { get; set; } = string.Empty;
/// <summary>
/// Whether the bot should stop due to this result
/// </summary>
public bool ShouldStopBot { get; set; }
}
/// <summary>
/// Reasons why a balance check might fail
/// </summary>
public enum BalanceCheckFailureReason
{
None,
InsufficientUsdcBelowMinimum,
InsufficientUsdcForSwap,
SwapInProgress,
SwapCooldownActive,
BalanceFetchError,
SwapExecutionError
}
}