mirror of
https://github.com/aljazceru/BTCPayServerPlugins.git
synced 2025-12-17 23:54:26 +01:00
fix issues
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
<PropertyGroup>
|
||||
<Product>LN Prism</Product>
|
||||
<Description>Automated value splits for lightning.</Description>
|
||||
<Version>1.0.3</Version>
|
||||
<Version>1.0.4</Version>
|
||||
</PropertyGroup>
|
||||
<!-- Plugin development properties -->
|
||||
<PropertyGroup>
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
namespace BTCPayServer.Plugins.Prism;
|
||||
|
||||
public record PendingPayout(long BalanceAmount, long FeeCharged);
|
||||
public record PendingPayout(long PayoutAmount, long FeeCharged);
|
||||
@@ -33,6 +33,7 @@ namespace BTCPayServer.Plugins.Prism
|
||||
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
|
||||
private readonly LightningClientFactoryService _lightningClientFactoryService;
|
||||
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
|
||||
private readonly BTCPayNetworkJsonSerializerSettings _btcPayNetworkJsonSerializerSettings;
|
||||
private Dictionary<string, PrismSettings> _prismSettings;
|
||||
|
||||
public SatBreaker(StoreRepository storeRepository,
|
||||
@@ -43,7 +44,8 @@ namespace BTCPayServer.Plugins.Prism
|
||||
LightningLikePayoutHandler lightningLikePayoutHandler,
|
||||
BTCPayNetworkProvider btcPayNetworkProvider,
|
||||
LightningClientFactoryService lightningClientFactoryService,
|
||||
IOptions<LightningNetworkOptions> lightningNetworkOptions) : base(eventAggregator, logger)
|
||||
IOptions<LightningNetworkOptions> lightningNetworkOptions,
|
||||
BTCPayNetworkJsonSerializerSettings btcPayNetworkJsonSerializerSettings) : base(eventAggregator, logger)
|
||||
{
|
||||
_storeRepository = storeRepository;
|
||||
_logger = logger;
|
||||
@@ -53,6 +55,7 @@ namespace BTCPayServer.Plugins.Prism
|
||||
_btcPayNetworkProvider = btcPayNetworkProvider;
|
||||
_lightningClientFactoryService = lightningClientFactoryService;
|
||||
_lightningNetworkOptions = lightningNetworkOptions;
|
||||
_btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings;
|
||||
}
|
||||
|
||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||
@@ -78,8 +81,12 @@ namespace BTCPayServer.Plugins.Prism
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
var payoutsToCheck = _prismSettings.ToDictionary(pair => pair.Key, pair => pair.Value.PendingPayouts);
|
||||
var payoutIds = payoutsToCheck.SelectMany(pair => pair.Value.Keys).ToArray();
|
||||
try
|
||||
{
|
||||
var payoutsToCheck =
|
||||
_prismSettings.ToDictionary(pair => pair.Key, pair => pair.Value.PendingPayouts);
|
||||
var payoutIds = payoutsToCheck
|
||||
.SelectMany(pair => pair.Value?.Keys.ToArray() ?? Array.Empty<string>()).ToArray();
|
||||
var payouts = (await _pullPaymentHostedService.GetPayouts(new PullPaymentHostedService.PayoutQuery()
|
||||
{
|
||||
PayoutIds = payoutIds,
|
||||
@@ -114,7 +121,8 @@ namespace BTCPayServer.Plugins.Prism
|
||||
.FirstOrDefault(d => d.PaymentId == id);
|
||||
if (existing?.GetExternalLightningUrl() is { } connectionString)
|
||||
{
|
||||
lnClient = _lightningClientFactoryService.Create(connectionString, network);
|
||||
lnClient = _lightningClientFactoryService.Create(connectionString,
|
||||
network);
|
||||
}
|
||||
else if (existing?.IsInternalNode is true &&
|
||||
_lightningNetworkOptions.Value.InternalLightningByCryptoCode
|
||||
@@ -143,7 +151,7 @@ namespace BTCPayServer.Plugins.Prism
|
||||
|
||||
break;
|
||||
case PayoutState.Cancelled:
|
||||
toCredit = pendingPayout.BalanceAmount + pendingPayout.FeeCharged;
|
||||
toCredit = pendingPayout.PayoutAmount + pendingPayout.FeeCharged;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -152,7 +160,9 @@ namespace BTCPayServer.Plugins.Prism
|
||||
new List<string>()));
|
||||
var credDest = res[payout.StoreDataId];
|
||||
credDest.PayoutsToRemove.Add(payout.Id);
|
||||
credDest.Destcredits.Add(payout.Destination, toCredit);
|
||||
|
||||
credDest.Destcredits.Add(payout.GetBlob(_btcPayNetworkJsonSerializerSettings).Destination,
|
||||
toCredit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,6 +170,12 @@ namespace BTCPayServer.Plugins.Prism
|
||||
PushEvent(new PayoutCheckResult(res.Values.ToArray(), tcs));
|
||||
//we wait for ProcessEvent to handle this result so that we avoid race conditions.
|
||||
await tcs.Task;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Error while checking payouts");
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromMinutes(1), cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -175,11 +191,12 @@ namespace BTCPayServer.Plugins.Prism
|
||||
return _prismSettings.TryGetValue(storeId, out var settings) ? settings : new PrismSettings();
|
||||
}
|
||||
|
||||
public async Task<bool> UpdatePrismSettingsForStore(string storeId, PrismSettings updatedSettings, bool skipLock = false)
|
||||
public async Task<bool> UpdatePrismSettingsForStore(string storeId, PrismSettings updatedSettings,
|
||||
bool skipLock = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(!skipLock)
|
||||
if (!skipLock)
|
||||
await _updateLock.WaitAsync();
|
||||
var currentSettings = await Get(storeId);
|
||||
|
||||
@@ -199,7 +216,7 @@ namespace BTCPayServer.Plugins.Prism
|
||||
|
||||
finally
|
||||
{
|
||||
if(!skipLock)
|
||||
if (!skipLock)
|
||||
_updateLock.Release();
|
||||
}
|
||||
|
||||
@@ -313,15 +330,18 @@ namespace BTCPayServer.Plugins.Prism
|
||||
|
||||
private async Task CreatePayouts(string storeId, PrismSettings prismSettings)
|
||||
{
|
||||
var threshold = (long) Math.Max(1,
|
||||
Math.Round(prismSettings.SatThreshold * 1.02, 0, MidpointRounding.AwayFromZero));
|
||||
foreach (var (destination, amtMsats) in prismSettings.DestinationBalance)
|
||||
{
|
||||
var amt = amtMsats / 1000;
|
||||
if (amt >= threshold)
|
||||
if (amt >= prismSettings.SatThreshold)
|
||||
{
|
||||
var reserveFee = (long) Math.Max(1, Math.Round(amt * 0.02, 0, MidpointRounding.AwayFromZero));
|
||||
var payoutAmount = amt - reserveFee;
|
||||
if (payoutAmount <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var payout = await _pullPaymentHostedService.Claim(new ClaimRequest()
|
||||
{
|
||||
Destination = new LNURLPayClaimDestinaton(destination),
|
||||
@@ -332,8 +352,9 @@ namespace BTCPayServer.Plugins.Prism
|
||||
});
|
||||
if (payout.Result == ClaimRequest.ClaimResult.Ok)
|
||||
{
|
||||
prismSettings.PendingPayouts??=new();
|
||||
prismSettings.PendingPayouts.Add(payout.PayoutData.Id, new PendingPayout(payoutAmount, reserveFee));
|
||||
prismSettings.PendingPayouts ??= new();
|
||||
prismSettings.PendingPayouts.Add(payout.PayoutData.Id,
|
||||
new PendingPayout(payoutAmount, reserveFee));
|
||||
prismSettings.DestinationBalance.AddOrReplace(destination,
|
||||
amtMsats - (payoutAmount + reserveFee) * 1000);
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@
|
||||
<tr>
|
||||
<td>@payoutId</td>
|
||||
<td>@pendingPayout.FeeCharged</td>
|
||||
<td>@pendingPayout.BalanceAmount</td>
|
||||
<td>@pendingPayout.PayoutAmount</td>
|
||||
</tr>
|
||||
}
|
||||
</table>
|
||||
|
||||
Reference in New Issue
Block a user