Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gruve-p committed Sep 1, 2024
2 parents 3349aab + 28156a5 commit 8a7db56
Show file tree
Hide file tree
Showing 103 changed files with 594 additions and 647 deletions.
2 changes: 1 addition & 1 deletion BTCPayServer.Client/BTCPayServer.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/btcpayserver/btcpayserver</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Configurations>Debug;Release;Altcoins-Debug;Altcoins-Release</Configurations>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<PropertyGroup>
Expand Down
5 changes: 0 additions & 5 deletions BTCPayServer.Common/BTCPayNetworkProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public BTCPayNetworkProvider(
Logs logs)
{
var networksList = networks.ToList();
#if !ALTCOINS
var onlyBTC = networksList.Count == 1 && networksList.First().IsBTC;
if (!onlyBTC)
throw new ConfigException($"This build of GRSPay Server does not support altcoins. Configured networks: {string.Join(',', networksList.Select(n => n.CryptoCode).ToArray())}");
#endif
_NBXplorerNetworkProvider = nbxplorerNetworkProvider;
NetworkType = nbxplorerNetworkProvider.NetworkType;
foreach (var network in networksList)
Expand Down
3 changes: 0 additions & 3 deletions BTCPayServer.Common/BTCPayServer.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<PackageReference Include="NBXplorer.Client" Version="4.3.1" />
<PackageReference Include="NicolasDorier.StandardConfiguration" Version="2.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(Altcoins)' != 'true'">
<Compile Remove="Altcoins\**\*.cs"></Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="Altcoins\" />
</ItemGroup>
Expand Down
7 changes: 1 addition & 6 deletions BTCPayServer.Common/SelectedChains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ public class SelectedChains
{
HashSet<string> chains = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
bool all = false;
public SelectedChains(IConfiguration configuration, Logs logs)
public SelectedChains(IConfiguration configuration)
{
foreach (var chain in (configuration["chains"] ?? "btc")
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.ToUpperInvariant()))
{
if (new[] { "ETH", "USDT20", "FAU" }.Contains(chain, StringComparer.OrdinalIgnoreCase))
{
logs.Configuration.LogWarning($"'{chain}' is not anymore supported, please remove it from 'chains'");
continue;
}
if (chain == "*")
{
all = true;
Expand Down
7 changes: 6 additions & 1 deletion BTCPayServer.Data/ApplicationDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;

namespace BTCPayServer.Data
{
public class ApplicationDbContextFactory : BaseDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContextFactory(IOptions<DatabaseOptions> options) : base(options, "")
public ApplicationDbContextFactory(IOptions<DatabaseOptions> options, ILoggerFactory loggerFactory) : base(options, "")
{
LoggerFactory = loggerFactory;
}

public ILoggerFactory LoggerFactory { get; }

public override ApplicationDbContext CreateContext(Action<NpgsqlDbContextOptionsBuilder> npgsqlOptionsAction = null)
{
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseLoggerFactory(LoggerFactory);
builder.AddInterceptors(Data.InvoiceData.MigrationInterceptor.Instance);
ConfigureBuilder(builder, npgsqlOptionsAction);
return new ApplicationDbContext(builder.Options);
Expand Down
4 changes: 4 additions & 0 deletions BTCPayServer.Data/BTCPayServer.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<ItemGroup>
<EmbeddedResource Include="DBScripts\*.sql" />
</ItemGroup>
<ItemGroup>
<None Remove="DBScripts\001.InvoiceFunctions.sql" />
<None Remove="DBScripts\002.RefactorPayouts.sql" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions BTCPayServer.Data/DBScripts/001.InvoiceFunctions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE OR REPLACE FUNCTION get_orderid(invoice_blob jsonb)
RETURNS text AS $$
SELECT invoice_blob->'metadata'->>'orderId';
$$ LANGUAGE sql IMMUTABLE;

CREATE OR REPLACE FUNCTION get_itemcode(invoice_blob jsonb)
RETURNS text AS $$
SELECT invoice_blob->'metadata'->>'itemCode';
$$ LANGUAGE sql IMMUTABLE;

CREATE INDEX IF NOT EXISTS "IX_Invoices_Metadata_OrderId" ON "Invoices" (get_orderid("Blob2")) WHERE get_orderid("Blob2") IS NOT NULL;
CREATE INDEX IF NOT EXISTS "IX_Invoices_Metadata_ItemCode" ON "Invoices" (get_itemcode("Blob2")) WHERE get_itemcode("Blob2") IS NOT NULL;
61 changes: 61 additions & 0 deletions BTCPayServer.Data/DBScripts/002.RefactorPayouts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- Rename column
ALTER TABLE "Payouts" RENAME COLUMN "PaymentMethodId" TO "PayoutMethodId";

-- Add Currency column, guessed from the PaymentMethodId
ALTER TABLE "Payouts" ADD COLUMN "Currency" TEXT;
UPDATE "Payouts" SET
"Currency" = split_part("PayoutMethodId", '_', 1),
"PayoutMethodId"=
CASE
WHEN split_part("PayoutMethodId", '_', 2) = 'LightningLike' THEN split_part("PayoutMethodId", '_', 1) || '-LN'
ELSE split_part("PayoutMethodId", '_', 1) || '-CHAIN'
END;
ALTER TABLE "Payouts" ALTER COLUMN "Currency" SET NOT NULL;

-- Remove Currency and Limit from PullPayment Blob, and put it into the columns in the table
ALTER TABLE "PullPayments" ADD COLUMN "Currency" TEXT;
UPDATE "PullPayments" SET "Currency" = "Blob"->>'Currency';
ALTER TABLE "PullPayments" ALTER COLUMN "Currency" SET NOT NULL;
ALTER TABLE "PullPayments" ADD COLUMN "Limit" NUMERIC;
UPDATE "PullPayments" SET "Limit" = ("Blob"->>'Limit')::NUMERIC;
ALTER TABLE "PullPayments" ALTER COLUMN "Limit" SET NOT NULL;

-- Remove unused properties, rename SupportedPaymentMethods, and fix legacy payment methods IDs
UPDATE "PullPayments" SET
"Blob" = jsonb_set(
"Blob" - 'SupportedPaymentMethods' - 'Limit' - 'Currency' - 'Period',
'{SupportedPayoutMethods}',
(SELECT jsonb_agg(to_jsonb(
CASE
WHEN split_part(value::TEXT, '_', 2) = 'LightningLike' THEN split_part(value::TEXT, '_', 1) || '-LN'
ELSE split_part(value::TEXT, '_', 1) || '-CHAIN'
END))
FROM jsonb_array_elements_text("Blob"->'SupportedPaymentMethods') AS value
));

--Remove "Amount" and "CryptoAmount" from Payout Blob, and put it into the columns in the table
-- Respectively "OriginalAmount" and "Amount"

ALTER TABLE "Payouts" ADD COLUMN "Amount" NUMERIC;
UPDATE "Payouts" SET "Amount" = ("Blob"->>'CryptoAmount')::NUMERIC;

ALTER TABLE "Payouts" ADD COLUMN "OriginalAmount" NUMERIC;
UPDATE "Payouts" SET "OriginalAmount" = ("Blob"->>'Amount')::NUMERIC;
ALTER TABLE "Payouts" ALTER COLUMN "OriginalAmount" SET NOT NULL;

ALTER TABLE "Payouts" ADD COLUMN "OriginalCurrency" TEXT;


UPDATE "Payouts" p
SET
"OriginalCurrency" = "Currency",
"Blob" = "Blob" - 'Amount' - 'CryptoAmount'
WHERE "PullPaymentDataId" IS NULL AND "OriginalCurrency" IS NULL;

UPDATE "Payouts" p
SET
"OriginalCurrency" = pp."Currency"
FROM "PullPayments" pp
WHERE "OriginalCurrency" IS NULL AND pp."Id"=p."PullPaymentDataId";

ALTER TABLE "Payouts" ALTER COLUMN "OriginalCurrency" SET NOT NULL;
5 changes: 0 additions & 5 deletions BTCPayServer.Data/Data/InvoiceData.Migration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ public object InitializedInstance(MaterializationInterceptionData materializatio
{
paymentData.Migrate();
}
else if (entity is PayoutData payoutData && payoutData.Currency is null)
{
payoutData.Migrate();
}
return entity;
}
}
Expand Down Expand Up @@ -236,7 +232,6 @@ public void Migrate()
var isTopup = blob["type"]?.Value<string>() is "TopUp";
var amount = decimal.Parse(blob["price"].Value<string>(), CultureInfo.InvariantCulture);
Amount = isTopup && amount == 0 ? null : decimal.Parse(blob["price"].Value<string>(), CultureInfo.InvariantCulture);
CustomerEmail = null;
foreach (var prop in superflousProperties)
blob.Property(prop)?.Remove();
if (blob["speedPolicy"] is JValue { Type: JTokenType.Integer, Value: 0 or 0L })
Expand Down
16 changes: 9 additions & 7 deletions BTCPayServer.Data/Data/InvoiceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json.Linq;

namespace BTCPayServer.Data
{
Expand All @@ -20,19 +21,18 @@ public partial class InvoiceData : IHasBlobUntyped
[Obsolete("Use Blob2 instead")]
public byte[] Blob { get; set; }
public string Blob2 { get; set; }
public string ItemCode { get; set; }
public string OrderId { get; set; }
public string Status { get; set; }
public string ExceptionStatus { get; set; }
[Obsolete("Unused")]
public string CustomerEmail { get; set; }
public List<AddressInvoiceData> AddressInvoices { get; set; }
public bool Archived { get; set; }
public List<PendingInvoiceData> PendingInvoices { get; set; }
public List<InvoiceSearchData> InvoiceSearchData { get; set; }
public List<RefundData> Refunds { get; set; }

[Timestamp]
public static string GetOrderId(string blob) => throw new NotSupportedException();
public static string GetItemCode(string blob) => throw new NotSupportedException();

[Timestamp]
// With this, update of InvoiceData will fail if the row was modified by another process
public uint XMin { get; set; }
internal static void OnModelCreating(ModelBuilder builder, DatabaseFacade databaseFacade)
Expand All @@ -41,14 +41,16 @@ internal static void OnModelCreating(ModelBuilder builder, DatabaseFacade databa
.HasOne(o => o.StoreData)
.WithMany(a => a.Invoices).OnDelete(DeleteBehavior.Cascade);
builder.Entity<InvoiceData>().HasIndex(o => o.StoreDataId);
builder.Entity<InvoiceData>().HasIndex(o => o.OrderId);
builder.Entity<InvoiceData>().HasIndex(o => o.Created);
builder.Entity<InvoiceData>()
.Property(o => o.Blob2)
.HasColumnType("JSONB");
builder.Entity<InvoiceData>()
.Property(o => o.Amount)
.HasColumnType("NUMERIC");
}
builder.HasDbFunction(typeof(InvoiceData).GetMethod(nameof(GetOrderId), new[] { typeof(string) }), b => b.HasName("get_orderid"));
builder.HasDbFunction(typeof(InvoiceData).GetMethod(nameof(GetItemCode), new[] { typeof(string) }), b => b.HasName("get_itemcode"));

}
}
}
18 changes: 0 additions & 18 deletions BTCPayServer.Data/Data/PayoutData.Migration.cs

This file was deleted.

16 changes: 16 additions & 0 deletions BTCPayServer.Data/Data/PayoutData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,23 @@ public partial class PayoutData
public DateTimeOffset Date { get; set; }
public string PullPaymentDataId { get; set; }
public string StoreDataId { get; set; }
/// <summary>
/// The currency of the payout (eg. BTC)
/// </summary>
public string Currency { get; set; }
/// <summary>
/// The amount of the payout in Currency.
/// The Amount only get set when the payout is actually approved.
/// </summary>
public decimal? Amount { get; set; }
/// <summary>
/// The original currency of the payout (eg. USD)
/// </summary>
public string OriginalCurrency { get; set; }
/// <summary>
/// The amount of the payout in OriginalCurrency
/// </summary>
public decimal OriginalAmount { get; set; }
public PullPaymentData PullPaymentData { get; set; }
[MaxLength(20)]
public PayoutState State { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions BTCPayServer.Data/Data/PullPaymentData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class PullPaymentData
public StoreData StoreData { get; set; }
[MaxLength(50)]
public string StoreId { get; set; }
public string Currency { get; set; }
public decimal Limit { get; set; }
public DateTimeOffset StartDate { get; set; }
public DateTimeOffset? EndDate { get; set; }
public bool Archived { get; set; }
Expand Down
29 changes: 0 additions & 29 deletions BTCPayServer.Data/Migrations/20240520042729_payoutsmigration.cs

This file was deleted.

35 changes: 35 additions & 0 deletions BTCPayServer.Data/Migrations/20240826065950_removeinvoicecols.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using BTCPayServer.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BTCPayServer.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20240826065950_removeinvoicecols")]
[DBScript("001.InvoiceFunctions.sql")]
public partial class removeinvoicecols : DBScriptsMigration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Invoices_OrderId",
table: "Invoices");

migrationBuilder.DropColumn(
name: "ItemCode",
table: "Invoices");

migrationBuilder.DropColumn(
name: "OrderId",
table: "Invoices");

migrationBuilder.DropColumn(
name: "CustomerEmail",
table: "Invoices");
base.Up(migrationBuilder);
}
}
}
15 changes: 15 additions & 0 deletions BTCPayServer.Data/Migrations/20240827034505_migratepayouts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using BTCPayServer.Data;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BTCPayServer.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20240827034505_migratepayouts")]
[DBScript("002.RefactorPayouts.sql")]
public partial class migratepayouts : DBScriptsMigration
{
}
}
Loading

0 comments on commit 8a7db56

Please sign in to comment.