2

What's the best and recommended way of getting the current time inside an Anchor program? It shouldn't more than a few seconds or one minute old.

My program is really time sensitive so any wrong time would cause problems.

I've seen this: https://docs.rs/anchor-lang/latest/anchor_lang/prelude/struct.Clock.html#structfield.unix_timestamp But it says that it's an estimation so I can't rely on that

1 Answer 1

1

The Clock sysvar will be your best bet in the short term. It is just an estimation, but it's typically close, unless there's an outage on the network, in which case it takes some time to catch up to the real time.

Otherwise, you'll need to use some other time-specific oracle, which I don't think exists on-chain yet since the unix_timestamp has been sufficient.

You can also use the slot portion of the clock, which is guaranteed to be monotonically increasing with every block. Without more information about what you're trying to do, it's hard to give more alternatives.

2
  • I'm basically building a SPL vault where people can withdraw their money at certain point. Do you know how far behind Clock usually is? Commented Mar 26 at 12:00
  • 2
    In that case, the Clock is probably fine. You can see where it's at looking at "Cluster time" on the main explorer page explorer.solana.com. It only falls behind during an outage, but usually picks back up pretty quickly
    – Jon C
    Commented Mar 26 at 13:25

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.