close.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 Close {
  6. instruction: MyInstruction;
  7. constructor(props: {
  8. instruction: MyInstruction;
  9. }) {
  10. this.instruction = props.instruction;
  11. }
  12. toBuffer() {
  13. return Buffer.from(borsh.serialize(CloseSchema, this));
  14. }
  15. static fromBuffer(buffer: Buffer) {
  16. return borsh.deserialize(CloseSchema, Close, buffer);
  17. }
  18. }
  19. export const CloseSchema = new Map([
  20. [
  21. Close,
  22. {
  23. kind: 'struct',
  24. fields: [['instruction', 'u8']],
  25. },
  26. ],
  27. ]);
  28. export function createCloseUserInstruction(target: PublicKey, payer: PublicKey, programId: PublicKey): TransactionInstruction {
  29. const instructionObject = new Close({
  30. instruction: MyInstruction.CloseUser,
  31. });
  32. const ix = new TransactionInstruction({
  33. keys: [
  34. { pubkey: target, isSigner: false, isWritable: true },
  35. { pubkey: payer, isSigner: true, isWritable: true },
  36. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  37. ],
  38. programId: programId,
  39. data: instructionObject.toBuffer(),
  40. });
  41. return ix;
  42. }