Skip to content

Commit

Permalink
Load more transactions for wallet or token
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed May 10, 2024
1 parent fc92b37 commit 645d51a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 23 deletions.
11 changes: 11 additions & 0 deletions src/aoWebWallet/Pages/TokenDetail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
<TransactionComponent transfer="transfer" />
}
</MudTimeline>

@if (BindingContext.TokenTransferList.DataLoader.LoadingState == LoadingState.Finished && BindingContext.CanLoadMoreTransactions)
{
<MudButton OnClick="() => LoadMoreTransactions()">Load More</MudButton>
}
<DataLoaderProgress DataLoader="BindingContext.TokenTransferList.DataLoader" Title="transactions" />

}

</MudStack>
Expand All @@ -56,5 +63,9 @@
[Parameter]
public string? TokenId { get; set; }

private Task LoadMoreTransactions()
{
return BindingContext.LoadMoreTransactions();
}

}
12 changes: 12 additions & 0 deletions src/aoWebWallet/Pages/WalletDetail.razor
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@
<TransactionComponent transfer="transfer" SelectedAddress="@BindingContext.SelectedWallet?.Wallet.Address" />
}
</MudTimeline>

@if (BindingContext.TokenTransferList.DataLoader.LoadingState == LoadingState.Finished && BindingContext.CanLoadMoreTransactions)
{
<MudButton OnClick="() => LoadMoreTransactions()">Load More</MudButton>
}
<DataLoaderProgress DataLoader="BindingContext.TokenTransferList.DataLoader" Title="transactions" />

}
</MudStack>
</MudItem>
Expand Down Expand Up @@ -292,4 +299,9 @@
await BindingContext.Claim3();
}

private Task LoadMoreTransactions()
{
return BindingContext.LoadMoreTransactions();
}

}
29 changes: 25 additions & 4 deletions src/aoWebWallet/ViewModels/TokenDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class TokenDetailViewModel : ObservableObject
private readonly MainViewModel mainViewModel;
private readonly GraphqlClient graphqlClient;
private readonly TokenDataService dataService;
private string? _tokenId;

private List<TokenTransfer> tokenTransactions = new();

public bool CanLoadMoreTransactions { get; set; } = true;

public DataLoaderViewModel<Token> Token { get; set; } = new();

Expand All @@ -28,6 +33,9 @@ public TokenDetailViewModel(MainViewModel mainViewModel, GraphqlClient graphqlCl

public async Task Initialize(string tokenId)
{
_tokenId = tokenId;
TokenTransferList.Data = new();

await this.LoadTokenData(tokenId);

this.LoadTokenTransferListForToken(tokenId);
Expand All @@ -47,18 +55,31 @@ public Task LoadTokenData(string tokenId) => Token.DataLoader.LoadAsync(async ()

public Task LoadTokenTransferListForToken(string tokenId) => TokenTransferList.DataLoader.LoadAsync(async () =>
{
TokenTransferList.Data = new();
tokenTransactions = await graphqlClient.GetTransactionsForToken(tokenId, GetCursor(tokenTransactions));
CanLoadMoreTransactions = tokenTransactions.Any();
var all = await graphqlClient.GetTransactionsForToken(tokenId);
var existing = TokenTransferList.Data ?? new();
TokenTransferList.Data = all.ToList();
TokenTransferList.Data = existing.Concat(tokenTransactions).OrderByDescending(x => x.Timestamp).ToList();
var allTokenIds = all.Select(x => x.TokenId).Distinct().ToList();
var allTokenIds = tokenTransactions.Select(x => x.TokenId).Distinct().ToList();
dataService.TryAddTokenIds(allTokenIds);
return TokenTransferList.Data;
});

public async Task LoadMoreTransactions()
{
if (_tokenId != null)
{
await LoadTokenTransferListForToken(_tokenId);
}
}

private static string? GetCursor(List<TokenTransfer> transactions)
{
return transactions.Select(x => x.Cursor).LastOrDefault();
}
}
}
44 changes: 32 additions & 12 deletions src/aoWebWallet/ViewModels/WalletDetailViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
using CommunityToolkit.Mvvm.ComponentModel;
using MudBlazor;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using webvNext.DataLoader;
using webvNext.DataLoader.Cache;
using static MudBlazor.Colors;

namespace aoWebWallet.ViewModels
{
Expand All @@ -26,8 +23,14 @@ public partial class WalletDetailViewModel : ObservableObject
private readonly ISnackbar snackbar;
private readonly MemoryDataCache memoryDataCache;

private List<TokenTransfer> incoming = new();
private List<TokenTransfer> outgoing = new();
private List<TokenTransfer> outgoingProcess = new();


private string? selectedAddress = null;

public bool CanLoadMoreTransactions { get; set; } = true;

[ObservableProperty]
private bool canClaim1;
Expand Down Expand Up @@ -111,6 +114,19 @@ public async Task GetActiveArConnectAddress()
}

public async Task RefreshTokenTransferList()
{
if (selectedAddress != null)
{
incoming = new();
outgoing = new();
outgoingProcess = new();
TokenTransferList.Data = new();

await LoadTokenTransferList(selectedAddress);
}
}

public async Task LoadMoreTransactions()
{
if (selectedAddress != null)
{
Expand All @@ -130,7 +146,7 @@ public async Task RefreshBalance()
{
if (selectedAddress != null)
{
await LoadTokenTransferList(selectedAddress);
await RefreshTokenTransferList();
await LoadBalanceDataList(selectedAddress);
}
}
Expand Down Expand Up @@ -199,23 +215,27 @@ private async Task SelectWallet(string? address)

public Task LoadTokenTransferList(string address) => TokenTransferList.DataLoader.LoadAsync(async () =>
{
TokenTransferList.Data = new();
incoming = await graphqlClient.GetTransactionsIn(address, GetCursor(incoming));
outgoing = await graphqlClient.GetTransactionsOut(address, GetCursor(outgoing));
outgoingProcess = await graphqlClient.GetTransactionsOutFromProcess(address, GetCursor(outgoingProcess));
var incoming = await graphqlClient.GetTransactionsIn(address);
var outgoing = await graphqlClient.GetTransactionsOut(address);
var outgoingProcess = await graphqlClient.GetTransactionsOutFromProcess(address);
var allNew = incoming.Concat(outgoing).Concat(outgoingProcess).OrderByDescending(x => x.Timestamp).ToList();
CanLoadMoreTransactions = allNew.Any();
var all = incoming.Concat(outgoing).Concat(outgoingProcess);
var existing = TokenTransferList.Data ?? new();
TokenTransferList.Data = all.OrderByDescending(x => x.Timestamp).ToList();
TokenTransferList.Data = existing.Concat(allNew).OrderByDescending(x => x.Timestamp).ToList();
var allTokenIds = all.Select(x => x.TokenId).Distinct().ToList();
var allTokenIds = allNew.Select(x => x.TokenId).Distinct().ToList();
dataService.TryAddTokenIds(allTokenIds);

Check warning on line 230 in src/aoWebWallet/ViewModels/WalletDetailViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
return TokenTransferList.Data;
});


private static string? GetCursor(List<TokenTransfer> transactions)
{
return transactions.Select(x => x.Cursor).LastOrDefault();
}

public async Task LoadBalanceDataList(string address, bool onlyNew = false)

Check warning on line 240 in src/aoWebWallet/ViewModels/WalletDetailViewModel.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
Expand Down
23 changes: 16 additions & 7 deletions src/aoww.Services/GraphqlClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ public GraphqlClient(HttpClient httpClient)
this.httpClient = httpClient;
}

public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string? fromTxId = null)
public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string? cursor = null)
{
string query = $$"""
query {
transactions(
first: 100
first: 50
after: "{{cursor}}"
sort: HEIGHT_DESC
tags: [
{ name: "Data-Protocol", values: ["ao"] }
Expand All @@ -29,6 +30,7 @@ public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string
]
) {
edges {
cursor
node {
id
recipient
Expand Down Expand Up @@ -77,6 +79,7 @@ public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string
var transaction = new TokenTransfer()
{
Id = edge.Node.Id,
Cursor = edge.Cursor,
From = edge.Node.Owner?.Address ?? string.Empty,
TokenTransferType = Enums.TokenTransferType.Transfer
};
Expand Down Expand Up @@ -132,12 +135,13 @@ public async Task<List<TokenTransfer>> GetTransactionsIn(string adddress, string
return processInfo;
}

public async Task<List<TokenTransfer>> GetTransactionsOut(string address, string? fromTxId = null)
public async Task<List<TokenTransfer>> GetTransactionsOut(string address, string? cursor = null)
{
string query = $$"""
query {
transactions(
first: 100
first: 50
after: "{{cursor}}"
sort: HEIGHT_DESC
owners: ["{{address}}"]
tags: [
Expand All @@ -146,6 +150,7 @@ public async Task<List<TokenTransfer>> GetTransactionsOut(string address, string
]
) {
edges {
cursor
node {
id
recipient
Expand Down Expand Up @@ -180,12 +185,13 @@ public async Task<List<TokenTransfer>> GetTransactionsOut(string address, string
return result;
}

public async Task<List<TokenTransfer>> GetTransactionsOutFromProcess(string address, string? fromTxId = null)
public async Task<List<TokenTransfer>> GetTransactionsOutFromProcess(string address, string? cursor = null)
{
string query = $$"""
query {
transactions(
first: 100
first: 50
after: "{{cursor}}"
sort: HEIGHT_DESC
tags: [
{ name: "From-Process", values: ["{{address}}"] }
Expand All @@ -194,6 +200,7 @@ public async Task<List<TokenTransfer>> GetTransactionsOutFromProcess(string addr
]
) {
edges {
cursor
node {
id
recipient
Expand Down Expand Up @@ -277,12 +284,13 @@ public async Task<List<TokenTransfer>> GetTransactionsOutFromProcess(string addr
return result.FirstOrDefault();
}

public async Task<List<TokenTransfer>> GetTransactionsForToken(string tokenId, string? fromTxId = null)
public async Task<List<TokenTransfer>> GetTransactionsForToken(string tokenId, string? cursor = null)
{
string query = $$"""
query {
transactions(
first: 50
after: "{{cursor}}"
sort: HEIGHT_DESC
recipients: ["{{tokenId}}"]
tags: [
Expand All @@ -291,6 +299,7 @@ public async Task<List<TokenTransfer>> GetTransactionsForToken(string tokenId, s
]
) {
edges {
cursor
node {
id
recipient
Expand Down
3 changes: 3 additions & 0 deletions src/aoww.Services/Models/GraphqlResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class Data

public class Edge
{
[JsonPropertyName("cursor")]
public string? Cursor { get; set; }

[JsonPropertyName("node")]
public Node? Node { get; set; }
}
Expand Down
1 change: 1 addition & 0 deletions src/aoww.Services/Models/TokenTransfer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public class TokenTransfer
public long Quantity { get; set; }

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

0 comments on commit 645d51a

Please sign in to comment.