-
Notifications
You must be signed in to change notification settings - Fork 11
add mint to command #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { Command } from 'commander'; | ||
| import chalk from 'chalk'; | ||
| import ora from 'ora'; | ||
| import { | ||
| createMintToTransaction, | ||
| getMintInfo, | ||
| decimalAmountToRaw, | ||
| } from '@mosaic/sdk'; | ||
| import { createSolanaClient } from '../utils/rpc.js'; | ||
| import { loadKeypair } from '../utils/solana.js'; | ||
| import { signTransactionMessageWithSigners, type Address } from 'gill'; | ||
|
|
||
| interface MintOptions { | ||
| mintAddress: string; | ||
| recipient: string; | ||
| amount: string; | ||
| rpcUrl?: string; | ||
| keypair?: string; | ||
| } | ||
|
|
||
| export const mintCommand = new Command('mint') | ||
| .description('Mint tokens to a recipient address') | ||
| .requiredOption( | ||
| '-m, --mint-address <mint-address>', | ||
| 'The mint address of the token' | ||
| ) | ||
| .requiredOption( | ||
| '-r, --recipient <recipient>', | ||
| 'The recipient wallet address (ATA owner)' | ||
| ) | ||
| .requiredOption( | ||
| '-a, --amount <amount>', | ||
| 'The decimal amount to mint (e.g., 1.5)' | ||
| ) | ||
| .action(async (options: MintOptions, command) => { | ||
| const spinner = ora('Minting tokens...').start(); | ||
|
|
||
| try { | ||
| // Get global options from parent command | ||
| const parentOpts = command.parent?.opts() || {}; | ||
| const rpcUrl = options.rpcUrl || parentOpts.rpcUrl; | ||
| const keypairPath = options.keypair || parentOpts.keypair; | ||
|
|
||
| // Create Solana client | ||
| const { rpc, sendAndConfirmTransaction } = createSolanaClient(rpcUrl); | ||
|
|
||
| // Load mint authority keypair (assuming it's the configured keypair) | ||
| const mintAuthorityKeypair = await loadKeypair(keypairPath); | ||
|
|
||
| spinner.text = 'Getting mint information...'; | ||
|
|
||
| // Get mint info to determine decimals | ||
| const mintInfo = await getMintInfo(rpc, options.mintAddress as Address); | ||
| const decimals = mintInfo.decimals; | ||
|
|
||
| // Parse and validate amount | ||
| const decimalAmount = parseFloat(options.amount); | ||
| if (isNaN(decimalAmount) || decimalAmount <= 0) { | ||
| throw new Error('Amount must be a positive number'); | ||
| } | ||
|
|
||
| // Convert decimal amount to raw amount | ||
| const rawAmount = decimalAmountToRaw(decimalAmount, decimals); | ||
|
|
||
| spinner.text = 'Building mint transaction...'; | ||
|
|
||
| // Create mint transaction | ||
| const transaction = await createMintToTransaction( | ||
| rpc, | ||
| options.mintAddress as Address, | ||
| options.recipient as Address, | ||
| rawAmount, | ||
| mintAuthorityKeypair, | ||
| mintAuthorityKeypair // Use same keypair as fee payer | ||
| ); | ||
|
|
||
| spinner.text = 'Signing transaction...'; | ||
|
|
||
| // Sign the transaction | ||
| const signedTransaction = | ||
| await signTransactionMessageWithSigners(transaction); | ||
|
|
||
| spinner.text = 'Sending transaction...'; | ||
|
|
||
| // Send and confirm transaction | ||
| const signature = await sendAndConfirmTransaction(signedTransaction); | ||
|
|
||
| spinner.succeed('Tokens minted successfully!'); | ||
|
|
||
| // Display results | ||
| console.log(chalk.green('✅ Mint Transaction Successful')); | ||
| console.log(chalk.cyan('📋 Details:')); | ||
| console.log(` ${chalk.bold('Mint Address:')} ${options.mintAddress}`); | ||
| console.log(` ${chalk.bold('Recipient:')} ${options.recipient}`); | ||
| console.log( | ||
| ` ${chalk.bold('Amount:')} ${decimalAmount} (${rawAmount.toString()} raw units)` | ||
| ); | ||
| console.log(` ${chalk.bold('Decimals:')} ${decimals}`); | ||
| console.log(` ${chalk.bold('Transaction:')} ${signature}`); | ||
| console.log( | ||
| ` ${chalk.bold('Mint Authority:')} ${mintAuthorityKeypair.address}` | ||
| ); | ||
|
|
||
| console.log(chalk.cyan('\\n🎯 Result:')); | ||
| console.log( | ||
| ` ${chalk.green('✓')} Associated Token Account created/updated` | ||
| ); | ||
| console.log( | ||
| ` ${chalk.green('✓')} ${decimalAmount} tokens minted to recipient` | ||
| ); | ||
| } catch (error) { | ||
| spinner.fail('Failed to mint tokens'); | ||
| console.error( | ||
| chalk.red('\\n❌ Error:'), | ||
| error instanceof Error ? error : 'Unknown error' | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export { Token } from './issuance'; | ||
| export * from './templates'; | ||
| export * from './management/mint'; | ||
| export * from './transactionUtil'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import type { | ||
| Address, | ||
| Rpc, | ||
| SolanaRpcApi, | ||
| FullTransaction, | ||
| TransactionMessageWithFeePayer, | ||
| TransactionVersion, | ||
| TransactionSigner, | ||
| TransactionWithBlockhashLifetime, | ||
| } from 'gill'; | ||
| import { createNoopSigner, createTransaction } from 'gill'; | ||
| import { | ||
| getAssociatedTokenAccountAddress, | ||
| TOKEN_2022_PROGRAM_ADDRESS, | ||
| getMintTokensInstructions, | ||
| } from 'gill/programs/token'; | ||
|
|
||
| /** | ||
| * Creates a transaction to mint tokens to a recipient's associated token account. | ||
| * Will create the ATA if it doesn't exist. | ||
| * | ||
| * @param rpc - The Solana RPC client instance | ||
| * @param mint - The mint address | ||
| * @param recipient - The recipient's wallet address (owner of the ATA) | ||
| * @param amount - The raw token amount (already adjusted for decimals) | ||
| * @param mintAuthority - The mint authority signer | ||
| * @param feePayer - The fee payer signer | ||
| * @returns A promise that resolves to a FullTransaction object for minting tokens | ||
| */ | ||
| export const createMintToTransaction = async ( | ||
| rpc: Rpc<SolanaRpcApi>, | ||
| mint: Address, | ||
| recipient: Address, | ||
| amount: bigint, | ||
| mintAuthority: Address | TransactionSigner<string>, | ||
| feePayer: Address | TransactionSigner<string> | ||
| ): Promise< | ||
| FullTransaction< | ||
| TransactionVersion, | ||
| TransactionMessageWithFeePayer, | ||
| TransactionWithBlockhashLifetime | ||
| > | ||
| > => { | ||
| console.error(recipient); | ||
| const feePayerSigner = | ||
| typeof feePayer === 'string' ? createNoopSigner(feePayer) : feePayer; | ||
| const mintAuthoritySigner = | ||
| typeof mintAuthority === 'string' | ||
| ? createNoopSigner(mintAuthority) | ||
| : mintAuthority; | ||
| const instructions = getMintTokensInstructions({ | ||
| mint, | ||
| destination: recipient, | ||
| amount, | ||
| mintAuthority: mintAuthoritySigner, | ||
| feePayer: feePayerSigner, | ||
| ata: await getAssociatedTokenAccountAddress( | ||
| mint, | ||
| recipient, | ||
| TOKEN_2022_PROGRAM_ADDRESS | ||
| ), | ||
| tokenProgram: TOKEN_2022_PROGRAM_ADDRESS, | ||
| }); | ||
|
|
||
| // Get latest blockhash for transaction | ||
| const { value: latestBlockhash } = await rpc.getLatestBlockhash().send(); | ||
|
|
||
| return createTransaction({ | ||
| feePayer: feePayerSigner, | ||
| version: 'legacy', | ||
| latestBlockhash, | ||
| instructions, | ||
| }); | ||
| }; | ||
|
|
||
| /** | ||
| * Gets mint information including decimals | ||
| * | ||
| * @param rpc - The Solana RPC client instance | ||
| * @param mint - The mint address | ||
| * @returns Promise with mint information including decimals | ||
| */ | ||
| export async function getMintInfo(rpc: Rpc<SolanaRpcApi>, mint: Address) { | ||
| const accountInfo = await rpc | ||
| .getAccountInfo(mint, { encoding: 'base64' }) | ||
| .send(); | ||
|
|
||
| if (!accountInfo.value) { | ||
| throw new Error(`Mint account ${mint} not found`); | ||
| } | ||
|
|
||
| // Parse mint data to get decimals (simplified parsing) | ||
| // In Token-2022, decimals are at offset 44 in the mint account data | ||
| const data = Buffer.from(accountInfo.value.data[0], 'base64'); | ||
| const decimals = data[44]; | ||
|
|
||
| return { | ||
| decimals, | ||
| data: accountInfo.value, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.