Skip to content

Commit

Permalink
Feature to add swaps to an address instead of a wallet (#342)
Browse files Browse the repository at this point in the history
* feat: database migration to add address to LiquidityRule object

* feat: optional wallet id in liquidity rules

* feat: wallet in liquidity rule nullable

* fix: check conditions for new liquidityrule treatment and optional fields in proto liquidityrule

* fix: fix problems in the addition or update of liquidity rules

* fix: fixing tests

* feat: add separation between swap and reverse swap wallet

* fix: reword phrase
  • Loading branch information
markettes authored Nov 20, 2023
1 parent a0071c5 commit 2527601
Show file tree
Hide file tree
Showing 18 changed files with 4,191 additions and 53 deletions.
8 changes: 6 additions & 2 deletions src/Automapper/MapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ public MapperProfile()
CreateMap<LiquidityRule, Nodeguard.LiquidityRule>()
.ForMember(x => x.MinimumLocalBalance, opt => opt.MapFrom(x => x.MinimumLocalBalance ?? 0))
.ForMember(x => x.MinimumRemoteBalance, opt => opt.MapFrom(x => x.MinimumRemoteBalance ?? 0))
.ForMember(x => x.IsReverseSwapWalletRule, opt => opt.MapFrom(x => x.IsReverseSwapWalletRule))
.ForMember(x => x.ReverseSwapAddress, opt => opt.MapFrom(x => x.ReverseSwapAddress ?? string.Empty))
.ForMember(x => x.ChannelId, opt => opt.MapFrom(x => x.Channel.ChanId))
.ForMember(x => x.WalletId, opt => opt.MapFrom(x => x.WalletId))
.ForMember(x => x.SwapWalletId, opt => opt.MapFrom(x => x.SwapWalletId))
.ForMember(x => x.ReverseSwapWalletId, opt => opt.MapFrom(x => x.ReverseSwapWalletId ?? 0))
.ForMember(x => x.RebalanceTarget, opt => opt.MapFrom(x => x.RebalanceTarget ?? 0))
.ForMember(x => x.NodePubkey, opt => opt.MapFrom(x => x.Node.PubKey));

CreateMap<LiquidityRule, LiquidityRule>()
.ForMember(x => x.Wallet, opt => opt.Ignore())
.ForMember(x => x.SwapWallet, opt => opt.Ignore())
.ForMember(x => x.ReverseSwapWallet, opt => opt.Ignore())
.ForMember(x => x.Channel, opt => opt.Ignore());

CreateMap<Node, Nodeguard.Node>()
Expand Down
14 changes: 11 additions & 3 deletions src/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ChannelOperationRequest>()
.HasOne(cor => cor.SourceNode)
.WithMany(node => node.ChannelOperationRequestsAsSource)
.HasForeignKey(cor => cor.SourceNodeId);
.HasOne(cor => cor.SourceNode)
.WithMany(node => node.ChannelOperationRequestsAsSource)
.HasForeignKey(cor => cor.SourceNodeId);
modelBuilder.Entity<ChannelOperationRequest>()
.HasOne(cor => cor.DestNode)
.WithMany(node => node.ChannelOperationRequestsAsDestination)
.HasForeignKey(cor => cor.DestNodeId);
modelBuilder.Entity<LiquidityRule>()
.HasOne(lr => lr.SwapWallet)
.WithMany(wallet => wallet.LiquidityRulesAsSwapWallet)
.HasForeignKey(lr => lr.SwapWalletId);
modelBuilder.Entity<LiquidityRule>()
.HasOne(lr => lr.ReverseSwapWallet)
.WithMany(wallet => wallet.LiquidityRulesAsReverseSwapWallet)
.HasForeignKey(lr => lr.ReverseSwapWalletId);

modelBuilder.Entity<Node>().HasIndex(x => x.PubKey).IsUnique();
modelBuilder.Entity<Wallet>().HasIndex(x => new {x.InternalWalletSubDerivationPath, x.InternalWalletMasterFingerprint}).IsUnique();
Expand Down
3 changes: 2 additions & 1 deletion src/Data/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using NBXplorer;
using NBXplorer.DerivationStrategy;
using NBXplorer.Models;
using NodeGuard.Data.Repositories;
using Key = NodeGuard.Data.Models.Key;

namespace NodeGuard.Data
Expand All @@ -45,7 +46,7 @@ public static void Initialize(IServiceProvider serviceProvider)
serviceProvider.GetRequiredService<IChannelOperationRequestRepository>();
var walletRepository = serviceProvider.GetRequiredService<IWalletRepository>();
var keyRepository = serviceProvider.GetRequiredService<IKeyRepository>();

var liquidityRuleRepository = serviceProvider.GetRequiredService<ILiquidityRuleRepository>();
var webHostEnvironment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
var logger = serviceProvider.GetService<ILogger<Program>>();
//Nbxplorer setup & check
Expand Down
19 changes: 17 additions & 2 deletions src/Data/Models/LiquidityRule.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace NodeGuard.Data.Models;

/// <summary>
Expand All @@ -15,13 +17,26 @@ public class LiquidityRule: Entity
/// </summary>
public decimal? RebalanceTarget { get; set; }

/// <summary>
/// Let's you know if the rule has a wallet or an address as a target for the rebalancing operation
/// </summary>
public bool IsReverseSwapWalletRule { get; set; }

/// <summary>
/// In case that is a rule that sends the funds to an address instead of a wallet this is the address
/// </summary>
public string? ReverseSwapAddress { get; set; }

#region Relationships

public int ChannelId { get; set; }
public Channel Channel { get; set; }

public int WalletId { get; set; }
public Wallet Wallet { get; set; }
public int SwapWalletId { get; set; }
public Wallet SwapWallet { get; set; }

public int? ReverseSwapWalletId { get; set; }
public Wallet? ReverseSwapWallet { get; set; }

public int NodeId { get; set; }
public Node Node { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion src/Data/Models/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ public class Wallet : Entity
public InternalWallet? InternalWallet { get; set; }


public ICollection<LiquidityRule> LiquidityRules { get; set; }
public ICollection<LiquidityRule> LiquidityRulesAsSwapWallet { get; set; }

public ICollection<LiquidityRule> LiquidityRulesAsReverseSwapWallet { get; set; }

#endregion Relationships

Expand Down
8 changes: 6 additions & 2 deletions src/Data/Repositories/ChannelRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public async Task<List<Channel>> GetAll()
.Include(x => x.LiquidityRules)
.ThenInclude(x => x.Node)
.Include(x => x.LiquidityRules)
.ThenInclude(x => x.Wallet)
.ThenInclude(x => x.SwapWallet)
.Include(x => x.LiquidityRules)
.ThenInclude(x => x.ReverseSwapWallet)
.ToListAsync();
}

Expand Down Expand Up @@ -210,7 +212,9 @@ public async Task<List<Channel>> GetAllManagedByUserNodes(string loggedUserId)
.Include(x => x.LiquidityRules)
.ThenInclude(x => x.Node)
.Include(x => x.LiquidityRules)
.ThenInclude(x => x.Wallet).AsSplitQuery()
.ThenInclude(x => x.SwapWallet)
.Include(x => x.LiquidityRules)
.ThenInclude(x => x.ReverseSwapWallet).AsSplitQuery()
.Where(x => x.SourceNode.Users.Select(user => user.Id).Contains(loggedUserId) ||
x.DestinationNode.Users.Select(user => user.Id).Contains(loggedUserId)).ToList();

Expand Down
4 changes: 3 additions & 1 deletion src/Data/Repositories/LiquidityRuleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public async Task<ICollection<LiquidityRule>> GetByNodePubKey(string nodePubKey)

var result = applicationDbContext.LiquidityRules
.Include(x=> x.Node)
.Include(x=> x.Wallet)
.Include(x=> x.SwapWallet)
.ThenInclude(x=> x.InternalWallet)
.Include(x => x.ReverseSwapWallet)
.ThenInclude(x => x.InternalWallet)
.Include(x=> x.Channel)
.Where(x=> x.Node.PubKey == nodePubKey && x.Channel.IsAutomatedLiquidityEnabled && x.Channel.Status != Channel.ChannelStatus.Closed).ToList();

Expand Down
Loading

0 comments on commit 2527601

Please sign in to comment.