Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add service for refreshing the utxo's and utilize it in Create.razor #238

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Angor/Client/Pages/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
@inject IHtmlStripperService HtmlStripperService;

@inject IFounderTransactionActions _founderTransactionActions
@inject IUTXOService UTXOService

<NotificationComponent @ref="notificationComponent"/>
<PasswordComponent @ref="passwordComponent"/>

Expand Down Expand Up @@ -858,6 +860,8 @@

try
{
await UTXOService.RefreshUTXOsAsync();
notificationComponent.ShowNotificationMessage("UTXOs refreshed successfully!");
var words = await passwordComponent.GetWalletAsync();
var accountInfo = storage.GetAccountInfo(network.Name);

Expand Down
2 changes: 2 additions & 0 deletions src/Angor/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
builder.Services.AddScoped<NavMenuState>();
builder.Services.AddScoped<ICurrencyService, CurrencyService>();
builder.Services.AddScoped<ICurrencyRateService, CurrencyRateService>();
builder.Services.AddScoped<IUTXOService, UTXOService>();


builder.Services.AddScoped<IIndexerService, IndexerService>();
builder.Services.AddScoped<INetworkService, NetworkService>();
Expand Down
7 changes: 7 additions & 0 deletions src/Angor/Client/Services/IUTXOService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Angor.Client.Services
{
public interface IUTXOService
{
Task RefreshUTXOsAsync();
}
}
66 changes: 66 additions & 0 deletions src/Angor/Client/Services/UTXOService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Angor.Client.Storage;
using Angor.Shared;
using Microsoft.Extensions.Logging;

namespace Angor.Client.Services
{
public class UTXOService : IUTXOService
{
private readonly IWalletStorage _walletStorage;
private readonly IClientStorage _storage;
private readonly ICacheStorage _cacheStorage;
private readonly IWalletOperations _walletOperations;
private readonly ILogger<UTXOService> _logger;
private readonly INetworkConfiguration _networkConfiguration;

public UTXOService(
IWalletStorage walletStorage,
IClientStorage storage,
ICacheStorage cacheStorage,
IWalletOperations walletOperations,
ILogger<UTXOService> logger,
INetworkConfiguration networkConfiguration)
{
_walletStorage = walletStorage ?? throw new ArgumentNullException(nameof(walletStorage));
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
_cacheStorage = cacheStorage ?? throw new ArgumentNullException(nameof(cacheStorage));
_walletOperations = walletOperations ?? throw new ArgumentNullException(nameof(walletOperations));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_networkConfiguration = networkConfiguration ?? throw new ArgumentNullException(nameof(networkConfiguration));
}

public async Task RefreshUTXOsAsync()
{
try
{
var network = _networkConfiguration.GetNetwork();
var accountInfo = _storage.GetAccountInfo(network.Name);
var unconfirmedInboundFunds = _cacheStorage.GetUnconfirmedInboundFunds();

_logger.LogInformation($"Refreshing UTXOs for network: {network.Name} ({network.CoinTicker})");

await _walletOperations.UpdateDataForExistingAddressesAsync(accountInfo);
await _walletOperations.UpdateAccountInfoWithNewAddressesAsync(accountInfo);

_storage.SetAccountInfo(network.Name, accountInfo);

// Remove spent UTXOs from unconfirmed cache
var utxos = accountInfo.AllUtxos().Select(x => x.outpoint.ToString()).ToList();
var spentToUpdate = unconfirmedInboundFunds.RemoveAll(x => utxos.Contains(x.outpoint.ToString()));

if (spentToUpdate > 0)
_cacheStorage.SetUnconfirmedInboundFunds(unconfirmedInboundFunds);

_logger.LogInformation("UTXOs refreshed successfully.");
}
catch (Exception ex)
{
_logger.LogError(ex, "Error refreshing UTXOs");
throw;
}
}
}
}
Loading