instructions.ts 1.6 KB

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