I'm trying to generate random numbers but libs like nanorand, fastrand, oorandom seems doese not work in solana program. So is there any way to get transaction count, slot, Block height or Epoch in program solana?
2 Answers
Yes,
Clock::get().unwrap()
contains the slot, epoch, time etc, which are all predictable though, RecentBlockhashes::get().unwrap()
is deprecated, but contains the latest blockhashes.
-
1To add to this, transaction count is not exposed on-chain. so there's no way to get it in a program– Jon CCommented Feb 3, 2023 at 12:43
-
Edit: The answer below is for getting the slot/blockhash off-chain, and the question is for how to get the information on-chain within a program
You can get the slot like this:
const slot = await connection.getSlot()
Or get a recent blockhash like this:
const { blockhash, lastValidBlockHeight } =
await connection.getLatestBlockhash()
-
2The question is about getting the information on-chain, in a program, and not off-chain through a client library, so it might be worth clarifying that this only works off-chain– Jon CCommented Feb 3, 2023 at 12:43