using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; using NBitcoin; namespace BTCPayServer.Client { public partial class BTCPayServerClient { public virtual async Task ShowOnChainWalletOverview(string storeId, string cryptoCode, CancellationToken token = default) { var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet"), token); return await HandleResponse(response); } public virtual async Task GetOnChainFeeRate(string storeId, string cryptoCode, int? blockTarget = null, CancellationToken token = default) { Dictionary queryParams = new Dictionary(); if (blockTarget != null) { queryParams.Add("blockTarget", blockTarget); } var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/feeRate", queryParams), token); return await HandleResponse(response); } public virtual async Task GetOnChainWalletReceiveAddress(string storeId, string cryptoCode, bool forceGenerate = false, CancellationToken token = default) { var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address", new Dictionary() { {"forceGenerate", forceGenerate} }), token); return await HandleResponse(response); } public virtual async Task UnReserveOnChainWalletReceiveAddress(string storeId, string cryptoCode, CancellationToken token = default) { var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address", method: HttpMethod.Delete), token); await HandleResponse(response); } public virtual async Task> ShowOnChainWalletTransactions( string storeId, string cryptoCode, TransactionStatus[] statusFilter = null, string labelFilter = null, CancellationToken token = default) { var query = new Dictionary(); if (statusFilter?.Any() is true) { query.Add(nameof(statusFilter), statusFilter); } if (labelFilter != null) { query.Add(nameof(labelFilter), labelFilter); } var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", query), token); return await HandleResponse>(response); } public virtual async Task GetOnChainWalletTransaction( string storeId, string cryptoCode, string transactionId, CancellationToken token = default) { var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions/{transactionId}"), token); return await HandleResponse(response); } public virtual async Task PatchOnChainWalletTransaction( string storeId, string cryptoCode, string transactionId, PatchOnChainTransactionRequest request, bool force = false, CancellationToken token = default) { var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions/{transactionId}", queryPayload: new Dictionary() { {"force", force} }, bodyPayload: request, HttpMethod.Patch), token); return await HandleResponse(response); } public virtual async Task> GetOnChainWalletUTXOs(string storeId, string cryptoCode, CancellationToken token = default) { var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/utxos"), token); return await HandleResponse>(response); } public virtual async Task CreateOnChainTransaction(string storeId, string cryptoCode, CreateOnChainTransactionRequest request, CancellationToken token = default) { if (!request.ProceedWithBroadcast) { throw new ArgumentOutOfRangeException(nameof(request.ProceedWithBroadcast), "Please use CreateOnChainTransactionButDoNotBroadcast when wanting to only create the transaction"); } var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", null, request, HttpMethod.Post), token); return await HandleResponse(response); } public virtual async Task CreateOnChainTransactionButDoNotBroadcast(string storeId, string cryptoCode, CreateOnChainTransactionRequest request, Network network, CancellationToken token = default) { if (request.ProceedWithBroadcast) { throw new ArgumentOutOfRangeException(nameof(request.ProceedWithBroadcast), "Please use CreateOnChainTransaction when wanting to also broadcast the transaction"); } var response = await _httpClient.SendAsync( CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", null, request, HttpMethod.Post), token); return Transaction.Parse(await HandleResponse(response), network); } } }