Skip to content

Commit

Permalink
Merge pull request #8 from michielpost/feature/milestone2-graphql
Browse files Browse the repository at this point in the history
Feature/milestone2 graphql
  • Loading branch information
michielpost authored May 30, 2024
2 parents 9268e7e + 4d365f1 commit 56a4e69
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
env:
APP_VERSION: ${{ env.GITHUB_REF_SLUG }}-$(git rev-parse --short "$GITHUB_SHA")
- name: Setup dotnet
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Build with dotnet
Expand Down
4 changes: 2 additions & 2 deletions src/aoWebWallet.Tests/aoWebWallet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/aoWebWallet/ViewModels/ReceiveViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public Task LoadTokenTransferList() => TokenTransferList.DataLoader.LoadAsync(as
{
var address = Address;

var incoming = await graphqlClient.GetTransactionsIn(address);
var incoming = await graphqlClient.GetTokenTransfersIn(address);

incoming = incoming.Where(x => x.Timestamp > StartTime).OrderByDescending(x => x.Timestamp).ToList();

Expand Down
2 changes: 1 addition & 1 deletion src/aoWebWallet/ViewModels/WalletDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private async Task SelectWallet(string? address)

public Task LoadTokenTransferList(string address) => TokenTransferList.DataLoader.LoadAsync(async () =>
{
incoming = await graphqlClient.GetTransactionsIn(address, GetCursor(incoming));
incoming = await graphqlClient.GetTokenTransfersIn(address, GetCursor(incoming));
outgoing = await graphqlClient.GetTransactionsOut(address, GetCursor(outgoing));
outgoingProcess = await graphqlClient.GetTransactionsOutFromProcess(address, GetCursor(outgoingProcess));

Expand Down
4 changes: 2 additions & 2 deletions src/aoWebWallet/aoWebWallet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
<PackageReference Include="BlazorWasmPreRendering.Build" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.5" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.6" PrivateAssets="all" />
<PackageReference Include="ReactorBlazorQRCodeScanner" Version="1.0.7" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/aoww.Services.Tests/GraphqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task GetTransactionsTest()
{
var graph = new GraphqlClient(new HttpClient(), Options.Create<GraphqlConfig>(new()));

var result = await graph.GetTransactionsIn("4NdFkWsgFQIEmJnzFSYrO88UmRPf0ABfVh_fRc2u130");
var result = await graph.GetTokenTransfersIn("4NdFkWsgFQIEmJnzFSYrO88UmRPf0ABfVh_fRc2u130");

Assert.IsNotNull(result);
}
Expand All @@ -21,7 +21,7 @@ public async Task GetMintTest()
{
var graph = new GraphqlClient(new HttpClient(), Options.Create<GraphqlConfig>(new()));

var result = await graph.GetTransactionsIn("CeiYr2VjUVAFXmPJvfj-Pfk6zmprBzeqNeRWAbImbOo");
var result = await graph.GetTokenTransfersIn("CeiYr2VjUVAFXmPJvfj-Pfk6zmprBzeqNeRWAbImbOo");

Assert.IsNotNull(result);
}
Expand Down
4 changes: 2 additions & 2 deletions src/aoww.Services.Tests/aoww.Services.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.4.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down
97 changes: 89 additions & 8 deletions src/aoww.Services/GraphqlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,58 @@ public GraphqlClient(HttpClient httpClient, IOptions<GraphqlConfig> config)
this.config = config.Value;
}

public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string? cursor = null)
public async Task<List<AoTransaction>> GetTransactionsIn(string address, string? cursor = null)
{
string query = $$"""
query {
transactions(
first: 50
after: "{{cursor}}"
sort: HEIGHT_DESC
tags: [
{ name: "Data-Protocol", values: ["ao"] }
{ name: "Recipient", values: ["{{address}}"] }
]
) {
edges {
cursor
node {
id
recipient
owner {
address
}
block {
timestamp
height
}
tags {
name
value
}
}
}
}
}
""";
var queryResult = await PostQueryAsync(query);

var result = new List<AoTransaction>();

foreach (var edge in queryResult?.Data?.Transactions?.Edges ?? new())
{
AoTransaction? transaction = GetAoTransaction(edge);

if (transaction != null)
result.Add(transaction);
}

return result;
}


public async Task<List<TokenTransfer>> GetTokenTransfersIn(string address, string? cursor = null)
{
string query = $$"""
query {
Expand All @@ -29,7 +80,7 @@ public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string
tags: [
{ name: "Data-Protocol", values: ["ao"] }
{ name: "Action", values: ["Transfer", "Mint-Token"] }
{ name: "Recipient", values: ["{{adddress}}"] }
{ name: "Recipient", values: ["{{address}}"] }
]
) {
edges {
Expand Down Expand Up @@ -60,7 +111,7 @@ public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string

foreach (var edge in queryResult?.Data?.Transactions?.Edges ?? new())
{
TokenTransfer? transaction = GetTransaction(edge);
TokenTransfer? transaction = GetTokenTransfer(edge);

if (transaction != null)
result.Add(transaction);
Expand All @@ -69,7 +120,37 @@ public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string
return result;
}

private static TokenTransfer? GetTransaction(Edge edge)
private static AoTransaction? GetAoTransaction(Edge edge)
{
if (edge == null || edge.Node == null)
return null;

var transaction = new AoTransaction()
{
Id = edge.Node.Id,
Cursor = edge.Cursor,
From = edge.Node.Owner?.Address ?? string.Empty,
};

if (edge.Node.Block != null)
{
transaction.Timestamp = DateTimeOffset.FromUnixTimeSeconds(edge.Node.Block.Timestamp);
transaction.BlockHeight = edge.Node.Block.Height;
}
else
transaction.Timestamp = DateTimeOffset.UtcNow;

var fromProcess = edge.Node.Tags.Where(x => x.Name == "From-Process").Select(x => x.Value).FirstOrDefault();
if (!string.IsNullOrEmpty(fromProcess))
transaction.From = fromProcess;


transaction.To = edge.Node.Tags.Where(x => x.Name == "Recipient").Select(x => x.Value).FirstOrDefault();

return transaction;
}

private static TokenTransfer? GetTokenTransfer(Edge edge)
{
if (edge == null || edge.Node == null)
return null;
Expand Down Expand Up @@ -179,7 +260,7 @@ public async Task<List<TokenTransfer>> GetTransactionsOut(string address, string

foreach (var edge in queryResult?.Data?.Transactions?.Edges ?? new())
{
TokenTransfer? transaction = GetTransaction(edge);
TokenTransfer? transaction = GetTokenTransfer(edge);

if (transaction != null)
result.Add(transaction);
Expand Down Expand Up @@ -230,7 +311,7 @@ public async Task<List<TokenTransfer>> GetTransactionsOutFromProcess(string addr

foreach (var edge in queryResult?.Data?.Transactions?.Edges ?? new())
{
TokenTransfer? transaction = GetTransaction(edge);
TokenTransfer? transaction = GetTokenTransfer(edge);

if (transaction != null)
result.Add(transaction);
Expand Down Expand Up @@ -278,7 +359,7 @@ public async Task<List<TokenTransfer>> GetTransactionsOutFromProcess(string addr

foreach (var edge in queryResult?.Data?.Transactions?.Edges ?? new())
{
TokenTransfer? transaction = GetTransaction(edge);
TokenTransfer? transaction = GetTokenTransfer(edge);

if (transaction != null)
result.Add(transaction);
Expand Down Expand Up @@ -328,7 +409,7 @@ public async Task<List<TokenTransfer>> GetTransactionsForToken(string tokenId, s

foreach (var edge in queryResult?.Data?.Transactions?.Edges ?? new())
{
TokenTransfer? transaction = GetTransaction(edge);
TokenTransfer? transaction = GetTokenTransfer(edge);

if (transaction != null)
result.Add(transaction);
Expand Down
23 changes: 23 additions & 0 deletions src/aoww.Services/Models/AoTransaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using aoww.Services.Enums;

namespace aoww.Services.Models
{
public class AoTransaction
{
public required string Id { get; set; }
public DateTimeOffset Timestamp { get; set; }
public int? BlockHeight { get; set; }
public required string From { get; set; }
public string? To { get; set; }

public string? Cursor { get; set; }

//public string? Data { get; set; }

}
}
8 changes: 1 addition & 7 deletions src/aoww.Services/Models/TokenTransfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@

namespace aoww.Services.Models
{
public class TokenTransfer
public class TokenTransfer : AoTransaction
{
public required string Id { get; set; }
public DateTimeOffset Timestamp { get; set; }
public int? BlockHeight { get; set; }
public string? TokenId { get; set; }
public required string From { get; set; }
public string? To { get; set; }
public long Quantity { get; set; }

public TokenTransferType TokenTransferType { get; set; }
public string? Cursor { get; set; }
}
}

0 comments on commit 56a4e69

Please sign in to comment.