When a user is creating a staked deposit they have the option to lock their deposit for a period of time to increase the rewards they gain during the specified period. This must be no more than 365 days from the stake submission.
Additionally, at any point the user is able to update their locking period. This extension of their existing locking period must be a point in the future that is no longer than 365 days. This means that the longest theoretical lock period is 2 years. If you stake a deposit and lock it for a period of 365 days, you are able to add to that lock period on the final day for another full year.
Calculating the locking period programmatically requires two steps. First, we can use the
Date.Now()provided in TypeScript to get the Unix timestamp at that exact moment. A problem exists that this is in milliseconds, not seconds which
block.timestampuses in Solidity. This is easy enough to fix by doing
Date.now() / 1000.
Now that we have the correct timestamp all we have to do is multiply for the given number of seconds in a day (
86400), and the number of days the user specified to lock their stake. For example, if a user requests to lock their stake for 120 days, the calculation would be as follows.
lock = (Date.now() / 1000) + (86400 * 120) lockPeriod = ethers.BigNumber.from(lock)