Search code examples
blockchainethereumsoliditynft

How to specify a date when the function will be available for calling?


I have not figured out yet how to open NFT minting after specific date. For example, if I deploy my smart contract right now, I want users to be able to mint NFT's starting from 1st January of 2022.

I came across block.timestamp, but I can't figure out a way how to use it.

Or would it to be better to just deploy the contract on 1st January?


Solution

  • block.timestamp is a global variable returning the time of the block when the function is executed (by sending a transaction). Or the current time if the function is called (by performing a read-only call).

    pragma solidity ^0.8;
    
    contract MyContract {
        function mint() external {
            require(block.timestamp >= 1640995200, "Not yet available");
            // ... rest of your code
        }
    }