Time (in) Smart Contract(s)

Buns Enchantress

Buns Enchantress

· 2 min read
5 rows of identical clocks with different times, each casting a white background as a shadow

Special Variables and Functions

There are special global variables and functions that always exist in the namespace and are mainly used to provide information about the blockchain or are general-use utility functions. One example of such a global variable is the block variable, which enables you to acquire useful information regarding the properties of the blocks on the blockchain.

For example, you may be interested in locking tokens for a specified amount of time — for example, an ERC20 time lock smart contract. When you are interested in time, you may be want to know the block.timestamp, which represents the current block timestamp represented in (uint) unix epoch time, which is calculated in seconds. Another potentially useful variable is block.number, which represents the current block number.

More concretely, in the event that you are interested in locking tokens, you may consider using block.timestamp to specify a require condition wherein which the block.timestamp is ≥ lock expiration time (represented in unix epoch time).

Solidity makes things even more convenient by providing time units, such as days and years, which are particularly useful when you are interested in computing longer time spans, so you may consider working these convenient tools to help you save… time, kek.

Time contract that does nothing beyond remembering when it was created, voila!

TLDR;

  • Each block in the blockchain includes a timestamp specified as the number of seconds since the Unix epoch. Time values are integers (uint).
  • Solidity smart contracts can access the timestamp of the current block as now or block.timestamp , however, please note that the aliasnow is removed in Solidity 0.7.0.
  • Solidity provides convenient time units like days and years, which are helpful in computing time spans.
Buns Enchantress

About Buns Enchantress

0xBuns is the operated by Web3 developer Buns Enchantress, who is an experienced full-stack developer and Cofounder of SoulSwap Finance and Luxor Money.