Any reason why block number or timestamp is not accessible via contract?

According to https://docs.onflow.org/docs/faq#how-do-i-access-block-information-like-timestamp-and-block-number-in-cadence , they are currently not supported.

Could you give us a technical explanation of why this is the case? I can understand that Cadence is so new that many building blocks such as Oracle or Stable coins are missing (and other blockchain like Ethereum didn’t have it from day one) . However, block number and its associated timestamp probably existed from very early on. I feel like this is a deal breaker for many dapp ideas which requires some notion of time, and prefer not to roll out my own Oracle service just for this purpose.

2 Likes

Hi @makoto I’m totally with you on that. The TimeStamp is the proof that you had that asset at this time and a transaction without timestamp it is no good for traceability/transactional history of the asset ownership transfer.

@makoto, the block number and timestamp actually are available in Cadence right now. We just have been slow about adding the documentation for it to the language reference and FAQ. I’m finding the syntax for it now…

3 Likes

This is great news!!!

@makoto and @jsd

  • Get the current block:
    fun getCurrentBlock(): Block
  • Get a block at a given height:
    fun getBlock(at height: UInt64): Block?
  • The block info is:
struct Block {
      let id: [UInt8; 32]
      let height: UInt64
      let timestamp: UFix64
}
3 Likes

Nice! I used getCurrentBlock in this week’s assignment, but it looks like the Playground is having issues with Block.height

@ebreuers did you try a different one of the fields? I think Cadence might still be a little out of data in the playground so that might be why that is missing

@flowjosh I’ve only tried the height field. No problem though!

I just wanted to point that out here in case anyone else runs into it

Right now Playground version of getCurrentBlock will return you a struct with 2 fields: number and hash:

Block(number: 1, hash: 0x0000000000000000000000000000000000000000000000000000000000000001)