From 5d928cfd42224a6531fa296993ffb31edc7c44db Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Thu, 14 Dec 2023 03:14:10 +0900 Subject: [PATCH 1/2] Added HelperQuery --- .../Queries/HelperQueryTest.cs | 63 +++++++++++++++++++ Libplanet.Explorer/Queries/ExplorerQuery.cs | 1 + Libplanet.Explorer/Queries/HelperQuery.cs | 48 ++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 Libplanet.Explorer.Tests/Queries/HelperQueryTest.cs create mode 100644 Libplanet.Explorer/Queries/HelperQuery.cs diff --git a/Libplanet.Explorer.Tests/Queries/HelperQueryTest.cs b/Libplanet.Explorer.Tests/Queries/HelperQueryTest.cs new file mode 100644 index 00000000000..8ef1be73ef2 --- /dev/null +++ b/Libplanet.Explorer.Tests/Queries/HelperQueryTest.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using GraphQL; +using GraphQL.Execution; +using Libplanet.Common; +using static Libplanet.Explorer.Tests.GraphQLTestUtils; +using Libplanet.Explorer.Queries; +using Libplanet.Types.Assets; +using Xunit; + +namespace Libplanet.Explorer.Tests.Queries; + +public class HelperQueryTest +{ + [Fact] + public async Task CurrencyHash() + { + Currency currency = Currency.Uncapped("ABC", 2, minters: null); + + ExecutionResult result = await ExecuteQueryAsync(@" + { + currencyHash ( + currency: { ticker: ""ABC"", decimalPlaces: 2, totalSupplyTrackable: true } + ) + } + "); + + Assert.Null(result.Errors); + ExecutionNode resultData = Assert.IsAssignableFrom(result.Data); + IDictionary resultDict = + Assert.IsAssignableFrom>(resultData!.ToValue()); + string hash = + Assert.IsAssignableFrom(resultDict["currencyHash"]); + Assert.Equal(ByteUtil.Hex(currency.Hash.ByteArray), hash); + } + + [Fact] + public async Task BencodexValue() + { + ExecutionResult result = await ExecuteQueryAsync(@" + { + bencodexValue (hex: ""7531323a68656c6c6f20776f726c6421"") { + hex + json + inspection + } + } + "); + + Assert.Null(result.Errors); + ExecutionNode resultData = Assert.IsAssignableFrom(result.Data); + IDictionary resultDict = + Assert.IsAssignableFrom>(resultData!.ToValue()); + IDictionary bencodexValue = + Assert.IsAssignableFrom>(resultDict["bencodexValue"]); + string hex = Assert.IsAssignableFrom(bencodexValue["hex"]); + string json = Assert.IsAssignableFrom(bencodexValue["json"]); + string inspection = Assert.IsAssignableFrom(bencodexValue["inspection"]); + Assert.Equal("7531323a68656c6c6f20776f726c6421", hex); + Assert.Equal("\"\\uFEFFhello world!\"", json); + Assert.Equal("\"hello world!\"", inspection); + } +} diff --git a/Libplanet.Explorer/Queries/ExplorerQuery.cs b/Libplanet.Explorer/Queries/ExplorerQuery.cs index 5829a49ff88..bf210edcae7 100644 --- a/Libplanet.Explorer/Queries/ExplorerQuery.cs +++ b/Libplanet.Explorer/Queries/ExplorerQuery.cs @@ -28,6 +28,7 @@ public ExplorerQuery(IBlockChainContext chainContext) "nodeState", resolve: context => chainContext ); + Field("helperQuery", resolve: context => new { }); Name = "ExplorerQuery"; } diff --git a/Libplanet.Explorer/Queries/HelperQuery.cs b/Libplanet.Explorer/Queries/HelperQuery.cs new file mode 100644 index 00000000000..49e6e5063ef --- /dev/null +++ b/Libplanet.Explorer/Queries/HelperQuery.cs @@ -0,0 +1,48 @@ +using System.Security.Cryptography; +using Bencodex; +using GraphQL; +using GraphQL.Types; +using Libplanet.Common; +using Libplanet.Explorer.GraphTypes; +using Libplanet.Types.Assets; + +namespace Libplanet.Explorer.Queries +{ + public class HelperQuery : ObjectGraphType + { + private static Codec _codec = new Codec(); + + public HelperQuery() + { + Name = "HelperQuery"; + Description = "A number of queries for convenience."; + + Field>>( + name: "currencyHash", + description: "Converts currency info to currency hash.", + arguments: new QueryArguments( + new QueryArgument> + { + Name = "currency", + Description = "The currency to convert.", + } + ), + resolve: context => context.GetArgument("currency").Hash + ); + + Field>( + name: "bencodexValue", + description: "Decodes hex encoded bencodex value", + arguments: new QueryArguments( + new QueryArgument> + { + Name = "hex", + Description = "The byte array in hex representation to decode.", + } + ), + resolve: context => + _codec.Decode(ByteUtil.ParseHex(context.GetArgument("hex"))) + ); + } + } +} From 0b56ea5a3c2dcc7962e8c5514d223ec459c04e28 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Thu, 14 Dec 2023 10:25:44 +0900 Subject: [PATCH 2/2] Changelog --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index bd97294d2a5..d473bc3cf8f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,9 +13,12 @@ To be released. `ObjectGraphType` instead of `StringGraphType`. Instead of simply being a hexadecimal representation of `byte[]` encoded `IValue`, now one can choose its representation format. [[#3560]] + - (Libplanet.Explorer) Added `HelperQuery`, a collection of utility like + queries. [[#3561]] [#3559]: https://github.com/planetarium/libplanet/pull/3559 [#3560]: https://github.com/planetarium/libplanet/pull/3560 +[#3561]: https://github.com/planetarium/libplanet/pull/3561 Version 3.9.2