Create Tokens on Solana with TypeScript

sumana10

Sumana

Posted on August 16, 2024

Create Tokens on Solana with TypeScript

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:

  1. Node.js: Make sure you have Node.js installed on your machine.
  2. Solana CLI: Install the Solana CLI to interact with the Solana blockchain.
  3. 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
Enter fullscreen mode Exit fullscreen mode

Step 2: Installing Dependencies

Install the required libraries for interacting with the Solana blockchain:

npm install @solana/web3.js @solana/spl-token
Enter fullscreen mode Exit fullscreen mode

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()}`);
})();
Enter fullscreen mode Exit fullscreen mode

Step 4: Building and Running Your Code

Build your TypeScript project:

npm run build
Enter fullscreen mode Exit fullscreen mode

Run the compiled JavaScript:

node dist/create-tokens.js
Enter fullscreen mode Exit fullscreen mode

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! 🚀

💖 💪 🙅 🚩
sumana10
Sumana

Posted on August 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related