instructions.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 TransferTokensInstruction {
  16. Create = 0,
  17. Mint = 1,
  18. Transfer = 2,
  19. }
  20. export class CreateTokenArgs {
  21. instruction: number;
  22. name: Buffer;
  23. symbol: Buffer;
  24. uri: Buffer;
  25. decimals: number;
  26. constructor(name: string, symbol: string, uri: string, decimals: number) {
  27. this.instruction = TransferTokensInstruction.Create;
  28. this.name = strToBytes(name, 32);
  29. this.symbol = strToBytes(symbol, 8);
  30. this.uri = strToBytes(uri, 64);
  31. this.decimals = decimals;
  32. }
  33. toBuffer(): Buffer {
  34. // Added 1 byte for decimals to the total buffer size
  35. const buffer = Buffer.alloc(1 + 32 + 8 + 64 + 1);
  36. let offset = 0;
  37. // Write instruction
  38. buffer.writeUInt8(this.instruction, offset);
  39. offset += 1;
  40. // Write name
  41. this.name.copy(buffer, offset);
  42. offset += 32;
  43. // Write symbol
  44. this.symbol.copy(buffer, offset);
  45. offset += 8;
  46. // Write uri
  47. this.uri.copy(buffer, offset);
  48. offset += 64;
  49. // Write decimals
  50. buffer.writeUInt8(this.decimals, offset);
  51. return buffer;
  52. }
  53. }
  54. export class MintToArgs {
  55. instruction: number;
  56. quantity: BN;
  57. constructor(quantity: number) {
  58. this.instruction = TransferTokensInstruction.Mint;
  59. this.quantity = new BN(quantity);
  60. }
  61. toBuffer(): Buffer {
  62. const buffer = Buffer.alloc(9); // 1 byte for instruction + 8 bytes for u64 quantity
  63. let offset = 0;
  64. // Write instruction
  65. buffer.writeUInt8(this.instruction, offset);
  66. offset += 1;
  67. // Write quantity as u64 LE (8 bytes)
  68. this.quantity.toBuffer('le', 8).copy(buffer, offset);
  69. return buffer;
  70. }
  71. }
  72. export class TransferTokensArgs {
  73. instruction: number;
  74. quantity: BN;
  75. constructor(quantity: number) {
  76. this.instruction = TransferTokensInstruction.Transfer;
  77. this.quantity = new BN(quantity);
  78. }
  79. toBuffer(): Buffer {
  80. const buffer = Buffer.alloc(9); // 1 byte for instruction + 8 bytes for u64 quantity
  81. let offset = 0;
  82. // Write instruction
  83. buffer.writeUInt8(this.instruction, offset);
  84. offset += 1;
  85. // Write quantity as u64 LE (8 bytes)
  86. this.quantity.toBuffer('le', 8).copy(buffer, offset);
  87. return buffer;
  88. }
  89. }