instructions.ts 1.3 KB

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