This commit is contained in:
2024-01-13 21:57:22 +00:00
parent 5d349cf5f4
commit 6b69a4373b

View File

@ -1,4 +1,5 @@
using FASTER.core; using FASTER.core;
using Nostr.Client.Json;
using Nostr.Client.Keys; using Nostr.Client.Keys;
using Nostr.Client.Messages; using Nostr.Client.Messages;
using Nostr.Client.Requests; using Nostr.Client.Requests;
@ -11,18 +12,21 @@ namespace PayForReactions;
public class ZapperRelay : INostrRelay, IDisposable public class ZapperRelay : INostrRelay, IDisposable
{ {
private readonly ILogger<ZapperRelay> _logger;
private readonly Config _config; private readonly Config _config;
private readonly Lnurl _lnurl; private readonly Lnurl _lnurl;
private readonly AlbyApi _albyApi; private readonly AlbyApi _albyApi;
private readonly NostrServicesClient _nostrServices; private readonly NostrServicesClient _nostrServices;
private readonly EventSession _session; private readonly EventSession _session;
public ZapperRelay(Lnurl lnurl, AlbyApi albyApi, NostrServicesClient nostrServices, Config config, NostrStore store) public ZapperRelay(Lnurl lnurl, AlbyApi albyApi, NostrServicesClient nostrServices, Config config, NostrStore store,
ILogger<ZapperRelay> logger)
{ {
_lnurl = lnurl; _lnurl = lnurl;
_albyApi = albyApi; _albyApi = albyApi;
_nostrServices = nostrServices; _nostrServices = nostrServices;
_config = config; _config = config;
_logger = logger;
_session = store.MainStore.For(new SimpleFunctions<byte[], byte[]>()).NewSession<SimpleFunctions<byte[], byte[]>>(); _session = store.MainStore.For(new SimpleFunctions<byte[], byte[]>()).NewSession<SimpleFunctions<byte[], byte[]>>();
} }
@ -44,6 +48,7 @@ public class ZapperRelay : INostrRelay, IDisposable
var id = Convert.FromHexString(ev.Id!); var id = Convert.FromHexString(ev.Id!);
var obj = NostrBuf.Encode(ev); var obj = NostrBuf.Encode(ev);
(await _session.UpsertAsync(ref id, ref obj)).Complete(); (await _session.UpsertAsync(ref id, ref obj)).Complete();
_logger.LogInformation("Got targets event, saving {ev}", NostrJson.Serialize(ev));
return new(true, ""); return new(true, "");
} }
@ -96,6 +101,7 @@ public class ZapperRelay : INostrRelay, IDisposable
$"blocked: so sad... couldn't send a zap because you don't have a lightning address in your profile {senderProfile.Name}!"); $"blocked: so sad... couldn't send a zap because you don't have a lightning address in your profile {senderProfile.Name}!");
} }
_logger.LogInformation("Starting payment for {name} - {addr}", senderProfile.Name, senderProfile.LightningAddress);
var svc = await _lnurl.LoadAsync(parsedTarget.ToString()); var svc = await _lnurl.LoadAsync(parsedTarget.ToString());
if (svc == default) if (svc == default)
{ {
@ -124,6 +130,7 @@ public class ZapperRelay : INostrRelay, IDisposable
return new(false, $"blocked: failed to get invoice from {parsedTarget}"); return new(false, $"blocked: failed to get invoice from {parsedTarget}");
} }
_logger.LogInformation("Paying invoice {pr}", invoice.Pr);
if (!await _albyApi.PayInvoice(invoice.Pr)) if (!await _albyApi.PayInvoice(invoice.Pr))
{ {
return new(false, "blocked: failed to pay invoice!"); return new(false, "blocked: failed to pay invoice!");
@ -132,6 +139,7 @@ public class ZapperRelay : INostrRelay, IDisposable
var seenEvent = NostrBuf.Encode(ev); var seenEvent = NostrBuf.Encode(ev);
(await _session.UpsertAsync(ref seenId, ref seenEvent)).Complete(); (await _session.UpsertAsync(ref seenId, ref seenEvent)).Complete();
_logger.LogInformation("Zapped {name}!", senderProfile.Name);
return new(true, "Zapped!"); return new(true, "Zapped!");
} }