Skip to content

Instantly share code, notes, and snippets.

@mihaiserban
Forked from beautyfree/snippets.js
Created December 7, 2021 10:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mihaiserban/1551402492b80ffed0d9a7ab84e2fd66 to your computer and use it in GitHub Desktop.
Solana Web3 Snippets
import {
Account,
clusterApiUrl,
Connection,
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
} from '@solana/web3.js';
// Create connection
function createConnection(url = clusterApiUrl('devnet')) {
return new Connection(url);
}
const connection = createConnection();
// Generate account
import * as bip39 from 'bip39';
import nacl from 'tweetnacl';
function generateAccount(mnemonic) {
const seed = bip39.mnemonicToSeedSync(mnemonic); // prefer async mnemonicToSeed
const keyPair = nacl.sign.keyPair.fromSeed(seed.slice(0, 32));
return new Account(keyPair.secretKey);
}
const mnemonic = bip39.generateMnemonic();
console.log('Your password:', mnemonic);
const account = generateAccount(mnemonic);
console.log('Account:', account);
// Get balance
function getBalance(connection, publicKey) {
return connection.getBalance(publicKey);
}
const balance = getBalance(connection, account.publicKey);
console.log('Balance:', balance);
// Airdrop request
// lamports is 0.000000001 of SOL
function requestAirdrop(connection, publicKey, lamports = 1000000) {
return connection.requestAirdrop(publicKey, lamports);
}
requestAirdrop(connection, account.publicKey);
// Send transaction
async function sendTransaction(connection, recipientPublicKey, recipientAmount, payer) {
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: payer.publicKey,
toPubkey: new PublicKey(recipientPublicKey),
lamports: recipientAmount,
}),
);
const signature = await sendAndConfirmTransaction(connection, transaction, [payer]);
return signature;
}
// sendTransaction(connection, someaccount.publicKey, 1000, account);
// Get history
// https://solana-labs.github.io/solana-web3.js/class/src/connection.js~Connection.html#instance-method-getConfirmedSignaturesForAddress
function getHistory(connection, publicKey, options = { limit: 20 }) {
return connection.getConfirmedSignaturesForAddress2(publicKey, options);
}
const history = getHistory(connection, account.publicKey);
console.log('Signatures:', history);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment