Create Tokens With The Token Program
date
Jul 9, 2024
slug
create-solana-token
status
Published
tags
Solana
NodeJs
summary
• You may recall SOL is the 'native token' of Solana. All other tokens, fungible and non-fungible tokens (NFTs), are called SPL Tokens
• The Token Program contains instructions for creating and interacting with SPL Tokens
• Token Mints are accounts that define a specific token. This includes information about the token itself (like how many decimals it has), the account allowed to mint more tokens (called the mint authority), and where to find more information about the token like a description, image, etc. The mint authority can use the token mint to make more tokens!
• Token Accounts hold tokens of a specific Token Mint. For most users, their balances of each token mint are stored in Associated Token Accounts - accounts with addresses made from their wallet address and the token's mint.
• Creating Token Mints and Token Accounts requires allocating rent in SOL. The rent for a Token Account can be refunded when the account is closed, however, Token Mints currently cannot be closed.
type
Post
Token Mint
const tokenMint = await createMint( connection, payer, mintAuthority, freezeAuthority, decimal );
import * as web3 from '@solana/web3' import * as token from '@solana/spl-token' async function buildCreateMintTransaction( connection: web3.Connection, payer: web3.PublicKey, decimals: number ): Promise<web3.Transaction> { const lamports = await token.getMinimumBalanceForRentExemptMint(connection); const accountKeypair = web3.Keypair.generate(); const programId = token.TOKEN_PROGRAM_ID const transaction = new web3.Transaction().add( web3.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: accountKeypair.publicKey, space: token.MINT_SIZE, lamports, programId, }), token.createInitializeMintInstruction( accountKeypair.publicKey, decimals, payer, payer, programId ) ); return transaction }
const tokenAccount = await createAccount( connection, payer, mint, owner, keypair );
The createAccount function returns the publicKey of the new token account. This function requires the following arguments:
- connection - the JSON-RPC connection to the cluster
- payer - the account of the payer for the transaction
- mint - the token mint that the new token account is associated with
- owner - the account of the owner of the new token account
- keypair - this is an optional parameter for specifying the new token account address. If no keypair is provided, the createAccount function defaults to a derivation from the associated mint and owner accounts.
import * as web3 from '@solana/web3' import * as token from '@solana/spl-token' async function buildCreateTokenAccountTransaction( connection: web3.Connection, payer: web3.PublicKey, mint: web3.PublicKey ): Promise<web3.Transaction> { const mintState = await token.getMint(connection, mint) const accountKeypair = await web3.Keypair.generate() const space = token.getAccountLenForMint(mintState); const lamports = await connection.getMinimumBalanceForRentExemption(space); const programId = token.TOKEN_PROGRAM_ID const transaction = new web3.Transaction().add( web3.SystemProgram.createAccount({ fromPubkey: payer, newAccountPubkey: accountKeypair.publicKey, space, lamports, programId, }), token.createInitializeAccountInstruction( accountKeypair.publicKey, mint, payer, programId ) ); return transaction }
const associatedTokenAccount = await createAssociatedTokenAccount( connection, payer, mint, owner, );
import * as web3 from '@solana/web3' import * as token from '@solana/spl-token' async function buildCreateAssociatedTokenAccountTransaction( payer: web3.PublicKey, mint: web3.PublicKey ): Promise<web3.Transaction> { const associatedTokenAddress = await token.getAssociatedTokenAddress(mint, payer, false); const transaction = new web3.Transaction().add( token.createAssociatedTokenAccountInstruction( payer, associatedTokenAddress, payer, mint ) ) return transaction }
Mint Tokens
const transactionSignature = await mintTo( connection, payer, mint, destination, authority, amount );
The mintTo function returns a TransactionSignature that can be viewed on the Solana Explorer. The mintTo function requires the following arguments:
- connection - the JSON-RPC connection to the cluster
- payer - the account of the payer for the transaction
- mint - the token mint that the new token account is associated with
- destination - the token account that tokens will be minted to
- authority - the account authorized to mint tokens
- amount - the raw amount of tokens to mint outside of decimals, e.g. if Scrooge Coin mint's decimals property was set to 2 then to get 1 full Scrooge Coin you would need to set this property to 100
Transfer Tokens
const transactionSignature = await transfer( connection, payer, source, destination, owner, amount )
import * as web3 from '@solana/web3' import * as token from '@solana/spl-token' async function buildTransferTransaction( source: web3.PublicKey, destination: web3.PublicKey, owner: web3.PublicKey, amount: number ): Promise<web3.Transaction> { const transaction = new web3.Transaction().add( token.createTransferInstruction( source, destination, owner, amount, ) ) return transaction }