| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import {
- InstructionPlan,
- sequentialInstructionPlan,
- Address,
- TransactionSigner,
- } from '@solana/kit';
- import {
- findAssociatedTokenPda,
- getCreateAssociatedTokenIdempotentInstruction,
- getMintToCheckedInstruction,
- TOKEN_PROGRAM_ADDRESS,
- } from './generated';
- type MintToATAInstructionPlanInput = {
- /** Funding account (must be a system account). */
- payer: TransactionSigner;
- /** Associated token account address to mint to.
- * Will be created if it does not already exist.
- * Note: Use {@link mintToATAInstructionPlanAsync} instead to derive this automatically.
- * Note: Use {@link findAssociatedTokenPda} to derive the associated token account address.
- */
- ata: Address;
- /** Wallet address for the associated token account. */
- owner: Address;
- /** The token mint for the associated token account. */
- mint: Address;
- /** The mint's minting authority or its multisignature account. */
- mintAuthority: Address | TransactionSigner;
- /** The amount of new tokens to mint. */
- amount: number | bigint;
- /** Expected number of base 10 digits to the right of the decimal place. */
- decimals: number;
- multiSigners?: Array<TransactionSigner>;
- };
- type MintToATAInstructionPlanConfig = {
- systemProgram?: Address;
- tokenProgram?: Address;
- associatedTokenProgram?: Address;
- };
- export function mintToATAInstructionPlan(
- input: MintToATAInstructionPlanInput,
- config?: MintToATAInstructionPlanConfig
- ): InstructionPlan {
- return sequentialInstructionPlan([
- getCreateAssociatedTokenIdempotentInstruction(
- {
- payer: input.payer,
- ata: input.ata,
- owner: input.owner,
- mint: input.mint,
- systemProgram: config?.systemProgram,
- tokenProgram: config?.tokenProgram,
- },
- {
- programAddress: config?.associatedTokenProgram,
- }
- ),
- // mint to this token account
- getMintToCheckedInstruction(
- {
- mint: input.mint,
- token: input.ata,
- mintAuthority: input.mintAuthority,
- amount: input.amount,
- decimals: input.decimals,
- multiSigners: input.multiSigners,
- },
- {
- programAddress: config?.tokenProgram,
- }
- ),
- ]);
- }
- type MintToATAInstructionPlanAsyncInput = Omit<
- MintToATAInstructionPlanInput,
- 'ata'
- >;
- export async function mintToATAInstructionPlanAsync(
- input: MintToATAInstructionPlanAsyncInput,
- config?: MintToATAInstructionPlanConfig
- ): Promise<InstructionPlan> {
- const [ataAddress] = await findAssociatedTokenPda({
- owner: input.owner,
- tokenProgram: config?.tokenProgram ?? TOKEN_PROGRAM_ADDRESS,
- mint: input.mint,
- });
- return mintToATAInstructionPlan(
- {
- ...input,
- ata: ataAddress,
- },
- config
- );
- }
|