mintTo.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. AccountRole,
  10. combineCodec,
  11. getStructDecoder,
  12. getStructEncoder,
  13. getU64Decoder,
  14. getU64Encoder,
  15. getU8Decoder,
  16. getU8Encoder,
  17. transformEncoder,
  18. type AccountMeta,
  19. type AccountSignerMeta,
  20. type Address,
  21. type FixedSizeCodec,
  22. type FixedSizeDecoder,
  23. type FixedSizeEncoder,
  24. type Instruction,
  25. type InstructionWithAccounts,
  26. type InstructionWithData,
  27. type ReadonlyAccount,
  28. type ReadonlySignerAccount,
  29. type ReadonlyUint8Array,
  30. type TransactionSigner,
  31. type WritableAccount,
  32. } from '@solana/kit';
  33. import { TOKEN_PROGRAM_ADDRESS } from '../programs';
  34. import { getAccountMetaFactory, type ResolvedAccount } from '../shared';
  35. export const MINT_TO_DISCRIMINATOR = 7;
  36. export function getMintToDiscriminatorBytes() {
  37. return getU8Encoder().encode(MINT_TO_DISCRIMINATOR);
  38. }
  39. export type MintToInstruction<
  40. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  41. TAccountMint extends string | AccountMeta<string> = string,
  42. TAccountToken extends string | AccountMeta<string> = string,
  43. TAccountMintAuthority extends string | AccountMeta<string> = string,
  44. TRemainingAccounts extends readonly AccountMeta<string>[] = [],
  45. > = Instruction<TProgram> &
  46. InstructionWithData<ReadonlyUint8Array> &
  47. InstructionWithAccounts<
  48. [
  49. TAccountMint extends string
  50. ? WritableAccount<TAccountMint>
  51. : TAccountMint,
  52. TAccountToken extends string
  53. ? WritableAccount<TAccountToken>
  54. : TAccountToken,
  55. TAccountMintAuthority extends string
  56. ? ReadonlyAccount<TAccountMintAuthority>
  57. : TAccountMintAuthority,
  58. ...TRemainingAccounts,
  59. ]
  60. >;
  61. export type MintToInstructionData = {
  62. discriminator: number;
  63. /** The amount of new tokens to mint. */
  64. amount: bigint;
  65. };
  66. export type MintToInstructionDataArgs = {
  67. /** The amount of new tokens to mint. */
  68. amount: number | bigint;
  69. };
  70. export function getMintToInstructionDataEncoder(): FixedSizeEncoder<MintToInstructionDataArgs> {
  71. return transformEncoder(
  72. getStructEncoder([
  73. ['discriminator', getU8Encoder()],
  74. ['amount', getU64Encoder()],
  75. ]),
  76. (value) => ({ ...value, discriminator: MINT_TO_DISCRIMINATOR })
  77. ) as FixedSizeEncoder<MintToInstructionDataArgs>;
  78. }
  79. export function getMintToInstructionDataDecoder(): FixedSizeDecoder<MintToInstructionData> {
  80. return getStructDecoder([
  81. ['discriminator', getU8Decoder()],
  82. ['amount', getU64Decoder()],
  83. ]) as FixedSizeDecoder<MintToInstructionData>;
  84. }
  85. export function getMintToInstructionDataCodec(): FixedSizeCodec<
  86. MintToInstructionDataArgs,
  87. MintToInstructionData
  88. > {
  89. return combineCodec(
  90. getMintToInstructionDataEncoder(),
  91. getMintToInstructionDataDecoder()
  92. ) as FixedSizeCodec<MintToInstructionDataArgs, MintToInstructionData>;
  93. }
  94. export type MintToInput<
  95. TAccountMint extends string = string,
  96. TAccountToken extends string = string,
  97. TAccountMintAuthority extends string = string,
  98. > = {
  99. /** The mint account. */
  100. mint: Address<TAccountMint>;
  101. /** The account to mint tokens to. */
  102. token: Address<TAccountToken>;
  103. /** The mint's minting authority or its multisignature account. */
  104. mintAuthority:
  105. | Address<TAccountMintAuthority>
  106. | TransactionSigner<TAccountMintAuthority>;
  107. amount: MintToInstructionDataArgs['amount'];
  108. multiSigners?: Array<TransactionSigner>;
  109. };
  110. export function getMintToInstruction<
  111. TAccountMint extends string,
  112. TAccountToken extends string,
  113. TAccountMintAuthority extends string,
  114. TProgramAddress extends Address = typeof TOKEN_PROGRAM_ADDRESS,
  115. >(
  116. input: MintToInput<TAccountMint, TAccountToken, TAccountMintAuthority>,
  117. config?: { programAddress?: TProgramAddress }
  118. ): MintToInstruction<
  119. TProgramAddress,
  120. TAccountMint,
  121. TAccountToken,
  122. (typeof input)['mintAuthority'] extends TransactionSigner<TAccountMintAuthority>
  123. ? ReadonlySignerAccount<TAccountMintAuthority> &
  124. AccountSignerMeta<TAccountMintAuthority>
  125. : TAccountMintAuthority
  126. > {
  127. // Program address.
  128. const programAddress = config?.programAddress ?? TOKEN_PROGRAM_ADDRESS;
  129. // Original accounts.
  130. const originalAccounts = {
  131. mint: { value: input.mint ?? null, isWritable: true },
  132. token: { value: input.token ?? null, isWritable: true },
  133. mintAuthority: { value: input.mintAuthority ?? 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. // Remaining accounts.
  142. const remainingAccounts: AccountMeta[] = (args.multiSigners ?? []).map(
  143. (signer) => ({
  144. address: signer.address,
  145. role: AccountRole.READONLY_SIGNER,
  146. signer,
  147. })
  148. );
  149. const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
  150. const instruction = {
  151. accounts: [
  152. getAccountMeta(accounts.mint),
  153. getAccountMeta(accounts.token),
  154. getAccountMeta(accounts.mintAuthority),
  155. ...remainingAccounts,
  156. ],
  157. programAddress,
  158. data: getMintToInstructionDataEncoder().encode(
  159. args as MintToInstructionDataArgs
  160. ),
  161. } as MintToInstruction<
  162. TProgramAddress,
  163. TAccountMint,
  164. TAccountToken,
  165. (typeof input)['mintAuthority'] extends TransactionSigner<TAccountMintAuthority>
  166. ? ReadonlySignerAccount<TAccountMintAuthority> &
  167. AccountSignerMeta<TAccountMintAuthority>
  168. : TAccountMintAuthority
  169. >;
  170. return instruction;
  171. }
  172. export type ParsedMintToInstruction<
  173. TProgram extends string = typeof TOKEN_PROGRAM_ADDRESS,
  174. TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
  175. > = {
  176. programAddress: Address<TProgram>;
  177. accounts: {
  178. /** The mint account. */
  179. mint: TAccountMetas[0];
  180. /** The account to mint tokens to. */
  181. token: TAccountMetas[1];
  182. /** The mint's minting authority or its multisignature account. */
  183. mintAuthority: TAccountMetas[2];
  184. };
  185. data: MintToInstructionData;
  186. };
  187. export function parseMintToInstruction<
  188. TProgram extends string,
  189. TAccountMetas extends readonly AccountMeta[],
  190. >(
  191. instruction: Instruction<TProgram> &
  192. InstructionWithAccounts<TAccountMetas> &
  193. InstructionWithData<ReadonlyUint8Array>
  194. ): ParsedMintToInstruction<TProgram, TAccountMetas> {
  195. if (instruction.accounts.length < 3) {
  196. // TODO: Coded error.
  197. throw new Error('Not enough accounts');
  198. }
  199. let accountIndex = 0;
  200. const getNextAccount = () => {
  201. const accountMeta = instruction.accounts![accountIndex]!;
  202. accountIndex += 1;
  203. return accountMeta;
  204. };
  205. return {
  206. programAddress: instruction.programAddress,
  207. accounts: {
  208. mint: getNextAccount(),
  209. token: getNextAccount(),
  210. mintAuthority: getNextAccount(),
  211. },
  212. data: getMintToInstructionDataDecoder().decode(instruction.data),
  213. };
  214. }