create.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { Buffer } from 'node:buffer';
  2. import { type PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
  3. import * as borsh from 'borsh';
  4. import { MyInstruction } from '.';
  5. export class Create {
  6. instruction: MyInstruction;
  7. name: string;
  8. constructor(props: { instruction: MyInstruction; name: string }) {
  9. this.instruction = props.instruction;
  10. this.name = props.name;
  11. }
  12. toBuffer() {
  13. return Buffer.from(borsh.serialize(CreateSchema, this));
  14. }
  15. static fromBuffer(buffer: Buffer) {
  16. return borsh.deserialize(CreateSchema, Create, buffer);
  17. }
  18. }
  19. export const CreateSchema = new Map([
  20. [
  21. Create,
  22. {
  23. kind: 'struct',
  24. fields: [
  25. ['instruction', 'u8'],
  26. ['name', 'string'],
  27. ],
  28. },
  29. ],
  30. ]);
  31. export function createCreateUserInstruction(target: PublicKey, payer: PublicKey, programId: PublicKey, name: string): TransactionInstruction {
  32. const instructionObject = new Create({
  33. instruction: MyInstruction.CreateUser,
  34. name,
  35. });
  36. const ix = new TransactionInstruction({
  37. keys: [
  38. { pubkey: target, isSigner: false, isWritable: true },
  39. { pubkey: payer, isSigner: true, isWritable: true },
  40. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  41. ],
  42. programId: programId,
  43. data: instructionObject.toBuffer(),
  44. });
  45. return ix;
  46. }