From 0eead300dbee637e7b1295bcd4e1afd7b72163f8 Mon Sep 17 00:00:00 2001 From: Kieran Date: Thu, 13 Jul 2023 14:08:43 +0100 Subject: [PATCH] Convert balance to milli-sats --- NostrStreamer/Config.cs | 4 ++-- NostrStreamer/Controllers/NostrController.cs | 4 ++-- NostrStreamer/Database/User.cs | 2 +- .../Migrations/20230713140000_MillisatsBalance.cs | 15 +++++++++++++++ NostrStreamer/Services/LndInvoiceStream.cs | 2 +- NostrStreamer/Services/StreamManager.cs | 10 +++++----- 6 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 NostrStreamer/Migrations/20230713140000_MillisatsBalance.cs diff --git a/NostrStreamer/Config.cs b/NostrStreamer/Config.cs index 9c01ff0..1930588 100644 --- a/NostrStreamer/Config.cs +++ b/NostrStreamer/Config.cs @@ -38,9 +38,9 @@ public class Config public LndConfig Lnd { get; init; } = null!; /// - /// Cost/min + /// Cost/min (milli-sats) /// - public int Cost { get; init; } = 10; + public int Cost { get; init; } = 10_000; /// /// List of video variants diff --git a/NostrStreamer/Controllers/NostrController.cs b/NostrStreamer/Controllers/NostrController.cs index 21faa7d..c6ef4bb 100644 --- a/NostrStreamer/Controllers/NostrController.cs +++ b/NostrStreamer/Controllers/NostrController.cs @@ -39,7 +39,7 @@ public class NostrController : Controller user = new() { PubKey = pk, - Balance = 1000, + Balance = 1000_000, StreamKey = Guid.NewGuid().ToString() }; @@ -58,7 +58,7 @@ public class NostrController : Controller { Unit = "min", Rate = _config.Cost, - Remaining = user.Balance + Remaining = (long)Math.Floor(user.Balance / 1000m) } }; diff --git a/NostrStreamer/Database/User.cs b/NostrStreamer/Database/User.cs index f01f995..0db3880 100644 --- a/NostrStreamer/Database/User.cs +++ b/NostrStreamer/Database/User.cs @@ -15,7 +15,7 @@ public class User public string? Event { get; set; } /// - /// Sats balance + /// Milli sats balance /// public long Balance { get; set; } diff --git a/NostrStreamer/Migrations/20230713140000_MillisatsBalance.cs b/NostrStreamer/Migrations/20230713140000_MillisatsBalance.cs new file mode 100644 index 0000000..06afe09 --- /dev/null +++ b/NostrStreamer/Migrations/20230713140000_MillisatsBalance.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using NostrStreamer.Database; + +namespace NostrStreamer.Migrations; + +[DbContext(typeof(StreamerContext))] +[Migration("20230713140000_MillisatsBalance")] +public class MillisatsBalance : Migration { + + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.Sql("update \"Users\" set \"Balance\" = \"Balance\" * 1000"); + } +} diff --git a/NostrStreamer/Services/LndInvoiceStream.cs b/NostrStreamer/Services/LndInvoiceStream.cs index 9edc969..dfb80ca 100644 --- a/NostrStreamer/Services/LndInvoiceStream.cs +++ b/NostrStreamer/Services/LndInvoiceStream.cs @@ -54,7 +54,7 @@ public class LndInvoicesStream : BackgroundService if (payment is {IsPaid: false} && msg.State is Invoice.Types.InvoiceState.Settled) { payment.IsPaid = true; - payment.User.Balance += (long)payment.Amount; + payment.User.Balance += (long)(payment.Amount * 1000L); await db.SaveChangesAsync(stoppingToken); } } diff --git a/NostrStreamer/Services/StreamManager.cs b/NostrStreamer/Services/StreamManager.cs index 5ecc08b..677b378 100644 --- a/NostrStreamer/Services/StreamManager.cs +++ b/NostrStreamer/Services/StreamManager.cs @@ -61,8 +61,8 @@ public class StreamManager var user = await GetUserFromStreamKey(streamKey); if (user == default) throw new Exception("No stream key found"); - const long balanceAlertThreshold = 500; - var cost = (int)Math.Ceiling(_config.Cost * (duration / 60d)); + const long balanceAlertThreshold = 500_000; + var cost = (long)Math.Ceiling(_config.Cost * (duration / 60d)); if (cost > 0) { await _db.Users @@ -70,11 +70,11 @@ public class StreamManager .ExecuteUpdateAsync(o => o.SetProperty(v => v.Balance, v => v.Balance - cost)); } - _logger.LogInformation("Stream consumed {n} seconds for {pubkey} costing {cost} sats", duration, user.PubKey, cost); + _logger.LogInformation("Stream consumed {n} seconds for {pubkey} costing {cost:#,##0} milli-sats", duration, user.PubKey, cost); if (user.Balance >= balanceAlertThreshold && user.Balance - cost < balanceAlertThreshold) { _nostr.Send(new NostrEventRequest(CreateStreamChat(user, - $"Your balance is below {balanceAlertThreshold} sats, please topup"))); + $"Your balance is below {(int)(balanceAlertThreshold / 1000m)} sats, please topup"))); } if (user.Balance <= 0) @@ -161,7 +161,7 @@ public class StreamManager if (status == "live") { var starts = existingEvent?.Tags?.FindFirstTagValue("starts") ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); - tags.Add(new ("starts", starts)); + tags.Add(new("starts", starts)); tags.Add( new("current_participants", (viewers.HasValue ? viewers.ToString() : null) ??