Skip to content

Commit

Permalink
Issue 204 - Blocking pages from server side (#217)
Browse files Browse the repository at this point in the history
* add server proof for Invest.razor

* add link block for create also

* fixup

* fix error

* act on review

* act on review

* add dan's comment

* Add an IApplicationLogicService

---------

Co-authored-by: dangershony <[email protected]>
  • Loading branch information
itailiors and dangershony authored Dec 21, 2024
1 parent e0ceac3 commit 677868b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 30 deletions.
28 changes: 19 additions & 9 deletions src/Angor/Client/Pages/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
return;
}

@if (!isValid)
{
<div class="alert alert-danger">
<h4>Error</h4>
<p>@errorMessage</p>
</div>
}

<div class="row">
<div class="card card-body">
Expand Down Expand Up @@ -570,6 +577,8 @@

private int totalDuration;
private int numberOfStages;
private bool isValid = true;
private string errorMessage;

bool createProfileSpinner;
bool createApplicationDataSpinner;
Expand All @@ -586,16 +595,21 @@
return;
}

var projects = storage.GetFounderProjects() ?? new List<FounderProject>();
var projects = storage.GetFounderProjects();

var keys = _walletStorage.GetFounderKeys();

if (projects.Count(p => !string.IsNullOrEmpty(p.CreationTransactionId)) >= keys.Keys.Count)
// if CreationTransactionId is not null this means the project was published to the blockchain
var startedProjects = projects.Where(p => !string.IsNullOrEmpty(p.CreationTransactionId)).ToList();

if (startedProjects.Count >= keys.Keys.Count)
{
notificationComponent.ShowErrorMessage("All founder keys have been used for this wallet!");
errorMessage = "All founder keys have been used for this wallet!";
isValid = false;
return;
}

var latestProject = projects.Where(_ => !string.IsNullOrEmpty(_.CreationTransactionId)).MaxBy(p => p.ProjectIndex);
var latestProject = startedProjects.MaxBy(p => p.ProjectIndex);
var projectsKeys = _derivationOperations.GetProjectKey(keys, latestProject?.ProjectIndex + 1 ?? 1);

project = projects.FirstOrDefault(p => p.ProjectInfo.ProjectIdentifier == projectsKeys.ProjectIdentifier)
Expand Down Expand Up @@ -1226,8 +1240,4 @@
project.Metadata.Picture = spacePhotos[random.Next(spacePhotos.Length)];
}
}




}
}
22 changes: 15 additions & 7 deletions src/Angor/Client/Pages/Invest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
@inject IJSRuntime JS

@inject ILogger<Invest> _Logger;
@inject IDerivationOperations _derivationOperations
@inject IDerivationOperations _derivationOperations;
@inject IClientStorage storage;
@inject ICacheStorage SessionStorage;
@inject IWalletOperations _WalletOperations
@inject IWalletOperations _WalletOperations;
@inject IApplicationLogicService applicationLogicService;
@inject ISignService _SignService;
@inject IRelayService _RelayService;

@inject ISignService _SignService
@inject IRelayService _RelayService

@inject IInvestorTransactionActions _InvestorTransactionActions
@inject IInvestorTransactionActions _InvestorTransactionActions;

@inject ISerializer serializer
@inject IEncryptionService encryption
Expand Down Expand Up @@ -386,7 +386,7 @@ else
}

Project? findProject = storage.GetInvestmentProjects().FirstOrDefault(p => p.ProjectInfo.ProjectIdentifier == ProjectId);

if (findProject != null)
{
var investmentProject = findProject as InvestorProject;
Expand All @@ -413,6 +413,14 @@ else
}
}
}

if (!applicationLogicService.IsInvestmentWindowOpen(project?.ProjectInfo))
{
notificationComponent.ShowNotificationMessage("You cannot invest in this project.", 5);
NavigationManager.NavigateTo($"/view/{ProjectId}");
return;
}

await CheckIfSeederTimeHasPassed();

UpdateStagesBreakdown(new ChangeEventArgs { Value = Investment.InvestmentAmount });
Expand Down
23 changes: 9 additions & 14 deletions src/Angor/Client/Pages/View.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@inject IJSRuntime Js;
@inject ILogger<Browse> Logger;
@inject IHtmlStripperService HtmlStripperService;

@inject IApplicationLogicService applicationLogicService;
@inject NostrConversionHelper NostrHelper


Expand Down Expand Up @@ -305,25 +305,19 @@ else
Seize the opportunity to invest in this project.
</p>
@{
var timeLeft = (project.ProjectInfo.StartDate - DateTime.UtcNow).Days;

if (timeLeft <= 0 && network.NetworkType == NetworkType.Testnet)
{
// on testnet we will not disable the ability to invest at all
timeLeft = 1;
}
var canInvest = applicationLogicService.IsInvestmentWindowOpen(project?.ProjectInfo);
}
<button class="btn btn-border mb-3" data-cy="INVEST_BUTTON"
@onclick="InvestInProject"
disabled="@(timeLeft <= 0 ? "disabled" : null)"
title="@(timeLeft <= 0 ? "The investing period is over" : null)">
@if (timeLeft > 0)
disabled="@(!canInvest)">

@if (!canInvest)
{
<text>Invest Now</text>
<text>The investing period is over</text>
}
else
{
<text>The investing period is over</text>
<text>Invest Now</text>
}
</button>
</div>
Expand Down Expand Up @@ -442,6 +436,7 @@ else
private bool isGeneratingNsec;
private string errorMessage = string.Empty;


private string error;

private List<(string Hash, int Amount)> SelectedSeeders = new List<(string hash, int amount)>
Expand Down Expand Up @@ -759,5 +754,5 @@ else
string sanitizedInput = HtmlStripperService.StripHtmlTags(input);
return new MarkupString(sanitizedInput);
}
}

}
2 changes: 2 additions & 0 deletions src/Angor/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
builder.Services.AddTransient<ISeederScriptTreeBuilder, SeederScriptTreeBuilder>();
builder.Services.AddTransient<ITaprootScriptBuilder, TaprootScriptBuilder>();

builder.Services.AddScoped<IApplicationLogicService, ApplicationLogicService>();

builder.Services.AddSingleton<INostrCommunicationFactory, NostrCommunicationFactory>();
builder.Services.AddScoped<IRelaySubscriptionsHandling, RelaySubscriptionsHandling>();
builder.Services.AddSingleton<IPasswordCacheService, PasswordCacheService>();
Expand Down
30 changes: 30 additions & 0 deletions src/Angor/Shared/Services/ApplicationLogicService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Angor.Shared.Models;
using Blockcore.Networks;

namespace Angor.Shared.Services
{
public class ApplicationLogicService : IApplicationLogicService
{
private readonly INetworkConfiguration _networkConfiguration;

public ApplicationLogicService(INetworkConfiguration networkConfiguration)
{
_networkConfiguration = networkConfiguration;
}

public bool IsInvestmentWindowOpen(ProjectInfo? project)
{
if (project == null) return false;

// on testnet we always allow to invest for testing purposes.
if (_networkConfiguration.GetNetwork().NetworkType == NetworkType.Testnet) return true;

var now = DateTime.UtcNow;

if (now <= project.StartDate) return true;

return false;
}
}
}
11 changes: 11 additions & 0 deletions src/Angor/Shared/Services/IApplicationLogicService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Angor.Shared.Models;
using Blockcore.Networks;

namespace Angor.Shared.Services
{
public interface IApplicationLogicService
{
bool IsInvestmentWindowOpen(ProjectInfo? project);
}
}

0 comments on commit 677868b

Please sign in to comment.