Transactions and fee [solana]

 // Import Solana web3 functionalities

const {

    Connection,

    PublicKey,

    clusterApiUrl,

    Keypair,

    LAMPORTS_PER_SOL,

    Transaction,

    SystemProgram,

    sendAndConfirmTransaction

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


// Making a keypair and getting the private key

const newPair = Keypair.generate();

console.log("Below is what you will paste into your code:\n")

console.log(newPair.secretKey);

 

const DEMO_FROM_SECRET_KEY = new Uint8Array(

    // paste your secret key inside this empty array

    // then uncomment transferSol() at the bottom

    [

        

    ]            

);


const transferSol = async() => {

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


    // Get Keypair from Secret Key

    var from = Keypair.fromSecretKey(DEMO_FROM_SECRET_KEY);


    // (Optional) - Other things you can try: 

    // 1) Form array from userSecretKey

    // const from = Keypair.fromSecretKey(Uint8Array.from(userSecretKey));

    // 2) Make a new Keypair (starts with 0 SOL)

    // const from = Keypair.generate();


    // Generate another Keypair (account we'll be sending to)

    const to = Keypair.generate();


    // Aidrop 2 SOL to Sender wallet

    console.log("Airdopping some SOL to Sender wallet!");

    const fromAirDropSignature = await connection.requestAirdrop(

        new PublicKey(from.publicKey),

        2 * LAMPORTS_PER_SOL

    );


    // Latest blockhash (unique identifer of the block) of the cluster

    let latestBlockHash = await connection.getLatestBlockhash();


    // Confirm transaction using the last valid block height (refers to its time)

    // to check for transaction expiration

    await connection.confirmTransaction({

        blockhash: latestBlockHash.blockhash,

        lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,

        signature: fromAirDropSignature

    });


    console.log("Airdrop completed for the Sender account");


    // Send money from "from" wallet and into "to" wallet

    var transaction = new Transaction().add(

        SystemProgram.transfer({

            fromPubkey: from.publicKey,

            toPubkey: to.publicKey,

            lamports: LAMPORTS_PER_SOL / 100

        })

    );


    // Sign transaction

    var signature = await sendAndConfirmTransaction(

        connection,

        transaction,

        [from]

    );

    console.log('Signature is', signature);

}


//transferSol();



Comments