Update get backtests
This commit is contained in:
@@ -40,8 +40,8 @@ public class KaigenService : IKaigenService
|
||||
|
||||
// Check if credits are enabled via environment variable
|
||||
var creditsEnabledEnv = Environment.GetEnvironmentVariable("KAIGEN_CREDITS_ENABLED");
|
||||
_creditsEnabled = string.IsNullOrEmpty(creditsEnabledEnv) || creditsEnabledEnv.ToLower() == "true";
|
||||
|
||||
_creditsEnabled = !string.IsNullOrEmpty(creditsEnabledEnv) && creditsEnabledEnv.ToLower() == "true";
|
||||
|
||||
if (!_creditsEnabled)
|
||||
{
|
||||
_logger.LogInformation("Kaigen credits are disabled via KAIGEN_CREDITS_ENABLED environment variable");
|
||||
@@ -59,7 +59,8 @@ public class KaigenService : IKaigenService
|
||||
// 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.");
|
||||
throw new InvalidOperationException(
|
||||
"Kaigen PrivateKey is not configured. Please set the KAIGEN_PRIVATE_KEY environment variable.");
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(_settings.BaseUrl))
|
||||
@@ -74,7 +75,8 @@ public class KaigenService : IKaigenService
|
||||
if (!_creditsEnabled)
|
||||
{
|
||||
var dummyRequestId = Guid.NewGuid().ToString();
|
||||
_logger.LogInformation("Credits disabled - skipping debit for user {UserName}. Returning dummy request ID {RequestId}",
|
||||
_logger.LogInformation(
|
||||
"Credits disabled - skipping debit for user {UserName}. Returning dummy request ID {RequestId}",
|
||||
user.Name, dummyRequestId);
|
||||
return dummyRequestId;
|
||||
}
|
||||
@@ -83,10 +85,10 @@ public class KaigenService : IKaigenService
|
||||
{
|
||||
var walletAddress = GetUserWalletAddress(user);
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
|
||||
// Create the message to sign (concatenate the values)
|
||||
var message = $"{requestId}{walletAddress}{debitAmount}";
|
||||
|
||||
|
||||
// Sign the message
|
||||
var signature = SignMessage(message, _settings.PrivateKey);
|
||||
|
||||
@@ -99,24 +101,26 @@ public class KaigenService : IKaigenService
|
||||
signature = signature
|
||||
};
|
||||
|
||||
_logger.LogInformation("Debiting {Amount} credits for user {UserName} (wallet: {WalletAddress}) with request ID {RequestId}",
|
||||
_logger.LogInformation(
|
||||
"Debiting {Amount} credits for user {UserName} (wallet: {WalletAddress}) with request ID {RequestId}",
|
||||
debitAmount, user.Name, walletAddress, requestId);
|
||||
|
||||
var response = await _httpClient.PutAsJsonAsync(
|
||||
$"{_settings.BaseUrl}{_settings.DebitEndpoint}",
|
||||
requestPayload,
|
||||
$"{_settings.BaseUrl}{_settings.DebitEndpoint}",
|
||||
requestPayload,
|
||||
_jsonOptions);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError("Failed to debit credits. Status: {StatusCode}, Error: {Error}",
|
||||
_logger.LogError("Failed to debit credits. Status: {StatusCode}, Error: {Error}",
|
||||
response.StatusCode, errorContent);
|
||||
throw new Exception($"Failed to debit credits: {response.StatusCode} - {errorContent}");
|
||||
}
|
||||
|
||||
var result = await response.Content.ReadFromJsonAsync<KaigenResponse>(_jsonOptions);
|
||||
_logger.LogInformation("Successfully debited {Amount} credits for user {UserName} (wallet: {WalletAddress})",
|
||||
_logger.LogInformation(
|
||||
"Successfully debited {Amount} credits for user {UserName} (wallet: {WalletAddress})",
|
||||
debitAmount, user.Name, walletAddress);
|
||||
|
||||
return requestId;
|
||||
@@ -133,7 +137,7 @@ public class KaigenService : IKaigenService
|
||||
// If credits are disabled, return true (success) immediately
|
||||
if (!_creditsEnabled)
|
||||
{
|
||||
_logger.LogInformation("Credits disabled - skipping refund for user {UserName} with request ID {RequestId}",
|
||||
_logger.LogInformation("Credits disabled - skipping refund for user {UserName} with request ID {RequestId}",
|
||||
user.Name, requestId);
|
||||
return true;
|
||||
}
|
||||
@@ -141,10 +145,10 @@ public class KaigenService : IKaigenService
|
||||
try
|
||||
{
|
||||
var walletAddress = GetUserWalletAddress(user);
|
||||
|
||||
|
||||
// Create the message to sign (concatenate the values)
|
||||
var message = $"{requestId}{walletAddress}";
|
||||
|
||||
|
||||
// Sign the message
|
||||
var signature = SignMessage(message, _settings.PrivateKey);
|
||||
|
||||
@@ -156,23 +160,25 @@ public class KaigenService : IKaigenService
|
||||
signature = signature
|
||||
};
|
||||
|
||||
_logger.LogInformation("Refunding credits for user {UserName} (wallet: {WalletAddress}) with request ID {RequestId}",
|
||||
_logger.LogInformation(
|
||||
"Refunding credits for user {UserName} (wallet: {WalletAddress}) with request ID {RequestId}",
|
||||
user.Name, walletAddress, requestId);
|
||||
|
||||
var response = await _httpClient.PutAsJsonAsync(
|
||||
$"{_settings.BaseUrl}{_settings.RefundEndpoint}",
|
||||
requestPayload,
|
||||
$"{_settings.BaseUrl}{_settings.RefundEndpoint}",
|
||||
requestPayload,
|
||||
_jsonOptions);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
_logger.LogError("Failed to refund credits. Status: {StatusCode}, Error: {Error}",
|
||||
_logger.LogError("Failed to refund credits. Status: {StatusCode}, Error: {Error}",
|
||||
response.StatusCode, errorContent);
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Successfully refunded credits for user {UserName} (wallet: {WalletAddress})", user.Name, walletAddress);
|
||||
_logger.LogInformation("Successfully refunded credits for user {UserName} (wallet: {WalletAddress})",
|
||||
user.Name, walletAddress);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -191,7 +197,7 @@ public class KaigenService : IKaigenService
|
||||
|
||||
// Use the first account's key as the wallet address
|
||||
var walletAddress = user.Accounts[0].Key;
|
||||
|
||||
|
||||
if (string.IsNullOrEmpty(walletAddress))
|
||||
{
|
||||
throw new InvalidOperationException($"No wallet address found for user {user.Name}");
|
||||
|
||||
Reference in New Issue
Block a user