Framework.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Solana.Unity.Wallet;
  3. using Solana.Unity.Bolt;
  4. using Solana.Unity.Rpc;
  5. using Solana.Unity.Rpc.Models;
  6. using System;
  7. using System.Threading.Tasks;
  8. using Solana.Unity.Wallet.Bip39;
  9. using World.Program;
  10. using Solana.Unity.Programs;
  11. using Solana.Unity.Rpc.Builders;
  12. using System.Threading.Tasks.Dataflow;
  13. using System.Collections.Generic;
  14. namespace Solana.Unity.Bolt.Test
  15. {
  16. public enum Direction
  17. {
  18. Left,
  19. Right,
  20. Up,
  21. Down
  22. }
  23. public class Framework
  24. {
  25. public Wallet.Wallet Wallet { get; set; }
  26. public IRpcClient Client { get; set; }
  27. public PublicKey WorldPda { get; set; }
  28. public ulong WorldId { get; set; }
  29. public PublicKey RegistryPda { get; set; }
  30. public Wallet.Wallet SecondAuthority { get; set; }
  31. public Wallet.Wallet SessionSigner { get; set; }
  32. public PublicKey SessionEntityPda { get; set; }
  33. public PublicKey SessionComponentPositionPda { get; set; }
  34. public PublicKey Entity1Pda { get; set; }
  35. public PublicKey Entity2Pda { get; set; }
  36. public PublicKey Entity4Pda { get; set; }
  37. public PublicKey ExampleComponentPosition { get; set; }
  38. public PublicKey ExampleComponentVelocity { get; set; }
  39. public PublicKey ComponentPositionEntity1Pda { get; set; }
  40. public PublicKey ComponentVelocityEntity1Pda { get; set; }
  41. public PublicKey ComponentPositionEntity2Pda { get; set; }
  42. public PublicKey ComponentPositionEntity4Pda { get; set; }
  43. public PublicKey SystemSimpleMovement { get; set; }
  44. public PublicKey SessionToken { get; set; }
  45. public Framework()
  46. {
  47. SecondAuthority = new Wallet.Wallet(new Mnemonic(WordList.English, WordCount.Twelve));
  48. Wallet = new Wallet.Wallet(new Mnemonic(WordList.English, WordCount.Twelve));
  49. SessionSigner = new Wallet.Wallet(new Mnemonic(WordList.English, WordCount.Twelve));
  50. Client = ClientFactory.GetClient("http://localhost:8899");
  51. ExampleComponentPosition = new PublicKey(Position.Program.PositionProgram.ID);
  52. ExampleComponentVelocity = new PublicKey(Velocity.Program.VelocityProgram.ID);
  53. SystemSimpleMovement = new PublicKey("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");
  54. }
  55. public async Task Initialize()
  56. {
  57. var result = await Client.RequestAirdropAsync(Wallet.Account.PublicKey, 2000000000);
  58. if (!result.WasSuccessful)
  59. {
  60. throw new Exception(result.Reason);
  61. }
  62. await Client.ConfirmTransaction(result.Result);
  63. }
  64. public async Task<string> SendAndConfirmInstruction(TransactionInstruction instruction, List<Account> signers = null, PublicKey payer = null)
  65. {
  66. if (signers == null) {
  67. signers = new List<Account> { Wallet.Account };
  68. }
  69. var blockHashResponse = await Client.GetLatestBlockHashAsync();
  70. if (!blockHashResponse.WasSuccessful || blockHashResponse.Result?.Value?.Blockhash == null)
  71. throw new Exception("Failed to get latest blockhash");
  72. var blockhash = blockHashResponse.Result.Value.Blockhash;
  73. var transaction = new TransactionBuilder()
  74. .SetFeePayer(payer ?? Wallet.Account.PublicKey)
  75. .SetRecentBlockHash(blockhash)
  76. .AddInstruction(instruction)
  77. .Build(signers);
  78. var signature = await Client.SendAndConfirmTransactionAsync(transaction);
  79. if (signature.WasSuccessful)
  80. {
  81. return signature.Result;
  82. }
  83. string errorMessage = signature.Reason.ToString();
  84. errorMessage += "\n" + signature.RawRpcResponse;
  85. if (signature.ErrorData != null) {
  86. errorMessage += "\n" + string.Join("\n", signature.ErrorData.Logs);
  87. }
  88. throw new Exception(errorMessage);
  89. }
  90. public async Task<AccountInfo> GetAccountInfo(PublicKey publicKey)
  91. {
  92. var accountInfo = await Client.GetAccountInfoAsync(publicKey);
  93. if (accountInfo.WasSuccessful)
  94. {
  95. return accountInfo.Result.Value;
  96. }
  97. throw new Exception(string.Join("\n", accountInfo.ErrorData.Logs));
  98. }
  99. }
  100. }