Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
solana js examples
const web3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
(async () => {
//create connection to devnet
const connection = new web3.Connection(web3.clusterApiUrl("devnet"));
//generate keypair and airdrop 1000000000 Lamports (1 SOL)
const myKeypair = web3.Keypair.generate();
await connection.requestAirdrop(myKeypair.publicKey, 1000000000);
console.log('solana public address: ' + myKeypair.publicKey.toBase58());
//set timeout to account for airdrop finalization
let mint;
var myToken
setTimeout(async function(){
//create mint
mint = await splToken.Token.createMint(connection, myKeypair, myKeypair.publicKey, null, 9, splToken.TOKEN_PROGRAM_ID)
console.log('mint public address: ' + mint.publicKey.toBase58());
//get the token accont of this solana address, if it does not exist, create it
myToken = await mint.getOrCreateAssociatedAccountInfo(
console.log('token public address: ' + myToken.address.toBase58());
//minting 100 new tokens to the token address we just created
await mint.mintTo(myToken.address, myKeypair.publicKey, [], 1000000000);
}, 20000);
var web3 = require("@solana/web3.js");
(async () => {
// Connect to cluster
var connection = new web3.Connection(web3.clusterApiUrl("devnet"));
// Construct a `Keypair` from secret key
var from = web3.Keypair.generate();
await connection.requestAirdrop(from.publicKey, 1000000000);
// Generate a new random public key
var to = web3.Keypair.generate();
await connection.requestAirdrop(to.publicKey, 1000000000);
//waiting 1000 miliseconds to make the the airdrop transactions are complete
setTimeout(async function(){
// Add transfer instruction to transaction
var transaction = new web3.Transaction().add(
fromPubkey: from.publicKey,
toPubkey: to.publicKey,
lamports: web3.LAMPORTS_PER_SOL / 100,
// Sign transaction, broadcast, and confirm
var signature = await web3.sendAndConfirmTransaction(
{commitment: 'confirmed'}
console.log("SIGNATURE", signature);
}, 1000);
Copy link

HassanRaza02 commented Dec 14, 2021

That's Awesome!! but we don't actually generate a new wallet each time. How it can be done using phantom wallet ??

Copy link

VikashAnandJha commented Dec 19, 2021

How to get List of other tokens in the same wallet address (minted)

Copy link

Balajiselvakumar commented Jan 30, 2022

i have one wallet key(public key) and i want to send 1 sol to another walletkey. so how to write code?

var transaction = new web3.Transaction().add(
fromPubkey: from.publicKey,
toPubkey: to.publicKey,//i want to put here my own wallet key so how to write code ?is there any syntax for this?
lamports: web3.LAMPORTS_PER_SOL / 100,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment