Skip to content

Commit

Permalink
add "balance" GraphQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed Sep 13, 2024
1 parent 029514a commit 10a2954
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions Mimir/GraphQL/Queries/Query.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using HotChocolate.AspNetCore;
using Lib9c.GraphQL.Extensions;
using Lib9c.GraphQL.InputObjects;
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.GraphQL.Objects;
Expand All @@ -14,8 +17,35 @@ public class Query
/// <param name="collectionName">The name of the collection.</param>
/// <param name="repo"></param>
/// <returns>The metadata</returns>
public async Task<MetadataDocument> GetMetadataAsync(string collectionName, [Service] MetadataRepository repo)
=> await repo.GetByCollectionAsync(collectionName);
public async Task<MetadataDocument> GetMetadataAsync(string collectionName, [Service] MetadataRepository repo) =>
await repo.GetByCollectionAsync(collectionName);

/// <summary>
/// Get the balance of a specific currency for a given address.
/// Choose one of the following parameters to specify the currency: currency, currencyTicker
/// </summary>
/// <param name="currency">The currency object.</param>
/// <param name="currencyTicker">The ticker of the currency.</param>
/// <param name="address">The address of the balance.</param>
/// <exception cref="GraphQLRequestException"></exception>
public async Task<string> GetBalanceAsync(
CurrencyInput? currency,
string? currencyTicker,
Address address,
[Service] BalanceRepository repo)
{
if (currency is not null)
{
return (await repo.GetByAddressAsync(currency.ToCurrency(), address)).Object;
}

if (currencyTicker is not null)
{
return (await repo.GetByAddressAsync(currencyTicker.ToCurrency(), address)).Object;
}

throw new GraphQLRequestException("Either currency or currencyTicker must be provided.");
}

/// <summary>
/// Get an agent state by address.
Expand Down

0 comments on commit 10a2954

Please sign in to comment.