DelegateComponent.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma warning disable CS1591
  2. #pragma warning disable CS1998
  3. using Solana.Unity.Programs;
  4. using Solana.Unity.Rpc;
  5. using Solana.Unity.Rpc.Models;
  6. using Solana.Unity.Rpc.Types;
  7. using Solana.Unity.Wallet;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using World.Program;
  13. namespace Bolt {
  14. public partial class World {
  15. public class DelegateComponentInstruction {
  16. public PublicKey Pda { get; set; }
  17. public TransactionInstruction Instruction { get; set; }
  18. }
  19. public static async Task<DelegateComponentInstruction> DelegateComponent(PublicKey payer, PublicKey entity, PublicKey componentId, string seed = "") {
  20. var componentPda = WorldProgram.FindComponentPda(componentId, entity, seed);
  21. var componentBuffer = WorldProgram.FindBufferPda(componentPda);
  22. var componentDelegationRecord = WorldProgram.FindDelegationProgramPda("delegation", componentPda);
  23. var componentDelegationMetadata = WorldProgram.FindDelegationProgramPda("delegation-metadata", componentPda);
  24. var worldProgram = new PublicKey(WorldProgram.ID);
  25. var bufferDelegationRecord = WorldProgram.FindDelegationProgramPda("delegation", componentBuffer);
  26. var bufferDelegationMetadata = WorldProgram.FindDelegationProgramPda("delegation-metadata", componentBuffer);
  27. var bufferBuffer = WorldProgram.FindBufferPda(componentBuffer, worldProgram);
  28. byte[] discriminator = new byte[] { 191, 212, 179, 193, 178, 94, 119, 93 };
  29. uint commitFrequencyMs = 0;
  30. byte[] commitFrequencyBytes = BitConverter.GetBytes(commitFrequencyMs);
  31. if (!BitConverter.IsLittleEndian) Array.Reverse(commitFrequencyBytes);
  32. var validator = new byte[1];
  33. validator[0] = 0;
  34. var data = new byte[discriminator.Length + commitFrequencyBytes.Length + validator.Length];
  35. Array.Copy(discriminator, data, discriminator.Length);
  36. Array.Copy(commitFrequencyBytes, 0, data, discriminator.Length, commitFrequencyBytes.Length);
  37. Array.Copy(validator, 0, data, discriminator.Length + commitFrequencyBytes.Length, validator.Length);
  38. TransactionInstruction instruction = new TransactionInstruction() {
  39. ProgramId = new PublicKey(WorldProgram.ID),
  40. Keys = new List<AccountMeta>() {
  41. AccountMeta.Writable(payer, true),
  42. AccountMeta.Writable(componentPda, false),
  43. AccountMeta.Writable(componentBuffer, false),
  44. AccountMeta.ReadOnly(componentId, false),
  45. AccountMeta.Writable(WorldProgram.FindBufferPda(componentPda, componentId), false),
  46. AccountMeta.Writable(componentDelegationRecord, false),
  47. AccountMeta.Writable(componentDelegationMetadata, false),
  48. AccountMeta.ReadOnly(WorldProgram.DelegationProgram, false),
  49. AccountMeta.ReadOnly(SystemProgram.ProgramIdKey, false),
  50. AccountMeta.ReadOnly(entity, false),
  51. AccountMeta.ReadOnly(worldProgram, false),
  52. AccountMeta.Writable(bufferBuffer, false),
  53. AccountMeta.Writable(bufferDelegationRecord, false),
  54. AccountMeta.Writable(bufferDelegationMetadata, false),
  55. },
  56. Data = data,
  57. };
  58. return new DelegateComponentInstruction() {
  59. Pda = componentPda,
  60. Instruction = instruction,
  61. };
  62. }
  63. }
  64. }