diff --git a/src/Managing.Infrastructure.Web3/Services/KaigenService.cs b/src/Managing.Infrastructure.Web3/Services/KaigenService.cs index 73dcd14..140c036 100644 --- a/src/Managing.Infrastructure.Web3/Services/KaigenService.cs +++ b/src/Managing.Infrastructure.Web3/Services/KaigenService.cs @@ -26,6 +26,7 @@ public class KaigenService : IKaigenService private readonly KaigenSettings _settings; private readonly ILogger _logger; private readonly JsonSerializerOptions _jsonOptions; + private readonly bool _creditsEnabled; public KaigenService(IOptions options, ILogger logger) { @@ -37,6 +38,16 @@ public class KaigenService : IKaigenService PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + // Check if credits are enabled via environment variable + var creditsEnabledEnv = Environment.GetEnvironmentVariable("KAIGEN_CREDITS_ENABLED"); + _creditsEnabled = string.IsNullOrEmpty(creditsEnabledEnv) || creditsEnabledEnv.ToLower() == "true"; + + if (!_creditsEnabled) + { + _logger.LogInformation("Kaigen credits are disabled via KAIGEN_CREDITS_ENABLED environment variable"); + return; // Skip private key validation if credits are disabled + } + // Always read from environment variable for security var envPrivateKey = Environment.GetEnvironmentVariable("KAIGEN_PRIVATE_KEY"); if (!string.IsNullOrEmpty(envPrivateKey)) @@ -45,7 +56,7 @@ public class KaigenService : IKaigenService _logger.LogInformation("Using KAIGEN_PRIVATE_KEY from environment variable"); } - // Validate required settings + // Validate required settings only if credits are enabled if (string.IsNullOrEmpty(_settings.PrivateKey)) { throw new InvalidOperationException("Kaigen PrivateKey is not configured. Please set the KAIGEN_PRIVATE_KEY environment variable."); @@ -59,6 +70,15 @@ public class KaigenService : IKaigenService public async Task DebitUserCreditsAsync(User user, decimal debitAmount) { + // If credits are disabled, return a dummy request ID + if (!_creditsEnabled) + { + var dummyRequestId = Guid.NewGuid().ToString(); + _logger.LogInformation("Credits disabled - skipping debit for user {UserName}. Returning dummy request ID {RequestId}", + user.Name, dummyRequestId); + return dummyRequestId; + } + try { var walletAddress = GetUserWalletAddress(user); @@ -110,6 +130,14 @@ public class KaigenService : IKaigenService public async Task RefundUserCreditsAsync(string requestId, User user) { + // If credits are disabled, return true (success) immediately + if (!_creditsEnabled) + { + _logger.LogInformation("Credits disabled - skipping refund for user {UserName} with request ID {RequestId}", + user.Name, requestId); + return true; + } + try { var walletAddress = GetUserWalletAddress(user);