Skip to content

Commit

Permalink
Send: Lightning updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisreimann committed Dec 11, 2024
1 parent 8162f0a commit 44170d0
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
6 changes: 2 additions & 4 deletions BTCPayApp.Core/BTCPayServer/BTCPayPaymentsNotifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ await _connectionManager.HubProxy
.SendInvoiceUpdate(e.ToInvoice());
}



public async Task StopAsync(CancellationToken cancellationToken)
{
_paymentsManager.OnPaymentUpdate -= OnPaymentUpdate;
Expand All @@ -47,6 +45,6 @@ public async Task StopAsync(CancellationToken cancellationToken)
public void StartListen()
{
_listening = true;

}
}
}
5 changes: 2 additions & 3 deletions BTCPayApp.Core/LDK/PaymentsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,14 @@ public async Task<AppLightningPayment> PayInvoice(BOLT11PaymentRequest paymentRe
amt = Math.Max(amt, explicitAmount?.MilliSatoshi ?? 0);

//check if we have a db record with same pay hash but has the preimage set

await using var context = await _dbContextFactory.CreateDbContextAsync();
var inbound = await context.LightningPayments.FirstOrDefaultAsync(lightningPayment =>
lightningPayment.PaymentHash == paymentRequest.PaymentHash && lightningPayment.Inbound);

if (inbound is not null)
{
var successSelfPay = false;
var newOutbound = new AppLightningPayment()
var newOutbound = new AppLightningPayment
{
Inbound = false,
Value = amt,
Expand Down Expand Up @@ -216,7 +215,7 @@ public async Task<AppLightningPayment> PayInvoice(BOLT11PaymentRequest paymentRe
return newOutbound;
}

var outbound = new AppLightningPayment()
var outbound = new AppLightningPayment
{
Inbound = false,
Value = amt,
Expand Down
4 changes: 2 additions & 2 deletions BTCPayApp.Core/Wallet/OnChainWalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private set
}

public event AsyncEventHandler<(OnChainWalletState Old, OnChainWalletState New)>? StateChanged;
public event AsyncEventHandler<CoinSnapshot>? SnapshotUpdated;
public event AsyncEventHandler<CoinSnapshot>? OnSnapshotUpdate;

public OnChainWalletManager(
ConfigProvider configProvider,
Expand Down Expand Up @@ -368,7 +368,7 @@ private async Task UpdateSnapshot()
}).ToArray())
};
await _configProvider.Set(WalletConfig.Key, config, true);
SnapshotUpdated?.Invoke(this, config.CoinSnapshot);
OnSnapshotUpdate?.Invoke(this, config.CoinSnapshot);
}
finally
{
Expand Down
19 changes: 12 additions & 7 deletions BTCPayApp.UI/Pages/SendPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@using BTCPayApp.UI.Features
@using BTCPayApp.UI.Components.Layout
@using BTCPayApp.UI.Models
@using BTCPayApp.UI.Pages.SignedOut
@using BTCPayApp.UI.Util
@using BTCPayServer.Client
@using BTCPayServer.Client.App
@using BTCPayServer.Client.App.Models
Expand Down Expand Up @@ -393,7 +395,7 @@
public string? Destination { get; set; }
[Required]
public decimal? Amount { get; set; }
[Required]
[RequiredIf(nameof(Address), RequiredIfTargetValue.NotNull)]
public decimal? FeeRate { get; set; }

public BOLT11PaymentRequest? Bolt11 { get; set; }
Expand All @@ -413,7 +415,8 @@
? await LightningNodeManager.IsLightningOurs(lightning) ? SourceState.Supported : SourceState.NotSupported
: SourceState.NotConfigured;

OnChainWalletManager.SnapshotUpdated += OnSnapshotUpdated;
OnChainWalletManager.OnSnapshotUpdate += OnTransactionUpdated;
if (PaymentsManager != null) PaymentsManager.OnPaymentUpdate += OnTransactionUpdated;

_ = LoadTransactions();
}
Expand All @@ -422,12 +425,13 @@
{
base.DisposeAsyncCore(disposing);

OnChainWalletManager.SnapshotUpdated -= OnSnapshotUpdated;
OnChainWalletManager.OnSnapshotUpdate -= OnTransactionUpdated;
if (PaymentsManager != null) PaymentsManager.OnPaymentUpdate -= OnTransactionUpdated;

return ValueTask.CompletedTask;
}

private async Task OnSnapshotUpdated(object? sender, CoinSnapshot _)
private async Task OnTransactionUpdated(object? sender, object _)
{
await LoadTransactions();
}
Expand Down Expand Up @@ -501,6 +505,7 @@
{
Model.Bolt11 = null;
Model.Address = null;
Model.FeeRate = null;
Model.Transaction = null;
return;
}
Expand All @@ -509,6 +514,7 @@
{
Model.Bolt11 = BOLT11PaymentRequest.Parse(destination, network);
Model.Amount = Model.Bolt11.MinimumAmount.ToDecimal(UnitLightMoney);
return;
}
catch
{
Expand Down Expand Up @@ -643,12 +649,11 @@
var result = await PaymentsManager.PayInvoice(bolt11, amount);
SuccessMessage = $"Payment {result.PaymentId} sent with status {result.Status}";
Model = new SendModel();
_ = LoadTransactions();
return new FormResult(true, "Payment sent");
return new FormResult(true, "Lightning payment sent");
}
catch (Exception ex)
{
return new FormResult(false, "Payment failed: " + ex.Message);
return new FormResult(false, "Lightning p failed: " + ex.Message);
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion BTCPayApp.UI/StateMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private async Task ListenIn(IDispatcher dispatcher)
}
};

onChainWalletManager.SnapshotUpdated += async (sender, args) =>
onChainWalletManager.OnSnapshotUpdate += async (sender, args) =>
{
if (accountManager.GetCurrentStore() is { } store)
{
Expand Down
10 changes: 8 additions & 2 deletions BTCPayApp.UI/Util/RequiredIfAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace BTCPayApp.UI.Util;

public class RequiredIfAttribute(string otherProperty, object targetValue) : ValidationAttribute
public enum RequiredIfTargetValue
{
NotNull
}

public class RequiredIfAttribute(string otherProperty, object? targetValue) : ValidationAttribute
{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
var otherPropertyValue = validationContext.ObjectType
.GetProperty(otherProperty)?
.GetValue(validationContext.ObjectInstance);
if (otherPropertyValue is null || !otherPropertyValue.Equals(targetValue)) return ValidationResult.Success;
if (otherPropertyValue is null || targetValue is RequiredIfTargetValue.NotNull ||
!otherPropertyValue.Equals(targetValue)) return ValidationResult.Success;
return string.IsNullOrWhiteSpace(value?.ToString())
? new ValidationResult(ErrorMessage ?? "This field is required.")
: ValidationResult.Success;
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,30 @@ Click the Connect button, use `http://localhost:14142` as the server URL and an

After the first run of `DEV ALL` on a Linux machine with a new .NET setup, you may run into the [dotnet dev-certs - Untrusted Root](https://github.com/dotnet/aspnetcore/issues/41503)
error, and you may find a solution at the [following link](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-dev-certs)

## Lightning Channels

To establish channels for local testing, you can use the Docker Lightning CLI scripts like this:

```bash
# The scripts are inside the submodule's test directory
cd submodules/btcpayserver/BTCPayServer.Tests

# Run the general channel setup for the BTCPay LN nodes
./docker-lightning-channel-setup.sh
```

Besides establishing channel connections between the various BTCPay LN testing nodes, this will also give you their node URIs.

### Create channel

- App: Go to the [Lightning Channels view](https://localhost:7016/settings/lightning/channels) and connect to one of the peer node URIs from the comand above
- App: Open Channel to that peer
- CLI: Run `./docker-bitcoin-generate.sh 5`
- App: Refresh the Lightning Channels view and see if the channel got confirmed
- App: Go to the [Send view](https://localhost:7016/send) and see if your Lightning local/outbound capacity is present

### Send payment

- CLI: Generate an invoice with the peer script, e.g. `./docker-customer-lncli.sh addinvoice --amt 10000`
- App: Pay the Lightning invoice on the Send view

0 comments on commit 44170d0

Please sign in to comment.