2022-08-26 13:13:40 -05:00
using Microsoft.AspNetCore.Components ;
2022-08-27 22:32:35 -05:00
using NftFaucetRadzen.Extensions ;
2022-08-26 13:13:40 -05:00
using NftFaucetRadzen.Models ;
2022-08-27 17:22:50 -05:00
using NftFaucetRadzen.Plugins.NetworkPlugins ;
using NftFaucetRadzen.Plugins.ProviderPlugins ;
2022-08-27 12:37:49 -05:00
using NftFaucetRadzen.Services ;
2022-08-26 13:13:40 -05:00
namespace NftFaucetRadzen.Components ;
public abstract class BasicLayout : LayoutComponentBase
{
[Inject]
protected NavigationManager UriHelper { get ; set ; }
[Inject]
protected ScopedAppState AppState { get ; set ; }
[Inject]
protected RefreshMediator RefreshMediator { get ; set ; }
2022-08-27 17:22:50 -05:00
[Inject]
protected PluginLoader PluginLoader { get ; set ; }
2022-08-26 13:13:40 -05:00
protected override void OnInitialized ( )
{
2022-08-27 22:32:35 -05:00
LoadDataFromPlugins ( ) ;
2022-08-26 13:13:40 -05:00
RefreshMediator . StateChanged + = async ( ) = > await InvokeAsync ( StateHasChangedSafe ) ;
}
protected void StateHasChangedSafe ( )
{
try
{
InvokeAsync ( StateHasChanged ) ;
}
catch ( Exception )
{
// ignored
}
}
2022-08-27 22:32:35 -05:00
private void LoadDataFromPlugins ( )
{
var isFirstRun = AppState . Storage . Networks = = null & &
AppState . Storage . Providers = = null & &
AppState . Storage . Contracts = = null ;
PluginLoader . EnsurePluginsLoaded ( ) ;
AppState . Storage . Networks ? ? = PluginLoader . NetworkPlugins . SelectMany ( x = > x . Networks ) . Where ( x = > x ! = null ) . ToArray ( ) ;
AppState . Storage . Providers ? ? = PluginLoader . ProviderPlugins . SelectMany ( x = > x . Providers ) . Where ( x = > x ! = null ) . ToArray ( ) ;
AppState . Storage . Contracts ? ? = AppState . Storage . Networks . SelectMany ( x = > x . DeployedContracts ) . Where ( x = > x ! = null ) . ToArray ( ) ;
if ( isFirstRun )
{
ValidatePluginsData ( ) ;
}
}
private void ValidatePluginsData ( )
{
var networkIds = AppState . Storage . Networks . Select ( x = > x . Id ) . ToArray ( ) ;
var providerIds = AppState . Storage . Providers . Select ( x = > x . Id ) . ToArray ( ) ;
var contractIds = AppState . Storage . Contracts . Select ( x = > x . Id ) . ToArray ( ) ;
var networkIdDuplicates = networkIds . Duplicates ( ) . ToArray ( ) ;
if ( networkIdDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are networks with same ids: {string.Join(" , ", networkIdDuplicates)}" ) ;
}
var providerIdDuplicates = providerIds . Duplicates ( ) . ToArray ( ) ;
if ( providerIdDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are providers with same ids: {string.Join(" , ", providerIdDuplicates)}" ) ;
}
var contractIdDuplicates = contractIds . Duplicates ( ) . ToArray ( ) ;
if ( contractIdDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are contracts with same ids: {string.Join(" , ", contractIdDuplicates)}" ) ;
}
var allIds = networkIds . Concat ( providerIds ) . Concat ( contractIds ) . ToArray ( ) ;
var allIdDuplicates = allIds . Duplicates ( ) . ToArray ( ) ;
if ( allIdDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are plugin data items (networks/providers/contracts) with same ids: {string.Join(" , ", allIdDuplicates)}" ) ;
}
var networkShortNames = AppState . Storage . Networks . Select ( x = > x . ShortName ) . Where ( x = > x ! = null ) . ToArray ( ) ;
var networkShortNameDuplicates = networkShortNames . Duplicates ( ) . ToArray ( ) ;
if ( networkShortNameDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are networks with same short name: {string.Join(" , ", networkShortNameDuplicates)}" ) ;
}
var providerShortNames = AppState . Storage . Providers . Select ( x = > x . ShortName ) . Where ( x = > x ! = null ) . ToArray ( ) ;
var providerShortNameDuplicates = providerShortNames . Duplicates ( ) . ToArray ( ) ;
if ( providerShortNameDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are providers with same short name: {string.Join(" , ", providerShortNameDuplicates)}" ) ;
}
var txHashes = AppState . Storage . Contracts . Select ( x = > x . DeploymentTxHash ) . Where ( x = > x ! = null ) . ToArray ( ) ;
var txHashDuplicates = txHashes . Duplicates ( ) . ToArray ( ) ;
if ( txHashDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are contracts with same tx hash: {string.Join(" , ", txHashDuplicates)}" ) ;
}
var txDeploymentDates = AppState . Storage . Contracts . Select ( x = > x . DeployedAt ) . Where ( x = > x ! = null ) . ToArray ( ) ;
var txDeploymentDateDuplicates = txDeploymentDates . Duplicates ( ) . ToArray ( ) ;
if ( txDeploymentDateDuplicates . Any ( ) )
{
throw new ApplicationException ( $"[{nameof(ValidatePluginsData)}] There are contracts with same deployment datetime: {string.Join(" , ", txDeploymentDateDuplicates)}" ) ;
}
}
2022-08-26 13:13:40 -05:00
}