Skip to content

Commit

Permalink
feat: add dpos graphql query and mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Jul 4, 2024
1 parent 7c9b51b commit 1064aae
Show file tree
Hide file tree
Showing 2 changed files with 350 additions and 9 deletions.
311 changes: 302 additions & 9 deletions NineChronicles.Headless/GraphTypes/ActionMutation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
using Libplanet.Explorer.GraphTypes;
using Libplanet.Types.Tx;
using Nekoyume.Action;
using Nekoyume.Model.State;
using Serilog;
using System;
using System.Collections.Generic;
using Nekoyume.Module;
using System.Numerics;
using Lib9c;
using Libplanet.Types.Assets;
using Nekoyume.Action.DPoS;
using Nekoyume.Action.DPoS.Misc;
using Nekoyume.Action.DPoS.Model;

namespace NineChronicles.Headless.GraphTypes
{
Expand Down Expand Up @@ -82,7 +86,11 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(privateKey, actions);
Transaction tx = blockChain.MakeTransaction(
privateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down Expand Up @@ -165,7 +173,11 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(service.MinerPrivateKey, actions);
Transaction tx = blockChain.MakeTransaction(
service.MinerPrivateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down Expand Up @@ -225,7 +237,11 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(service.MinerPrivateKey, actions);
Transaction tx = blockChain.MakeTransaction(
service.MinerPrivateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down Expand Up @@ -289,7 +305,11 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(privatekey, actions);
Transaction tx = blockChain.MakeTransaction(
privatekey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down Expand Up @@ -332,7 +352,11 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(privateKey, actions);
Transaction tx = blockChain.MakeTransaction(
privateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down Expand Up @@ -374,7 +398,11 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(privateKey, actions);
Transaction tx = blockChain.MakeTransaction(
privateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down Expand Up @@ -427,7 +455,272 @@ public ActionMutation(NineChroniclesNodeService service)
};

var actions = new ActionBase[] { action };
Transaction tx = blockChain.MakeTransaction(service.MinerPrivateKey, actions);
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>>("promoteValidator",
description: "Promote validator.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<StringGraphType>>
{
Name = "validator",
Description = "Validator public key to promote."
},
new QueryArgument<NonNullGraphType<BigIntGraphType>>
{
Name = "amount",
Description = "Amount of NCG to stake."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

string validatorString = context.GetArgument<string>("validator");
PublicKey validator = PublicKey.FromHex(validatorString);
BigInteger amount = context.GetArgument<BigInteger>("amount");
var fav = new FungibleAssetValue(Asset.GovernanceToken, amount, 0);

var action = new PromoteValidator(validator, fav);

var actions = new[] { action };
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>>("delegate",
description: "A action for DPoS that delegate specified amount of tokens " +
"to a given validator.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AddressType>>
{
Name = "validator",
Description = "Validator address to delegate."
},
new QueryArgument<NonNullGraphType<BigIntGraphType>>
{
Name = "amount",
Description = "Amount of governance token to delegate."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

Address validator = context.GetArgument<Address>("validator");
BigInteger amount = context.GetArgument<BigInteger>("amount");
var fav = new FungibleAssetValue(Asset.GovernanceToken, amount, 0);

var action = new Nekoyume.Action.DPoS.Delegate(validator, fav);

var actions = new[] { action };
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>>("undelegate",
description: "A action for DPoS that cancels delegate " +
"specified amount of shared tokens to a given validator.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AddressType>>
{
Name = "validator",
Description = "Validator address to undelegate."
},
new QueryArgument<NonNullGraphType<BigIntGraphType>>
{
Name = "amount",
Description = "Amount of share to undelegate."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

Address validator = context.GetArgument<Address>("validator");
BigInteger amount = context.GetArgument<BigInteger>("amount");
var fav = new FungibleAssetValue(Asset.Share, amount, 0);

var action = new Undelegate(validator, fav);

var actions = new[] { action };
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>>("cancelUndelegation",
description: "A action for DPoS that cancel undelegate " +
"specified amount of tokens to a given validator.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AddressType>>
{
Name = "validator",
Description = "Validator address to undelegate."
},
new QueryArgument<NonNullGraphType<BigIntGraphType>>
{
Name = "amount",
Description = "Amount of consensus token to undelegate."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

Address validator = context.GetArgument<Address>("validator");
BigInteger amount = context.GetArgument<BigInteger>("amount");
var fav = new FungibleAssetValue(Asset.ConsensusToken, amount, 0);

var action = new CancelUndelegation(validator, fav);

var actions = new[] { action };
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>>("withdrawDelegator",
description: "Withdraw reward for delegator.",
arguments: new QueryArguments(
new QueryArgument<NonNullGraphType<AddressType>>
{
Name = "address",
Description = "Address to withdraw reward."
}
),
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

Address address = context.GetArgument<Address>("address");
var action = new WithdrawDelegator(Validator.DeriveAddress(address));

var actions = new[] { action };
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>>("withdrawValidator",
description: "Withdraw reward for validator.",
resolve: context =>
{
try
{
BlockChain? blockChain = service.BlockChain;
if (blockChain is null)
{
throw new InvalidOperationException($"{nameof(blockChain)} is null.");
}

var action = new WithdrawValidator();
var actions = new[] { action };
Transaction tx = blockChain.MakeTransaction(
service.MinerPrivateKey,
actions,
Currencies.Mead * 1,
1L);
return tx.Id;
}
catch (Exception e)
Expand Down
Loading

0 comments on commit 1064aae

Please sign in to comment.