|
| 1 | +import { Command } from 'commander'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import ora from 'ora'; |
| 4 | +import { createStablecoinInitTransaction } from '@mosaic/sdk'; |
| 5 | +import { createSolanaClient } from '../../utils/rpc.js'; |
| 6 | +import { loadKeypair } from '../../utils/solana.js'; |
| 7 | +import { |
| 8 | + generateKeyPairSigner, |
| 9 | + signTransactionMessageWithSigners, |
| 10 | + type Address, |
| 11 | +} from 'gill'; |
| 12 | + |
| 13 | +interface StablecoinOptions { |
| 14 | + name: string; |
| 15 | + symbol: string; |
| 16 | + decimals: string; |
| 17 | + uri?: string; |
| 18 | + mintAuthority?: string; |
| 19 | + metadataAuthority?: string; |
| 20 | + pausableAuthority?: string; |
| 21 | + confidentialBalancesAuthority?: string; |
| 22 | + permanentDelegateAuthority?: string; |
| 23 | + mintKeypair?: string; |
| 24 | + rpcUrl?: string; |
| 25 | + keypair?: string; |
| 26 | +} |
| 27 | + |
| 28 | +export const createStablecoinCommand = new Command('stablecoin') |
| 29 | + .description('Create a new stablecoin with Token-2022 extensions') |
| 30 | + .requiredOption('-n, --name <name>', 'Token name') |
| 31 | + .requiredOption('-s, --symbol <symbol>', 'Token symbol') |
| 32 | + .option('-d, --decimals <decimals>', 'Number of decimals', '6') |
| 33 | + .option('-u, --uri <uri>', 'Metadata URI', '') |
| 34 | + .option( |
| 35 | + '--mint-authority <address>', |
| 36 | + 'Mint authority address (defaults to signer)' |
| 37 | + ) |
| 38 | + .option( |
| 39 | + '--metadata-authority <address>', |
| 40 | + 'Metadata authority address (defaults to mint authority)' |
| 41 | + ) |
| 42 | + .option( |
| 43 | + '--pausable-authority <address>', |
| 44 | + 'Pausable authority address (defaults to mint authority)' |
| 45 | + ) |
| 46 | + .option( |
| 47 | + '--confidential-balances-authority <address>', |
| 48 | + 'Confidential balances authority address (defaults to mint authority)' |
| 49 | + ) |
| 50 | + .option( |
| 51 | + '--permanent-delegate-authority <address>', |
| 52 | + 'Permanent delegate authority address (defaults to mint authority)' |
| 53 | + ) |
| 54 | + .option( |
| 55 | + '--mint-keypair <path>', |
| 56 | + 'Path to mint keypair file (generates new one if not provided)' |
| 57 | + ) |
| 58 | + .action(async (options: StablecoinOptions, command) => { |
| 59 | + const spinner = ora('Creating stablecoin...').start(); |
| 60 | + |
| 61 | + try { |
| 62 | + // Get global options from parent command |
| 63 | + const parentOpts = command.parent?.opts() || {}; |
| 64 | + const rpcUrl = options.rpcUrl || parentOpts.rpcUrl; |
| 65 | + const keypairPath = options.keypair || parentOpts.keypair; |
| 66 | + |
| 67 | + // Create Solana client with sendAndConfirmTransaction |
| 68 | + const { rpc, sendAndConfirmTransaction } = createSolanaClient(rpcUrl); |
| 69 | + |
| 70 | + // Load signer keypair |
| 71 | + const signerKeypair = await loadKeypair(keypairPath); |
| 72 | + const signerAddress = signerKeypair.address; |
| 73 | + |
| 74 | + spinner.text = 'Loading keypairs...'; |
| 75 | + |
| 76 | + // Generate or load mint keypair |
| 77 | + let mintKeypair; |
| 78 | + if (options.mintKeypair) { |
| 79 | + mintKeypair = await loadKeypair(options.mintKeypair); |
| 80 | + } else { |
| 81 | + mintKeypair = await generateKeyPairSigner(); |
| 82 | + } |
| 83 | + |
| 84 | + // Parse decimals |
| 85 | + const decimals = parseInt(options.decimals, 10); |
| 86 | + if (isNaN(decimals) || decimals < 0 || decimals > 9) { |
| 87 | + throw new Error('Decimals must be a number between 0 and 9'); |
| 88 | + } |
| 89 | + |
| 90 | + // Set authorities (default to signer if not provided) |
| 91 | + const mintAuthority = (options.mintAuthority || signerAddress) as Address; |
| 92 | + const metadataAuthority = (options.metadataAuthority || |
| 93 | + mintAuthority) as Address; |
| 94 | + const pausableAuthority = (options.pausableAuthority || |
| 95 | + mintAuthority) as Address; |
| 96 | + const confidentialBalancesAuthority = |
| 97 | + (options.confidentialBalancesAuthority || mintAuthority) as Address; |
| 98 | + const permanentDelegateAuthority = (options.permanentDelegateAuthority || |
| 99 | + mintAuthority) as Address; |
| 100 | + |
| 101 | + spinner.text = 'Building transaction...'; |
| 102 | + |
| 103 | + // Create stablecoin using Token class |
| 104 | + const transaction = await createStablecoinInitTransaction( |
| 105 | + rpc, |
| 106 | + options.name, |
| 107 | + options.symbol, |
| 108 | + decimals, |
| 109 | + options.uri || '', |
| 110 | + mintAuthority, |
| 111 | + mintKeypair, |
| 112 | + signerKeypair, |
| 113 | + metadataAuthority, |
| 114 | + pausableAuthority, |
| 115 | + confidentialBalancesAuthority, |
| 116 | + permanentDelegateAuthority |
| 117 | + ); |
| 118 | + |
| 119 | + spinner.text = 'Signing transaction...'; |
| 120 | + |
| 121 | + // Sign the transaction (buildTransaction includes signers but doesn't auto-sign) |
| 122 | + const signedTransaction = |
| 123 | + await signTransactionMessageWithSigners(transaction); |
| 124 | + |
| 125 | + spinner.text = 'Sending transaction...'; |
| 126 | + |
| 127 | + // Send and confirm transaction |
| 128 | + const signature = await sendAndConfirmTransaction(signedTransaction); |
| 129 | + |
| 130 | + spinner.succeed('Stablecoin created successfully!'); |
| 131 | + |
| 132 | + // Display results |
| 133 | + console.log(chalk.green('✅ Stablecoin Creation Successful')); |
| 134 | + console.log(chalk.cyan('📋 Details:')); |
| 135 | + console.log(` ${chalk.bold('Name:')} ${options.name}`); |
| 136 | + console.log(` ${chalk.bold('Symbol:')} ${options.symbol}`); |
| 137 | + console.log(` ${chalk.bold('Decimals:')} ${decimals}`); |
| 138 | + console.log(` ${chalk.bold('Mint Address:')} ${mintKeypair.address}`); |
| 139 | + console.log(` ${chalk.bold('Transaction:')} ${signature}`); |
| 140 | + |
| 141 | + console.log(chalk.cyan('🔐 Authorities:')); |
| 142 | + console.log(` ${chalk.bold('Mint Authority:')} ${mintAuthority}`); |
| 143 | + console.log( |
| 144 | + ` ${chalk.bold('Metadata Authority:')} ${metadataAuthority}` |
| 145 | + ); |
| 146 | + console.log( |
| 147 | + ` ${chalk.bold('Pausable Authority:')} ${pausableAuthority}` |
| 148 | + ); |
| 149 | + console.log( |
| 150 | + ` ${chalk.bold('Confidential Balances Authority:')} ${confidentialBalancesAuthority}` |
| 151 | + ); |
| 152 | + console.log( |
| 153 | + ` ${chalk.bold('Permanent Delegate Authority:')} ${permanentDelegateAuthority}` |
| 154 | + ); |
| 155 | + |
| 156 | + console.log(chalk.cyan('🛡️ Token Extensions:')); |
| 157 | + console.log(` ${chalk.green('✓')} Metadata`); |
| 158 | + console.log(` ${chalk.green('✓')} Pausable`); |
| 159 | + console.log(` ${chalk.green('✓')} Default Account State (Blocklist)`); |
| 160 | + console.log(` ${chalk.green('✓')} Confidential Balances`); |
| 161 | + console.log(` ${chalk.green('✓')} Permanent Delegate`); |
| 162 | + |
| 163 | + if (options.uri) { |
| 164 | + console.log(`${chalk.bold('Metadata URI:')} ${options.uri}`); |
| 165 | + } |
| 166 | + } catch (error) { |
| 167 | + spinner.fail('Failed to create stablecoin'); |
| 168 | + console.error( |
| 169 | + chalk.red('❌ Error:'), |
| 170 | + error instanceof Error ? error.message : 'Unknown error' |
| 171 | + ); |
| 172 | + process.exit(1); |
| 173 | + } |
| 174 | + }); |
0 commit comments