From f3127c3b47dae57b63e1b03837fedc7671127379 Mon Sep 17 00:00:00 2001 From: chaseeb Date: Thu, 15 Jul 2021 13:38:26 -0400 Subject: [PATCH] chore: add examples folder and two example scripts (#18668) add examples to web3 --- web3.js/examples/send_sol.js | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 web3.js/examples/send_sol.js diff --git a/web3.js/examples/send_sol.js b/web3.js/examples/send_sol.js new file mode 100644 index 000000000..68e772dd2 --- /dev/null +++ b/web3.js/examples/send_sol.js @@ -0,0 +1,37 @@ +import * as web3 from '@solana/web3.js'; + +(async () => { + // Connect to cluster + var connection = new web3.Connection( + web3.clusterApiUrl('devnet'), + 'confirmed', + ); + + // Generate a new random public key + var from = web3.Keypair.generate(); + var airdropSignature = await connection.requestAirdrop( + from.publicKey, + web3.LAMPORTS_PER_SOL, + ); + await connection.confirmTransaction(airdropSignature); + + // Generate a new random public key + var to = web3.Keypair.generate(); + + // 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], + ); + console.log('SIGNATURE', signature); +})();