Need help understanding block.timestamp and how time works in the blockchain

Hello all!

I am very new to this technology, and am attempting my first "real" Dapp. I basically want the user to be able to withdraw funds from the contract after an hour, but to do this I would see to set a timer of sorts. My question is, how does time work on the blockchain, and how can I take advantage of the functions like block.timestamp, now, block.number etc. I know what I need to do, but I am not sure how the time is done this platform. Thank you in advance!

Comments

  • qqwyqqwy Member Posts: 16
    You can access the current block time using 'block.timestamp' ('now' is a synonym of this). The time will be returned as a POSIX timestamp (basically the monotonous number of seconds since 1970-01-01 00:00:00 UTC).

    However, this timestamp is 'set' by the miner that ends up mining your transaction. As such, the miner can manipulate the timestamp. There are certain rules to this; other parties will not accept a block if the given timestamp occurs in the future, for instance (more detail about this). But this does mean that the number cannot be used for e.g. random number generation.

    If you want someone to only perform an action after one hour, you can at an earlier action (such as when the contract is created), store the current time, e.g.: `action_time = now;`, and then in another call, perform a check like: `require (now > (action_time + 1 hour));`
Sign In or Register to comment.