From db7c24f679e92ebfbf15b6c5bbcb383751776bfc Mon Sep 17 00:00:00 2001 From: s2quake Date: Mon, 6 Jan 2025 15:44:40 +0900 Subject: [PATCH] feat: Add state query for delegator --- .../GraphTypes/DelegatorType.cs | 31 ++++++++++++++++ .../GraphTypes/StateQuery.cs | 37 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 NineChronicles.Headless/GraphTypes/DelegatorType.cs diff --git a/NineChronicles.Headless/GraphTypes/DelegatorType.cs b/NineChronicles.Headless/GraphTypes/DelegatorType.cs new file mode 100644 index 000000000..b5b7c6db5 --- /dev/null +++ b/NineChronicles.Headless/GraphTypes/DelegatorType.cs @@ -0,0 +1,31 @@ +using System.Numerics; +using GraphQL.Types; +using Libplanet.Types.Assets; +using Nekoyume.ValidatorDelegation; + +namespace NineChronicles.Headless.GraphTypes; + +public class DelegatorType : ObjectGraphType +{ + public long LastDistributeHeight { get; set; } + + public BigInteger Share { get; set; } + + public FungibleAssetValue Fav { get; set; } + + public DelegatorType() + { + Field>( + nameof(LastDistributeHeight), + description: "LastDistributeHeight of delegator", + resolve: context => context.Source.LastDistributeHeight); + Field>( + nameof(Share), + description: "Share of delegator", + resolve: context => context.Source.Share.ToString("N0")); + Field>( + nameof(Fav), + description: "Delegated FAV calculated based on Share value", + resolve: context => context.Source.Fav); + } +} diff --git a/NineChronicles.Headless/GraphTypes/StateQuery.cs b/NineChronicles.Headless/GraphTypes/StateQuery.cs index d1f526e9d..bd662f16d 100644 --- a/NineChronicles.Headless/GraphTypes/StateQuery.cs +++ b/NineChronicles.Headless/GraphTypes/StateQuery.cs @@ -790,6 +790,43 @@ public StateQuery() return ValidatorType.FromDelegatee(delegatee); } ); + + Field( + name: "delegator", + description: "State for delegator.", + arguments: new QueryArguments( + new QueryArgument> + { + Name = "agentAddress", + Description = "Address of agent." + } + ), + resolve: context => + { + var address = context.GetArgument
("agentAddress"); + var agentAddress = new AgentAddress(address); + var repository = new GuildRepository(new World(context.Source.WorldState), new HallowActionContext { }); + if (repository.TryGetGuildParticipant(agentAddress, out var guildParticipant)) + { + var guild = repository.GetGuild(guildParticipant.GuildAddress); + var delegatee = repository.GetDelegatee(guild.ValidatorAddress); + var bond = repository.GetBond(delegatee, guildParticipant.Address); + var totalDelegated = delegatee.Metadata.TotalDelegatedFAV; + var totalShare = delegatee.Metadata.TotalShares; + var lastDistributeHeight = bond.LastDistributeHeight ?? -1; + var share = bond.Share; + var fav = (share * totalDelegated).DivRem(totalShare).Quotient; + return new DelegatorType + { + LastDistributeHeight = lastDistributeHeight, + Share = share, + Fav = fav, + }; + } + + return null; + } + ); } public static List GetRuneOptions(