0

I want a store where someone can become tokenOwner for a certain amount of time. The following solutions seems the easiest:

pragma solidity ^0.4.24;

import "../node_modules/zeppelin-solidity/contracts/ownership/Ownable.sol";

contract PromotedDapp is Ownable{

    uint price;
    address tokenOwner;
    uint nextOwnerAllowed;

    constructor() public {
        owner = msg.sender;
    }

    function becomeOwner() public {
        require(now >= nextOwner);
        nextOwnerAllowed = now + 604800; // In a week
        tokenOwner = msg.sender;
    }

}

I know another option is to work with blockHeight since one block gets minted +/- every 10 seconds, what would be a reason to chose blockHeight over time in seconds?

  • 1
    Correction: a block is mined about every 10 seconds. – user19510 Sep 13 '18 at 14:00
  • You are completely right, edited it. – jasper Sep 13 '18 at 14:05
1

Time is an interesting thing on the blockchain. The two choices are to use time (or timestamp) and the block height.

Block number is generally not advised, as the average block time is 15s. Because it is average, this means that blocks could be shorter (1s) or longer (1 min+). Depending on your application, this variance could mess up your calculations. Additionally, while the block times are ~15s now, that may change in the future because of the ice age or potential future upgrades that reduce this number. Again, it is almost never a good idea to use block number.

Timestamps are generally advised in code like this. Timestamps on Ethereum are sent by the miners in each block they mine. There are rules surrounding this to avoid any malicious actions: miners cannot send a timestamp that is less than the one in the previous block and they cannot send one that is greater than 15s in the future. Because of these bounds, using a timestamp in solidity is a much better choice.

  • It sounds like for my app timestamps are for sure the best way to go, a 15 second margin would not matter. Thanks a lot shane :) – jasper Sep 13 '18 at 14:49

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.