├── .gitignore ├── package.json ├── README.md └── Transactions-and-Fees-challenge.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@solana/web3.js": "^1.87.6" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Transactions-and-Fees 2 | A simple code that calculates the wallet balance of the sender wallet. Then, transfer 50% of the balance to another wallet. 3 | 4 | ## How to use 5 | - clone the repo 6 | - On the CLI type `npm i` 7 | - Hit enter 8 | - On the CLI type `Transactions-and-Fees-challenge.js` 9 | - Hit enter 10 | - 11 | Congrats 🎉 You have successfully transferred SOL tokens. 12 | -------------------------------------------------------------------------------- /Transactions-and-Fees-challenge.js: -------------------------------------------------------------------------------- 1 | // Import Solana web3 functinalities 2 | const { 3 | Connection, 4 | PublicKey, 5 | clusterApiUrl, 6 | Keypair, 7 | LAMPORTS_PER_SOL, 8 | Transaction, 9 | SystemProgram, 10 | sendAndConfirmTransaction 11 | } = require("@solana/web3.js"); 12 | const { log } = require("console"); 13 | 14 | const transferSol = async() => { 15 | const connection = new Connection("http://127.0.0.1:8899", "confirmed"); 16 | 17 | // Get Keypair from Secret Key 18 | var from = Keypair.generate(); 19 | 20 | // Generate another Keypair (account we'll be sending to) 21 | const to = Keypair.generate(); 22 | 23 | // Aidrop 2 SOL to Sender wallet 24 | console.log("Airdopping some SOL to Sender wallet!"); 25 | const fromAirDropSignature = await connection.requestAirdrop( 26 | new PublicKey(from.publicKey), 27 | 2 * LAMPORTS_PER_SOL 28 | ); 29 | 30 | // Latest blockhash (unique identifer of the block) of the cluster 31 | let latestBlockHash = await connection.getLatestBlockhash(); 32 | 33 | // Confirm transaction using the last valid block height (refers to its time) 34 | // to check for transaction expiration 35 | await connection.confirmTransaction({ 36 | blockhash: latestBlockHash.blockhash, 37 | lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, 38 | signature: fromAirDropSignature 39 | }); 40 | 41 | console.log("Airdrop completed for the Sender account"); 42 | console.log("\n"); 43 | 44 | const walletBalanceFrom = await connection.getBalance(from.publicKey); 45 | console.log("-- from Wallet balance:", `${parseInt(walletBalanceFrom) / LAMPORTS_PER_SOL}`); 46 | const walletBalanceTo = await connection.getBalance(to.publicKey); 47 | console.log("-- to Wallet balance:", `${parseInt(walletBalanceTo) / LAMPORTS_PER_SOL}`); 48 | console.log("\n") 49 | 50 | const halfOfwalletBalanceFrom = walletBalanceFrom / 2; 51 | 52 | // Send money from "from" wallet and into "to" wallet 53 | var transaction = new Transaction().add( 54 | SystemProgram.transfer({ 55 | fromPubkey: from.publicKey, 56 | toPubkey: to.publicKey, 57 | lamports: halfOfwalletBalanceFrom 58 | }) 59 | ); 60 | 61 | // Sign transaction 62 | var signature = await sendAndConfirmTransaction( 63 | connection, 64 | transaction, 65 | [from] 66 | ); 67 | console.log('Signature is', signature); 68 | console.log("\n"); 69 | 70 | const walletBalanceFromAfter = await connection.getBalance(from.publicKey); 71 | console.log("-> from Wallet balance:", `${parseInt(walletBalanceFromAfter) / LAMPORTS_PER_SOL}`); 72 | const walletBalanceToAfter = await connection.getBalance(to.publicKey); 73 | console.log("-> to Wallet balance:", `${parseInt(walletBalanceToAfter) / LAMPORTS_PER_SOL}`); 74 | } 75 | 76 | transferSol(); --------------------------------------------------------------------------------