initializeToken.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. getStructDecoder,
  21. getStructEncoder,
  22. getU8Decoder,
  23. getU8Encoder,
  24. transformEncoder,
  25. } from '@solana/web3.js';
  26. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  27. import { ResolvedAccount, getAccountMetaFactory } from '../shared';
  28. export type InitializeTokenInstruction<
  29. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  30. TAccountAccount extends string | IAccountMeta<string> = string,
  31. TAccountMint extends string | IAccountMeta<string> = string,
  32. TAccountOwner extends string | IAccountMeta<string> = string,
  33. TAccountRent extends
  34. | string
  35. | IAccountMeta<string> = 'SysvarRent111111111111111111111111111111111',
  36. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  37. > = IInstruction<TProgram> &
  38. IInstructionWithData<Uint8Array> &
  39. IInstructionWithAccounts<
  40. [
  41. TAccountAccount extends string
  42. ? WritableAccount<TAccountAccount>
  43. : TAccountAccount,
  44. TAccountMint extends string
  45. ? ReadonlyAccount<TAccountMint>
  46. : TAccountMint,
  47. TAccountOwner extends string
  48. ? ReadonlyAccount<TAccountOwner>
  49. : TAccountOwner,
  50. TAccountRent extends string
  51. ? ReadonlyAccount<TAccountRent>
  52. : TAccountRent,
  53. ...TRemainingAccounts,
  54. ]
  55. >;
  56. export type InitializeTokenInstructionData = { discriminator: number };
  57. export type InitializeTokenInstructionDataArgs = {};
  58. export function getInitializeTokenInstructionDataEncoder(): Encoder<InitializeTokenInstructionDataArgs> {
  59. return transformEncoder(
  60. getStructEncoder([['discriminator', getU8Encoder()]]),
  61. (value) => ({ ...value, discriminator: 1 })
  62. );
  63. }
  64. export function getInitializeTokenInstructionDataDecoder(): Decoder<InitializeTokenInstructionData> {
  65. return getStructDecoder([['discriminator', getU8Decoder()]]);
  66. }
  67. export function getInitializeTokenInstructionDataCodec(): Codec<
  68. InitializeTokenInstructionDataArgs,
  69. InitializeTokenInstructionData
  70. > {
  71. return combineCodec(
  72. getInitializeTokenInstructionDataEncoder(),
  73. getInitializeTokenInstructionDataDecoder()
  74. );
  75. }
  76. export type InitializeTokenInput<
  77. TAccountAccount extends string = string,
  78. TAccountMint extends string = string,
  79. TAccountOwner extends string = string,
  80. TAccountRent extends string = string,
  81. > = {
  82. account: Address<TAccountAccount>;
  83. mint: Address<TAccountMint>;
  84. owner: Address<TAccountOwner>;
  85. rent?: Address<TAccountRent>;
  86. };
  87. export function getInitializeTokenInstruction<
  88. TAccountAccount extends string,
  89. TAccountMint extends string,
  90. TAccountOwner extends string,
  91. TAccountRent extends string,
  92. >(
  93. input: InitializeTokenInput<
  94. TAccountAccount,
  95. TAccountMint,
  96. TAccountOwner,
  97. TAccountRent
  98. >
  99. ): InitializeTokenInstruction<
  100. typeof TOKEN_PROGRAM_ADDRESS,
  101. TAccountAccount,
  102. TAccountMint,
  103. TAccountOwner,
  104. TAccountRent
  105. > {
  106. // Program address.
  107. const programAddress = TOKEN_PROGRAM_ADDRESS;
  108. // Original accounts.
  109. const originalAccounts = {
  110. account: { value: input.account ?? null, isWritable: true },
  111. mint: { value: input.mint ?? null, isWritable: false },
  112. owner: { value: input.owner ?? null, isWritable: false },
  113. rent: { value: input.rent ?? null, isWritable: false },
  114. };
  115. const accounts = originalAccounts as Record<
  116. keyof typeof originalAccounts,
  117. ResolvedAccount
  118. >;
  119. // Resolve default values.
  120. if (!accounts.rent.value) {
  121. accounts.rent.value =
  122. 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
  123. }
  124. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  125. const instruction = {
  126. accounts: [
  127. getAccountMeta(accounts.account),
  128. getAccountMeta(accounts.mint),
  129. getAccountMeta(accounts.owner),
  130. getAccountMeta(accounts.rent),
  131. ],
  132. programAddress,
  133. data: getInitializeTokenInstructionDataEncoder().encode({}),
  134. } as InitializeTokenInstruction<
  135. typeof TOKEN_PROGRAM_ADDRESS,
  136. TAccountAccount,
  137. TAccountMint,
  138. TAccountOwner,
  139. TAccountRent
  140. >;
  141. return instruction;
  142. }
  143. export type ParsedInitializeTokenInstruction<
  144. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  145. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  146. > = {
  147. programAddress: Address<TProgram>;
  148. accounts: {
  149. account: TAccountMetas[0];
  150. mint: TAccountMetas[1];
  151. owner: TAccountMetas[2];
  152. rent: TAccountMetas[3];
  153. };
  154. data: InitializeTokenInstructionData;
  155. };
  156. export function parseInitializeTokenInstruction<
  157. TProgram extends string,
  158. TAccountMetas extends readonly IAccountMeta[],
  159. >(
  160. instruction: IInstruction<TProgram> &
  161. IInstructionWithAccounts<TAccountMetas> &
  162. IInstructionWithData<Uint8Array>
  163. ): ParsedInitializeTokenInstruction<TProgram, TAccountMetas> {
  164. if (instruction.accounts.length < 4) {
  165. // TODO: Coded error.
  166. throw new Error('Not enough accounts');
  167. }
  168. let accountIndex = 0;
  169. const getNextAccount = () => {
  170. const accountMeta = instruction.accounts![accountIndex]!;
  171. accountIndex += 1;
  172. return accountMeta;
  173. };
  174. return {
  175. programAddress: instruction.programAddress,
  176. accounts: {
  177. account: getNextAccount(),
  178. mint: getNextAccount(),
  179. owner: getNextAccount(),
  180. rent: getNextAccount(),
  181. },
  182. data: getInitializeTokenInstructionDataDecoder().decode(instruction.data),
  183. };
  184. }