Provide more data through OnChain Wallet API (#2420)

Provides unconf/conf balanaces, keypath + address + timestamp of utxos
This commit is contained in:
Andrew Camilleri
2021-04-08 05:43:51 +02:00
committed by GitHub
parent f367480857
commit ad1b708da5
6 changed files with 63 additions and 18 deletions

View File

@@ -7,5 +7,11 @@ namespace BTCPayServer.Client.Models
{ {
[JsonConverter(typeof(NumericStringJsonConverter))] [JsonConverter(typeof(NumericStringJsonConverter))]
public decimal Balance { get; set; } public decimal Balance { get; set; }
[JsonConverter(typeof(NumericStringJsonConverter))]
public decimal UnconfirmedBalance { get; set; }
[JsonConverter(typeof(NumericStringJsonConverter))]
public decimal ConfirmedBalance { get; set; }
public string Label { get; set; }
} }
} }

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using BTCPayServer.JsonConverters; using BTCPayServer.JsonConverters;
using NBitcoin; using NBitcoin;
@@ -14,7 +15,11 @@ namespace BTCPayServer.Client.Models
[JsonConverter(typeof(OutpointJsonConverter))] [JsonConverter(typeof(OutpointJsonConverter))]
public OutPoint Outpoint { get; set; } public OutPoint Outpoint { get; set; }
public string Link { get; set; } public string Link { get; set; }
public Dictionary<string, LabelData> Labels { get; set; } public Dictionary<string, LabelData> Labels { get; set; }
[JsonConverter(typeof(DateTimeToUnixTimeConverter))]
public DateTimeOffset Timestamp { get; set; }
[JsonConverter(typeof(KeyPathJsonConverter))]
public KeyPath KeyPath { get; set; }
public string Address { get; set; }
} }
} }

View File

@@ -84,9 +84,14 @@ namespace BTCPayServer.Controllers.GreenField
out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult; out DerivationSchemeSettings derivationScheme, out IActionResult actionResult)) return actionResult;
var wallet = _btcPayWalletProvider.GetWallet(network); var wallet = _btcPayWalletProvider.GetWallet(network);
var balance = await wallet.GetBalance(derivationScheme.AccountDerivation);
return Ok(new OnChainWalletOverviewData() return Ok(new OnChainWalletOverviewData()
{ {
Balance = await wallet.GetBalance(derivationScheme.AccountDerivation) Label = derivationScheme.ToPrettyString(),
Balance = balance.Total.GetValue(network),
UnconfirmedBalance= balance.Unconfirmed.GetValue(network),
ConfirmedBalance= balance.Confirmed.GetValue(network),
}); });
} }
@@ -149,7 +154,6 @@ namespace BTCPayServer.Controllers.GreenField
var wallet = _btcPayWalletProvider.GetWallet(network); var wallet = _btcPayWalletProvider.GetWallet(network);
var walletId = new WalletId(storeId, cryptoCode); var walletId = new WalletId(storeId, cryptoCode);
var walletBlobAsync = await _walletRepository.GetWalletInfo(walletId);
var walletTransactionsInfoAsync = await _walletRepository.GetWalletTransactionsInfo(walletId); var walletTransactionsInfoAsync = await _walletRepository.GetWalletTransactionsInfo(walletId);
var txs = await wallet.FetchTransactions(derivationScheme.AccountDerivation); var txs = await wallet.FetchTransactions(derivationScheme.AccountDerivation);
@@ -222,7 +226,10 @@ namespace BTCPayServer.Controllers.GreenField
Comment = info?.Comment, Comment = info?.Comment,
Labels = info?.Labels, Labels = info?.Labels,
Link = string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink, Link = string.Format(CultureInfo.InvariantCulture, network.BlockExplorerLink,
coin.OutPoint.Hash.ToString()) coin.OutPoint.Hash.ToString()),
Timestamp = coin.Timestamp,
KeyPath = coin.KeyPath,
Address = network.NBXplorerNetwork.CreateAddress(derivationScheme.AccountDerivation, coin.KeyPath, coin.ScriptPubKey).ToString()
}; };
}).ToList() }).ToList()
); );

View File

@@ -470,7 +470,7 @@ namespace BTCPayServer.Controllers
.ToArray(); .ToArray();
var balance = _walletProvider.GetWallet(network).GetBalance(paymentMethod.AccountDerivation); var balance = _walletProvider.GetWallet(network).GetBalance(paymentMethod.AccountDerivation);
model.NBXSeedAvailable = await GetSeed(walletId, network) != null; model.NBXSeedAvailable = await GetSeed(walletId, network) != null;
model.CurrentBalance = await balance; model.CurrentBalance = (await balance).Total.GetValue(network);
await Task.WhenAll(recommendedFees); await Task.WhenAll(recommendedFees);
model.RecommendedSatoshiPerByte = model.RecommendedSatoshiPerByte =
@@ -1029,19 +1029,16 @@ namespace BTCPayServer.Controllers
private static async Task<string> GetBalanceString(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy) private static async Task<string> GetBalanceString(BTCPayWallet wallet, DerivationStrategyBase derivationStrategy)
{ {
using (CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10))) using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
{
try try
{ {
return (await wallet.GetBalance(derivationStrategy, cts.Token)).ShowMoney(wallet.Network return (await wallet.GetBalance(derivationStrategy, cts.Token)).Total.ShowMoney(wallet.Network);
.Divisibility);
} }
catch catch
{ {
return "--"; return "--";
} }
} }
}
private string GetUserId() private string GetUserId()
{ {

View File

@@ -263,13 +263,13 @@ namespace BTCPayServer.Services.Wallets
}).ToArray(); }).ToArray();
} }
public Task<decimal> GetBalance(DerivationStrategyBase derivationStrategy, CancellationToken cancellation = default(CancellationToken)) public Task<GetBalanceResponse> GetBalance(DerivationStrategyBase derivationStrategy, CancellationToken cancellation = default(CancellationToken))
{ {
return _MemoryCache.GetOrCreateAsync("CACHEDBALANCE_" + derivationStrategy.ToString(), async (entry) => return _MemoryCache.GetOrCreateAsync("CACHEDBALANCE_" + derivationStrategy.ToString(), async (entry) =>
{ {
var result = await _Client.GetBalanceAsync(derivationStrategy, cancellation); var result = await _Client.GetBalanceAsync(derivationStrategy, cancellation);
entry.AbsoluteExpiration = DateTimeOffset.UtcNow + CacheSpan; entry.AbsoluteExpiration = DateTimeOffset.UtcNow + CacheSpan;
return result.Total.GetValue(_Network); return result;
}); });
} }
} }

View File

@@ -511,7 +511,17 @@
"balance": { "balance": {
"type": "string", "type": "string",
"format": "decimal", "format": "decimal",
"description": "The current balance of the wallet" "description": "The total current balance of the wallet"
},
"unconfirmedBalance": {
"type": "string",
"format": "decimal",
"description": "The current unconfirmed balance of the wallet"
},
"confirmedBalance": {
"type": "string",
"format": "decimal",
"description": "The current confirmed balance of the wallet"
} }
} }
}, },
@@ -654,6 +664,26 @@
"type": "string", "type": "string",
"format": "{txid}:{outputIndex}", "format": "{txid}:{outputIndex}",
"description": "outpoint of this utxo" "description": "outpoint of this utxo"
},
"timestamp": {
"description": "The time of the utxo",
"allOf": [ {"$ref": "#/components/schemas/UnixTimestamp"}]
},
"keyPath": {
"type": "string",
"format": "keypath",
"description": "the derivation path in relation to the HD account"
},
"address": {
"type": "string",
"description": "The wallet address of this utxo"
},
"labels": {
"description": "Labels linked to this transaction",
"type": "object",
"additionalProperties": {
"$ref": "#/components/schemas/LabelData"
}
} }
} }
}, },