123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { BN } from 'bn.js';
- class Assignable {
- constructor(properties) {
- for (const [key, value] of Object.entries(properties)) {
- this[key] = value;
- }
- }
- }
- // Helper function to pad strings to fixed length buffers
- function strToBytes(str: string, length: number): Buffer {
- const buffer = Buffer.alloc(length);
- buffer.write(str);
- return buffer;
- }
- export enum TransferTokensInstruction {
- Create = 0,
- Mint = 1,
- Transfer = 2,
- }
- export class CreateTokenArgs {
- instruction: number;
- name: Buffer;
- symbol: Buffer;
- uri: Buffer;
- decimals: number;
- constructor(name: string, symbol: string, uri: string, decimals: number) {
- this.instruction = TransferTokensInstruction.Create;
- this.name = strToBytes(name, 32);
- this.symbol = strToBytes(symbol, 8);
- this.uri = strToBytes(uri, 64);
- this.decimals = decimals;
- }
- toBuffer(): Buffer {
- // Added 1 byte for decimals to the total buffer size
- const buffer = Buffer.alloc(1 + 32 + 8 + 64 + 1);
- let offset = 0;
- // Write instruction
- buffer.writeUInt8(this.instruction, offset);
- offset += 1;
- // Write name
- this.name.copy(buffer, offset);
- offset += 32;
- // Write symbol
- this.symbol.copy(buffer, offset);
- offset += 8;
- // Write uri
- this.uri.copy(buffer, offset);
- offset += 64;
- // Write decimals
- buffer.writeUInt8(this.decimals, offset);
- return buffer;
- }
- }
- export class MintToArgs {
- instruction: number;
- quantity: BN;
- constructor(quantity: number) {
- this.instruction = TransferTokensInstruction.Mint;
- this.quantity = new BN(quantity);
- }
- toBuffer(): Buffer {
- const buffer = Buffer.alloc(9); // 1 byte for instruction + 8 bytes for u64 quantity
- let offset = 0;
- // Write instruction
- buffer.writeUInt8(this.instruction, offset);
- offset += 1;
- // Write quantity as u64 LE (8 bytes)
- this.quantity.toBuffer('le', 8).copy(buffer, offset);
- return buffer;
- }
- }
- export class TransferTokensArgs {
- instruction: number;
- quantity: BN;
- constructor(quantity: number) {
- this.instruction = TransferTokensInstruction.Transfer;
- this.quantity = new BN(quantity);
- }
- toBuffer(): Buffer {
- const buffer = Buffer.alloc(9); // 1 byte for instruction + 8 bytes for u64 quantity
- let offset = 0;
- // Write instruction
- buffer.writeUInt8(this.instruction, offset);
- offset += 1;
- // Write quantity as u64 LE (8 bytes)
- this.quantity.toBuffer('le', 8).copy(buffer, offset);
- return buffer;
- }
- }
|