initializeMint.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. getAddressDecoder,
  11. getAddressEncoder,
  12. getOptionDecoder,
  13. getOptionEncoder,
  14. getStructDecoder,
  15. getStructEncoder,
  16. getU8Decoder,
  17. getU8Encoder,
  18. none,
  19. transformEncoder,
  20. type Address,
  21. type Codec,
  22. type Decoder,
  23. type Encoder,
  24. type IAccountMeta,
  25. type IInstruction,
  26. type IInstructionWithAccounts,
  27. type IInstructionWithData,
  28. type Option,
  29. type OptionOrNullable,
  30. type ReadonlyAccount,
  31. type WritableAccount,
  32. } from '@solana/kit';
  33. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  34. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  35. export const INITIALIZE_MINT_DISCRIMINATOR = 0;
  36. export function getInitializeMintDiscriminatorBytes() {
  37. return getU8Encoder().encode(INITIALIZE_MINT_DISCRIMINATOR);
  38. }
  39. export type InitializeMintInstruction<
  40. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  41. TAccountMint extends string | IAccountMeta<string> = string,
  42. TAccountRent extends
  43. | string
  44. | IAccountMeta<string> = 'SysvarRent111111111111111111111111111111111',
  45. TRemainingAccounts extends readonly IAccountMeta<string>[] = [],
  46. > = IInstruction<TProgram> &
  47. IInstructionWithData<Uint8Array> &
  48. IInstructionWithAccounts<
  49. [
  50. TAccountMint extends string
  51. ? WritableAccount<TAccountMint>
  52. : TAccountMint,
  53. TAccountRent extends string
  54. ? ReadonlyAccount<TAccountRent>
  55. : TAccountRent,
  56. ...TRemainingAccounts,
  57. ]
  58. >;
  59. export type InitializeMintInstructionData = {
  60. discriminator: number;
  61. /** Number of decimals in token account amounts. */
  62. decimals: number;
  63. /** Minting authority. */
  64. mintAuthority: Address;
  65. /** Optional authority that can freeze token accounts. */
  66. freezeAuthority: Option<Address>;
  67. };
  68. export type InitializeMintInstructionDataArgs = {
  69. /** Number of decimals in token account amounts. */
  70. decimals: number;
  71. /** Minting authority. */
  72. mintAuthority: Address;
  73. /** Optional authority that can freeze token accounts. */
  74. freezeAuthority?: OptionOrNullable<Address>;
  75. };
  76. export function getInitializeMintInstructionDataEncoder(): Encoder<InitializeMintInstructionDataArgs> {
  77. return transformEncoder(
  78. getStructEncoder([
  79. ['discriminator', getU8Encoder()],
  80. ['decimals', getU8Encoder()],
  81. ['mintAuthority', getAddressEncoder()],
  82. ['freezeAuthority', getOptionEncoder(getAddressEncoder())],
  83. ]),
  84. (value) => ({
  85. ...value,
  86. discriminator: INITIALIZE_MINT_DISCRIMINATOR,
  87. freezeAuthority: value.freezeAuthority ?? none(),
  88. })
  89. );
  90. }
  91. export function getInitializeMintInstructionDataDecoder(): Decoder<InitializeMintInstructionData> {
  92. return getStructDecoder([
  93. ['discriminator', getU8Decoder()],
  94. ['decimals', getU8Decoder()],
  95. ['mintAuthority', getAddressDecoder()],
  96. ['freezeAuthority', getOptionDecoder(getAddressDecoder())],
  97. ]);
  98. }
  99. export function getInitializeMintInstructionDataCodec(): Codec<
  100. InitializeMintInstructionDataArgs,
  101. InitializeMintInstructionData
  102. > {
  103. return combineCodec(
  104. getInitializeMintInstructionDataEncoder(),
  105. getInitializeMintInstructionDataDecoder()
  106. );
  107. }
  108. export type InitializeMintInput<
  109. TAccountMint extends string = string,
  110. TAccountRent extends string = string,
  111. > = {
  112. /** Token mint account. */
  113. mint: Address<TAccountMint>;
  114. /** Rent sysvar. */
  115. rent?: Address<TAccountRent>;
  116. decimals: InitializeMintInstructionDataArgs['decimals'];
  117. mintAuthority: InitializeMintInstructionDataArgs['mintAuthority'];
  118. freezeAuthority?: InitializeMintInstructionDataArgs['freezeAuthority'];
  119. };
  120. export function getInitializeMintInstruction<
  121. TAccountMint extends string,
  122. TAccountRent extends string,
  123. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  124. >(
  125. input: InitializeMintInput<TAccountMint, TAccountRent>,
  126. config?: { programAddress?: TProgramAddress }
  127. ): InitializeMintInstruction<TProgramAddress, TAccountMint, TAccountRent> {
  128. // Program address.
  129. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  130. // Original accounts.
  131. const originalAccounts = {
  132. mint: { value: input.mint ?? null, isWritable: true },
  133. rent: { value: input.rent ?? null, isWritable: false },
  134. };
  135. const accounts = originalAccounts as Record<
  136. keyof typeof originalAccounts,
  137. ResolvedAccount
  138. >;
  139. // Original args.
  140. const args = { ...input };
  141. // Resolve default values.
  142. if (!accounts.rent.value) {
  143. accounts.rent.value =
  144. 'SysvarRent111111111111111111111111111111111' as Address<'SysvarRent111111111111111111111111111111111'>;
  145. }
  146. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  147. const instruction = {
  148. accounts: [getAccountMeta(accounts.mint), getAccountMeta(accounts.rent)],
  149. programAddress,
  150. data: getInitializeMintInstructionDataEncoder().encode(
  151. args as InitializeMintInstructionDataArgs
  152. ),
  153. } as InitializeMintInstruction<TProgramAddress, TAccountMint, TAccountRent>;
  154. return instruction;
  155. }
  156. export type ParsedInitializeMintInstruction<
  157. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  158. TAccountMetas extends readonly IAccountMeta[] = readonly IAccountMeta[],
  159. > = {
  160. programAddress: Address<TProgram>;
  161. accounts: {
  162. /** Token mint account. */
  163. mint: TAccountMetas[0];
  164. /** Rent sysvar. */
  165. rent: TAccountMetas[1];
  166. };
  167. data: InitializeMintInstructionData;
  168. };
  169. export function parseInitializeMintInstruction<
  170. TProgram extends string,
  171. TAccountMetas extends readonly IAccountMeta[],
  172. >(
  173. instruction: IInstruction<TProgram> &
  174. IInstructionWithAccounts<TAccountMetas> &
  175. IInstructionWithData<Uint8Array>
  176. ): ParsedInitializeMintInstruction<TProgram, TAccountMetas> {
  177. if (instruction.accounts.length < 2) {
  178. // TODO: Coded error.
  179. throw new Error('Not enough accounts');
  180. }
  181. let accountIndex = 0;
  182. const getNextAccount = () => {
  183. const accountMeta = instruction.accounts![accountIndex]!;
  184. accountIndex += 1;
  185. return accountMeta;
  186. };
  187. return {
  188. programAddress: instruction.programAddress,
  189. accounts: {
  190. mint: getNextAccount(),
  191. rent: getNextAccount(),
  192. },
  193. data: getInitializeMintInstructionDataDecoder().decode(instruction.data),
  194. };
  195. }