Skip to content

Commit

Permalink
feat: Add action mutations for undelegation and reward claiming
Browse files Browse the repository at this point in the history
  • Loading branch information
s2quake committed Dec 2, 2024
1 parent 1c006a6 commit f69e1b7
Showing 1 changed file with 75 additions and 4 deletions.
79 changes: 75 additions & 4 deletions NineChronicles.Headless/GraphTypes/ActionMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,12 @@ public ActionMutation(NineChroniclesNodeService service)
);

Field<NonNullGraphType<TxIdType>>("stake",
description: "Claim reward for self delegation of validator.",
description: "Stake the NCG.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<IntGraphType>>
{
Name = "amount",
Description = "Amount to stake."
Description = "Total amount of NCG to stake."
}
),
resolve: context =>
Expand Down Expand Up @@ -731,12 +731,12 @@ public ActionMutation(NineChroniclesNodeService service)
}
);
Field<NonNullGraphType<TxIdType>>("delegateValidator",
description: "Unjail validator",
description: "Delegate Validator",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<BigIntGraphType>>
{
Name = "amount",
Description = "Amount of guild gold to stake."
Description = "Amount of guild gold to delegate."
}
),
resolve: context =>
Expand Down Expand Up @@ -767,6 +767,77 @@ public ActionMutation(NineChroniclesNodeService service)
}
}
);
Field<NonNullGraphType<TxIdType>>("undelegateValidator",
description: "Undelegate validator",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<BigIntGraphType>>
{
Name = "share",
Description = "Share to undelegate."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

BigInteger share = context.GetArgument<BigInteger>("amount");
var actions = new[] { new UndelegateValidator(share) };
Transaction tx = blockChain.MakeTransaction(
service.MinerPrivateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
{
var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
context.Errors.Add(new ExecutionError(msg, e));
throw;
}
}
);
Field<NonNullGraphType<TxIdType>>("claimStakeReward",
description: "Claim Stake Reward",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AddressType>>
{
Name = "avatarAddress",
Description = "Avatar address to claim stake reward."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

var actions = new[] { new ClaimStakeReward() };
Transaction tx = blockChain.MakeTransaction(
service.MinerPrivateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
{
var msg = $"Unexpected exception occurred during {typeof(ActionMutation)}: {e}";
context.Errors.Add(new ExecutionError(msg, e));
throw;
}
}
);
}
}
}

0 comments on commit f69e1b7

Please sign in to comment.