Skip to content

Commit

Permalink
feat: Add state query for delegator
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Jan 6, 2025
1 parent d8d5a21 commit db7c24f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
31 changes: 31 additions & 0 deletions NineChronicles.Headless/GraphTypes/DelegatorType.cs
Original file line number Diff line number Diff line change
@@ -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<DelegatorType>
{
public long LastDistributeHeight { get; set; }

public BigInteger Share { get; set; }

public FungibleAssetValue Fav { get; set; }

public DelegatorType()
{
Field<NonNullGraphType<LongGraphType>>(
nameof(LastDistributeHeight),
description: "LastDistributeHeight of delegator",
resolve: context => context.Source.LastDistributeHeight);
Field<NonNullGraphType<StringGraphType>>(
nameof(Share),
description: "Share of delegator",
resolve: context => context.Source.Share.ToString("N0"));
Field<NonNullGraphType<FungibleAssetValueType>>(
nameof(Fav),
description: "Delegated FAV calculated based on Share value",
resolve: context => context.Source.Fav);
}
}
37 changes: 37 additions & 0 deletions NineChronicles.Headless/GraphTypes/StateQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,43 @@ public StateQuery()
return ValidatorType.FromDelegatee(delegatee);
}
);

Field<DelegatorType>(
name: "delegator",
description: "State for delegator.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AddressType>>
{
Name = "agentAddress",
Description = "Address of agent."
}
),
resolve: context =>
{
var address = context.GetArgument<Address>("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<RuneOptionSheet.Row.RuneOptionInfo> GetRuneOptions(
Expand Down

0 comments on commit db7c24f

Please sign in to comment.