initializeAccount3.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * This code was AUTOGENERATED using the kinobi library.
  3. * Please DO NOT EDIT THIS FILE, instead use visitors
  4. * to add features, then rerun kinobi to update it.
  5. *
  6. * @see https://github.com/metaplex-foundation/kinobi
  7. */
  8. import {
  9. Address,
  10. Codec,
  11. Decoder,
  12. Encoder,
  13. IAccountMeta,
  14. IInstruction,
  15. IInstructionWithAccounts,
  16. IInstructionWithData,
  17. ReadonlyAccount,
  18. WritableAccount,
  19. combineCodec,
  20. getAddressDecoder,
  21. getAddressEncoder,
  22. getStructDecoder,
  23. getStructEncoder,
  24. getU8Decoder,
  25. getU8Encoder,
  26. transformEncoder,
  27. } from '@solana/web3.js';
  28. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  29. import { ResolvedAccount, getAccountMetaFactory } from '../shared';
  30. export type InitializeAccount3Instruction<
  31. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  32. TAccountAccount extends string | IAccountMeta<string> = string,
  33. TAccountMint extends string | IAccountMeta<string> = string,
  34. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  35. > = IInstruction<TProgram> &
  36. IInstructionWithData<Uint8Array> &
  37. IInstructionWithAccounts<
  38. [
  39. TAccountAccount extends string
  40. ? WritableAccount<TAccountAccount>
  41. : TAccountAccount,
  42. TAccountMint extends string
  43. ? ReadonlyAccount<TAccountMint>
  44. : TAccountMint,
  45. ...TRemainingAccounts,
  46. ]
  47. >;
  48. export type InitializeAccount3InstructionData = {
  49. discriminator: number;
  50. /** The new account's owner/multisignature. */
  51. owner: Address;
  52. };
  53. export type InitializeAccount3InstructionDataArgs = {
  54. /** The new account's owner/multisignature. */
  55. owner: Address;
  56. };
  57. export function getInitializeAccount3InstructionDataEncoder(): Encoder<InitializeAccount3InstructionDataArgs> {
  58. return transformEncoder(
  59. getStructEncoder([
  60. ['discriminator', getU8Encoder()],
  61. ['owner', getAddressEncoder()],
  62. ]),
  63. (value) => ({ ...value, discriminator: 18 })
  64. );
  65. }
  66. export function getInitializeAccount3InstructionDataDecoder(): Decoder<InitializeAccount3InstructionData> {
  67. return getStructDecoder([
  68. ['discriminator', getU8Decoder()],
  69. ['owner', getAddressDecoder()],
  70. ]);
  71. }
  72. export function getInitializeAccount3InstructionDataCodec(): Codec<
  73. InitializeAccount3InstructionDataArgs,
  74. InitializeAccount3InstructionData
  75. > {
  76. return combineCodec(
  77. getInitializeAccount3InstructionDataEncoder(),
  78. getInitializeAccount3InstructionDataDecoder()
  79. );
  80. }
  81. export type InitializeAccount3Input<
  82. TAccountAccount extends string = string,
  83. TAccountMint extends string = string,
  84. > = {
  85. /** The account to initialize. */
  86. account: Address<TAccountAccount>;
  87. /** The mint this account will be associated with. */
  88. mint: Address<TAccountMint>;
  89. owner: InitializeAccount3InstructionDataArgs['owner'];
  90. };
  91. export function getInitializeAccount3Instruction<
  92. TAccountAccount extends string,
  93. TAccountMint extends string,
  94. >(
  95. input: InitializeAccount3Input<TAccountAccount, TAccountMint>
  96. ): InitializeAccount3Instruction<
  97. typeof TOKEN_PROGRAM_ADDRESS,
  98. TAccountAccount,
  99. TAccountMint
  100. > {
  101. // Program address.
  102. const programAddress = TOKEN_PROGRAM_ADDRESS;
  103. // Original accounts.
  104. const originalAccounts = {
  105. account: { value: input.account ?? null, isWritable: true },
  106. mint: { value: input.mint ?? null, isWritable: false },
  107. };
  108. const accounts = originalAccounts as Record<
  109. keyof typeof originalAccounts,
  110. ResolvedAccount
  111. >;
  112. // Original args.
  113. const args = { ...input };
  114. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  115. const instruction = {
  116. accounts: [getAccountMeta(accounts.account), getAccountMeta(accounts.mint)],
  117. programAddress,
  118. data: getInitializeAccount3InstructionDataEncoder().encode(
  119. args as InitializeAccount3InstructionDataArgs
  120. ),
  121. } as InitializeAccount3Instruction<
  122. typeof TOKEN_PROGRAM_ADDRESS,
  123. TAccountAccount,
  124. TAccountMint
  125. >;
  126. return instruction;
  127. }
  128. export type ParsedInitializeAccount3Instruction<
  129. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  130. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  131. > = {
  132. programAddress: Address<TProgram>;
  133. accounts: {
  134. /** The account to initialize. */
  135. account: TAccountMetas[0];
  136. /** The mint this account will be associated with. */
  137. mint: TAccountMetas[1];
  138. };
  139. data: InitializeAccount3InstructionData;
  140. };
  141. export function parseInitializeAccount3Instruction<
  142. TProgram extends string,
  143. TAccountMetas extends readonly IAccountMeta[],
  144. >(
  145. instruction: IInstruction<TProgram> &
  146. IInstructionWithAccounts<TAccountMetas> &
  147. IInstructionWithData<Uint8Array>
  148. ): ParsedInitializeAccount3Instruction<TProgram, TAccountMetas> {
  149. if (instruction.accounts.length < 2) {
  150. // TODO: Coded error.
  151. throw new Error('Not enough accounts');
  152. }
  153. let accountIndex = 0;
  154. const getNextAccount = () => {
  155. const accountMeta = instruction.accounts![accountIndex]!;
  156. accountIndex += 1;
  157. return accountMeta;
  158. };
  159. return {
  160. programAddress: instruction.programAddress,
  161. accounts: {
  162. account: getNextAccount(),
  163. mint: getNextAccount(),
  164. },
  165. data: getInitializeAccount3InstructionDataDecoder().decode(
  166. instruction.data
  167. ),
  168. };
  169. }