create_token
This is the pseudo-code for creating a token using @solana /web3.js in pure JavaScript.
import { SystemProgram, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RENT_PUBKEY, TransactionInstruction } from '@solana/web3.js';
import { Buffer } from 'buffer';
import * as borsh from '@coral-xyz/borsh';
import { FEE_RECEIVER, METADATA_PROGRAM_ID, PROGRAM_ID } from '../utils/constants';
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
getAssociatedTokenAddressSync,
TOKEN_2022_PROGRAM_ID
} from '@solana/spl-token';
import { sendAndConfirmTx } from '../utils/conn';
import { getClaimAuthAddress, getConfigAddress, getFeedAddress, getMetadataAddress } from '../utils/pda';
const discriminator = Buffer.from([84, 52, 204, 228, 24, 140, 234, 75]);
const createTokenLayout = borsh.struct([
borsh.str('name'),
borsh.str('symbol'),
borsh.str('uri'),
borsh.u16('transfer_fee_basis_points'),
borsh.u8('fee_ratio'),
borsh.u32('claim_interval'),
borsh.u8('claim_type'),
borsh.u8('platform_type'),
]);
export const createToken = async (connection, wallet, mintKeypair, claimMint, otherParams) => {
// encode params
const buffer = Buffer.alloc(1000);
createTokenLayout.encode(otherParams, buffer);
const serializedData = buffer.slice(0, createTokenLayout.getSpan(buffer));
// @ts-ignore
const instructionData = Buffer.concat([discriminator, serializedData]);
// accounts
const claimAuthority = getClaimAuthAddress(mintKeypair.publicKey);
const mintAta = getAssociatedTokenAddressSync(mintKeypair.publicKey, wallet.publicKey, false, TOKEN_2022_PROGRAM_ID);
const feed = getFeedAddress(claimMint);
const accounts = [
{ pubkey: wallet.publicKey, isSigner: true, isWritable: true },
{ pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },
{ pubkey: getConfigAddress(), isSigner: false, isWritable: false },
{ pubkey: claimAuthority, isSigner: false, isWritable: true },
{ pubkey: mintAta, isSigner: false, isWritable: true },
{ pubkey: feed, isSigner: false, isWritable: false },
{ pubkey: getMetadataAddress(mintKeypair.publicKey), isSigner: false, isWritable: true },
{ pubkey: FEE_RECEIVER, isSigner: false, isWritable: true },
{ pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: ASSOCIATED_TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
{ pubkey: METADATA_PROGRAM_ID, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false },
];
// create_token instruction
const instruction = new TransactionInstruction({
keys: accounts,
programId: PROGRAM_ID,
data: instructionData,
});
// send and confirm transaction
return sendAndConfirmTx(connection, wallet, [instruction], [mintKeypair]);
};Last updated