2

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 2

4

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.

2
  • 1
    To add to this, transaction count is not exposed on-chain. so there's no way to get it in a program
    – Jon C
    Commented Feb 3, 2023 at 12:43
  • Is there an alternative to RecentBlockhashes?
    – Gil LB
    Commented Oct 25 at 18:10
0

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()
1
  • 2
    The 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 C
    Commented Feb 3, 2023 at 12:43

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.