initializeToken3.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 InitializeToken3Instruction<
  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 InitializeToken3InstructionData = {
  49. discriminator: number;
  50. owner: Address;
  51. };
  52. export type InitializeToken3InstructionDataArgs = { owner: Address };
  53. export function getInitializeToken3InstructionDataEncoder(): Encoder<InitializeToken3InstructionDataArgs> {
  54. return transformEncoder(
  55. getStructEncoder([
  56. ['discriminator', getU8Encoder()],
  57. ['owner', getAddressEncoder()],
  58. ]),
  59. (value) => ({ ...value, discriminator: 18 })
  60. );
  61. }
  62. export function getInitializeToken3InstructionDataDecoder(): Decoder<InitializeToken3InstructionData> {
  63. return getStructDecoder([
  64. ['discriminator', getU8Decoder()],
  65. ['owner', getAddressDecoder()],
  66. ]);
  67. }
  68. export function getInitializeToken3InstructionDataCodec(): Codec<
  69. InitializeToken3InstructionDataArgs,
  70. InitializeToken3InstructionData
  71. > {
  72. return combineCodec(
  73. getInitializeToken3InstructionDataEncoder(),
  74. getInitializeToken3InstructionDataDecoder()
  75. );
  76. }
  77. export type InitializeToken3Input<
  78. TAccountAccount extends string = string,
  79. TAccountMint extends string = string,
  80. > = {
  81. account: Address<TAccountAccount>;
  82. mint: Address<TAccountMint>;
  83. owner: InitializeToken3InstructionDataArgs['owner'];
  84. };
  85. export function getInitializeToken3Instruction<
  86. TAccountAccount extends string,
  87. TAccountMint extends string,
  88. >(
  89. input: InitializeToken3Input<TAccountAccount, TAccountMint>
  90. ): InitializeToken3Instruction<
  91. typeof TOKEN_PROGRAM_ADDRESS,
  92. TAccountAccount,
  93. TAccountMint
  94. > {
  95. // Program address.
  96. const programAddress = TOKEN_PROGRAM_ADDRESS;
  97. // Original accounts.
  98. const originalAccounts = {
  99. account: { value: input.account ?? null, isWritable: true },
  100. mint: { value: input.mint ?? null, isWritable: false },
  101. };
  102. const accounts = originalAccounts as Record<
  103. keyof typeof originalAccounts,
  104. ResolvedAccount
  105. >;
  106. // Original args.
  107. const args = { ...input };
  108. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  109. const instruction = {
  110. accounts: [getAccountMeta(accounts.account), getAccountMeta(accounts.mint)],
  111. programAddress,
  112. data: getInitializeToken3InstructionDataEncoder().encode(
  113. args as InitializeToken3InstructionDataArgs
  114. ),
  115. } as InitializeToken3Instruction<
  116. typeof TOKEN_PROGRAM_ADDRESS,
  117. TAccountAccount,
  118. TAccountMint
  119. >;
  120. return instruction;
  121. }
  122. export type ParsedInitializeToken3Instruction<
  123. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  124. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  125. > = {
  126. programAddress: Address<TProgram>;
  127. accounts: {
  128. account: TAccountMetas[0];
  129. mint: TAccountMetas[1];
  130. };
  131. data: InitializeToken3InstructionData;
  132. };
  133. export function parseInitializeToken3Instruction<
  134. TProgram extends string,
  135. TAccountMetas extends readonly IAccountMeta[],
  136. >(
  137. instruction: IInstruction<TProgram> &
  138. IInstructionWithAccounts<TAccountMetas> &
  139. IInstructionWithData<Uint8Array>
  140. ): ParsedInitializeToken3Instruction<TProgram, TAccountMetas> {
  141. if (instruction.accounts.length < 2) {
  142. // TODO: Coded error.
  143. throw new Error('Not enough accounts');
  144. }
  145. let accountIndex = 0;
  146. const getNextAccount = () => {
  147. const accountMeta = instruction.accounts![accountIndex]!;
  148. accountIndex += 1;
  149. return accountMeta;
  150. };
  151. return {
  152. programAddress: instruction.programAddress,
  153. accounts: {
  154. account: getNextAccount(),
  155. mint: getNextAccount(),
  156. },
  157. data: getInitializeToken3InstructionDataDecoder().decode(instruction.data),
  158. };
  159. }