DeveloperBreeze

Introduction

Solana is known for its high throughput and low transaction costs, making it an excellent platform for creating and managing custom tokens. In this tutorial, we'll use the Solana CLI and the SPL Token Program to create, mint, and transfer a custom token on the Solana Devnet. You'll learn how to interact with the blockchain using the command-line interface and manage your tokens effectively.

Objectives

By the end of this tutorial, you will:

  • Set up your Solana development environment.
  • Create a new token on the Solana blockchain.
  • Mint new tokens to your wallet.
  • Transfer tokens to another wallet.

Prerequisites

  • Ubuntu system with Solana CLI installed (as outlined in the previous tutorial).
  • Basic understanding of blockchain and token concepts.
  • Solana wallet with some SOL tokens for transaction fees (you can request SOL from the Solana faucet for testing).

Step 1: Set Up Your Environment

  1. Switch to Devnet:

Ensure you're working on the Solana Devnet to avoid any accidental transactions on the Mainnet.

   solana config set --url https://api.devnet.solana.com
  1. Create a New Wallet:

If you don't have a Solana wallet, create one using the following command:

   solana-keygen new --outfile ~/my-solana-wallet.json

This command will generate a new keypair file and save it to your specified path. Make sure to back up this file securely.

  1. Get Some SOL Tokens:

You need some SOL tokens in your wallet to pay for transaction fees. Request tokens from the Solana Devnet faucet:

   solana airdrop 2 ~/my-solana-wallet.json

Replace ~/my-solana-wallet.json with the path to your wallet keypair file if different.

Step 2: Install the SPL Token CLI

  1. Install the SPL Token CLI:

The SPL Token CLI is a command-line tool for managing SPL tokens. Install it using Cargo:

   cargo install spl-token-cli
  1. Verify the Installation:

Check that the SPL Token CLI is installed correctly:

   spl-token --version

You should see the version number of the SPL Token CLI.

Step 3: Create a New Token

  1. Create the Token:

Use the SPL Token CLI to create a new token. This command will generate a new token mint address:

   spl-token create-token

Note the token mint address returned by this command, as you'll need it for minting and transferring tokens.

  1. Create a Token Account:

Create a token account to hold your newly created tokens. This account is associated with your wallet:

   spl-token create-account <TOKEN_MINT_ADDRESS>

Replace <TOKEN_MINT_ADDRESS> with your actual token mint address.

Step 4: Mint Tokens

  1. Mint New Tokens:

Mint tokens to your token account. Specify the number of tokens you want to mint:

   spl-token mint <TOKEN_MINT_ADDRESS> <AMOUNT> <RECIPIENT_ADDRESS>

Replace <TOKEN_MINT_ADDRESS> with your token mint address, <AMOUNT> with the number of tokens you want to mint, and <RECIPIENT_ADDRESS> with your token account address.

Step 5: Transfer Tokens

  1. Create a Recipient Wallet:

If you don't have a recipient wallet, create one and get its token account:

   solana-keygen new --outfile ~/recipient-wallet.json
   spl-token create-account <TOKEN_MINT_ADDRESS> --owner ~/recipient-wallet.json

This will create a new wallet and a token account associated with it.

  1. Transfer Tokens:

Transfer tokens from your account to the recipient's token account:

   spl-token transfer <TOKEN_MINT_ADDRESS> <AMOUNT> <RECIPIENT_TOKEN_ACCOUNT>

Replace <RECIPIENT_TOKEN_ACCOUNT> with the token account address of the recipient.

Conclusion

Congratulations! You have successfully created, minted, and transferred a custom token on the Solana blockchain. This tutorial introduced you to the SPL Token Program and demonstrated how to manage tokens using the Solana and SPL Token CLIs. With this knowledge, you can start building more complex decentralized applications on Solana and explore its capabilities further.

Solana's speed and efficiency make it an ideal platform for token creation and management. As you continue exploring Solana, consider integrating tokens into your dApps to provide users with unique features and functionalities.

Continue Reading

Handpicked posts just for you — based on your current read.

Tracking Solana Address for New Trades and Amounts

async function trackAddress(address) {
  try {
    const publicKey = new solanaWeb3.PublicKey(address);

    console.log(`Tracking address: ${publicKey.toBase58()}`);

    let lastSignature = '';

    // Fetch initial transactions
    const signatures = await connection.getSignaturesForAddress(publicKey, { limit: 1 });
    if (signatures.length > 0) {
      lastSignature = signatures[0].signature;
    }

    // Monitor for new transactions
    setInterval(async () => {
      const signatures = await connection.getSignaturesForAddress(publicKey, {
        limit: 10,
      });

      for (const signatureInfo of signatures) {
        if (signatureInfo.signature !== lastSignature) {
          const transaction = await connection.getConfirmedTransaction(signatureInfo.signature);

          if (transaction) {
            const { meta, transaction: tx } = transaction;

            const instructions = tx.message.instructions;
            const postBalances = meta.postBalances;
            const preBalances = meta.preBalances;

            // Check if the transaction involves token transfers
            instructions.forEach((instruction, index) => {
              const programId = instruction.programId.toBase58();

              // Solana's SPL Token Program ID
              if (programId === solanaWeb3.TOKEN_PROGRAM_ID.toBase58()) {
                const data = Buffer.from(instruction.data);
                const command = data.readUInt8(0);

                // 3 represents the Token Transfer instruction
                if (command === 3) {
                  const amount = data.readUInt64LE(1);
                  const fromAccount = instruction.keys[0].pubkey.toBase58();
                  const toAccount = instruction.keys[1].pubkey.toBase58();

                  const balanceChange = (preBalances[index] - postBalances[index]) / solanaWeb3.LAMPORTS_PER_SOL;

                  console.log(`New Trade Detected!`);
                  console.log(`- Signature: ${signatureInfo.signature}`);
                  console.log(`- From: ${fromAccount}`);
                  console.log(`- To: ${toAccount}`);
                  console.log(`- Amount: ${balanceChange} SOL`);
                }
              }
            });
          }

          lastSignature = signatureInfo.signature;
        }
      }
    }, 10000); // Check every 10 seconds
  } catch (error) {
    console.error('Error tracking address:', error);
  }
}

// Replace with the Solana address you want to track
const addressToTrack = 'YourSolanaAddressHere';
trackAddress(addressToTrack);
  • Fetch Transactions: We fetch the transaction signatures for the specified address and track them using getSignaturesForAddress.
  • Monitor New Transactions: Using setInterval, we periodically fetch new transactions and check for token transfer instructions.
  • Parse Transactions: We parse each transaction's instructions to identify token transfers and log the details to the console.
  • Token Program ID: We check if the program ID matches Solana's SPL Token Program to identify relevant instructions.

Aug 09, 2024 Tutorial

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!