Skip to content
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

Update index.js #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 62 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
import { clusterApiUrl, Connection, Keypair, LAMPORTS_PER_SOL } from '@solana/web3.js';
import { createMint, getOrCreateAssociatedTokenAccount, mintTo, transfer } from '@solana/spl-token';

(async () => {
// Step 1: Connect to cluster and generate a new Keypair


// Step 2: Airdrop SOL into your from wallet

// Step 3: Create new token mint and get the token account of the fromWallet address
//If the token account does not exist, create it


//Step 4: Mint a new token to the from account


//Step 5: Get the token account of the to-wallet address and if it does not exist, create it


//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

})();
async function main() {
try {
// Step 1: Connect to Solana Devnet and generate new keypairs
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const fromWallet = Keypair.generate();
const toWallet = Keypair.generate();

// Step 2: Airdrop SOL into fromWallet
console.log(`Requesting airdrop for ${fromWallet.publicKey.toBase58()}`);
await requestAirdrop(connection, fromWallet.publicKey);

// Step 3: Create new token mint and get fromTokenAccount
const mint = await createMint(connection, fromWallet, fromWallet.publicKey, null, 9);
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, fromWallet.publicKey);

// Step 4: Mint tokens to fromTokenAccount
const amountToMint = 1000000000; // Amount in smallest unit (lamports)
let mintSignature = await mintTo(connection, fromWallet, mint, fromTokenAccount.address, fromWallet.publicKey, amountToMint);
console.log('Mint transaction:', mintSignature);

// Step 5: Get or create toTokenAccount
const toTokenAccount = await getOrCreateAssociatedTokenAccount(connection, fromWallet, mint, toWallet.publicKey);

// Step 6: Transfer tokens from fromTokenAccount to toTokenAccount
let transferSignature = await transfer(connection, fromWallet, fromTokenAccount.address, toTokenAccount.address, fromWallet.publicKey, amountToMint);
console.log('Transfer transaction:', transferSignature);

} catch (error) {
console.error('Error:', error.message);
}
}

async function requestAirdrop(connection, publicKey) {
const lamports = LAMPORTS_PER_SOL; // Request 1 SOL (in lamports)
let retries = 5;
while (retries > 0) {
try {
const signature = await connection.requestAirdrop(publicKey, lamports);
await connection.confirmTransaction(signature, { commitment: 'confirmed' });
console.log('Airdrop successful:', signature);
return;
} catch (error) {
console.error('Error requesting airdrop:', error.message);
if (error.message.includes('429')) {
console.log('Rate limit exceeded. Waiting before retrying...');
await sleep(retries * 4000); // Exponential backoff with longer delay
retries--;
} else {
throw new Error('Airdrop request failed.');
}
}
}
throw new Error('Max retries reached. Airdrop request failed.');
}

// Utility function for delay
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

// Execute main function
main();