accounts.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // @ts-nocheck
  2. import * as B from "@native-to-anchor/buffer-layout";
  3. import { AccountsCoder, Idl } from "@coral-xyz/anchor";
  4. import { IdlTypeDef } from "@coral-xyz/anchor/dist/cjs/idl";
  5. export class SplTokenSwapAccountsCoder<A extends string = string>
  6. implements AccountsCoder
  7. {
  8. constructor(_idl: Idl) {}
  9. public async encode<T = any>(accountName: A, account: T): Promise<Buffer> {
  10. switch (accountName) {
  11. case "swap": {
  12. const buffer = Buffer.alloc(324);
  13. const len = SWAP_LAYOUT.encode(account, buffer);
  14. return buffer.slice(0, len);
  15. }
  16. default: {
  17. throw new Error(`Invalid account name: ${accountName}`);
  18. }
  19. }
  20. }
  21. public decode<T = any>(accountName: A, ix: Buffer): T {
  22. return this.decodeUnchecked(accountName, ix);
  23. }
  24. public decodeUnchecked<T = any>(accountName: A, ix: Buffer): T {
  25. switch (accountName) {
  26. case "swap": {
  27. return decodeSwapAccount(ix);
  28. }
  29. default: {
  30. throw new Error(`Invalid account name: ${accountName}`);
  31. }
  32. }
  33. }
  34. public memcmp(
  35. accountName: A,
  36. _appendData?: Buffer
  37. ): { dataSize?: number; offset?: number; bytes?: string } {
  38. switch (accountName) {
  39. case "swap": {
  40. return {
  41. dataSize: 324,
  42. };
  43. }
  44. default: {
  45. throw new Error(`Invalid account name: ${accountName}`);
  46. }
  47. }
  48. }
  49. public size(idlAccount: IdlTypeDef): number {
  50. switch (idlAccount.name) {
  51. case "swap": {
  52. return 324;
  53. }
  54. default: {
  55. throw new Error(`Invalid account name: ${idlAccount.name}`);
  56. }
  57. }
  58. }
  59. }
  60. function decodeSwapAccount<T = any>(ix: Buffer): T {
  61. return SWAP_LAYOUT.decode(ix) as T;
  62. }
  63. const SWAP_LAYOUT: any = B.struct([
  64. B.u8("version"),
  65. B.bool("isInitialized"),
  66. B.u8("bumpSeed"),
  67. B.publicKey("tokenProgramId"),
  68. B.publicKey("tokenA"),
  69. B.publicKey("tokenB"),
  70. B.publicKey("poolMint"),
  71. B.publicKey("tokenAMint"),
  72. B.publicKey("tokenBMint"),
  73. B.publicKey("poolFeeAccount"),
  74. B.struct(
  75. [
  76. B.u64("tradeFeeNumerator"),
  77. B.u64("tradeFeeDenominator"),
  78. B.u64("ownerTradeFeeNumerator"),
  79. B.u64("ownerTradeFeeDenominator"),
  80. B.u64("ownerWithdrawFeeNumerator"),
  81. B.u64("ownerWithdrawFeeDenominator"),
  82. B.u64("hostFeeNumerator"),
  83. B.u64("hostFeeDenominator"),
  84. ],
  85. "fees"
  86. ),
  87. B.struct(
  88. [
  89. ((p: string) => {
  90. const U = B.union(B.u8("discriminator"), null, p);
  91. U.addVariant(0, B.struct([]), "constantProduct");
  92. U.addVariant(1, B.struct([]), "constantPrice");
  93. U.addVariant(2, B.struct([]), "stable");
  94. U.addVariant(3, B.struct([]), "offset");
  95. return U;
  96. })("curveType"),
  97. B.seq(B.u8(), 32, "calculator"),
  98. ],
  99. "swapCurve"
  100. ),
  101. ]);