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.

Prerequisit…


This content originally appeared on DEV Community and was authored by Sumana

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

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


This content originally appeared on DEV Community and was authored by Sumana


Print Share Comment Cite Upload Translate Updates
APA

Sumana | Sciencx (2024-08-16T18:23:27+00:00) Create Tokens on Solana with TypeScript. Retrieved from https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/

MLA
" » Create Tokens on Solana with TypeScript." Sumana | Sciencx - Friday August 16, 2024, https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/
HARVARD
Sumana | Sciencx Friday August 16, 2024 » Create Tokens on Solana with TypeScript., viewed ,<https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/>
VANCOUVER
Sumana | Sciencx - » Create Tokens on Solana with TypeScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/
CHICAGO
" » Create Tokens on Solana with TypeScript." Sumana | Sciencx - Accessed . https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/
IEEE
" » Create Tokens on Solana with TypeScript." Sumana | Sciencx [Online]. Available: https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/. [Accessed: ]
rf:citation
» Create Tokens on Solana with TypeScript | Sumana | Sciencx | https://www.scien.cx/2024/08/16/create-tokens-on-solana-with-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.