instructions.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { BN } from 'bn.js';
  2. class Assignable {
  3. constructor(properties) {
  4. for (const [key, value] of Object.entries(properties)) {
  5. this[key] = value;
  6. }
  7. }
  8. }
  9. // Helper function to pad strings to fixed length buffers
  10. function strToBytes(str: string, length: number): Buffer {
  11. const buffer = Buffer.alloc(length);
  12. buffer.write(str);
  13. return buffer;
  14. }
  15. export enum PDAMintAuthorityInstruction {
  16. Init = 0,
  17. Create = 1,
  18. Mint = 2,
  19. }
  20. export class InitArgs {
  21. instruction: number;
  22. constructor() {
  23. this.instruction = PDAMintAuthorityInstruction.Init;
  24. }
  25. toBuffer(): Buffer {
  26. // Only need 1 byte for the instruction as there are no other fields
  27. const buffer = Buffer.alloc(1);
  28. buffer.writeUInt8(this.instruction, 0);
  29. return buffer;
  30. }
  31. }
  32. export class CreateTokenArgs {
  33. instruction: number;
  34. name: Buffer;
  35. symbol: Buffer;
  36. uri: Buffer;
  37. constructor(name: string, symbol: string, uri: string) {
  38. this.instruction = PDAMintAuthorityInstruction.Create;
  39. this.name = strToBytes(name, 32);
  40. this.symbol = strToBytes(symbol, 8);
  41. this.uri = strToBytes(uri, 64);
  42. }
  43. toBuffer(): Buffer {
  44. const buffer = Buffer.alloc(1 + 32 + 8 + 64);
  45. let offset = 0;
  46. buffer.writeUInt8(this.instruction, offset);
  47. offset += 1;
  48. this.name.copy(buffer, offset);
  49. offset += 32;
  50. this.symbol.copy(buffer, offset);
  51. offset += 8;
  52. this.uri.copy(buffer, offset);
  53. return buffer;
  54. }
  55. }
  56. export class MintToArgs {
  57. instruction: number;
  58. quantity: BN;
  59. constructor(quantity: number) {
  60. this.instruction = PDAMintAuthorityInstruction.Mint;
  61. this.quantity = new BN(quantity);
  62. }
  63. toBuffer(): Buffer {
  64. const buffer = Buffer.alloc(9); // 1 byte for instruction + 8 bytes for u64 quantity
  65. let offset = 0;
  66. // Write instruction
  67. buffer.writeUInt8(this.instruction, offset);
  68. offset += 1;
  69. // Write quantity as u64 LE (8 bytes)
  70. this.quantity.toBuffer('le', 8).copy(buffer, offset);
  71. return buffer;
  72. }
  73. }