Skip to content

Instantly share code, notes, and snippets.

@lakshmankashyap
Forked from zurgl/mint.js
Created January 5, 2022 05:46
Show Gist options
  • Save lakshmankashyap/37f33f2fa223d2e93ef89b6fc29a7924 to your computer and use it in GitHub Desktop.
Save lakshmankashyap/37f33f2fa223d2e93ef89b6fc29a7924 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);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment