index.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { PublicKey } from "@solana/web3.js";
  2. import BN from "bn.js";
  3. export * from "./accounts";
  4. export * from "./instructions";
  5. export * from "./transactions/transactions";
  6. /**
  7. * Program address
  8. *
  9. * @category constants
  10. * @category generated
  11. */
  12. export const PROGRAM_ADDRESS = "WorLD15A7CrDwLcLy4fRqtaTb9fbd8o8iqiEMUDse2n";
  13. export const SYSVAR_INSTRUCTIONS_PUBKEY = new PublicKey(
  14. "Sysvar1nstructions1111111111111111111111111"
  15. );
  16. /**
  17. * Program public key
  18. *
  19. * @category constants
  20. * @category generated
  21. */
  22. export const PROGRAM_ID = new PublicKey(PROGRAM_ADDRESS);
  23. export function FindWorldRegistryPda(
  24. programId: PublicKey = new PublicKey(PROGRAM_ID)
  25. ) {
  26. return PublicKey.findProgramAddressSync(
  27. [Buffer.from("registry")],
  28. programId
  29. )[0];
  30. }
  31. export function FindWorldPda(
  32. id: BN | string | number | Uint8Array,
  33. programId: PublicKey = new PublicKey(PROGRAM_ID)
  34. ) {
  35. id = CastToBN(id);
  36. const idBuffer = Buffer.from(id.toArrayLike(Buffer, "be", 8));
  37. return PublicKey.findProgramAddressSync(
  38. [Buffer.from("world"), idBuffer],
  39. programId
  40. )[0];
  41. }
  42. export function FindEntityPda(
  43. worldId: BN | string | number | Uint8Array,
  44. entityId: BN | string | number | Uint8Array,
  45. extraSeed?: string,
  46. programId: PublicKey = new PublicKey(PROGRAM_ID)
  47. ) {
  48. worldId = CastToBN(worldId);
  49. entityId = CastToBN(entityId);
  50. const worldIdBuffer = Buffer.from(worldId.toArrayLike(Buffer, "be", 8));
  51. const entityIdBuffer = Buffer.from(entityId.toArrayLike(Buffer, "be", 8));
  52. const seeds = [Buffer.from("entity"), worldIdBuffer];
  53. if (extraSeed != null) {
  54. seeds.push(Buffer.from(new Uint8Array(8)));
  55. seeds.push(Buffer.from(extraSeed));
  56. } else {
  57. seeds.push(entityIdBuffer);
  58. }
  59. return PublicKey.findProgramAddressSync(seeds, programId)[0];
  60. }
  61. export function FindComponentPda(
  62. componentProgramId: PublicKey,
  63. entity: PublicKey,
  64. componentId: string = ""
  65. ) {
  66. return PublicKey.findProgramAddressSync(
  67. [Buffer.from(componentId), entity.toBytes()],
  68. componentProgramId
  69. )[0];
  70. }
  71. function CastToBN(id: BN | string | number | Uint8Array) {
  72. if (!(id instanceof BN)) {
  73. id = new BN(id);
  74. }
  75. return id;
  76. }
  77. /**
  78. * Serialize arguments to a buffer
  79. * @param args
  80. * @constructor
  81. */
  82. export function SerializeArgs(args: any = {}) {
  83. const jsonString = JSON.stringify(args);
  84. const encoder = new TextEncoder();
  85. const binaryData = encoder.encode(jsonString);
  86. return Buffer.from(
  87. binaryData.buffer,
  88. binaryData.byteOffset,
  89. binaryData.byteLength
  90. );
  91. }