close.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { Buffer } from 'node:buffer';
  2. import { type PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
  3. import { closeAccountSchema, MyInstruction } from '.';
  4. export class Close {
  5. instruction: MyInstruction;
  6. constructor(props: { instruction: MyInstruction }) {
  7. this.instruction = props.instruction;
  8. }
  9. toBuffer() {
  10. const buffer = Buffer.alloc(1000);
  11. closeAccountSchema.encode(
  12. {
  13. CloseUser: '',
  14. },
  15. buffer,
  16. );
  17. return buffer.subarray(0, closeAccountSchema.getSpan(buffer));
  18. }
  19. }
  20. export function createCloseUserInstruction(payer: PublicKey, target: PublicKey, programId: PublicKey): TransactionInstruction {
  21. const instructionObject = new Close({
  22. instruction: MyInstruction.CloseUser,
  23. });
  24. const ix = new TransactionInstruction({
  25. keys: [
  26. { pubkey: payer, isSigner: true, isWritable: true },
  27. { pubkey: target, isSigner: false, isWritable: true },
  28. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  29. ],
  30. programId: programId,
  31. data: instructionObject.toBuffer(),
  32. });
  33. return ix;
  34. }