instructions.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // @ts-nocheck
  2. import * as B from "@native-to-anchor/buffer-layout";
  3. import { Idl, InstructionCoder } from "@coral-xyz/anchor";
  4. export class SplBinaryOraclePairInstructionCoder implements InstructionCoder {
  5. constructor(_idl: Idl) {}
  6. encode(ixName: string, ix: any): Buffer {
  7. switch (ixName) {
  8. case "initPool": {
  9. return encodeInitPool(ix);
  10. }
  11. case "deposit": {
  12. return encodeDeposit(ix);
  13. }
  14. case "withdraw": {
  15. return encodeWithdraw(ix);
  16. }
  17. case "decide": {
  18. return encodeDecide(ix);
  19. }
  20. default: {
  21. throw new Error(`Invalid instruction: ${ixName}`);
  22. }
  23. }
  24. }
  25. encodeState(_ixName: string, _ix: any): Buffer {
  26. throw new Error("SplBinaryOraclePair does not have state");
  27. }
  28. }
  29. function encodeInitPool({ mintEndSlot, decideEndSlot, bumpSeed }: any): Buffer {
  30. return encodeData(
  31. { initPool: { mintEndSlot, decideEndSlot, bumpSeed } },
  32. 1 + 8 + 8 + 1
  33. );
  34. }
  35. function encodeDeposit({ arg }: any): Buffer {
  36. return encodeData({ deposit: { arg } }, 1 + 8);
  37. }
  38. function encodeWithdraw({ arg }: any): Buffer {
  39. return encodeData({ withdraw: { arg } }, 1 + 8);
  40. }
  41. function encodeDecide({ arg }: any): Buffer {
  42. return encodeData({ decide: { arg } }, 1 + 1);
  43. }
  44. const LAYOUT = B.union(B.u8("instruction"));
  45. LAYOUT.addVariant(
  46. 0,
  47. B.struct([B.u64("mintEndSlot"), B.u64("decideEndSlot"), B.u8("bumpSeed")]),
  48. "initPool"
  49. );
  50. LAYOUT.addVariant(1, B.struct([B.u64("arg")]), "deposit");
  51. LAYOUT.addVariant(2, B.struct([B.u64("arg")]), "withdraw");
  52. LAYOUT.addVariant(3, B.struct([B.bool("arg")]), "decide");
  53. function encodeData(ix: any, span: number): Buffer {
  54. const b = Buffer.alloc(span);
  55. LAYOUT.encode(ix, b);
  56. return b;
  57. }