Skip to content

Commit e3ba5ff

Browse files
committed
feat: add Greenfield API for getting USDt configuration including addresses status
1 parent 31c3b6f commit e3ba5ff

6 files changed

Lines changed: 171 additions & 3 deletions

File tree

BTCPayServer.Plugins.USDt/BTCPayServer.Plugins.USDt.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
<PackageReference Include="Nethereum.RPC" Version="4.20.0"/>
4848
<PackageReference Include="Nethereum.StandardTokenEIP20" Version="4.20.0"/>
4949
<PackageReference Include="Nethereum.Web3" Version="4.20.0"/>
50-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4"/>
51-
50+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
51+
5252
</ItemGroup>
5353

5454

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using AngleSharp.Text;
4+
using BTCPayServer.Abstractions.Constants;
5+
using BTCPayServer.Client;
6+
using BTCPayServer.Data;
7+
using BTCPayServer.Payments;
8+
using BTCPayServer.Plugins.USDt.Configuration;
9+
using BTCPayServer.Plugins.USDt.Controllers.Models;
10+
using BTCPayServer.Plugins.USDt.Services;
11+
using BTCPayServer.Plugins.USDt.Services.Payments;
12+
using BTCPayServer.Services.Invoices;
13+
using Microsoft.AspNetCore.Authorization;
14+
using Microsoft.AspNetCore.Cors;
15+
using Microsoft.AspNetCore.Mvc;
16+
using StoreData = BTCPayServer.Data.StoreData;
17+
18+
namespace BTCPayServer.Plugins.USDt.Controllers;
19+
20+
[ApiController]
21+
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
22+
[EnableCors(CorsPolicies.All)]
23+
public class GreenfieldTronUSDtLikeStoreController(
24+
TronUSDtRPCProvider tronUSDtRpcProvider,
25+
PaymentMethodHandlerDictionary handlers,
26+
InvoiceRepository invoiceRepository,
27+
USDtPluginConfiguration pluginConfiguration) : ControllerBase
28+
{
29+
30+
private StoreData StoreData => HttpContext.GetStoreData();
31+
32+
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
33+
[HttpGet("~/api/v1/stores/{storeId}/tronUSDtlike/{paymentMethodId}")]
34+
public async Task<IActionResult> GetUSDtLikeStoreInformation(PaymentMethodId paymentMethodId)
35+
{
36+
if (pluginConfiguration.TronUSDtLikeConfigurationItems.ContainsKey(paymentMethodId) == false)
37+
return NotFound();
38+
39+
var excludeFilters = StoreData.GetStoreBlob().GetExcludedPaymentMethods();
40+
var matchedPaymentMethodConfig =
41+
StoreData.GetPaymentMethodConfig<TronUSDtPaymentMethodConfig>(paymentMethodId, handlers);
42+
43+
if (matchedPaymentMethodConfig == null)
44+
return NotFound();
45+
46+
var balances =
47+
await tronUSDtRpcProvider.GetBalances(paymentMethodId, [.. matchedPaymentMethodConfig.Addresses]);
48+
var reservedAddresses =
49+
await TronUSDtPaymentMethodConfig.GetReservedAddresses(paymentMethodId, invoiceRepository);
50+
51+
var data = new TronUSDtPaymentMethodInformation
52+
{
53+
StoreId = StoreData.Id,
54+
PaymentMethodId = paymentMethodId.ToString(),
55+
Enabled = !excludeFilters.Match(paymentMethodId),
56+
Addresses = matchedPaymentMethodConfig.Addresses.Select(s =>
57+
new TronUSDtPaymentMethodInformation.TronUSDtPaymentMethodAddressInformation()
58+
{
59+
Available = reservedAddresses.Contains(s) == false,
60+
Balance = balances.Single(x => x.Item1 == s).Item2 == null
61+
? null
62+
: balances.Single(x => x.Item1 == s).Item2!.Value,
63+
Value = s
64+
}).ToArray()
65+
};
66+
67+
return Ok(data);
68+
}
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace BTCPayServer.Plugins.USDt.Controllers.Models;
2+
3+
public class TronUSDtPaymentMethodInformation
4+
{
5+
public required string StoreId { get; init; }
6+
7+
public required string PaymentMethodId { get; init; }
8+
public required bool Enabled { get; init; }
9+
10+
public TronUSDtPaymentMethodAddressInformation[] Addresses { get; init; } =
11+
[];
12+
13+
public class TronUSDtPaymentMethodAddressInformation
14+
{
15+
public required string Value { get; init; }
16+
public bool Available { get; init; }
17+
public required decimal? Balance { get; init; }
18+
}
19+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Threading.Tasks;
2+
using BTCPayServer.Abstractions.Contracts;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace BTCPayServer.Plugins.USDt.Controllers;
6+
7+
public class SwaggerProvider : ISwaggerProvider
8+
{
9+
public Task<JObject> Fetch()
10+
{
11+
var swaggerJson = @"
12+
{
13+
""paths"": {
14+
""/api/v1/stores/{storeId}/tronUSDtlike/{paymentMethodId}"": {
15+
""get"": {
16+
""tags"": [""Store (Payment Methods)""],
17+
""summary"": ""Get Tron USDt configuration"",
18+
""operationId"": ""GetUSDtLikeStoreInformation"",
19+
""security"": [
20+
{
21+
""API_Key"": [
22+
""btcpay.store.canviewstoresettings""
23+
],
24+
""Basic"": []
25+
}
26+
],
27+
""parameters"": [
28+
{
29+
""$ref"": ""#/components/parameters/StoreId""
30+
},
31+
{
32+
""$ref"": ""#/components/parameters/PaymentMethodId""
33+
},
34+
],
35+
""responses"": {
36+
""200"": {
37+
""description"": ""Success"",
38+
""content"": {
39+
""application/json"": {
40+
""schema"": {
41+
""type"": ""object"",
42+
""properties"": {
43+
""storeId"": {""type"": ""string""},
44+
""paymentMethodId"": {""type"": ""string""},
45+
""enabled"": {""type"": ""boolean""},
46+
""addresses"": {
47+
""type"": ""array"",
48+
""items"": {
49+
""type"": ""object"",
50+
""properties"": {
51+
""value"": {""type"": ""string""},
52+
""available"": {""type"": ""boolean""},
53+
""balance"": {
54+
""type"": ""number"",
55+
""nullable"": true
56+
}
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
},
65+
""404"": {
66+
""description"": ""Not Found""
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}";
73+
74+
var jObject = JObject.Parse(swaggerJson);
75+
return Task.FromResult(jObject);
76+
}
77+
}

BTCPayServer.Plugins.USDt/USDtPlugin.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using BTCPayServer.Payments.Bitcoin;
1010
using BTCPayServer.Plugins.USDt.Configuration;
1111
using BTCPayServer.Plugins.USDt.Configuration.Tron;
12+
using BTCPayServer.Plugins.USDt.Controllers;
1213
using BTCPayServer.Plugins.USDt.Services;
1314
using BTCPayServer.Plugins.USDt.Services.Payments;
1415
using BTCPayServer.Services.Rates;
@@ -83,6 +84,8 @@ private void RegisterServices(IServiceCollection services)
8384
services.AddUIExtension("store-invoices-payments", "TronUSDtLike/ViewTronUSDtLikePaymentData");
8485
services.AddUIExtension("checkout-payment-method", "TronUSDtLike/EmptyCheckoutPaymentMethodExtension");
8586
services.AddSingleton<ISyncSummaryProvider, TronUSDtSyncSummaryProvider>();
87+
88+
services.AddSingleton<ISwaggerProvider, SwaggerProvider>();
8689
}
8790

8891

submodules/btcpayserver

Submodule btcpayserver updated 658 files

0 commit comments

Comments
 (0)