Convert balance to milli-sats
This commit is contained in:
@ -38,9 +38,9 @@ public class Config
|
||||
public LndConfig Lnd { get; init; } = null!;
|
||||
|
||||
/// <summary>
|
||||
/// Cost/min
|
||||
/// Cost/min (milli-sats)
|
||||
/// </summary>
|
||||
public int Cost { get; init; } = 10;
|
||||
public int Cost { get; init; } = 10_000;
|
||||
|
||||
/// <summary>
|
||||
/// List of video variants
|
||||
|
@ -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)
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -15,7 +15,7 @@ public class User
|
||||
public string? Event { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sats balance
|
||||
/// Milli sats balance
|
||||
/// </summary>
|
||||
public long Balance { get; set; }
|
||||
|
||||
|
15
NostrStreamer/Migrations/20230713140000_MillisatsBalance.cs
Normal file
15
NostrStreamer/Migrations/20230713140000_MillisatsBalance.cs
Normal 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");
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user