From 7f0fb906b2dafce1edf55d172d46581fa2ce03b3 Mon Sep 17 00:00:00 2001 From: dangershony Date: Fri, 24 Mar 2023 00:07:29 +0000 Subject: [PATCH] Fix some more bugs in the UI --- .../Extentions.cs | 28 +++++++++++++------ .../Pages/MySwaps.razor | 4 +-- .../Pages/ViewSwap.razor | 13 ++++++--- src/Blockcore.AtomicSwaps.Client/Storage.cs | 11 +++++++- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/Blockcore.AtomicSwaps.Client/Extentions.cs b/src/Blockcore.AtomicSwaps.Client/Extentions.cs index 2191006..a1ef3c7 100644 --- a/src/Blockcore.AtomicSwaps.Client/Extentions.cs +++ b/src/Blockcore.AtomicSwaps.Client/Extentions.cs @@ -1,11 +1,5 @@ -using Blockcore.AtomicSwaps.Client.Services; -using Blockcore.AtomicSwaps.Shared; -using Blockcore.Consensus.ScriptInfo; -using Blockcore.Consensus.TransactionInfo; -using Blockcore.Networks; -using Blockcore.Utilities; -using NBitcoin; -using NBitcoin.Crypto; +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; namespace Blockcore.AtomicSwaps.Client { @@ -23,5 +17,23 @@ public static void ThorwIfError(this string error) if (!string.IsNullOrEmpty(error)) throw new Exception(error); } + + public static async Task GetFromJsonNullableAsync(this HttpClient client, [StringSyntax(StringSyntaxAttribute.Uri)] string? requestUri, CancellationToken cancellationToken = default) + { + string? res = await client.GetStringAsync(requestUri, cancellationToken); + + if (string.IsNullOrEmpty(res)) + { + return default(TValue); + } + + var ser = JsonSerializer.Deserialize(res, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + WriteIndented = true + }); + + return ser; + } } } diff --git a/src/Blockcore.AtomicSwaps.Client/Pages/MySwaps.razor b/src/Blockcore.AtomicSwaps.Client/Pages/MySwaps.razor index 3db02d5..d108cdd 100644 --- a/src/Blockcore.AtomicSwaps.Client/Pages/MySwaps.razor +++ b/src/Blockcore.AtomicSwaps.Client/Pages/MySwaps.razor @@ -88,9 +88,9 @@ else { if (myswap.Status is SwapsDataStatus.Available or SwapsDataStatus.InProgress) { - var swap = await Http.GetFromJsonAsync($"api/SwapCoordinator/session/{myswap.SwapSessionId}"); + var swap = await Http.GetFromJsonNullableAsync($"api/SwapCoordinator/session/{myswap.SwapSessionId}"); - if (myswap.Status != swap.Status) + if (swap != null && myswap.Status != swap.Status) { myswaps.ToList().Remove(myswap); myswaps.ToList().Add(swap); diff --git a/src/Blockcore.AtomicSwaps.Client/Pages/ViewSwap.razor b/src/Blockcore.AtomicSwaps.Client/Pages/ViewSwap.razor index 9c913e5..9b9fb7f 100644 --- a/src/Blockcore.AtomicSwaps.Client/Pages/ViewSwap.razor +++ b/src/Blockcore.AtomicSwaps.Client/Pages/ViewSwap.razor @@ -143,7 +143,7 @@ *@ @if (swap.SwapMaker.SwapTransactionHash != null) { - var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.SwapTransactionHash}"; + var url = $"{GetExplorerUrl()}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.SwapTransactionHash}";

Maker on @swap.SwapMaker.CoinSymbol, confirmations - @MakerSwapTransactionConfirmations, trx - @swap.SwapMaker.SwapTransactionHash

@if (swap.SwapMaker.RecoveryLockTime.HasValue) @@ -186,7 +186,7 @@ *@ @if (swap.SwapTaker.SwapTransactionHash != null) { - var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.SwapTransactionHash}"; + var url = $"{GetExplorerUrl()}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.SwapTransactionHash}";

Taker on @swap.SwapTaker.CoinSymbol, confirmations : @TakerSwapTransactionConfirmations, trx - @swap.SwapTaker.SwapTransactionHash

@if (swap.SwapTaker.RecoveryLockTime.HasValue) @@ -231,7 +231,7 @@ @if (@swap.SwapTaker.ExchangeTransactionHash != null) { - var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.ExchangeTransactionHash}"; + var url = $"{GetExplorerUrl()}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.ExchangeTransactionHash}";

Maker on @swap.SwapTaker.CoinSymbol, Confirmations : @MakerExchangeTransactionConfirmations, trx - @swap.SwapTaker.ExchangeTransactionHash

MAKER EXCHANGE SUCCESS

@@ -278,7 +278,7 @@ @if (swap.SwapMaker.ExchangeTransactionHash != null) { - var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.ExchangeTransactionHash}"; + var url = $"{GetExplorerUrl()}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.ExchangeTransactionHash}";

Taker on @swap.SwapMaker.CoinSymbol, Confirmations : @TakerExchangeTransactionConfirmations, trx - @swap.SwapMaker.ExchangeTransactionHash

TAKER EXCHANGE SUCCESS

@@ -403,6 +403,11 @@ await this.RefreshConfirmations(); } + private string GetExplorerUrl() + { + return storage.GetExplorerUrl().Result; + } + private async Task DeleteSwap() { // this method is of course problematic because anyone can delete using an api call diff --git a/src/Blockcore.AtomicSwaps.Client/Storage.cs b/src/Blockcore.AtomicSwaps.Client/Storage.cs index 099c175..e199d5b 100644 --- a/src/Blockcore.AtomicSwaps.Client/Storage.cs +++ b/src/Blockcore.AtomicSwaps.Client/Storage.cs @@ -103,7 +103,16 @@ public void SetExplorerUrl(string address) public async Task? GetExplorerUrl() { - return _storage.GetItemAsString("explorer") ?? await GetExplorerUrlFromDDNS(); + var res = _storage.GetItemAsString("explorer"); + + if (string.IsNullOrEmpty(res)) + { + res = await GetExplorerUrlFromDDNS(); + + SetExplorerUrl(res); + } + + return res; } public async Task? GetExplorerUrlFromDDNS()