Created
April 25, 2022 01:37
-
-
Save goldzulu/06452a1da49632932d707289f36544e6 to your computer and use it in GitHub Desktop.
Solana Snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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