Code Monkey home page Code Monkey logo

token-creator's Introduction

Solana Token Creator

Creating a Solana Token

Demo

You can use the token creator application to create a token and sent it to your wallet. This application is purely for demonstration purposes.

Full Breakdown

Creating a Solana token requires the following steps:

  1. Creating a new Mint account
  2. Creating an associated token account
  3. Minting an amount of the token to the associated token account
  4. Adding the token metadata to the mint account

Creating a new mint account

Mint accounts hold information about the token such as how many decimals the token has and who can mint new tokens.

You can create a custom keypair for the mint account by grinding a keypair. This keypair's public key will be used to have a vanity address for the token. Create a new mint account with that key by first initializing the account and space for the mint account, then calling createInitializeMintInstruction. These instructions can be added and executed in the same transaction.

const Transaction = new Transaction().add(
  SystemProgram.createAccount({
      fromPubkey: publicKey,
      newAccountPubkey: mintKeypair.publicKey,
      space: MINT_SIZE,
      lamports: lamports,
      programId: TOKEN_PROGRAM_ID,
  }),
  createInitializeMintInstruction(
    mintKeypair.publicKey, 
    form.decimals, 
    publicKey, 
    publicKey, 
    TOKEN_PROGRAM_ID)
);

Creating an Associated Token Account

Associated Token Accounts are derived from the mint account's address. They hold the token's balance and can be used to transfer tokens from one wallet to another.

Normally when getting ATA's for a user, you would use getOrCreateAssociatedTokenAccount. This function will create the ATA if it doesn't exist.

In our case, we created a brand new mint account and know the ATA will not exist. So we can make a transaction with the createAssociatedTokenAccountInstruction, getting the derived address with getAssociatedTokenAddress.

const Transaction = new Transaction().add(
  createAssociatedTokenAccountInstruction(
    publicKey,
    tokenATA,
    publicKey,
    mintKeypair.publicKey,
  )
);

Minting Tokens

When you mint tokens, you increase the overall supply of the token. You can mint tokens by using createMintToInstruction in a transaction.

const Transaction = new Transaction().add(
  createMintToInstruction(
    mintKeypair.publicKey,
    tokenATA,
    publicKey,
    form.amount
  )
);

Adding the Token Metadata

When you create a token, you want to make sure that the token shows up in user's wallets with a name, ticker, and image. Solana uses the Token Metadata Program from Metaplex to achieve this.

The metadata account address is derived from the mint account. The metadata field requires a JSON file to be populated with at least the following:

{
  "name": "Coin name",
  "symbol": "Symbol",
  "image": "Image link"
}

You can find an example here.

To attach the metadata to the mint account, you can use CreateMetadataV2.

const createMetadataTransaction = new CreateMetadataV2(
  { feePayer: publicKey },
  {
    metadata: metadataPDA,
    metadataData: tokenMetadata,
    updateAuthority: publicKey,
    mint: mintKeypair.publicKey,
    mintAuthority: publicKey,
  },
)

tokenMetadata is the JSON file you want to attach to the mint account. Some people use Arweave to host their metadata. You can also use any other online storage solution.

All Done!

Following all of the steps above, you should now have a token minted.

The best part is that you can actually add all of these instructions to a single transaction:

const createMintTransaction = new Transaction().add(
  SystemProgram.createAccount({
      fromPubkey: publicKey,
      newAccountPubkey: mintKeypair.publicKey,
      space: MINT_SIZE,
      lamports: lamports,
      programId: TOKEN_PROGRAM_ID,
  }),
  createInitializeMintInstruction(
    mintKeypair.publicKey, 
    form.decimals, 
    publicKey, 
    publicKey, 
    TOKEN_PROGRAM_ID),
  createAssociatedTokenAccountInstruction(
    publicKey,
    tokenATA,
    publicKey,
    mintKeypair.publicKey,
  ),
  createMintToInstruction(
    mintKeypair.publicKey,
    tokenATA,
    publicKey,
    form.amount
  )
);

You can find the full source code for this application here.

Enjoy your new token!

token-creator's People

Contributors

bartosz-lipinski avatar jacobcreech avatar macalinao avatar donnysolana avatar arrowana avatar joncinque avatar marcnjaramillo avatar rmshea avatar tina1998612 avatar zyjliu avatar johnnieskywalker avatar johnrees avatar ebramanti avatar gabedottl avatar kevinrodriguez-io avatar max-block avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.