Framework.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 IRpcClient AcceleratorClient { get; set; }
  34. public PublicKey WorldPda { get; set; }
  35. public ulong WorldId { get; set; }
  36. public PublicKey RegistryPda { get; set; }
  37. public Wallet.Wallet SecondAuthority { get; set; }
  38. public Wallet.Wallet SessionSigner { get; set; }
  39. public PublicKey SessionEntityPda { get; set; }
  40. public PublicKey SessionComponentPositionPda { get; set; }
  41. public PublicKey Entity1Pda { get; set; }
  42. public PublicKey Entity2Pda { get; set; }
  43. public PublicKey Entity4Pda { get; set; }
  44. public PublicKey AccelerationEntityPda { get; set; }
  45. public PublicKey ExampleComponentPosition { get; set; }
  46. public PublicKey ExampleComponentVelocity { get; set; }
  47. public PublicKey ComponentPositionEntity1Pda { get; set; }
  48. public PublicKey ComponentVelocityEntity1Pda { get; set; }
  49. public PublicKey ComponentPositionEntity2Pda { get; set; }
  50. public PublicKey ComponentPositionEntity4Pda { get; set; }
  51. public PublicKey SystemSimpleMovement { get; set; }
  52. public PublicKey AccelerationComponentPositionPda { get; set; }
  53. public PublicKey SessionToken { get; set; }
  54. public Framework()
  55. {
  56. SecondAuthority = new Wallet.Wallet(new Mnemonic(WordList.English, WordCount.Twelve));
  57. Wallet = new Wallet.Wallet(new Mnemonic(WordList.English, WordCount.Twelve));
  58. SessionSigner = new Wallet.Wallet(new Mnemonic(WordList.English, WordCount.Twelve));
  59. Client = ClientFactory.GetClient("http://localhost:8899");
  60. AcceleratorClient = ClientFactory.GetClient("http://localhost:7799");
  61. ExampleComponentPosition = new PublicKey(Position.Program.PositionProgram.ID);
  62. ExampleComponentVelocity = new PublicKey(Velocity.Program.VelocityProgram.ID);
  63. SystemSimpleMovement = new PublicKey("FSa6qoJXFBR3a7ThQkTAMrC15p6NkchPEjBdd4n6dXxA");
  64. }
  65. public async Task Initialize()
  66. {
  67. await Profiler.Run("RequestAirdrop", async () => {
  68. var result = await Client.RequestAirdropAsync(Wallet.Account.PublicKey, 2000000000);
  69. if (!result.WasSuccessful)
  70. {
  71. throw new Exception(result.Reason);
  72. }
  73. await Client.ConfirmTransaction(result.Result, Commitment.Processed);
  74. });
  75. }
  76. public async Task<string> SendAndConfirmInstruction(IRpcClient client, TransactionInstruction instruction, List<Account>? signers = null, PublicKey? payer = null)
  77. {
  78. if (signers == null) {
  79. signers = new List<Account> { Wallet.Account };
  80. }
  81. var blockHashResponse = await client.GetLatestBlockHashAsync(Commitment.Processed);
  82. if (!blockHashResponse.WasSuccessful || blockHashResponse.Result?.Value?.Blockhash == null)
  83. throw new Exception("Failed to get latest blockhash");
  84. var blockhash = blockHashResponse.Result.Value.Blockhash;
  85. var transaction = new TransactionBuilder()
  86. .SetFeePayer(payer ?? Wallet.Account.PublicKey)
  87. .SetRecentBlockHash(blockhash)
  88. .AddInstruction(instruction)
  89. .Build(signers);
  90. var signature = await client.SendTransactionAsync(transaction, true, Commitment.Processed);
  91. var confirmed = await client.ConfirmTransaction(signature.Result, Commitment.Processed);
  92. if (signature.WasSuccessful && confirmed)
  93. {
  94. return signature.Result;
  95. }
  96. string errorMessage = signature.Reason.ToString();
  97. errorMessage += "\n" + signature.RawRpcResponse;
  98. if (signature.ErrorData != null) {
  99. errorMessage += "\n" + string.Join("\n", signature.ErrorData.Logs);
  100. }
  101. throw new Exception(errorMessage);
  102. }
  103. public async Task<string> SendAndConfirmInstruction(TransactionInstruction instruction, List<Account>? signers = null, PublicKey? payer = null)
  104. {
  105. return await SendAndConfirmInstruction(Client, instruction, signers, payer);
  106. }
  107. public async Task<AccountInfo> GetAccountInfo(IRpcClient client, PublicKey publicKey)
  108. {
  109. var accountInfo = await client.GetAccountInfoAsync(publicKey, Commitment.Processed);
  110. if (accountInfo.WasSuccessful)
  111. {
  112. return accountInfo.Result.Value;
  113. }
  114. throw new Exception(string.Join("\n", accountInfo.ErrorData.Logs));
  115. }
  116. public async Task<AccountInfo> GetAccountInfo(PublicKey publicKey)
  117. {
  118. return await GetAccountInfo(Client, publicKey);
  119. }
  120. }
  121. }