Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions csharp/TrainSolver.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
<Project Path="src/Infrastructure/Infrastructure.csproj" />
<Project Path="src/Infrastrucutre.Secret.Treasury/Infrastrucutre.Secret.Treasury.csproj" />
</Folder>
<Folder Name="/src/Shared/">
<Project Path="src/Common/Common.csproj" />
<Project Path="src/SmartNodeInvoker/SmartNodeInvoker.csproj" Id="fa65e82e-9472-49f9-90bf-de90d8efe45b" />
</Folder>
<Folder Name="/src/Workflow/">
<Project Path="src/Workflow.Abstractions/Workflow.Abstractions.csproj" />
<Project Path="src/Workflow.Common/Workflow.Common.csproj" />
<Project Path="src/Workflow.EVM/Workflow.EVM.csproj" />
<Project Path="src/Workflow.Swap/Workflow.Swap.csproj" />
</Folder>
<Folder Name="/src/Workflow/Shared/">
<Project Path="src/Common/Common.csproj" />
<Project Path="src/SmartNodeInvoker/SmartNodeInvoker.csproj" Id="fa65e82e-9472-49f9-90bf-de90d8efe45b" />
</Folder>
<Folder Name="/tools/">
<Project Path="tools/DataSeeder/DataSeeder.csproj" />
<Project Path="tools/ProtoGenerator/ProtoGenerator.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/API/Endpoints/SolverV1Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static async Task<IResult> BuildTransactionAsync(
{
var prepareTransactionResponse = await temporalClient
.ExecuteWorkflowAsync<PrepareTransactionDto>(
"TransactionBuilderWorkflow",
"TransactionBuilderWorkflow",
args: [request],
new(id: Guid.CreateVersion7().ToString(), taskQueue: "Core"));

Expand Down
24 changes: 24 additions & 0 deletions csharp/src/AdminAPI/Endpoints/NetworkTypeEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using Train.Solver.Common.Enums;
using Train.Solver.Data.Abstractions.Entities;
using Train.Solver.Data.Abstractions.Repositories;
using Train.Solver.Infrastructure.Abstractions.Models;
using Train.Solver.Infrastructure.Extensions;

namespace Train.Solver.AdminAPI.Endpoints;

public static class NetworkTypeEndpoints
{
public static RouteGroupBuilder MapNetworkTypeEndpoints(this RouteGroupBuilder group)
{
group.MapGet("/network-types", GetAllAsync)
.Produces<List<string>>();

return group;
}

private static async Task<IResult> GetAllAsync()
{
return Results.Ok(typeof(NetworkType).GetEnumNames());
}
}
33 changes: 31 additions & 2 deletions csharp/src/AdminAPI/Endpoints/RebalanceEndpoints.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using Temporalio.Api.Enums.V1;
using Temporalio.Client;
using Temporalio.Converters;
using Train.Solver.AdminAPI.Models;
using Train.Solver.Common.Enums;
using Train.Solver.Common.Extensions;
using Train.Solver.Common.Helpers;
using Train.Solver.Data.Abstractions.Repositories;
using Train.Solver.Infrastructure.Abstractions.Models;
using Train.Solver.Infrastructure.Extensions;
Expand All @@ -16,12 +18,35 @@ public static class RebalanceEndpoints
{
public static RouteGroupBuilder MapRebalanceEndpoints(this RouteGroupBuilder group)
{
group.MapGet("/rebalance", GetAllAsync)
.Produces(StatusCodes.Status200OK);

group.MapPost("/rebalance", RebalanceAsync)
.Produces(StatusCodes.Status200OK);

return group;
}

private static async Task<IResult> GetAllAsync(
ITemporalClient temporalClient)
{
var query = $"`WorkflowId` STARTS_WITH \"Rebalance\" AND `ExecutionStatus`=\"Running\"";
var results = new List<string>();

await foreach (var wf in temporalClient.ListWorkflowsAsync(query))
{
var whHandle = temporalClient.GetWorkflowHandle(wf.Id);
var describedWorkflow = await whHandle.DescribeAsync();

if (describedWorkflow.Memo.TryGetValue("Summary", out var summary) && summary != null)
{
results.Add(summary.ToString()!);
}
}

return Results.Ok(results);
}

private static async Task<IResult> RebalanceAsync(
INetworkRepository repository,
IWalletRepository walletRepository,
Expand Down Expand Up @@ -74,12 +99,16 @@ private static async Task<IResult> RebalanceAsync(
SignerAgentUrl = wallet.SignerAgent.Url,
},
new TransactionExecutionContext()],
new(id: TemporalHelper.BuildProcessorId(network.Name, TransactionType.Transfer, Guid.NewGuid()),
new(id: TemporalHelper.BuildRebalanceProcessorId(network.Name, Guid.NewGuid()),
taskQueue: network.Type.ToString())
{
IdReusePolicy = WorkflowIdReusePolicy.TerminateIfRunning,
Memo = new Dictionary<string, object>
{
{ "Summary", $"Rebalancing { TokenUnitHelper.FromBaseUnits(request.Amount, token.Decimals) } {token.Asset} in {network.DisplayName} from {wallet.Address} to {trustedWallet.Address}" },
}
});

return Results.Ok();
return Results.Ok(workflowId);
}
}
2 changes: 1 addition & 1 deletion csharp/src/AdminAPI/Models/CreateRouteRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public class CreateRouteRequest

public bool IgnoreExpenseFee { get; set; }

public string? ServiceFee { get; set; }
public string ServiceFee { get; set; } = default!;
}
2 changes: 1 addition & 1 deletion csharp/src/AdminAPI/Models/UpdateRouteRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class UpdateRouteRequest
public string RateProvider { get; set; } = default!;
public BigInteger MinAmount { get; set; }
public BigInteger MaxAmount { get; set; }
public string? ServiceFee { get; set; }
public string ServiceFee { get; set; } = null!;
public RouteStatus Status { get; set; }
}
5 changes: 5 additions & 0 deletions csharp/src/AdminAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
.RequireRateLimiting("Fixed")
.WithTags("Rate Provider");

app.MapGroup("/api")
.MapNetworkTypeEndpoints()
.RequireRateLimiting("Fixed")
.WithTags("Network Type");

app.MapGroup("/api")
.MapTokenPriceEndpoints()
.RequireRateLimiting("Fixed")
Expand Down
6 changes: 5 additions & 1 deletion csharp/src/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
</ItemGroup>

<Target Name="NSwagGenerate" BeforeTargets="BeforeBuild" Condition="'$(TrainUrl)' != ''">
<Exec Command="$(NSwagExe_Net80) run $(ProjectDir)client.nswag /variables:TrainUrl=$(TrainUrl)" />
<Delete Files="$(ProjectDir)TrainSolverApiClient.cs" ContinueOnError="true" />

<Exec Command='$(NSwagExe_Net80) run "$(ProjectDir)client.nswag" /variables:TrainUrl=$(TrainUrl)' />
</Target>



</Project>
1 change: 1 addition & 0 deletions csharp/src/Common/Enums/NetworkType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public enum NetworkType
Solana,
Starknet,
Fuel,
Aztec,
}
4 changes: 2 additions & 2 deletions csharp/src/Data.Abstractions/Entities/Route.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Route : EntityBase

public int RateProviderId { get; set; }

public int? ServiceFeeId { get; set; }
public int ServiceFeeId { get; set; }

public RouteStatus Status { get; set; }

Expand All @@ -35,5 +35,5 @@ public class Route : EntityBase

public Wallet DestinationWallet { get; set; } = null!;

public ServiceFee? ServiceFee { get; set; }
public ServiceFee ServiceFee { get; set; } = null!;
}
4 changes: 2 additions & 2 deletions csharp/src/Data.Abstractions/Repositories/IRouteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IRouteRepository
BigInteger minAmount,
BigInteger maxAmount,
bool ignoreExpenseFee,
string? serviceFee);
string serviceFee);

Task UpdateRoutesStatusAsync(int[] ids, RouteStatus status);

Expand All @@ -43,5 +43,5 @@ public interface IRouteRepository
BigInteger minAmount,
BigInteger maxAmount,
RouteStatus status,
string? serviceFeeName);
string serviceFeeName);
}
1 change: 1 addition & 0 deletions csharp/src/Data.Npgsql/EFNetworkRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ await dbContext.Nodes
NetworkId = network.Id,
};

dbContext.Tokens.Add(token);
await dbContext.SaveChangesAsync();

return token;
Expand Down
32 changes: 16 additions & 16 deletions csharp/src/Data.Npgsql/EFRouteRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class EFRouteRepository(
BigInteger minAmount,
BigInteger maxAmount,
bool ignoreExpenseFee,
string? serviceFeeName)
string serviceFeeName)
{
var sourceToken = await networkRepository.GetTokenAsync(sourceNetworkName, sourceTokenSymbol);

Expand Down Expand Up @@ -63,6 +63,13 @@ public class EFRouteRepository(
{
throw new ArgumentException($"Rate provider {rateProviderName} not found");
}

var serviceFee = await feeRepository.GetServiceFeeAsync(serviceFeeName);

if (serviceFee == null)
{
throw new ArgumentException($"Service fee {serviceFeeName} not found");
}

var route = new Route
{
Expand All @@ -75,16 +82,9 @@ public class EFRouteRepository(
MaxAmountInSource = maxAmount.ToString(),
Status = RouteStatus.Active,
IgnoreExpenseFee = ignoreExpenseFee,
ServiceFee = serviceFee,
};

var serviceFee = string.IsNullOrEmpty(serviceFeeName)
? null
: await feeRepository.GetServiceFeeAsync(serviceFeeName);

if (serviceFee != null)
{
route.ServiceFeeId = serviceFee.Id;
}


dbContext.Routes.Add(route);
await dbContext.SaveChangesAsync();
Expand All @@ -101,7 +101,7 @@ public class EFRouteRepository(
BigInteger minAmount,
BigInteger maxAmount,
RouteStatus status,
string? serviceFeeName)
string serviceFeeName)
{
var route = await GetAsync(
sourceNetworkName,
Expand All @@ -123,17 +123,17 @@ public class EFRouteRepository(
route.RateProviderId = rateProvider.Id;
}

var serviceFee = string.IsNullOrEmpty(serviceFeeName)
? null
: await feeRepository.GetServiceFeeAsync(serviceFeeName);
var serviceFee = await feeRepository.GetServiceFeeAsync(serviceFeeName);

if (serviceFee != null)
if (serviceFee == null)
{
route.ServiceFeeId = serviceFee.Id;
throw new ArgumentException($"Service fee {serviceFeeName} not found");
}


route.MinAmountInSource = minAmount.ToString();
route.MaxAmountInSource = maxAmount.ToString();
route.ServiceFee = serviceFee;
route.Status = status;

await dbContext.SaveChangesAsync();
Expand Down
Loading