fix: amount
This commit is contained in:
@ -57,122 +57,121 @@ public class ReactionQueueWorker : BackgroundService
|
|||||||
}
|
}
|
||||||
|
|
||||||
var amount = MapKindToAmount(ev.Kind);
|
var amount = MapKindToAmount(ev.Kind);
|
||||||
if (amount != default)
|
if (amount == default) continue;
|
||||||
|
|
||||||
|
_logger.LogInformation("Got reaction {json}", NostrJson.Serialize(ev));
|
||||||
|
var eTag = ev.Tags?.FirstOrDefault(a => a.TagIdentifier == "e");
|
||||||
|
var pTag = ev.Tags?.FirstOrDefault(a => a.TagIdentifier == "p" && a.AdditionalData[0] == target);
|
||||||
|
if (pTag == default || eTag == default)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Got reaction {json}", NostrJson.Serialize(ev));
|
_logger.LogInformation("blocked: must be a reaction to {target}", target);
|
||||||
var eTag = ev.Tags?.FirstOrDefault(a => a.TagIdentifier == "e");
|
continue;
|
||||||
var pTag = ev.Tags?.FirstOrDefault(a => a.TagIdentifier == "p" && a.AdditionalData[0] == target);
|
}
|
||||||
if (pTag == default || eTag == default)
|
|
||||||
|
var idBytes = Convert.FromHexString(eTag.AdditionalData[0]);
|
||||||
|
var refEventResult = (await _session.ReadAsync(ref idBytes)).Complete();
|
||||||
|
if (refEventResult.status.NotFound)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: parent event not found");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var seenId = Convert.FromHexString(ev.Id!);
|
||||||
|
var seenIdResult = (await _session.ReadAsync(ref seenId)).Complete();
|
||||||
|
if (seenIdResult.status.Found)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: already zapped this one, how dare you!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var refEvent = NostrBuf.Decode(refEventResult.output);
|
||||||
|
if (refEvent == default)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: parent event not found");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (refEvent.Pubkey != target)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: parent event not posted by {target}", target);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sender = ev.Pubkey!;
|
||||||
|
var senderProfile = await _nostrServices.Profile(NostrPublicKey.FromHex(sender).Bech32);
|
||||||
|
if (senderProfile == default)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: couldn't find your profile anon!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsedTarget = Lnurl.ParseLnUrl(senderProfile.LightningAddress ?? "");
|
||||||
|
if (parsedTarget == default)
|
||||||
|
{
|
||||||
|
_logger.LogInformation(
|
||||||
|
"blocked: so sad... couldn't send a zap because you don't have a lightning address in your profile {name}!",
|
||||||
|
senderProfile.Name);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Starting payment for {name} - {addr}", senderProfile.Name, senderProfile.LightningAddress);
|
||||||
|
var svc = await _lnurl.LoadAsync(parsedTarget.ToString());
|
||||||
|
if (svc == default)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: wallet is down, no zap for you!");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var keyEventWalletSeen = $"zapaped:{refEvent.Id}:{parsedTarget}";
|
||||||
|
var eventWalletZapTry = _cache.Get<int>(keyEventWalletSeen);
|
||||||
|
if (eventWalletZapTry > 0)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("blocked: hey i already zapped you! (count={count})", eventWalletZapTry);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
_cache.Set(keyEventWalletSeen, ++eventWalletZapTry);
|
||||||
|
|
||||||
|
var key = NostrPrivateKey.FromBech32(_config.PrivateKey);
|
||||||
|
var myPubkey = key.DerivePublicKey().Hex;
|
||||||
|
var zap = new NostrEvent
|
||||||
|
{
|
||||||
|
Kind = NostrKind.ZapRequest,
|
||||||
|
Content = "Thanks for your interaction!",
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
Pubkey = myPubkey,
|
||||||
|
Tags = new NostrEventTags(
|
||||||
|
new NostrEventTag("e", ev.Id!),
|
||||||
|
new NostrEventTag("p", sender),
|
||||||
|
new NostrEventTag("relays", "wss://relay.snort.social", "wss://nos.lol", "wss://relay.damus.io"),
|
||||||
|
new NostrEventTag("amount", (amount.Value * 1000).ToString())
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
var zapSigned = zap.Sign(key);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var invoice = await _lnurl.GetInvoiceAsync(svc, amount.Value, "Thanks for your interaction!", zapSigned);
|
||||||
|
if (string.IsNullOrEmpty(invoice.Pr))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("blocked: must be a reaction to {target}", target);
|
_logger.LogInformation("blocked: failed to get invoice from {target}", parsedTarget);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var idBytes = Convert.FromHexString(eTag.AdditionalData[0]);
|
_logger.LogInformation("Paying invoice {pr}", invoice.Pr);
|
||||||
var refEventResult = (await _session.ReadAsync(ref idBytes)).Complete();
|
if (!await _albyApi.PayInvoice(invoice.Pr))
|
||||||
if (refEventResult.status.NotFound)
|
|
||||||
{
|
{
|
||||||
_logger.LogInformation("blocked: parent event not found");
|
_logger.LogInformation("blocked: failed to pay invoice!");
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var seenId = Convert.FromHexString(ev.Id!);
|
var seenEvent = NostrBuf.Encode(ev);
|
||||||
var seenIdResult = (await _session.ReadAsync(ref seenId)).Complete();
|
(await _session.UpsertAsync(ref seenId, ref seenEvent, token: stoppingToken)).Complete();
|
||||||
if (seenIdResult.status.Found)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: already zapped this one, how dare you!");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var refEvent = NostrBuf.Decode(refEventResult.output);
|
_logger.LogInformation("Zapped {name}!", senderProfile.Name);
|
||||||
if (refEvent == default)
|
}
|
||||||
{
|
catch (Exception e)
|
||||||
_logger.LogInformation("blocked: parent event not found");
|
{
|
||||||
continue;
|
_logger.LogError(e.ToString());
|
||||||
}
|
|
||||||
|
|
||||||
if (refEvent.Pubkey != target)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: parent event not posted by {target}", target);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var sender = ev.Pubkey!;
|
|
||||||
var senderProfile = await _nostrServices.Profile(NostrPublicKey.FromHex(sender).Bech32);
|
|
||||||
if (senderProfile == default)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: couldn't find your profile anon!");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var parsedTarget = Lnurl.ParseLnUrl(senderProfile.LightningAddress ?? "");
|
|
||||||
if (parsedTarget == default)
|
|
||||||
{
|
|
||||||
_logger.LogInformation(
|
|
||||||
"blocked: so sad... couldn't send a zap because you don't have a lightning address in your profile {name}!",
|
|
||||||
senderProfile.Name);
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation("Starting payment for {name} - {addr}", senderProfile.Name, senderProfile.LightningAddress);
|
|
||||||
var svc = await _lnurl.LoadAsync(parsedTarget.ToString());
|
|
||||||
if (svc == default)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: wallet is down, no zap for you!");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var keyEventWalletSeen = $"zapaped:{refEvent.Id}:{parsedTarget}";
|
|
||||||
var eventWalletZapTry = _cache.Get<int>(keyEventWalletSeen);
|
|
||||||
if (eventWalletZapTry > 0)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: hey i already zapped you! (count={count})", eventWalletZapTry);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
_cache.Set(keyEventWalletSeen, ++eventWalletZapTry);
|
|
||||||
|
|
||||||
var key = NostrPrivateKey.FromBech32(_config.PrivateKey);
|
|
||||||
var myPubkey = key.DerivePublicKey().Hex;
|
|
||||||
var zap = new NostrEvent
|
|
||||||
{
|
|
||||||
Kind = NostrKind.ZapRequest,
|
|
||||||
Content = "Thanks for your interaction!",
|
|
||||||
CreatedAt = DateTime.UtcNow,
|
|
||||||
Pubkey = myPubkey,
|
|
||||||
Tags = new NostrEventTags(
|
|
||||||
new NostrEventTag("e", ev.Id!),
|
|
||||||
new NostrEventTag("p", sender),
|
|
||||||
new NostrEventTag("relays", "wss://relay.snort.social", "wss://nos.lol", "wss://relay.damus.io"),
|
|
||||||
new NostrEventTag("amount", (amount * 1000).ToString()!)
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
var zapSigned = zap.Sign(key);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var invoice = await _lnurl.GetInvoiceAsync(svc, 5, "Thanks for your interaction!", zapSigned);
|
|
||||||
if (string.IsNullOrEmpty(invoice.Pr))
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: failed to get invoice from {target}", parsedTarget);
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogInformation("Paying invoice {pr}", invoice.Pr);
|
|
||||||
if (!await _albyApi.PayInvoice(invoice.Pr))
|
|
||||||
{
|
|
||||||
_logger.LogInformation("blocked: failed to pay invoice!");
|
|
||||||
}
|
|
||||||
|
|
||||||
var seenEvent = NostrBuf.Encode(ev);
|
|
||||||
(await _session.UpsertAsync(ref seenId, ref seenEvent, token: stoppingToken)).Complete();
|
|
||||||
|
|
||||||
_logger.LogInformation("Zapped {name}!", senderProfile.Name);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_logger.LogError(e.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user