0

Need to create stake account and then delegate via wallet-adapter in NextJS. Took some code from Solana Cookbook. Getting an issue after accepting transaction in any wallet, crashes at the line sendTransaction() with WalletSendTransactionError: 400 : {"error":"Request failed with status code 502"}

Also would appreciate if somebody explains what nonceAccount and minContextSlot is, still can't get it.

  const handleStake = async () => {
if (!publicKey || !wallet) return;
const stakeAccount = new Keypair();

const minimumRent = await connection.getMinimumBalanceForRentExemption(
  StakeProgram.space
);
const amountUserWantsToStake = LAMPORTS_PER_SOL / 2; // This is can be user input. For now, we'll hardcode to 0.5 SOL
const amountToStake = minimumRent + amountUserWantsToStake;

const transaction = new Transaction().add(
  StakeProgram.createAccount({
    fromPubkey: publicKey,
    stakePubkey: stakeAccount.publicKey,
    lamports: amountToStake,
    lockup: new Lockup(0, 0, publicKey),
    authorized: new Authorized(publicKey, publicKey),
  })
);

const signature = await sendTransaction(transaction, connection);

const strategy: TransactionConfirmationStrategy = {
  signature: signature,
  nonceAccountPubkey: publicKey,
  minContextSlot: transaction.lastValidBlockHeight!,
  nonceValue: transaction.nonceInfo!.nonce,
};

await connection.confirmTransaction(strategy, "processed");

// Check our newly created stake account balance. This should be 0.5 SOL.
let stakeBalance = await connection.getBalance(stakeAccount.publicKey);
console.log(
  `Stake account balance: ${stakeBalance / LAMPORTS_PER_SOL} SOL`
);

// Verify the status of our stake account. This will start as inactive and will take some time to activate.
let stakeStatus = await connection.getStakeActivation(
  stakeAccount.publicKey
);
console.log(`Stake account status: ${stakeStatus.state}`);

};

1 Answer 1

0
const stakeAccount = new Keypair();

const signature = await sendTransaction(transaction, connection, {
  signers: [stakeAccount],
});

Try catch could help to dive in to RPC errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.