close.ts 1.3 KB

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