diff --git a/index.js b/index.js index b790bc5..8c2d23c 100644 --- a/index.js +++ b/index.js @@ -3,21 +3,62 @@ import { createMint, getOrCreateAssociatedTokenAccount, mintTo, transfer } from (async () => { // Step 1: Connect to cluster and generate a new Keypair - + const connection = new Connection(clusterApiUrl('devnet'), 'confirmed'); + + const fromWallet = Keypair.generate(); + const toWallet = Keypair.generate(); // Step 2: Airdrop SOL into your from wallet + + const fromAirdropSignature = await connection.requestAirdrop(fromWallet.publicKey, LAMPORTS_PER_SOL); + // Wait for airdrop confirmation + await connection.confirmTransaction(fromAirdropSignature, { commitment: "confirmed" }); + + // Step 3: Create new token mint and get the token account of the fromWallet address //If the token account does not exist, create it + const mint = await createMint(connection, fromWallet, fromWallet.publicKey, null, 9); + const fromTokenAccount = await getOrCreateAssociatedTokenAccount( + connection, + fromWallet, + mint, + fromWallet.publicKey +) + //Step 4: Mint a new token to the from account - + let signature = await mintTo( + connection, + fromWallet, + mint, + fromTokenAccount.address, + fromWallet.publicKey, + 1000000000, + [] + ); + console.log('mint tx:', signature); //Step 5: Get the token account of the to-wallet address and if it does not exist, create it - + const toTokenAccount = await getOrCreateAssociatedTokenAccount( + connection, + fromWallet, + mint, + toWallet.publicKey + ); //Step 6: Transfer the new token to the to-wallet's token account that was just created + // Transfer the new token to the "toTokenAccount" we just created - -})(); \ No newline at end of file + signature = await transfer( + connection, + fromWallet, + fromTokenAccount.address, + toTokenAccount.address, + fromWallet.publicKey, + 1000000000, + [] +); +console.log('transfer tx:', signature); +})();