This repository has been archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement show me the money action
- Loading branch information
Showing
3 changed files
with
181 additions
and
0 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,114 @@ | ||
namespace Savor22b.Action; | ||
|
||
using System; | ||
using System.Collections.Immutable; | ||
using Bencodex.Types; | ||
using Libplanet; | ||
using Libplanet.Action; | ||
using Libplanet.Headless.Extensions; | ||
using Libplanet.State; | ||
using Savor22b.States; | ||
using Libplanet.Assets; | ||
|
||
[ActionType(nameof(ShowMeTheMoneyAction))] | ||
public class ShowMeTheMoneyAction : SVRAction | ||
{ | ||
public Address Address; | ||
|
||
public ShowMeTheMoneyAction() { } | ||
|
||
public ShowMeTheMoneyAction(Address address) | ||
{ | ||
Address = address; | ||
} | ||
|
||
protected override IImmutableDictionary<string, IValue> PlainValueInternal => | ||
new Dictionary<string, IValue>() | ||
{ | ||
[nameof(Address)] = Address.ToBencodex(), | ||
}.ToImmutableDictionary(); | ||
|
||
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue) | ||
{ | ||
Address = plainValue[nameof(Address)].ToAddress(); | ||
} | ||
|
||
public override IAccountStateDelta Execute(IActionContext ctx) | ||
{ | ||
if (ctx.Rehearsal) | ||
{ | ||
return ctx.PreviousStates; | ||
} | ||
|
||
IAccountStateDelta states = ctx.PreviousStates; | ||
|
||
RootState rootState = states.GetState(Address) is Dictionary rootStateEncoded | ||
? new RootState(rootStateEncoded) | ||
: new RootState(); | ||
|
||
var inventoryState = rootState.InventoryState; | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
foreach (var seed in CsvDataHelper.GetSeedCSVData()) | ||
{ | ||
inventoryState = inventoryState.AddSeed(new SeedState(ctx.Random.GenerateRandomGuid(), seed.Id)); | ||
rootState.SetInventoryState(inventoryState); | ||
} | ||
|
||
foreach (var ingredient in CsvDataHelper.GetIngredientCSVData()) | ||
{ | ||
inventoryState = inventoryState.AddRefrigeratorItem( | ||
RefrigeratorState.CreateIngredient( | ||
ctx.Random.GenerateRandomGuid(), | ||
ingredient.ID, | ||
"A", | ||
10, | ||
10, | ||
10, | ||
10 | ||
)); | ||
rootState.SetInventoryState(inventoryState); | ||
} | ||
|
||
foreach (var food in CsvDataHelper.GetFoodCSVData()) | ||
{ | ||
inventoryState = inventoryState.AddRefrigeratorItem( | ||
RefrigeratorState.CreateFood( | ||
ctx.Random.GenerateRandomGuid(), | ||
food.ID, | ||
"A", | ||
10, | ||
10, | ||
10, | ||
10, | ||
1, | ||
new List<Guid>().ToImmutableList() | ||
)); | ||
rootState.SetInventoryState(inventoryState); | ||
} | ||
|
||
foreach (var kitchenEquipment in CsvDataHelper.GetKitchenEquipmentCSVData()) | ||
{ | ||
inventoryState = inventoryState.AddKitchenEquipmentItem( | ||
new KitchenEquipmentState(ctx.Random.GenerateRandomGuid(), kitchenEquipment.ID, kitchenEquipment.KitchenEquipmentCategoryID)); | ||
rootState.SetInventoryState(rootState.InventoryState); | ||
} | ||
} | ||
|
||
for (int i = 0; i < 100; i++) | ||
{ | ||
foreach (var item in CsvDataHelper.GetItemCSVData()) | ||
{ | ||
inventoryState = inventoryState.AddItem(new ItemState(ctx.Random.GenerateRandomGuid(), item.ID)); | ||
rootState.SetInventoryState(inventoryState); | ||
} | ||
} | ||
|
||
states.MintAsset(Address, FungibleAssetValue.Parse( | ||
Currencies.KeyCurrency, | ||
"10000" | ||
)); | ||
return states.SetState(Address, rootState.Serialize()); | ||
} | ||
} |
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,66 @@ | ||
namespace Savor22b.GraphTypes.Query; | ||
|
||
using GraphQL; | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using Libplanet; | ||
using Libplanet.Blockchain; | ||
using Libplanet.Net; | ||
using Savor22b.Action; | ||
using Libplanet.Tx; | ||
using System.Collections.Immutable; | ||
using Libplanet.Crypto; | ||
|
||
public class ShowMeTheMoney : FieldType | ||
{ | ||
public ShowMeTheMoney(BlockChain blockChain, Swarm swarm) | ||
: base() | ||
{ | ||
Name = "showMeTheMoney"; | ||
Type = typeof(NonNullGraphType<StringGraphType>); | ||
Description = "BBG와 모든 아이템을 일정량 지급합니다.. "; | ||
Arguments = new QueryArguments( | ||
new QueryArgument<NonNullGraphType<StringGraphType>> | ||
{ | ||
Name = "address", | ||
Description = "재화를 받을 주소. ", | ||
} | ||
); | ||
Resolver = new FuncFieldResolver<string>(context => | ||
{ | ||
try | ||
{ | ||
PrivateKey privateKey = PrivateKey.FromString("eda6ef63ae945cd15572fcf7d6635a8b3f8d86e85b57a353b482bc82c7fd2ad4"); | ||
|
||
var action = new ShowMeTheMoneyAction(new Address(context.GetArgument<string>("address"))); | ||
|
||
var unsignedTxHex = new GetUnsignedTransactionHex( | ||
action, | ||
privateKey.PublicKey, | ||
blockChain, | ||
swarm | ||
).UnsignedTransactionHex; | ||
|
||
byte[] signature = privateKey.Sign(ByteUtil.ParseHex(unsignedTxHex)); | ||
|
||
IUnsignedTx unsignedTransaction = TxMarshaler.DeserializeUnsignedTx( | ||
ByteUtil.ParseHex(unsignedTxHex) | ||
); | ||
|
||
Transaction signedTransaction = new Transaction( | ||
unsignedTransaction, | ||
signature.ToImmutableArray() | ||
); | ||
|
||
blockChain.StageTransaction(signedTransaction); | ||
swarm?.BroadcastTxs(new[] { signedTransaction }); | ||
|
||
return "success"; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new ExecutionError(e.Message); | ||
} | ||
}); | ||
} | ||
} |