mintToATA.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {
  2. InstructionPlan,
  3. sequentialInstructionPlan,
  4. Address,
  5. TransactionSigner,
  6. } from '@solana/kit';
  7. import {
  8. findAssociatedTokenPda,
  9. getCreateAssociatedTokenIdempotentInstruction,
  10. getMintToCheckedInstruction,
  11. TOKEN_PROGRAM_ADDRESS,
  12. } from './generated';
  13. type MintToATAInstructionPlanInput = {
  14. /** Funding account (must be a system account). */
  15. payer: TransactionSigner;
  16. /** Associated token account address to mint to.
  17. * Will be created if it does not already exist.
  18. * Note: Use {@link mintToATAInstructionPlanAsync} instead to derive this automatically.
  19. * Note: Use {@link findAssociatedTokenPda} to derive the associated token account address.
  20. */
  21. ata: Address;
  22. /** Wallet address for the associated token account. */
  23. owner: Address;
  24. /** The token mint for the associated token account. */
  25. mint: Address;
  26. /** The mint's minting authority or its multisignature account. */
  27. mintAuthority: Address | TransactionSigner;
  28. /** The amount of new tokens to mint. */
  29. amount: number | bigint;
  30. /** Expected number of base 10 digits to the right of the decimal place. */
  31. decimals: number;
  32. multiSigners?: Array<TransactionSigner>;
  33. };
  34. type MintToATAInstructionPlanConfig = {
  35. systemProgram?: Address;
  36. tokenProgram?: Address;
  37. associatedTokenProgram?: Address;
  38. };
  39. export function mintToATAInstructionPlan(
  40. input: MintToATAInstructionPlanInput,
  41. config?: MintToATAInstructionPlanConfig
  42. ): InstructionPlan {
  43. return sequentialInstructionPlan([
  44. getCreateAssociatedTokenIdempotentInstruction(
  45. {
  46. payer: input.payer,
  47. ata: input.ata,
  48. owner: input.owner,
  49. mint: input.mint,
  50. systemProgram: config?.systemProgram,
  51. tokenProgram: config?.tokenProgram,
  52. },
  53. {
  54. programAddress: config?.associatedTokenProgram,
  55. }
  56. ),
  57. // mint to this token account
  58. getMintToCheckedInstruction(
  59. {
  60. mint: input.mint,
  61. token: input.ata,
  62. mintAuthority: input.mintAuthority,
  63. amount: input.amount,
  64. decimals: input.decimals,
  65. multiSigners: input.multiSigners,
  66. },
  67. {
  68. programAddress: config?.tokenProgram,
  69. }
  70. ),
  71. ]);
  72. }
  73. type MintToATAInstructionPlanAsyncInput = Omit<
  74. MintToATAInstructionPlanInput,
  75. 'ata'
  76. >;
  77. export async function mintToATAInstructionPlanAsync(
  78. input: MintToATAInstructionPlanAsyncInput,
  79. config?: MintToATAInstructionPlanConfig
  80. ): Promise<InstructionPlan> {
  81. const [ataAddress] = await findAssociatedTokenPda({
  82. owner: input.owner,
  83. tokenProgram: config?.tokenProgram ?? TOKEN_PROGRAM_ADDRESS,
  84. mint: input.mint,
  85. });
  86. return mintToATAInstructionPlan(
  87. {
  88. ...input,
  89. ata: ataAddress,
  90. },
  91. config
  92. );
  93. }