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+ }
0 commit comments