initializeImmutableOwner.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. combineCodec,
  10. getStructDecoder,
  11. getStructEncoder,
  12. getU8Decoder,
  13. getU8Encoder,
  14. transformEncoder,
  15. type AccountMeta,
  16. type Address,
  17. type FixedSizeCodec,
  18. type FixedSizeDecoder,
  19. type FixedSizeEncoder,
  20. type Instruction,
  21. type InstructionWithAccounts,
  22. type InstructionWithData,
  23. type ReadonlyUint8Array,
  24. type WritableAccount,
  25. } from '@solana/kit';
  26. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  27. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  28. export const INITIALIZE_IMMUTABLE_OWNER_DISCRIMINATOR = 22;
  29. export function getInitializeImmutableOwnerDiscriminatorBytes() {
  30. return getU8Encoder().encode(INITIALIZE_IMMUTABLE_OWNER_DISCRIMINATOR);
  31. }
  32. export type InitializeImmutableOwnerInstruction<
  33. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  34. TAccountAccount extends string | AccountMeta<string> = string,
  35. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  36. > = Instruction<TProgram> &
  37. InstructionWithData<ReadonlyUint8Array> &
  38. InstructionWithAccounts<
  39. [
  40. TAccountAccount extends string
  41. ? WritableAccount<TAccountAccount>
  42. : TAccountAccount,
  43. ...TRemainingAccounts,
  44. ]
  45. >;
  46. export type InitializeImmutableOwnerInstructionData = { discriminator: number };
  47. export type InitializeImmutableOwnerInstructionDataArgs = {};
  48. export function getInitializeImmutableOwnerInstructionDataEncoder(): FixedSizeEncoder<InitializeImmutableOwnerInstructionDataArgs> {
  49. return transformEncoder(
  50. getStructEncoder([['discriminator', getU8Encoder()]]),
  51. (value) => ({
  52. ...value,
  53. discriminator: INITIALIZE_IMMUTABLE_OWNER_DISCRIMINATOR,
  54. })
  55. );
  56. }
  57. export function getInitializeImmutableOwnerInstructionDataDecoder(): FixedSizeDecoder<InitializeImmutableOwnerInstructionData> {
  58. return getStructDecoder([['discriminator', getU8Decoder()]]);
  59. }
  60. export function getInitializeImmutableOwnerInstructionDataCodec(): FixedSizeCodec<
  61. InitializeImmutableOwnerInstructionDataArgs,
  62. InitializeImmutableOwnerInstructionData
  63. > {
  64. return combineCodec(
  65. getInitializeImmutableOwnerInstructionDataEncoder(),
  66. getInitializeImmutableOwnerInstructionDataDecoder()
  67. );
  68. }
  69. export type InitializeImmutableOwnerInput<
  70. TAccountAccount extends string = string,
  71. > = {
  72. /** The account to initialize. */
  73. account: Address<TAccountAccount>;
  74. };
  75. export function getInitializeImmutableOwnerInstruction<
  76. TAccountAccount extends string,
  77. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  78. >(
  79. input: InitializeImmutableOwnerInput<TAccountAccount>,
  80. config?: { programAddress?: TProgramAddress }
  81. ): InitializeImmutableOwnerInstruction<TProgramAddress, TAccountAccount> {
  82. // Program address.
  83. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  84. // Original accounts.
  85. const originalAccounts = {
  86. account: { value: input.account ?? null, isWritable: true },
  87. };
  88. const accounts = originalAccounts as Record<
  89. keyof typeof originalAccounts,
  90. ResolvedAccount
  91. >;
  92. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  93. const instruction = {
  94. accounts: [getAccountMeta(accounts.account)],
  95. programAddress,
  96. data: getInitializeImmutableOwnerInstructionDataEncoder().encode({}),
  97. } as InitializeImmutableOwnerInstruction<TProgramAddress, TAccountAccount>;
  98. return instruction;
  99. }
  100. export type ParsedInitializeImmutableOwnerInstruction<
  101. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  102. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  103. > = {
  104. programAddress: Address<TProgram>;
  105. accounts: {
  106. /** The account to initialize. */
  107. account: TAccountMetas[0];
  108. };
  109. data: InitializeImmutableOwnerInstructionData;
  110. };
  111. export function parseInitializeImmutableOwnerInstruction<
  112. TProgram extends string,
  113. TAccountMetas extends readonly AccountMeta[],
  114. >(
  115. instruction: Instruction<TProgram> &
  116. InstructionWithAccounts<TAccountMetas> &
  117. InstructionWithData<ReadonlyUint8Array>
  118. ): ParsedInitializeImmutableOwnerInstruction<TProgram, TAccountMetas> {
  119. if (instruction.accounts.length < 1) {
  120. // TODO: Coded error.
  121. throw new Error('Not enough accounts');
  122. }
  123. let accountIndex = 0;
  124. const getNextAccount = () => {
  125. const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
  126. accountIndex += 1;
  127. return accountMeta;
  128. };
  129. return {
  130. programAddress: instruction.programAddress,
  131. accounts: {
  132. account: getNextAccount(),
  133. },
  134. data: getInitializeImmutableOwnerInstructionDataDecoder().decode(
  135. instruction.data
  136. ),
  137. };
  138. }