instructions.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. class Assignable {
  2. constructor(properties) {
  3. for (const [key, value] of Object.entries(properties)) {
  4. this[key] = value;
  5. }
  6. }
  7. }
  8. // Helper function to pad strings to fixed length buffers
  9. function strToBytes(str: string, length: number): Buffer {
  10. const buffer = Buffer.alloc(length);
  11. buffer.write(str);
  12. return buffer;
  13. }
  14. export enum CreateTokenInstruction {
  15. Create = 0,
  16. }
  17. export class CreateTokenArgs {
  18. instruction: number;
  19. name: Buffer;
  20. symbol: Buffer;
  21. uri: Buffer;
  22. decimals: number;
  23. constructor(name: string, symbol: string, uri: string, decimals: number) {
  24. this.instruction = CreateTokenInstruction.Create;
  25. this.name = strToBytes(name, 32);
  26. this.symbol = strToBytes(symbol, 8);
  27. this.uri = strToBytes(uri, 128);
  28. this.decimals = decimals;
  29. }
  30. toBuffer(): Buffer {
  31. // Added 1 byte for decimals to the total buffer size
  32. const buffer = Buffer.alloc(1 + 32 + 8 + 128 + 1);
  33. let offset = 0;
  34. // Write instruction
  35. buffer.writeUInt8(this.instruction, offset);
  36. offset += 1;
  37. // Write name
  38. this.name.copy(buffer, offset);
  39. offset += 32;
  40. // Write symbol
  41. this.symbol.copy(buffer, offset);
  42. offset += 8;
  43. // Write uri
  44. this.uri.copy(buffer, offset);
  45. offset += 128;
  46. // Write decimals
  47. buffer.writeUInt8(this.decimals, offset);
  48. return buffer;
  49. }
  50. }