-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from aboutcircles/feature/v2-pathfinder
Feature/v2 pathfinder
- Loading branch information
Showing
34 changed files
with
1,130 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Npgsql" Version="8.0.5" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Circles.Pathfinder.DTOs; | ||
|
||
public class FlowRequest | ||
{ | ||
public string? Source { get; set; } | ||
public string? Sink { get; set; } | ||
public string? TargetFlow { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Circles.Pathfinder.DTOs; | ||
|
||
public class MaxFlowResponse | ||
{ | ||
public string MaxFlow { get; set; } | ||
public List<TransferPathStep> Transfers { get; set; } | ||
|
||
public MaxFlowResponse(string maxFlow, List<TransferPathStep> transfers) | ||
{ | ||
MaxFlow = maxFlow; | ||
Transfers = transfers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Circles.Pathfinder.DTOs; | ||
|
||
public class TransferPathStep | ||
{ | ||
public string From { get; set; } = string.Empty; | ||
public string To { get; set; } = string.Empty; | ||
public string TokenOwner { get; set; } = string.Empty; | ||
public string Value { get; set; } = string.Empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Npgsql; | ||
|
||
namespace Circles.Pathfinder.Data | ||
{ | ||
// TODO: Use CirclesQuery<T> and remove the Npgsql dependency | ||
public class LoadGraph(string connectionString) | ||
{ | ||
public IEnumerable<(string Balance, string Account, string TokenAddress)> LoadV2Balances() | ||
{ | ||
var balanceQuery = @" | ||
select ""demurragedTotalBalance""::text, ""account"", ""tokenAddress"" | ||
from ""V_CrcV2_BalancesByAccountAndToken"" | ||
where ""demurragedTotalBalance"" > 0; | ||
"; | ||
|
||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
using var command = new NpgsqlCommand(balanceQuery, connection); | ||
using var reader = command.ExecuteReader(); | ||
|
||
while (reader.Read()) | ||
{ | ||
var balance = reader.GetString(0); | ||
var account = reader.GetString(1); | ||
var tokenAddress = reader.GetString(2); | ||
|
||
yield return (balance, account, tokenAddress); | ||
} | ||
} | ||
|
||
public IEnumerable<(string Truster, string Trustee, int Limit)> LoadV2Trust() | ||
{ | ||
var trustQuery = @" | ||
select truster, trustee | ||
from ""V_CrcV2_TrustRelations""; | ||
"; | ||
|
||
using var connection = new NpgsqlConnection(connectionString); | ||
connection.Open(); | ||
|
||
using var command = new NpgsqlCommand(trustQuery, connection); | ||
using var reader = command.ExecuteReader(); | ||
|
||
while (reader.Read()) | ||
{ | ||
var truster = reader.GetString(0); | ||
var trustee = reader.GetString(1); | ||
|
||
yield return (truster, trustee, 100); // Assuming a default trust limit of 100 in V2 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Numerics; | ||
|
||
namespace Circles.Pathfinder.Edges; | ||
|
||
/// <summary> | ||
/// Represents a capacity edge for potential token transfers between nodes. | ||
/// </summary> | ||
public class CapacityEdge : Edge | ||
{ | ||
public string Token { get; } | ||
public BigInteger InitialCapacity { get; } | ||
|
||
public CapacityEdge(string from, string to, string token, BigInteger initialCapacity) : base(from, to) | ||
{ | ||
Token = token; | ||
InitialCapacity = initialCapacity; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Circles.Pathfinder.Edges; | ||
|
||
public abstract class Edge | ||
{ | ||
public string From { get; } | ||
public string To { get; } | ||
|
||
public Edge(string from, string to) | ||
{ | ||
From = from; | ||
To = to; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Numerics; | ||
|
||
namespace Circles.Pathfinder.Edges; | ||
|
||
/// <summary> | ||
/// Represents a flow edge for actual token transfers between nodes. | ||
/// </summary> | ||
public class FlowEdge : CapacityEdge | ||
{ | ||
public BigInteger CurrentCapacity { get; set; } | ||
public BigInteger Flow { get; set; } | ||
public FlowEdge? ReverseEdge { get; set; } | ||
|
||
public FlowEdge(string from, string to, string token, BigInteger initialCapacity) | ||
: base(from, to, token, initialCapacity) | ||
{ | ||
CurrentCapacity = initialCapacity; | ||
Flow = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Circles.Pathfinder.Edges; | ||
|
||
/// <summary> | ||
/// Represents a trust relationship between two nodes. | ||
/// </summary> | ||
public class TrustEdge : Edge | ||
{ | ||
public TrustEdge(string from, string to) : base(from, to) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Numerics; | ||
using Circles.Pathfinder.Edges; | ||
using Circles.Pathfinder.Nodes; | ||
|
||
namespace Circles.Pathfinder.Graphs; | ||
|
||
public class BalanceGraph : IGraph<CapacityEdge> | ||
{ | ||
public IDictionary<string, Node> Nodes { get; } = new Dictionary<string, Node>(); | ||
public HashSet<CapacityEdge> Edges { get; } = new(); | ||
public IDictionary<string, BalanceNode> BalanceNodes { get; } = new Dictionary<string, BalanceNode>(); | ||
public IDictionary<string, AvatarNode> AvatarNodes { get; } = new Dictionary<string, AvatarNode>(); | ||
|
||
public void AddAvatar(string avatarAddress) | ||
{ | ||
Nodes.Add(avatarAddress, new AvatarNode(avatarAddress)); | ||
} | ||
|
||
public void AddBalance(string address, string token, BigInteger balance) | ||
{ | ||
address = address.ToLower(); | ||
token = token.ToLower(); | ||
if (!AvatarNodes.ContainsKey(address)) | ||
{ | ||
AvatarNodes.Add(address, new AvatarNode(address)); | ||
} | ||
|
||
var balanceNode = new BalanceNode(address, token, balance); | ||
Nodes.Add(balanceNode.Address, balanceNode); | ||
BalanceNodes.Add(balanceNode.Address, balanceNode); | ||
|
||
var capacityEdge = new CapacityEdge(address, balanceNode.Address, token, balance); | ||
Edges.Add(capacityEdge); | ||
|
||
AvatarNodes[address].OutEdges.Add(capacityEdge); | ||
BalanceNodes[balanceNode.Address].InEdges.Add(capacityEdge); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Numerics; | ||
using Circles.Pathfinder.Edges; | ||
using Circles.Pathfinder.Nodes; | ||
|
||
namespace Circles.Pathfinder.Graphs; | ||
|
||
public class CapacityGraph : IGraph<CapacityEdge> | ||
{ | ||
public IDictionary<string, Node> Nodes { get; } = new Dictionary<string, Node>(); | ||
public IDictionary<string, AvatarNode> AvatarNodes { get; } = new Dictionary<string, AvatarNode>(); | ||
public IDictionary<string, BalanceNode> BalanceNodes { get; } = new Dictionary<string, BalanceNode>(); | ||
public HashSet<CapacityEdge> Edges { get; } = new(); | ||
|
||
public void AddAvatar(string avatarAddress) | ||
{ | ||
avatarAddress = avatarAddress.ToLower(); | ||
if (!AvatarNodes.ContainsKey(avatarAddress)) | ||
{ | ||
AvatarNodes.Add(avatarAddress, new AvatarNode(avatarAddress)); | ||
Nodes.Add(avatarAddress, AvatarNodes[avatarAddress]); | ||
} | ||
} | ||
|
||
public void AddBalanceNode(string address, string token, BigInteger amount) | ||
{ | ||
address = address.ToLower(); | ||
token = token.ToLower(); | ||
|
||
var balanceNode = new BalanceNode(address, token, amount); | ||
balanceNode.Address = address; | ||
BalanceNodes.TryAdd(balanceNode.Address, balanceNode); | ||
Nodes.TryAdd(balanceNode.Address, balanceNode); | ||
} | ||
|
||
public void AddCapacityEdge(string from, string to, string token, BigInteger capacity) | ||
{ | ||
from = from.ToLower(); | ||
to = to.ToLower(); | ||
token = token.ToLower(); | ||
|
||
var edge = new CapacityEdge(from, to, token, capacity); | ||
Edges.Add(edge); | ||
|
||
// Optionally, you can manage adjacency lists if needed | ||
if (AvatarNodes.TryGetValue(from, out var node)) | ||
{ | ||
node.OutEdges.Add(edge); | ||
} | ||
else if (BalanceNodes.TryGetValue(from, out var balanceNode)) | ||
{ | ||
balanceNode.OutEdges.Add(edge); | ||
} | ||
|
||
if (AvatarNodes.TryGetValue(to, out var avatarNode)) | ||
{ | ||
avatarNode.InEdges.Add(edge); | ||
} | ||
else if (BalanceNodes.TryGetValue(to, out var balanceNode)) | ||
{ | ||
balanceNode.InEdges.Add(edge); | ||
} | ||
} | ||
} |
Oops, something went wrong.