0

**The Code for generating, i surfed through stackover and the code base i found explaining the solution are of old version of the imported library

The code to generate an account, associate account & mint a token to it, each function is defined clearly and it does what it has been namesd as**

import * as web3 from "@solana/web3.js"
import * as token from "@solana/spl-token"
    
    
    
    async function createNewMint(
        connection: web3.Connection,
        payer: web3.Keypair,
        mintAuthority: web3.PublicKey,
        freezeAuthority: web3.PublicKey,
        decimals: number,
    ): Promise<web3.PublicKey> {
        const tokenMint = await token.createMint(
            connection,
            payer,
            mintAuthority,
            freezeAuthority,
            decimals
        );
    
        console.log(
            `Token Mint: https://explorer.solana.com/address/${tokenMint}?cluster=devnet`
        )
    
        return tokenMint
    }
    
    async function createTokenAccount(
        connection: web3.Connection,
        payer: web3.Keypair,
        mint: web3.PublicKey,
        owner: web3.PublicKey
    ) {
        const tokenAccount = await token.getOrCreateAssociatedTokenAccount(
            connection,
            payer,
            mint,
            owner
        )
    
        console.log(
            `Token Account: https://explorer.solana.com/address/${tokenAccount.address}?cluster=devnet`
        )
    
        return tokenAccount
    
        console.log("Token Account:", tokenAccount)
    }
    
    async function mintTokens(
        connection: web3.Connection,
        payer: web3.Keypair,
        mint: web3.PublicKey,
        destination: web3.PublicKey,
        authority: web3.Keypair,
        amount: number
    ) {
        const transactionSignature = await token.mintTo(
            connection,
            payer,
            mint,
            destination,
            authority,
            amount
        )
    
        console.log(
            `Mint Token Transaction: https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
        )
    }
    
    async function approveDelegate(
      connection: web3.Connection,
      payer: web3.Keypair,
      account: web3.PublicKey,
      delegate: web3.PublicKey,
      owner: web3.Signer | web3.PublicKey,
      amount: number
    ) {
      const transactionSignature = await token.approve(
        connection,
        payer,
        account,
        delegate,
        owner,
        amount
      )
    
      console.log(
        `Approve Delegate Transaction: https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
      )
    }
    
    async function revokeDelegate(
      connection: web3.Connection,
      payer: web3.Keypair,
      account: web3.PublicKey,
      owner: web3.Signer | web3.PublicKey,
    ) {
      const transactionSignature = await token.revoke(
        connection,
        payer,
        account,
        owner,
      )
    
      console.log(
        `Revote Delegate Transaction: https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
      )
    }
    
    async function transferTokens(
        connection: web3.Connection,
        payer: web3.Keypair,
        source: web3.PublicKey,
        destination: web3.PublicKey,
        owner: web3.Keypair,
        amount: number
    ) {
        const transactionSignature = await token.transfer(
            connection,
            payer,
            source,
            destination,
            owner,
            amount
        )
    
        console.log(
            `Transfer Transaction: https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
        )
    }
    
    async function burnTokens(
        connection: web3.Connection,
        payer: web3.Keypair,
        account: web3.PublicKey,
        mint: web3.PublicKey,
        owner: web3.Keypair,
        amount: number
    ) {
        const transactionSignature = await token.burn(
            connection,
            payer,
            account,
            mint,
            owner,
            amount
        )
    
        console.log(
            `Burn Transaction: https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`
        )
    }
    
    async function main() {
        const connection = new web3.Connection(web3.clusterApiUrl("devnet"))
        const user = await initializeKeypair(connection)
    
        const mint = await createNewMint(
            connection,
            user,
            user.publicKey,
            user.publicKey,
            2
        )
        
        const mintInfo = await token.getMint(connection, mint);
    
        const tokenAccount = await createTokenAccount(
            connection,
            user,
            mint,
            user.publicKey
        )
    
        await mintTokens(
          connection,
          user,
          mint,
          tokenAccount.address,
          user,
          100 * 10 ** mintInfo.decimals
        )
        
        
        const delegate = web3.Keypair.generate();
        await approveDelegate(
          connection,
          user,
          tokenAccount.address,
          delegate.publicKey,
          user.publicKey,
          50 * 10 ** mintInfo.decimals
        )
    
        const receiver = web3.Keypair.generate().publicKey
        const receiverTokenAccount = await createTokenAccount(
            connection,
            user,
            mint,
            receiver
        )
    
        await transferTokens(
          connection,
          user,
          tokenAccount.address,
          receiverTokenAccount.address,
          delegate,
          50 * 10 ** mintInfo.decimals
        )
    
        await revokeDelegate(
          connection,
          user,
          tokenAccount.address,
          user.publicKey,
        )
    
        await burnTokens(
          connection,
          user,
          tokenAccount.address,
          mint,
          user,
          5 * 10 ** mintInfo.decimals
        )
    }

1 Answer 1

0

I saw an online tool "supposedly" for that purpose. I did not try it yet but it seems to come from Solana. I have no idea if it works or not but it looks real, from my point of view, due to the name "solana" in the web address (but, it can be anything, as it is not directly on the web site of solana):

https://solana-token-creator.vercel.app/

I already gave this answer to another colleague that had the same problem as you have now. Unfortunately, I did not have time to test it yet.

If it works well, please tell me.

1
  • I was able to find a way to mint the tokens and add the metadata using the code from quicknode. The library used here are different from the way different articles explained. The quick node article is fairly new dated January 5th 2024. You can find the link here: quicknode.com/guides/solana-development/spl-tokens/…
    – Maximctrl
    Commented Apr 5 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.