Framework.cs 4.8 KB

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