instruction7.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * This code was AUTOGENERATED using the Codama library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun Codama to update it.
  5. *
  6. * @see https://github.com/codama-idl/codama
  7. */
  8. import {
  9. type AccountMeta,
  10. type Address,
  11. type Instruction,
  12. type InstructionWithAccounts,
  13. type WritableAccount,
  14. } from '@solana/kit';
  15. import { DUMMY_PROGRAM_ADDRESS } from '../programs';
  16. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  17. export type Instruction7Instruction<
  18. TProgram extends string = typeof DUMMY_PROGRAM_ADDRESS,
  19. TAccountMyAccount extends string | AccountMeta<string> = string,
  20. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  21. > = Instruction<TProgram> &
  22. InstructionWithAccounts<
  23. [
  24. TAccountMyAccount extends string
  25. ? WritableAccount<TAccountMyAccount>
  26. : TAccountMyAccount,
  27. ...TRemainingAccounts,
  28. ]
  29. >;
  30. export type Instruction7Input<TAccountMyAccount extends string = string> = {
  31. myAccount?: Address<TAccountMyAccount>;
  32. };
  33. export function getInstruction7Instruction<
  34. TAccountMyAccount extends string,
  35. TProgramAddress extends Address = typeof DUMMY_PROGRAM_ADDRESS,
  36. >(
  37. input: Instruction7Input<TAccountMyAccount>,
  38. config?: { programAddress?: TProgramAddress }
  39. ): Instruction7Instruction<TProgramAddress, TAccountMyAccount> {
  40. // Program address.
  41. const programAddress = config?.programAddress ?? DUMMY_PROGRAM_ADDRESS;
  42. // Original accounts.
  43. const originalAccounts = {
  44. myAccount: { value: input.myAccount ?? null, isWritable: true },
  45. };
  46. const accounts = originalAccounts as Record<
  47. keyof typeof originalAccounts,
  48. ResolvedAccount
  49. >;
  50. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  51. return Object.freeze({
  52. accounts: [getAccountMeta(accounts.myAccount)],
  53. programAddress,
  54. } as Instruction7Instruction<TProgramAddress, TAccountMyAccount>);
  55. }
  56. export type ParsedInstruction7Instruction<
  57. TProgram extends string = typeof DUMMY_PROGRAM_ADDRESS,
  58. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  59. > = {
  60. programAddress: Address<TProgram>;
  61. accounts: {
  62. myAccount?: TAccountMetas[0] | undefined;
  63. };
  64. };
  65. export function parseInstruction7Instruction<
  66. TProgram extends string,
  67. TAccountMetas extends readonly AccountMeta[],
  68. >(
  69. instruction: Instruction<TProgram> & InstructionWithAccounts<TAccountMetas>
  70. ): ParsedInstruction7Instruction<TProgram, TAccountMetas> {
  71. if (instruction.accounts.length < 1) {
  72. // TODO: Coded error.
  73. throw new Error('Not enough accounts');
  74. }
  75. let accountIndex = 0;
  76. const getNextAccount = () => {
  77. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  78. accountIndex += 1;
  79. return accountMeta;
  80. };
  81. const getNextOptionalAccount = () => {
  82. const accountMeta = getNextAccount();
  83. return accountMeta.address === DUMMY_PROGRAM_ADDRESS
  84. ? undefined
  85. : accountMeta;
  86. };
  87. return {
  88. programAddress: instruction.programAddress,
  89. accounts: { myAccount: getNextOptionalAccount() },
  90. };
  91. }