instructions.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // @ts-nocheck
  2. import * as B from "@native-to-anchor/buffer-layout";
  3. import { Idl, InstructionCoder } from "@coral-xyz/anchor";
  4. export class SplAssociatedTokenAccountInstructionCoder
  5. implements InstructionCoder
  6. {
  7. constructor(_idl: Idl) {}
  8. encode(ixName: string, ix: any): Buffer {
  9. switch (ixName) {
  10. case "create": {
  11. return encodeCreate(ix);
  12. }
  13. case "createIdempotent": {
  14. return encodeCreateIdempotent(ix);
  15. }
  16. case "recoverNested": {
  17. return encodeRecoverNested(ix);
  18. }
  19. default: {
  20. throw new Error(`Invalid instruction: ${ixName}`);
  21. }
  22. }
  23. }
  24. encodeState(_ixName: string, _ix: any): Buffer {
  25. throw new Error("SplAssociatedTokenAccount does not have state");
  26. }
  27. }
  28. function encodeCreate({}: any): Buffer {
  29. return encodeData({ create: {} }, 1);
  30. }
  31. function encodeCreateIdempotent({}: any): Buffer {
  32. return encodeData({ createIdempotent: {} }, 1);
  33. }
  34. function encodeRecoverNested({}: any): Buffer {
  35. return encodeData({ recoverNested: {} }, 1);
  36. }
  37. const LAYOUT = B.union(B.u8("instruction"));
  38. LAYOUT.addVariant(0, B.struct([]), "create");
  39. LAYOUT.addVariant(1, B.struct([]), "createIdempotent");
  40. LAYOUT.addVariant(2, B.struct([]), "recoverNested");
  41. function encodeData(ix: any, span: number): Buffer {
  42. const b = Buffer.alloc(span);
  43. LAYOUT.encode(ix, b);
  44. return b;
  45. }