Skip to content

Equivalent code in JS

Course: Programs, Accounts and the Token Program - chapter 7

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();

Verify token mint on chain

Create an associated token account, mint some tokens
$1b

Check your balances in the explorer

Import the token in Phantom and see the balances