Skip to content

Equivalent code in JS

  • Create a new cli wallet

    solana-keygen new
  • Set the RPC url

    solana config set --url https://api.devnet.solana.com
  • Create an empty JS file

    npm init -y
    touch index.js
  • Install dependencies

    npm install @solana/web3.js @solana/spl-token
  • Write a function to airdrop yourself some solana

    const {Connection, LAMPORTS_PER_SOL, clusterApiUrl, PublicKey} = require('@solana/web3.js');
    const connection = new Connection(clusterApiUrl('devnet'));
    async function airdrop(publicKey, amount) {
    const airdropSignature = await connection.requestAirdrop(new PublicKey(publicKey), amount);
    await connection.confirmTransaction({signature: airdropSignature})
    }
    airdrop("GokppTzVZi2LT1MSTWoEprM4YLDPy7wQ478Rm3r77yEw", LAMPORTS_PER_SOL).then(signature => {
    console.log('Airdrop signature:', signature);
    });
  • Check your balance

    solana balance
  • Create token mint

    const { createMint } = require('@solana/spl-token');
    const { Keypair, Connection, clusterApiUrl, TOKEN_PROGRAM_ID } = require('@solana/web3.js');
    const payer = Keypair.fromSecretKey(Uint8Array.from([102,144,169,42,220,87,99,85,100,128,197,17,41,234,250,84,87,98,161,74,15,249,83,6,120,159,135,22,46,164,204,141,234,217,146,214,61,187,254,97,124,111,61,29,54,110,245,186,11,253,11,127,213,20,73,8,25,201,22,107,4,75,26,120]));
    const mintAthority = payer;
    const connection = new Connection(clusterApiUrl('devnet'));
    async function createMintForToken(payer, mintAuthority) {
    const mint = await createMint(
    connection,
    payer,
    mintAuthority,
    null,
    6,
    TOKEN_PROGRAM_ID
    );
    console.log('Mint created at', mint.toBase58());
    return mint;
    }
    async function main() {
    const mint = await createMintForToken(payer, mintAthority.publicKey);
    }
    main();

    Screenshot 2024-08-23 at 4.44.15 PM.png

  • Verify token mint on chain

    Screenshot 2024-08-23 at 4.42.55 PM.png

    Screenshot 2024-08-23 at 4.58.03 PM.png

  • Create an associated token account, mint some tokens

    const { createMint, getOrCreateAssociatedTokenAccount, mintTo } = require('@solana/spl-token');
    const { Keypair, Connection, clusterApiUrl, TOKEN_PROGRAM_ID, PublicKey } = require('@solana/web3.js');
    const payer = Keypair.fromSecretKey(Uint8Array.from([102,144,169,42,220,87,99,85,100,128,197,17,41,234,250,84,87,98,161,74,15,249,83,6,120,159,135,22,46,164,204,141,234,217,146,214,61,187,254,97,124,111,61,29,54,110,245,186,11,253,11,127,213,20,73,8,25,201,22,107,4,75,26,120]));
    const mintAthority = payer;
    const connection = new Connection(clusterApiUrl('devnet'));
    async function createMintForToken(payer, mintAuthority) {
    const mint = await createMint(
    connection,
    payer,
    mintAuthority,
    null,
    6,
    TOKEN_PROGRAM_ID
    );
    console.log('Mint created at', mint.toBase58());
    return mint;
    }
    async function mintNewTokens(mint, to, amount) {
    const tokenAccount = await getOrCreateAssociatedTokenAccount(
    connection,
    payer,
    mint,
    new PublicKey(to)
    );
    console.log('Token account created at', tokenAccount.address.toBase58());
    await mintTo(
    connection,
    payer,
    mint,
    tokenAccount.address,
    payer,
    amount
    )
    console.log('Minted', amount, 'tokens to', tokenAccount.address.toBase58());
    }
    async function main() {
    const mint = await createMintForToken(payer, mintAthority.publicKey);
    await mintNewTokens(mint, mintAthority.publicKey, 100);
    }
    main();

    Screenshot 2024-08-23 at 5.18.14 PM.png

  • Check your balances in the explorer

    Screenshot 2024-08-23 at 5.20.41 PM.png

  • Import the token in Phantom and see the balances

    Screenshot 2024-08-23 at 5.22.13 PM.png