Skip to content

Commit

Permalink
Test Transfer CSPR functionality
Browse files Browse the repository at this point in the history
Signed-off-by: David Hernando <david.hernando@make.services>
  • Loading branch information
davidatwhiletrue committed Aug 23, 2024
1 parent 16e3f86 commit 4b4907a
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Casper.Network.SDK.Web;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
using Radzen;

namespace NCTLWebExplorer.Components;
Expand Down
5 changes: 3 additions & 2 deletions Docs/Demos/NCTLWebExplorer/Pages/BlockDetail.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/blocks/{BlockHash}"
@using System.Globalization
@inherits ExplorerComponent;

@if (_block is null)
Expand All @@ -23,9 +24,9 @@ else
<div>Timestamp</div>
<b>@(_block.Timestamp)</b>
<div class="mt-1" >Block Height</div>
<b>@(_block.Height)</b>
<b>@(_block.Height.ToString("N0", CultureInfo.InvariantCulture))</b>
<div class="mt-1">Era Id</div>
<b>@(_block.EraId)</b>
<b>@(_block.EraId.ToString("N0", CultureInfo.InvariantCulture))</b>
<div class="mt-1">Switch Block</div>
<b>@(_block.EraEnd != null ? "Yes" : "No")</b>
</div>
Expand Down
5 changes: 3 additions & 2 deletions Docs/Demos/NCTLWebExplorer/Pages/BlockExplorer.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/blocks"
@using System.Globalization
@using Casper.Network.SDK.Types
@using NCTLWebExplorer.Models

Expand All @@ -19,12 +20,12 @@
<Columns>
<RadzenDataGridColumn TItem="BlockSummary" Property="BlockHeight" Title="Block Height" Type="typeof(int)">
<Template>
<a href="/blocks/@context.Hash">@context.Height</a>
<a href="/blocks/@context.Hash">@context.Height.ToString("N0", CultureInfo.InvariantCulture)</a>
</Template>
</RadzenDataGridColumn>
<RadzenDataGridColumn TItem="BlockSummary" Property="Era" Title="Era" Type="typeof(int)">
<Template>
@context.EraId
@context.EraId.ToString("N0", CultureInfo.InvariantCulture)
</Template>
</RadzenDataGridColumn>
<RadzenDataGridColumn TItem="BlockSummary" Property="TransactionCount" Title="Transactions" Type="typeof(int)">
Expand Down
21 changes: 16 additions & 5 deletions Docs/Demos/NCTLWebExplorer/Pages/TransferCspr.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/transfer-cspr"
@page "/transfer-cspr"
@inherits ExplorerComponent;
@inject CsprClickInterop _csprClickInterop;

<PageTitle>Transfer $CSPR</PageTitle>

Expand All @@ -23,9 +24,19 @@

<RadzenCard Class="w-100 mb-4">
<div class="row mb-4">
<PublicKeyTextBox Text="Enter the origin Public Key:"
LinkedToSigner="true"
@bind-Value="@_originPublicKey" Class="w-100"/>
<h5>Sender Public Key:</h5>
@if (!string.IsNullOrWhiteSpace(_originPublicKey))
{
<div>
<RadzenTextBox @bind-Value="_originPublicKey" Disabled="true" Class="w-100"/>
</div>
}
else
{
<div>
<RadzenTextBox Placeholder="Connect your wallet with CSPR.click" Disabled="true" Class="w-100"/>
</div>
}
</div>
<div class="row mb-4">
<PublicKeyTextBox Text="Enter the recipient Public Key:"
Expand All @@ -44,4 +55,4 @@
</div>
</RadzenCard>
</div>
</div>
</div>
162 changes: 119 additions & 43 deletions Docs/Demos/NCTLWebExplorer/Pages/TransferCspr.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Numerics;
using System.Numerics;
using Casper.Network.SDK;
using Casper.Network.SDK.JsonRpc;
using Casper.Network.SDK.Types;
Expand All @@ -11,55 +11,131 @@ public partial class TransferCspr
{
private string _originPublicKey;
private string _targetPublicKey;
private string _transferAmount;
private string _transferAmount = "2.5";

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
try
{
await _csprClickInterop.SetDotNetInstance();
Console.WriteLine("Getting key");
_originPublicKey = await _csprClickInterop.GetActiveKey();
}
catch (Exception e)
{
Console.WriteLine("Error getting key");
Console.WriteLine(e.Message);
}

_csprClickInterop.OnStateUpdate += (type, key) =>
{
_originPublicKey = key;
StateHasChanged();
};
StateHasChanged();
}
}

async Task SendTransferBtnClicked()
{
ErrorMessage = null;
SuccessMessage = null;

// var state = await SignerInterop.GetState();
//
// if (state is not {IsUnlocked: true})
// {
// NotificationService.Notify(new NotificationMessage
// {
// Severity = NotificationSeverity.Error, Summary = "Unlock Casper Signer to sign the deploy",
// Duration = 4000
// });
// return;
// }
//
// var casperService = CasperRpcService as CasperRPCService;
//
// var deploy = DeployTemplates.StandardTransfer(PublicKey.FromHexString(state.ActivePK),
// PublicKey.FromHexString(_targetPublicKey),
// BigInteger.Parse(_transferAmount) * 1_000_000_000,
// new BigInteger(100_000_000),
// casperService?.ChainName,
// 1);
//
// deploy = await SignDeployWithSigner(deploy, state.ActivePK, _targetPublicKey);
//
// try
// {
// var rpcResponse = await CasperRpcService?.PutDeploy(deploy)!;
// var result = rpcResponse.Parse();
// Console.WriteLine("RESULT: " + result.DeployHash);
// SuccessMessage = "<b>Deploy hash:&nbsp;</b>" + result.DeployHash;
//
// NotificationService.Notify(new NotificationMessage
// {Severity = NotificationSeverity.Success, Summary = "Transfer successfully sent.", Duration = 4000});
// }
// catch (RpcClientException e)
// {
// this.ErrorMessage = e.Message;
//
// NotificationService.Notify(new NotificationMessage
// {Severity = NotificationSeverity.Error, Summary = "Error sending the transaction.", Duration = 4000});
// }
if (string.IsNullOrWhiteSpace(_originPublicKey))
{
NotificationService.Notify(new NotificationMessage
{
Severity = NotificationSeverity.Error, Summary = "Sender account not valid.",
Duration = 4000
});
return;
}

try
{
if (string.IsNullOrWhiteSpace(_targetPublicKey))
throw new Exception();

if(_originPublicKey == _targetPublicKey)
throw new Exception();

var pk = PublicKey.FromHexString(_targetPublicKey);
}
catch (Exception e)
{
NotificationService.Notify(new NotificationMessage
{
Severity = NotificationSeverity.Error, Summary = "Recipient must be a valid public key.",
Duration = 4000
});
return;
}

BigInteger amount;

try
{
if (string.IsNullOrWhiteSpace(_transferAmount))
throw new Exception();

var cspr = float.Parse(_transferAmount);
if (cspr < 2.5)
throw new Exception();
var motes = (ulong)(cspr * 1_000_000_000);
amount = new BigInteger(motes);
}
catch (Exception e)
{
NotificationService.Notify(new NotificationMessage
{
Severity = NotificationSeverity.Error, Summary = "Amount not valid",
Duration = 4000
});
return;
}

var casperService = CasperRpcService as CasperRPCService;

var deploy = DeployTemplates.StandardTransfer(
PublicKey.FromHexString(_originPublicKey),
PublicKey.FromHexString(_targetPublicKey),
amount,
new BigInteger(100_000_000),
casperService?.ChainName,
1);

deploy = await _csprClickInterop.SignDeploy(deploy, _originPublicKey);

if (deploy is null)
{
NotificationService.Notify(new NotificationMessage
{
Severity = NotificationSeverity.Error, Summary = "Error getting the deploy signature.",
Duration = 4000
});
return;
}

try
{
var rpcResponse = await CasperRpcService?.PutDeploy(deploy)!;
var result = rpcResponse.Parse();
SuccessMessage = "<b>Deploy hash:&nbsp;</b>" + result.DeployHash;

NotificationService.Notify(new NotificationMessage
{Severity = NotificationSeverity.Success, Summary = "Transfer successfully sent.", Duration = 4000});
}
catch (RpcClientException e)
{
this.ErrorMessage = e.Message;

NotificationService.Notify(new NotificationMessage
{Severity = NotificationSeverity.Error, Summary = "Error sending the transaction.", Duration = 4000});
}

_targetPublicKey = null;
_transferAmount = null;
}
}
}
32 changes: 19 additions & 13 deletions Docs/Demos/NCTLWebExplorer/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png">
<link rel="icon" type="image/png" sizes="256x256" href="/favicon-256w.png">
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered"/>
<script src="./app.js"></script>
<script defer="defer" src="https://cdn.cspr.click/ui/v1.5.0/csprclick-client-1.5.0.js"></script>
</head>
<body>
@RenderBody()
<div id="app">
<div id="csprclick-ui-wrapper">
<div id="csprclick-ui"></div>
</div>
@RenderBody()

<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>

<script src="_framework/blazor.server.js"></script>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions Docs/Demos/NCTLWebExplorer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NCTLWebExplorer.Services;
using Casper.Network.SDK.Web;
using NCTLWebExplorer.Utils;
using Radzen;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -16,6 +17,8 @@
builder.Services.AddCasperSSEService(builder.Configuration);
builder.Services.AddSingleton<EventListener>();

builder.Services.AddScoped<CsprClickInterop>();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
1 change: 1 addition & 0 deletions Docs/Demos/NCTLWebExplorer/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<RadzenPanelMenuItem Text="Get Entity" Icon="label" Path="/get-entity"/>
<RadzenPanelMenuItem Text="Get Balance" Icon="label" Path="/get-balance"/>
<RadzenPanelMenuItem Text="Get Dictionary Item" Icon="label" Path="/dictionary-item"/>
<RadzenPanelMenuItem Text="Transfer CSPR" Icon="label" Path="/transfer-cspr"/>
</RadzenPanelMenu>
</RadzenSidebar>
<RadzenBody>
Expand Down
Loading

0 comments on commit 4b4907a

Please sign in to comment.