Skip to content

Commit

Permalink
Get the default indexer and explorer from ddns (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
miladsoft authored Mar 23, 2023
1 parent 7284732 commit 31aaa1b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<BlazorCacheBootResources>false</BlazorCacheBootResources>
<RunAOTCompilation>true</RunAOTCompilation>
<ImplicitUsings>enable</ImplicitUsings>
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
Expand Down
18 changes: 4 additions & 14 deletions src/Blockcore.AtomicSwaps.Client/Pages/Setting.razor
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
_networkList = SwapsConfiguration.Networks.Keys.ToList();

await LoadExplorers();
SelectedExplorer = storage.GetExplorerUrl().Replace("https://", "");
SelectedExplorer = (await storage.GetExplorerUrl()).Replace("https://", "");

await LoadIndexers();

Expand All @@ -163,27 +163,17 @@
foreach (var item in _networkList)
{
var indexers = await DnsService.GetServicesByNetwork(item);
_indexors.Add(new NetworkIndexers() { Symbol = item, Indexers = indexers.ToList(), SelectedIndexer = storage.GetIndexerUrl(item) != null ? storage.GetIndexerUrl(item).Replace("https://", "") : string.Empty });
_indexors.Add(new NetworkIndexers() { Symbol = item, Indexers = indexers.ToList(), SelectedIndexer = storage.GetIndexerUrlAsync(item) != null ? (await storage.GetIndexerUrlAsync(item)).Replace("https://", "").Replace("/api", "") : string.Empty });
}

}

private void UpdatedSelectedIndexer()
{

}

private async Task LoadExplorers()
{
var explorers = await DnsService.GetServicesByType("Explorer");
_explorers = explorers.ToList();
}

private void UpdatedSelectedExplorer()
{

}

private void WipeSwaps()
{
// todo: don't allow this if any open swaps
Expand All @@ -209,7 +199,7 @@

if (selectedExplorer != null)
{
storage.SetExplorerUrl("https://" + selectedExplorer);
storage.SetExplorerUrl("https://" + selectedExplorer );

}
}
Expand All @@ -220,7 +210,7 @@

if (item.SelectedIndexer != null)
{
storage.SetIndexerUrl(item.Symbol, "https://" + item.SelectedIndexer);
storage.SetIndexerUrl(item.Symbol, "https://" + item.SelectedIndexer + "/api");
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Blockcore.AtomicSwaps.Client/Pages/ViewSwap.razor
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
*@
@if (swap.SwapMaker.SwapTransactionHash != null)
{
var url = $"{SwapsConfiguration.ExplorerUrl}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.SwapTransactionHash}";
var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.SwapTransactionHash}";

<p>Maker on @swap.SwapMaker.CoinSymbol, confirmations - @MakerSwapTransactionConfirmations, trx - <a href="@url" target="_blank">@swap.SwapMaker.SwapTransactionHash</a></p>
@if (swap.SwapMaker.RecoveryLockTime.HasValue)
Expand Down Expand Up @@ -186,7 +186,7 @@
*@
@if (swap.SwapTaker.SwapTransactionHash != null)
{
var url = $"{SwapsConfiguration.ExplorerUrl}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.SwapTransactionHash}";
var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.SwapTransactionHash}";

<p>Taker on @swap.SwapTaker.CoinSymbol, confirmations : @TakerSwapTransactionConfirmations, trx - <a href="@url" target="_blank">@swap.SwapTaker.SwapTransactionHash</a></p>
@if (swap.SwapTaker.RecoveryLockTime.HasValue)
Expand Down Expand Up @@ -231,7 +231,7 @@

@if (@swap.SwapTaker.ExchangeTransactionHash != null)
{
var url = $"{SwapsConfiguration.ExplorerUrl}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.ExchangeTransactionHash}";
var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapTaker.CoinSymbol}/explorer/transaction/{swap.SwapTaker.ExchangeTransactionHash}";

<p>Maker on @swap.SwapTaker.CoinSymbol, Confirmations : @MakerExchangeTransactionConfirmations, trx - <a href="@url" target="_blank">@swap.SwapTaker.ExchangeTransactionHash</a></p>
<P class="text-success"><strong>MAKER EXCHANGE SUCCESS</strong></P>
Expand Down Expand Up @@ -278,7 +278,7 @@

@if (swap.SwapMaker.ExchangeTransactionHash != null)
{
var url = $"{SwapsConfiguration.ExplorerUrl}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.ExchangeTransactionHash}";
var url = $"{storage.GetExplorerUrl().Result}/{swap.SwapMaker.CoinSymbol}/explorer/transaction/{swap.SwapMaker.ExchangeTransactionHash}";

<p>Taker on @swap.SwapMaker.CoinSymbol, Confirmations : @TakerExchangeTransactionConfirmations, trx - <a href="@url" target="_blank">@swap.SwapMaker.ExchangeTransactionHash</a></p>
<P class="text-success"><strong>TAKER EXCHANGE SUCCESS</strong></P>
Expand Down Expand Up @@ -905,7 +905,7 @@
return;
}

var indexer = SwapsConfiguration.Indexers().First(f => f.Symbol == swap.SwapTaker.CoinSymbol);
var indexer =(await storage.Indexers()).First(f => f.Symbol == swap.SwapTaker.CoinSymbol);
var url = $"/query/transaction/{swap.SwapTaker.SwapTransactionHash}";
var res = await Http.GetFromJsonAsync<TransactionData>(indexer.Url + url);

Expand Down
2 changes: 1 addition & 1 deletion src/Blockcore.AtomicSwaps.Client/Pages/WalletData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
var accountIndex = 0; // for now only account 0
var purpose = 44; // for now only legacy
var indexer = SwapsConfiguration.Indexers().First(f => f.Symbol == selectedNetwork);
var indexer = (await storage.Indexers()).First(f => f.Symbol == selectedNetwork);

AccountInfo accountInfo = storage.GetAccountInfo(selectedNetwork);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ public class BlockchainApiService : IBlockchainApiService
private readonly ILogger<BlockchainApiService> _logger;
private readonly HttpClient _httpClient;
private readonly SwapsConfiguration _swapsConfiguration;
private readonly Storage _storage;

public BlockchainApiService(ILogger<BlockchainApiService> logger, HttpClient httpClient, SwapsConfiguration swapsConfiguration)
public BlockchainApiService(ILogger<BlockchainApiService> logger, HttpClient httpClient, SwapsConfiguration swapsConfiguration, Storage storage)
{
_logger = logger;
_httpClient = httpClient;
_swapsConfiguration = swapsConfiguration;
_storage = storage;
}

public async Task<int> GetConfirmationsAsync(string network, string trxId, string? trxHex = null)
{
try
{
var indexer = _swapsConfiguration.Indexers().First(f => f.Symbol == network);
var indexer = (await _storage.Indexers()).First(f => f.Symbol == network);
var url = $"/query/transaction/{trxId}";
var res = await _httpClient.GetFromJsonAsync<TransactionData>(indexer.Url + url);
return res?.confirmations ?? 0;
Expand All @@ -44,7 +46,7 @@ public async Task Broadcast(string network, string? trxHex)
{
if (!string.IsNullOrEmpty(trxHex))
{
var indexer = _swapsConfiguration.Indexers().First(f => f.Symbol == network);
var indexer = (await _storage.Indexers()).First(f => f.Symbol == network);
var url = $"/command/send";
var result = await _httpClient.PostAsync(indexer.Url + url, new StringContent(trxHex));
result.EnsureSuccessStatusCode();
Expand Down
55 changes: 49 additions & 6 deletions src/Blockcore.AtomicSwaps.Client/Storage.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using Blazored.LocalStorage;
using Blockcore.AtomicSwaps.BlockcoreDns;
using Blockcore.AtomicSwaps.BlockcoreDns.Models;
using Blockcore.AtomicSwaps.Shared;

namespace Blockcore.AtomicSwaps.Client
{
public class Storage
{
private readonly ISyncLocalStorageService _storage;

public Storage(ISyncLocalStorageService storage)
private readonly IBlockcoreDnsService _dnsService;
public Storage(ISyncLocalStorageService storage, IBlockcoreDnsService dnsService)
{
_storage = storage;
_dnsService = dnsService;
}

public Storage()
Expand Down Expand Up @@ -98,19 +101,59 @@ public void SetExplorerUrl(string address)
_storage.SetItemAsString("explorer", address);
}

public string? GetExplorerUrl()
public async Task<string>? GetExplorerUrl()
{
return _storage.GetItemAsString("explorer") ?? "https://explorer.blockcore.net";
return _storage.GetItemAsString("explorer") ?? await GetExplorerUrlFromDDNS();
}

public async Task<string>? GetExplorerUrlFromDDNS()
{
var Explorers = await _dnsService.GetServicesByType("Explorer");
foreach (var index in Explorers.ToList())
{
var onlineExplorers = index.DnsResults.FirstOrDefault(c => c.Online);
if (onlineExplorers != null)
{
return onlineExplorers.Domain;
}
}
return string.Empty;
}

public void SetIndexerUrl(string symbol, string url)
{
_storage.SetItemAsString(symbol.ToLower() + "-indexer", url);
}

public string? GetIndexerUrl(string symbol)
public async Task<string?> GetIndexerUrlAsync(string symbol)
{
return _storage.GetItemAsString(symbol.ToLower() + "-indexer") ?? await GetIndexerUrlFromDDNS(symbol);
}

public async Task<string>? GetIndexerUrlFromDDNS(string network)
{
var indexers = await _dnsService.GetServicesByTypeAndNetwork("Indexer", network);
foreach (var index in indexers.ToList())
{
var onlineIndexer = index.DnsResults.FirstOrDefault(c => c.Online);
if (onlineIndexer != null)
{
return onlineIndexer.Domain;
}
}
return string.Empty;
}

public async Task<List<IndexerUrl>> Indexers()
{
return _storage.GetItemAsString(symbol.ToLower() + "-indexer");
List<IndexerUrl> indexers = new List<IndexerUrl>();
indexers.Add(new IndexerUrl { Symbol = "BTC", Url = await GetIndexerUrlAsync("BTC") });
indexers.Add(new IndexerUrl { Symbol = "STRAX", Url = await GetIndexerUrlAsync("STRAX") });
indexers.Add(new IndexerUrl { Symbol = "CITY", Url = await GetIndexerUrlAsync("CITY") });
indexers.Add(new IndexerUrl { Symbol = "IMPLX", Url = await GetIndexerUrlAsync("IMPLX") });
indexers.Add(new IndexerUrl { Symbol = "RSC", Url = await GetIndexerUrlAsync("RSC") });
indexers.Add(new IndexerUrl { Symbol = "SBC", Url = await GetIndexerUrlAsync("SBC") });
return indexers;
}
}
}
18 changes: 0 additions & 18 deletions src/Blockcore.AtomicSwaps.Client/SwapsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,9 @@ namespace Blockcore.AtomicSwaps.Client
{
public class SwapsConfiguration
{
[Inject]
public Storage _storage { get; set; }

public Dictionary<string, SwapSession> Swaps { get; } = new();

public string ExplorerUrl()
{
return _storage.GetExplorerUrl();
}
public List<IndexerUrl> Indexers()
{
List<IndexerUrl> indexers = new List<IndexerUrl>();
indexers.Add(new IndexerUrl { Symbol = "BTC", Url = _storage.GetIndexerUrl("BTC") ?? "https://btc.indexer.coinvault.io/api" });
indexers.Add(new IndexerUrl { Symbol = "STRAX", Url = _storage.GetIndexerUrl("STRAX") ?? "https://strax.indexer.blockcore.net/api" });
indexers.Add(new IndexerUrl { Symbol = "CITY", Url = _storage.GetIndexerUrl("CITY") ?? "https://city.indexer.blockcore.net/api" });
indexers.Add(new IndexerUrl { Symbol = "IMPLX", Url = _storage.GetIndexerUrl("IMPLX") ?? "https://implx.indexer.blockcore.net/api" });
indexers.Add(new IndexerUrl { Symbol = "RSC", Url = _storage.GetIndexerUrl("RSC") ?? "https://rsc.indexer.blockcore.net/api" });
indexers.Add(new IndexerUrl { Symbol = "SBC", Url = _storage.GetIndexerUrl("SBC") ?? "https://sbc.indexer.blockcore.net/api" });
return indexers;
}

public Dictionary<string, Network> Networks { get; } = new()
{
{ "STRAX", AtomicSwaps.Shared.Networks.Networks.Strax.Mainnet() },
Expand Down

0 comments on commit 31aaa1b

Please sign in to comment.