How to create a time window in solidity .. so that you will allow a function call for example with in 2 hours .. I know timers do not exist in solidity so how do I only allow a function to be called within a certain period of time


There are two methods: Either you estimate the the block number of the beginning and end of your period, and do

require (block.number < ##yourStartBlock## && blocknumber < ##yourEndBlock##)

or you use block.timestamp, where an integer represents the unix time stamp of the block. This is only as precise as the miner wants it to be, meaning block.timestamp can turn out to be anything between the block.timestamp of the last and the next block.

require (block.timestamp < 1521379216 && block.timestamp < 1521343200)

where 1521379216 == 03/18/2018 @ 1:20pm (UTC) and

1521343200 == 03/18/2018 @ 3:20am (UTC)
  • how can I say the current block.timestamp + 2 hours? .. to make it generalized – Haya Raed Mar 18 '18 at 13:52
  • 1
    just like that: uint endTime = block.timestamp + 2 hours; then use it like require(block.timestamp < endTime); See solidity.readthedocs.io/en/v0.4.21/… – Nikita Fuchs Mar 18 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.