const solanaWeb3 = require('@solana/web3.js');
const bs58 = require("bs58");

async function main() {
  const connection = new solanaWeb3.Connection("https://solana-mainnet.core.chainstack.com/AUTHENTICATION", {wsEndpoint:"wss://solana-mainnet.core.chainstack.com/ws/AUTHENTICATION"});
  const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(bs58.decode(process.env['PRIVATE_KEY'])));

  // Create Stake Account
  const stakeAccount = solanaWeb3.Keypair.generate();
  const createStakeAccountInstruction = solanaWeb3.StakeProgram.createAccount({
    fromPubkey: walletKeyPair.publicKey,
    stakePubkey: stakeAccount.publicKey,
    authorized: new solanaWeb3.Authorized(walletKeyPair.publicKey, walletKeyPair.publicKey),
    lamports: solanaWeb3.LAMPORTS_PER_SOL * 0.02,
  });

  let createStakeAccountTransaction = new solanaWeb3.Transaction().add(createStakeAccountInstruction);
  createStakeAccountTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
  createStakeAccountTransaction.feePayer = walletKeyPair.publicKey;
  createStakeAccountTransaction.partialSign(stakeAccount);
  await solanaWeb3.sendAndConfirmTransaction(
    connection,
    createStakeAccountTransaction,
    [walletKeyPair, stakeAccount],
  );

  // Delegate Stake
  const votePubkey = new solanaWeb3.PublicKey('beefKGBWeSpHzYBHZXwp5So7wdQGX6mu4ZHCsH3uTar');
  const delegateInstruction = solanaWeb3.StakeProgram.delegate({
    stakePubkey: stakeAccount.publicKey,
    authorizedPubkey: walletKeyPair.publicKey,
    votePubkey,
  });

  let delegateTransaction = new solanaWeb3.Transaction().add(delegateInstruction);
  delegateTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
  delegateTransaction.feePayer = walletKeyPair.publicKey;
  delegateTransaction.sign(walletKeyPair);
  await solanaWeb3.sendAndConfirmTransaction(
    connection,
    delegateTransaction,
    [walletKeyPair],
  );
}

main();
const solanaWeb3 = require('@solana/web3.js');
const bs58 = require("bs58");

async function main() {
  const connection = new solanaWeb3.Connection("https://solana-mainnet.core.chainstack.com/AUTHENTICATION", {wsEndpoint:"wss://solana-mainnet.core.chainstack.com/ws/AUTHENTICATION"});
  const walletKeyPair = solanaWeb3.Keypair.fromSecretKey(new Uint8Array(bs58.decode(process.env['PRIVATE_KEY'])));

  // Create Stake Account
  const stakeAccount = solanaWeb3.Keypair.generate();
  const createStakeAccountInstruction = solanaWeb3.StakeProgram.createAccount({
    fromPubkey: walletKeyPair.publicKey,
    stakePubkey: stakeAccount.publicKey,
    authorized: new solanaWeb3.Authorized(walletKeyPair.publicKey, walletKeyPair.publicKey),
    lamports: solanaWeb3.LAMPORTS_PER_SOL * 0.02,
  });

  let createStakeAccountTransaction = new solanaWeb3.Transaction().add(createStakeAccountInstruction);
  createStakeAccountTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
  createStakeAccountTransaction.feePayer = walletKeyPair.publicKey;
  createStakeAccountTransaction.partialSign(stakeAccount);
  await solanaWeb3.sendAndConfirmTransaction(
    connection,
    createStakeAccountTransaction,
    [walletKeyPair, stakeAccount],
  );

  // Delegate Stake
  const votePubkey = new solanaWeb3.PublicKey('beefKGBWeSpHzYBHZXwp5So7wdQGX6mu4ZHCsH3uTar');
  const delegateInstruction = solanaWeb3.StakeProgram.delegate({
    stakePubkey: stakeAccount.publicKey,
    authorizedPubkey: walletKeyPair.publicKey,
    votePubkey,
  });

  let delegateTransaction = new solanaWeb3.Transaction().add(delegateInstruction);
  delegateTransaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;
  delegateTransaction.feePayer = walletKeyPair.publicKey;
  delegateTransaction.sign(walletKeyPair);
  await solanaWeb3.sendAndConfirmTransaction(
    connection,
    delegateTransaction,
    [walletKeyPair],
  );
}

main();