Convert balance to milli-sats

This commit is contained in:
2023-07-13 14:08:43 +01:00
parent 04e6b5fd57
commit 0eead300db
6 changed files with 26 additions and 11 deletions

View File

@ -38,9 +38,9 @@ public class Config
public LndConfig Lnd { get; init; } = null!; public LndConfig Lnd { get; init; } = null!;
/// <summary> /// <summary>
/// Cost/min /// Cost/min (milli-sats)
/// </summary> /// </summary>
public int Cost { get; init; } = 10; public int Cost { get; init; } = 10_000;
/// <summary> /// <summary>
/// List of video variants /// List of video variants

View File

@ -39,7 +39,7 @@ public class NostrController : Controller
user = new() user = new()
{ {
PubKey = pk, PubKey = pk,
Balance = 1000, Balance = 1000_000,
StreamKey = Guid.NewGuid().ToString() StreamKey = Guid.NewGuid().ToString()
}; };
@ -58,7 +58,7 @@ public class NostrController : Controller
{ {
Unit = "min", Unit = "min",
Rate = _config.Cost, Rate = _config.Cost,
Remaining = user.Balance Remaining = (long)Math.Floor(user.Balance / 1000m)
} }
}; };

View File

@ -15,7 +15,7 @@ public class User
public string? Event { get; set; } public string? Event { get; set; }
/// <summary> /// <summary>
/// Sats balance /// Milli sats balance
/// </summary> /// </summary>
public long Balance { get; set; } public long Balance { get; set; }

View File

@ -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");
}
}

View File

@ -54,7 +54,7 @@ public class LndInvoicesStream : BackgroundService
if (payment is {IsPaid: false} && msg.State is Invoice.Types.InvoiceState.Settled) if (payment is {IsPaid: false} && msg.State is Invoice.Types.InvoiceState.Settled)
{ {
payment.IsPaid = true; payment.IsPaid = true;
payment.User.Balance += (long)payment.Amount; payment.User.Balance += (long)(payment.Amount * 1000L);
await db.SaveChangesAsync(stoppingToken); await db.SaveChangesAsync(stoppingToken);
} }
} }

View File

@ -61,8 +61,8 @@ public class StreamManager
var user = await GetUserFromStreamKey(streamKey); var user = await GetUserFromStreamKey(streamKey);
if (user == default) throw new Exception("No stream key found"); if (user == default) throw new Exception("No stream key found");
const long balanceAlertThreshold = 500; const long balanceAlertThreshold = 500_000;
var cost = (int)Math.Ceiling(_config.Cost * (duration / 60d)); var cost = (long)Math.Ceiling(_config.Cost * (duration / 60d));
if (cost > 0) if (cost > 0)
{ {
await _db.Users await _db.Users
@ -70,11 +70,11 @@ public class StreamManager
.ExecuteUpdateAsync(o => o.SetProperty(v => v.Balance, v => v.Balance - cost)); .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) if (user.Balance >= balanceAlertThreshold && user.Balance - cost < balanceAlertThreshold)
{ {
_nostr.Send(new NostrEventRequest(CreateStreamChat(user, _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) if (user.Balance <= 0)
@ -161,7 +161,7 @@ public class StreamManager
if (status == "live") if (status == "live")
{ {
var starts = existingEvent?.Tags?.FindFirstTagValue("starts") ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(); var starts = existingEvent?.Tags?.FindFirstTagValue("starts") ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
tags.Add(new ("starts", starts)); tags.Add(new("starts", starts));
tags.Add( tags.Add(
new("current_participants", new("current_participants",
(viewers.HasValue ? viewers.ToString() : null) ?? (viewers.HasValue ? viewers.ToString() : null) ??