In this TypeScript tutorial, we’ll walk through creating a new token mint, transferring tokens between accounts, and exploring the Solana blockchain’s capabilities. By the end, you’ll be able to create and manage tokens with ease.
Prerequisites
Before we dive into the code, ensure you have the following set up:
- Node.js: Make sure you have Node.js installed on your machine.
- Solana CLI: Install the Solana CLI to interact with the Solana blockchain.
- TypeScript: Ensure you have TypeScript installed in your project.
Step 1: Setting Up Your TypeScript Project
First, initialize your TypeScript project:
npm init -y
npm install --save-dev typescript
npx tsc --init
Step 2: Installing Dependencies
Install the required libraries for interacting with the Solana blockchain:
npm install @solana/web3.js @solana/spl-token
Step 3: Writing the Code
Create a new file, create-tokens.ts
, and add the following code:
import { TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";
import { Connection, Keypair, PublicKey, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import { airdrop } from "../airdrop"; // Import your airdrop function here
const createMint = async (mintWallet: Keypair) => {
const connection = new Connection("http://127.0.0.1:8899", "confirmed");
const creatorToken = await Token.createMint(connection, mintWallet, mintWallet.publicKey, null, 8, TOKEN_PROGRAM_ID);
return creatorToken.publicKey;
}
const transferTokens = async (tokenAddress: PublicKey, mintWallet: Keypair, receiver: PublicKey) => {
const connection = new Connection("http://127.0.0.1:8899", "confirmed");
const creatorToken = new Token(connection, tokenAddress, TOKEN_PROGRAM_ID, mintWallet);
const mintTokenAccount = await creatorToken.getOrCreateAssociatedAccountInfo(mintWallet.publicKey);
await creatorToken.mintTo(mintTokenAccount.address, mintWallet.publicKey, [], 100000000);
const receiverTokenAccount = await creatorToken.getOrCreateAssociatedAccountInfo(receiver);
console.log(`ReceiverTokenAccount address: ${receiverTokenAccount.address}`);
const transaction = new Transaction().add(
Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
mintTokenAccount.address,
receiverTokenAccount.address,
mintWallet.publicKey,
[],
100000000
)
);
await sendAndConfirmTransaction(connection, transaction, [mintWallet], { commitment: "confirmed" });
}
(async () => {
const mintWallet = await Keypair.generate();
await airdrop(mintWallet.publicKey, 5); // Airdrop SOL to the mint wallet
const creatorTokenAddress = await createMint(mintWallet);
await transferTokens(creatorTokenAddress, mintWallet, new PublicKey("<your-wallet-address>"));
console.log(`Creator token address: ${creatorTokenAddress}`);
console.log(`Mint wallet address: ${mintWallet.publicKey.toBase58()}`);
})();
Step 4: Building and Running Your Code
Build your TypeScript project:
npm run build
Run the compiled JavaScript:
node dist/create-tokens.js
Conclusion
You’ve successfully created a token mint and transferred tokens on the Solana blockchain using TypeScript. You can view the created token on Solana Explorer here. This guide provides a foundation for working with tokens on Solana, and you can expand upon it to build more complex applications.
For additional resources on Solana, check out my previous posts on Airdrop and Wallet Balance.
Happy coding! 🚀