Skip to content

Instantly share code, notes, and snippets.

@zurgl
Created July 3, 2021 01:19
Show Gist options
  • Save zurgl/abca3e9883588f405abb1bec15bcb89e to your computer and use it in GitHub Desktop.
Save zurgl/abca3e9883588f405abb1bec15bcb89e to your computer and use it in GitHub Desktop.
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(
myKeypair.publicKey
)
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);
console.log('done');
}, 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(
web3.SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to.publicKey,
lamports: web3.LAMPORTS_PER_SOL / 100,
})
);
// Sign transaction, broadcast, and confirm
var signature = await web3.sendAndConfirmTransaction(
connection,
transaction,
[from],
{commitment: 'confirmed'}
);
console.log("SIGNATURE", signature);
console.log("SUCCESS");
}, 1000);
})();
@VikashAnandJha
Copy link

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

@Balajiselvakumar
Copy link

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(
web3.SystemProgram.transfer({
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