-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,483 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"strawberryshake.tools": { | ||
"version": "13.8.1", | ||
"commands": [ | ||
"dotnet-graphql" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"schema": "schema.graphql", | ||
"documents": "**/*.graphql", | ||
"extensions": { | ||
"strawberryShake": { | ||
"name": "HeadlessGQLClient", | ||
"namespace": "HeadlessGQL", | ||
"url": "https://9c-main-full-state.nine-chronicles.com/graphql", | ||
"records": { | ||
"inputs": false, | ||
"entities": false | ||
}, | ||
"transportProfiles": [ | ||
{ | ||
"default": "Http", | ||
"subscription": "WebSocket" | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
query GetTip { | ||
nodeStatus { | ||
tip { | ||
index | ||
} | ||
} | ||
} | ||
|
||
query GetState($address: Address!) { | ||
state(address: $address) | ||
} |
45 changes: 18 additions & 27 deletions
45
NineChroniclesUtilBackend/Services/HeadlessStateService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,40 @@ | ||
using System.Text; | ||
using System.Text.Json; | ||
using Bencodex; | ||
using Bencodex.Types; | ||
using HeadlessGQL; | ||
using Libplanet.Crypto; | ||
using Microsoft.Extensions.Options; | ||
using NineChroniclesUtilBackend.Options; | ||
using Libplanet.Types.Blocks; | ||
using StrawberryShake; | ||
|
||
namespace NineChroniclesUtilBackend.Services; | ||
|
||
public class HeadlessStateService(IOptions<HeadlessStateServiceOption> options) : IStateService | ||
public class HeadlessStateService(IHeadlessGQLClient client) : IStateService | ||
{ | ||
private readonly Uri _headlessEndpoint = options.Value.HeadlessEndpoint; | ||
private readonly HttpClient _httpClient = new(); | ||
|
||
private static readonly Codec Codec = new(); | ||
|
||
private (long, BlockHash) TipInfo { get; set; } | ||
|
||
public long TipIndex => TipInfo.Item1; | ||
|
||
public Task<IValue?[]> GetStates(Address[] addresses) | ||
{ | ||
return Task.WhenAll(addresses.Select(GetState)); | ||
} | ||
|
||
public async Task<IValue?> GetState(Address address) | ||
{ | ||
using StringContent jsonContent = new | ||
(JsonSerializer.Serialize(new | ||
{ | ||
query = @"query GetState($address: Address!) { state(address: $address) }", | ||
variables = new { | ||
address = address.ToString(), | ||
}, | ||
operationName = "GetState", | ||
}), | ||
Encoding.UTF8, | ||
"application/json"); | ||
using var response = await _httpClient.PostAsync(_headlessEndpoint, jsonContent); | ||
var resp = await response.Content.ReadFromJsonAsync<GetStateResponse>(); | ||
|
||
if (resp.Data.State is null) | ||
var result = await client.GetState.ExecuteAsync(address.ToString()); | ||
result.EnsureNoErrors(); | ||
|
||
if (result.Data?.State is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return Codec.Decode(Convert.FromHexString(resp.Data.State)); | ||
return Codec.Decode(Convert.FromHexString(result.Data.State)); | ||
} | ||
} | ||
|
||
record GetStateResponse(GetStateResponseData Data, object Errors); | ||
record GetStateResponseData(string? State); | ||
private static void UpdateTipIndex() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
scalar _KeyFieldSet | ||
|
||
directive @key(fields: _KeyFieldSet!) on SCHEMA | OBJECT | ||
|
||
directive @serializationType(name: String!) on SCALAR | ||
|
||
directive @runtimeType(name: String!) on SCALAR | ||
|
||
directive @enumValue(value: String!) on ENUM_VALUE | ||
|
||
directive @rename(name: String!) on INPUT_FIELD_DEFINITION | INPUT_OBJECT | ENUM | ENUM_VALUE | ||
|
||
extend schema @key(fields: "id") |
Oops, something went wrong.