0

I have a function which is used to check if 90 days have been passed or not after staking, but I can't figure out why 86400 is mentioned, like is it seconds or minutes or anything else and how can I change that 90 days to any other value of my choice.

        function getTimePassedSinceStake(address _owner, uint256 _index)
        public
        view
        returns (uint256)
    {
        return (block.timestamp - userInfo[_owner][_index].stakeTime) / 86400;
    }

2 Answers 2

0

From the documentation :

block.timestamp (uint): current block timestamp as seconds since unix epoch

So :

block.timestamp - userInfo[_owner][_index].stakeTime

Is the number of seconds between stakeTime and block.timestamp. Dividing by 86400 is dividing by the number of seconds in a day (60 seconds * 60 minutes * 24 hours = 86400)

 return (block.timestamp - userInfo[_owner][_index].stakeTime) / 86400;

returns the number of whole days between stakeTime and block.timstamp this has nothing to do with 90 days here.

An alternative syntax could be :

return (block.timestamp - userInfo[_owner][_index].stakeTime) / 1 days;

how can I change that 90 days to any other value of my choice.

I found a contract with the same code here, that has the following unstake function with a 1 day limit :

function unStake(uint256 _index) external {
    address unStaker = msg.sender;
    UserInfo storage userDetails = userInfo[unStaker][_index];
    uint256 tokenId1 = userDetails.tokenId1;
    uint256 timePassedSinceStaked = getTimePassedSinceStake(
        unStaker,
        _index
    );
    require(
        timePassedSinceStaked >= 1,
        "You can not unstake before 1 day"
    );
    userDetails = userInfo[unStaker][userInfo[unStaker].length - 1];
    userInfo[unStaker].pop();

    ERC721.safeTransferFrom(address(this), unStaker, tokenId1, "");
   

    GemToken.claimByLuckyDragon(unStaker, 0);
    emit UnStakedLuckyDragon(
        unStaker,
        tokenId1,
        block.timestamp
    );
}

Here it is limiting to 1 day of waiting time, by changing :

require(
    timePassedSinceStaked >= 1,
    "You can not unstake before 1 day"
);

to :

require(
    timePassedSinceStaked >= 90,
    "You can not unstake before 90 day"
);

It would require 90 days, of course you can set the value of your choice.

I hope that answers your question.

1
0

ETH contracts use Unix time:

https://www.epochconverter.com The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT)

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.