Setup [solana]

 Technical Setup for Local Dev Environments

If you decide to use a local dev environment instead of Gitpod, you will need to have:

  • A code editor. We recommend VS Code
  • Access to your terminal.
  • VS Code has a terminal that can be accessed by going to View → Terminal or by pressing Ctrl + `
  • NodeJS on your system. If you don't have it, you can click here to install it: NodeJS
When you have the technology ready, it's time to build the directory.

  • Create a directory called Lesson1-solana
  • Create a Node project in your directory by entering: npm init -y
  • Install the Solana Web3 JS library by entering: npm install --save @solana/web3.js
Once this is set up, you can create an index.js file in your project directory, and copy and paste the code below to get started! In the coming pages, we will walk through what everything means so you are prepared to create a transaction:

// Import Solana web3 functionalities

const {

    Connection,

    PublicKey,

    clusterApiUrl,

    Keypair,

    LAMPORTS_PER_SOL

} = require("@solana/web3.js");


// Create a new keypair

const newPair = new Keypair();


// Exact the public and private key from the keypair

const publicKey = new PublicKey(newPair._keypair.publicKey).toString();

const privateKey = newPair._keypair.secretKey;


// Connect to the Devnet

const connection = new Connection(clusterApiUrl("devnet"), "confirmed");


console.log("Public Key of the generated keypair", publicKey);


// Get the wallet balance from a given private key

const getWalletBalance = async () => {

    try {

        // Connect to the Devnet

        const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

        console.log("Connection object is:", connection);


        // Make a wallet (keypair) from privateKey and get its balance

        const myWallet = await Keypair.fromSecretKey(privateKey);

        const walletBalance = await connection.getBalance(

            new PublicKey(newPair.publicKey)

        );

        console.log(`Wallet balance: ${parseInt(walletBalance) / LAMPORTS_PER_SOL} SOL`);

    } catch (err) {

        console.log(err);

    }

};


const airDropSol = async () => {

    try {

        // Connect to the Devnet and make a wallet from privateKey

        const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

        const myWallet = await Keypair.fromSecretKey(privateKey);


        // Request airdrop of 2 SOL to the wallet

        console.log("Airdropping some SOL to my wallet!");

        const fromAirDropSignature = await connection.requestAirdrop(

            new PublicKey(myWallet.publicKey),

            2 * LAMPORTS_PER_SOL

        );

        await connection.confirmTransaction(fromAirDropSignature);

    } catch (err) {

        console.log(err);

    }

};


// Show the wallet balance before and after airdropping SOL

const mainFunction = async () => {

    await getWalletBalance();

    await airDropSol();

    await getWalletBalance();

}


mainFunction();



Comments