nft-faucet/NftFaucet/Pages/Step4Page.razor.cs

90 lines
2.7 KiB
C#
Raw Normal View History

2022-04-04 01:38:53 +02:00
using CSharpFunctionalExtensions;
2022-03-31 22:16:43 +02:00
using Microsoft.AspNetCore.Components;
2022-04-04 02:01:41 +02:00
using Microsoft.JSInterop;
2022-03-31 22:16:43 +02:00
using NftFaucet.Components;
2022-04-03 22:30:06 +02:00
using NftFaucet.Extensions;
2022-03-31 22:16:43 +02:00
using NftFaucet.Models.Enums;
using NftFaucet.Services;
2022-04-04 01:38:53 +02:00
using NftFaucet.Utils;
2022-03-31 22:16:43 +02:00
namespace NftFaucet.Pages;
public class Step4Component : BasicComponent
{
[Inject]
public IIpfsService IpfsService { get; set; }
[Inject]
public IEthereumTransactionService TransactionService { get; set; }
2022-04-04 02:01:41 +02:00
[Inject]
protected IJSRuntime JsRuntime { get; set; }
2022-04-04 01:38:53 +02:00
protected Result<string>? TransactionHash { get; set; }
2022-03-31 22:16:43 +02:00
protected override async Task OnInitializedAsync()
{
if (!await AppState.Metamask.IsReady() || string.IsNullOrEmpty(AppState.Storage.DestinationAddress))
{
2022-04-03 22:30:06 +02:00
UriHelper.NavigateToRelative("/");
2022-03-31 22:16:43 +02:00
}
else
{
Task.Run(Mint);
}
}
public async Task Mint()
{
2022-04-04 01:38:53 +02:00
var network = AppState.Metamask.Network!.Value;
var address = AppState.Storage.DestinationAddress;
var uri = AppState.Storage.TokenUrl;
2022-03-31 22:16:43 +02:00
if (AppState.Storage.TokenType == TokenType.ERC721)
{
2022-04-04 01:38:53 +02:00
TransactionHash = await ResultWrapper.Wrap(TransactionService.MintErc721Token(network, address, uri));
2022-03-31 22:16:43 +02:00
}
else
{
var amount = (int) AppState.Storage.TokenAmount;
2022-04-04 01:38:53 +02:00
TransactionHash = await ResultWrapper.Wrap(TransactionService.MintErc1155Token(network, address, amount, uri));
2022-03-31 22:16:43 +02:00
}
RefreshMediator.NotifyStateHasChangedSafe();
}
2022-04-04 02:01:41 +02:00
protected void ResetState()
2022-03-31 22:16:43 +02:00
{
AppState.Reset();
2022-04-03 22:30:06 +02:00
UriHelper.NavigateToRelative("/");
2022-03-31 22:16:43 +02:00
}
2022-04-04 01:38:53 +02:00
2022-04-04 02:01:41 +02:00
protected async Task ViewOnExplorer()
{
var baseUrl = AppState.Metamask.Network switch
{
EthereumNetwork.EthereumMainnet => "https://etherscan.io/tx/",
EthereumNetwork.Ropsten => "https://ropsten.etherscan.io/tx/",
EthereumNetwork.Rinkeby => "https://rinkeby.etherscan.io/tx/",
EthereumNetwork.Goerli => "https://goerli.etherscan.io/tx/",
EthereumNetwork.Kovan => "https://kovan.etherscan.io/tx/",
EthereumNetwork.PolygonMainnet => "https://polygonscan.com/tx/",
EthereumNetwork.PolygonMumbai => "https://mumbai.polygonscan.com/tx/",
_ => null,
};
if (baseUrl == null)
return;
var txHash = TransactionHash!.Value!.Value;
var txUrl = baseUrl + txHash;
await JsRuntime.InvokeAsync<object>("open", txUrl, "_blank");
}
protected async Task RetryTransaction()
2022-04-04 01:38:53 +02:00
{
TransactionHash = null;
RefreshMediator.NotifyStateHasChangedSafe();
Mint();
}
2022-03-31 22:16:43 +02:00
}