3

If I deploy a contract in the remix ide with a function like this:

function getTimeStamp() public view returns(uint) {
    return block.timestamp;
}

it doesn't return a constant (as I would expectm based on other answers to similar questions which is suggests it is the time the block is mined.

In remix, this function returns an integer which increments by one every second basically. (If you call the function repeatedly).

How is that a timestamp? what is it a timestamp of? I've seen other posts here which suggest that all the transactions in a block will share the same block.timestamp value, but what I'm seeing is not a constant in the first place.

3 Answers 3

2

What happens when we assign the block.timestamp to variable?? Will it change over time

1
  • 1
    of course you can save the value returned by block.timestamp in a variable. If you save the value of block.timestamp inside a function which sets the value of the state variable, then block timestamp will approximate the time that the state variable was set. But if you store the value in a memory variable inside of a view function, the memory variable will not change while the function is running, but it will not be meaningful. (That is the number returned by block.timestamp is arbitrary.)
    – GGizmos
    Sep 14, 2022 at 17:13
1

How is that a timestamp? what is it a timestamp of?

Block timestamp is the UTC time set on the block when the miner mines the block. If timestamp is used in the context of call (not transaction) then the timestamp does not have any meaningful value.

More information about the Ethereum transaction lifecycle here.

7
  • 1
    So are you saying that this pure function is one of those contexts that 'timestamp does not have any meaningful value'? I notice that in the truffle, block.timestamp returns a constant, while in remix, it seems to return this 'clock' like number which increases every second. What happens on mainnet?
    – GGizmos
    Sep 6, 2021 at 20:36
  • As I said, it does not return any meaningful value on a call outside a transaction. It can be a number between zero and the length of my pants. Sep 7, 2021 at 7:09
  • 1
    OK then! You'd think they'd mention that somewhere in the documentation.
    – GGizmos
    Sep 8, 2021 at 4:12
  • 1
  • 1
    docs.soliditylang.org/en/v0.8.6/…
    – GGizmos
    Sep 9, 2021 at 16:51
1

When the block.timestamp is called from a view function, it will return the best block timestamp (the timestamp of the block at the tip of the blockchain.

Only when we call it with a non-view or non/pure function (when we call a function that will actually modify the state), then, the block.timestamp will refer to the current block where the transaction was included (the new best block).

We can see that in Remix, and also in rinkeby.etherscan.io.

I deployed the following contract and we can read the getCurrentContractTimeStamp function multiple times, and it will change every time there is a new block added to the chain.

https://rinkeby.etherscan.io/address/0x567ca34aca62aa17922311ef87550e9f721d188e#readContract

We can write to it and the updateTime function will set the timestamp of the current block where the transaction was mined into.

https://rinkeby.etherscan.io/address/0x567ca34aca62aa17922311ef87550e9f721d188e#writeContract

contract Contract {

    uint256 public time;

    // A view function. The `block.timestamp` will return the timestamp of the best block
    // (the block at the tip of the blockchain)
    function getCurrentContractTimeStamp() public view returns(uint256) {
        return block.timestamp;
    }

    // `block.timestamp` will return the timestamp of the block where this transaction is mined in.
    function updateTime() public {
        time = block.timestamp;
    }

 }

enter image description here

enter image description here

3
  • Well, if that's true, it could be quite useful. To be able know within some known tolerance how much time has passed since the contract state was last updated is a lot more useful than a random number "anywhere between 0 and the length of my pants" as @mikko_ohtama says. Is he wrong?
    – GGizmos
    Aug 30, 2022 at 23:35
  • 1
    And by the way, your explanation is inconsistent with what happens in the remix ide, where calling the function repeatedly without making any state changes should not return different results, but it does. What's up with that?
    – GGizmos
    Aug 30, 2022 at 23:42
  • Yes. I was wrong. I updated the answer now. Take a look. Aug 31, 2022 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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