Skip to content

Commit

Permalink
Show Schema Protocol Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Sep 30, 2024
1 parent bad4268 commit 27aeaff
Show file tree
Hide file tree
Showing 10 changed files with 325 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/aoWebWallet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "aoWebWallet.Tests", "aoWebW
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoww.ProcesModels", "aoww.ProcesModels\aoww.ProcesModels.csproj", "{739E7471-30E4-4522-86B8-5D9BB5F0896A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aoww.ProcesModels.Tests", "aoww.ProcesModels.Tests\aoww.ProcesModels.Tests.csproj", "{F230462E-20DA-4EB9-A6E7-D6515F677C13}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -49,6 +51,10 @@ Global
{739E7471-30E4-4522-86B8-5D9BB5F0896A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{739E7471-30E4-4522-86B8-5D9BB5F0896A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{739E7471-30E4-4522-86B8-5D9BB5F0896A}.Release|Any CPU.Build.0 = Release|Any CPU
{F230462E-20DA-4EB9-A6E7-D6515F677C13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F230462E-20DA-4EB9-A6E7-D6515F677C13}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F230462E-20DA-4EB9-A6E7-D6515F677C13}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F230462E-20DA-4EB9-A6E7-D6515F677C13}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -59,6 +65,7 @@ Global
{322F4807-05CF-431D-B400-7420E1B29936} = {89AC47DF-65AD-4870-AA1D-74ABF1F3D8FE}
{12E9E40E-96D1-4501-A9A4-EBE4D4F43D8D} = {89AC47DF-65AD-4870-AA1D-74ABF1F3D8FE}
{739E7471-30E4-4522-86B8-5D9BB5F0896A} = {06E5BC39-764A-48B9-B4F9-F48387A2C965}
{F230462E-20DA-4EB9-A6E7-D6515F677C13} = {89AC47DF-65AD-4870-AA1D-74ABF1F3D8FE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {432E3F8E-53FF-4D9C-869D-48449BD3B8B4}
Expand Down
1 change: 1 addition & 0 deletions src/aoWebWallet/Pages/WalletDetail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
</MudTabPanel>
<MudTabPanel Text="Action Builder">
<MudGrid Spacing="2">
<SchemaComponent ProcessId="@Address" />
<MudItem xs="12" sm="6">
<ActionList ProcessId="@Address" />
</MudItem>
Expand Down
2 changes: 2 additions & 0 deletions src/aoWebWallet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using MudExtensions.Services;
using Soenneker.Blazor.Utils.Navigation.Registrars;
using Append.Blazor.WebShare;
using aoww.ProcesModels.SchemaProtocol;

namespace aoWebWallet
{
Expand Down Expand Up @@ -108,6 +109,7 @@ private static void ConfigureServices(IServiceCollection services, string baseAd
services.AddScoped<TransactionService>();
services.AddScoped<GatewayUrlHelper>();
services.AddScoped<CreateTokenService>();
services.AddScoped<SchemaProtocolClient>();

services.AddSingleton<MemoryDataCache>();

Expand Down
52 changes: 52 additions & 0 deletions src/aoWebWallet/Shared/Components/SchemaComponent.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@using aoww.ProcesModels.Metadata
@using aoww.ProcesModels.SchemaProtocol
@inject SchemaProtocolClient schemaProtocolClient
@inject NavigationManager NavigationManager

@if (actions != null && actions.Any())
{
<MudItem xs="12" sm="6">
<MudPaper Class="pa-4">
<MudText Typo="Typo.h6">Schema Protocol Actions</MudText>
<br />
@if (actions.Any())
{
@foreach (var action in actions)
{
<MudCard>
<MudCardContent>
<MudText>@action.Title</MudText>
<MudText Typo="Typo.body2">@action.Description</MudText>
</MudCardContent>
<MudCardActions>
<MudButton ButtonType="ButtonType.Button" Color="Color.Secondary" OnClick="() => GoToSend(action.AoAction)">@action.Name</MudButton>
</MudCardActions>
</MudCard>
}
}

</MudPaper>
</MudItem>
}


@code {
[Parameter]
public required string ProcessId { get; set; }

private List<ActionMetadata> actions = new();

protected override async Task OnParametersSetAsync()
{
actions = new();

actions = await schemaProtocolClient.GetSchemaProtocolActions(ProcessId);

StateHasChanged();
}

private void GoToSend(AoAction aoAction)
{
NavigationManager.NavigateTo($"/action?{aoAction.ToQueryString()}");
}
}
37 changes: 37 additions & 0 deletions src/aoww.ProcesModels.Tests/SchemaProtocolClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using aoww.ProcesModels.SchemaProtocol;
using ArweaveAO;
using ArweaveAO.Models;
using Microsoft.Extensions.Options;

namespace aoww.ProcesModels.Tests
{
[TestClass]
public sealed class SchemaProtocolClientTests
{
[TestMethod]
public async Task GetActionsTests()
{
var processId = "ptvbacSmqJPfgCXxPc9bcobs5Th2B_SxTf81vRNkRzk";

var client = new SchemaProtocolClient(new AODataClient(Options.Create<ArweaveConfig>(new()), new HttpClient()));

var result = await client.GetSchemaProtocolActions(processId);

Assert.IsNotNull(result);
Assert.IsTrue(result.Any());
}

[TestMethod]
public async Task NoActionsTests()
{
var processId = "YCH7ugqKwIo_bQebG_eT_7QY0sxVXbnv7BTgmRi7FZk";

var client = new SchemaProtocolClient(new AODataClient(Options.Create<ArweaveConfig>(new()), new HttpClient()));

var result = await client.GetSchemaProtocolActions(processId);

Assert.IsNotNull(result);
Assert.IsFalse(result.Any());
}
}
}
31 changes: 31 additions & 0 deletions src/aoww.ProcesModels.Tests/aoww.ProcesModels.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\aoww.ProcesModels\aoww.ProcesModels.csproj" />
</ItemGroup>


<ItemGroup>
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/aoww.ProcesModels/Metadata/ActionMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ namespace aoww.ProcesModels.Metadata
public class ActionMetadata
{
public required string Name { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }

public int Order { get; set; }

public ActionType ActionType { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using aoww.ProcesModels.Action;
using aoww.ProcesModels.Metadata;
using aoww.ProcesModels.SchemaProtocol.Models;

namespace aoww.ProcesModels.SchemaProtocol.Extensions
{
public static class SchemaProtocolExtensions
{
public static ActionMetadata? ToActionMetadata(this KeyValuePair<string, SchemaProtocolActionModel> input, string targetProcessId)
{
AoAction? aoAction = input.Value.ToAoAction();
if (aoAction == null)
return null;

if(!aoAction.Params.Where(x => x.ParamType == ActionParamType.Target).Any())
aoAction.Params.Add(new ActionParam() { Key = "Target", Value = targetProcessId, ParamType = ActionParamType.Target });

ActionMetadata result = new ActionMetadata
{
Name = input.Key,
Title = input.Value.Title,
Description = input.Value.Description,
ActionType = ActionType.Message,
AoAction = aoAction
};


return result;
}

public static AoAction? ToAoAction(this SchemaProtocolActionModel input)
{
var props = input?.Schema?.Tags?.Properties;
if (props == null)
return null;

List<ActionParam> paramList = props.Select(x => x.ToParams())
.Where(x => x != null)
.Select(x => x!).ToList();

AoAction result = new AoAction
{
Params = paramList
};


return result;
}

public static ActionParam? ToParams(this KeyValuePair<string, SchemaProtocolPropertyModel> input)
{
ActionParam result = new ActionParam
{
Key = input.Key,
Value = input.Value.Const,
ParamType = ActionParamType.Input
};

if (!string.IsNullOrWhiteSpace(result.Value))
result.ParamType = ActionParamType.Filled;
else if(input.Value.Type == "integer")
result.ParamType = ActionParamType.Integer;


return result;
}
}
}
77 changes: 77 additions & 0 deletions src/aoww.ProcesModels/SchemaProtocol/Models/SchemaProtocolModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Text.Json.Serialization;

namespace aoww.ProcesModels.SchemaProtocol.Models
{
public class SchemaProtocolModel : Dictionary<string, SchemaProtocolActionModel>
{


}

public class SchemaProtocolActionModel
{
[JsonPropertyName("Title")]
public string? Title { get; set; }

[JsonPropertyName("Description")]
public string? Description { get; set; }

[JsonPropertyName("Schema")]
public SchemaProtocolSchemaModel? Schema { get; set; }


}

public class SchemaProtocolSchemaModel
{
[JsonPropertyName("Tags")]
public SchemaProtocolTagsModel? Tags { get; set; }

//[JsonPropertyName("Data")]
//public string? Data { get; set; }

}



public class SchemaProtocolTagsModel
{
[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("required")]
public List<string> Required { get; set; } = new();

[JsonPropertyName("properties")]
public Dictionary<string, SchemaProtocolPropertyModel> Properties { get; set; } = new();
}

public class SchemaProtocolPropertyModel
{
[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("const")]
public string? Const { get; set; }

[JsonPropertyName("title")]
public string? Title { get; set; }

[JsonPropertyName("description")]
public string? Description { get; set; }

[JsonPropertyName("minimum")]
public int? Minimum { get; set; }

[JsonPropertyName("maximum")]
public int? Maximum { get; set; }

[JsonPropertyName("$comment")]
public string? Comment { get; set; }

[JsonPropertyName("maxLength")]
public int? MaxLength { get; set; }

public List<string>? Enum { get; set; }
}
}
47 changes: 47 additions & 0 deletions src/aoww.ProcesModels/SchemaProtocol/SchemaProtocolClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using aoww.ProcesModels.Metadata;
using aoww.ProcesModels.SchemaProtocol.Extensions;
using aoww.ProcesModels.SchemaProtocol.Models;
using ArweaveAO;
using ArweaveAO.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aoww.ProcesModels.SchemaProtocol
{
public class SchemaProtocolClient(AODataClient aoDataClient)
{
public async Task<List<ActionMetadata>> GetSchemaProtocolActions(string processId)
{
try
{
var result = await aoDataClient.DryRun(processId, new ArweaveAO.Requests.DryRunRequest
{
Target = processId,
Tags = new List<ArweaveAO.Models.Tag>
{
new ArweaveAO.Models.Tag() { Name = "Action", Value = "Schema" }
}
});

var data = result?.GetFirstDataValue();

if (data != null)
{
var model = System.Text.Json.JsonSerializer.Deserialize<SchemaProtocolModel>(data);

if (model != null)
{
List<ActionMetadata> actionList = model.Select(x => x.ToActionMetadata(processId)).Where(x => x != null).Select(x => x!).ToList();
return actionList;
}
}
}
catch { }

return new();
}
}
}

0 comments on commit 27aeaff

Please sign in to comment.