Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick95550 committed Jun 25, 2024
1 parent 35f7127 commit 4c23d37
Show file tree
Hide file tree
Showing 2 changed files with 451 additions and 136 deletions.
147 changes: 118 additions & 29 deletions contracts/ProtocolTimeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ contract ProtocolTimeManager is
*/
uint256 start;

Check warning on line 22 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

Explicitly mark visibility of state

/**
* @notice Holds the start delay for the cycle/period intervals in unix
*/
uint256 delay;

Check warning on line 27 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

Explicitly mark visibility of state

/**
* @notice Holds the cycle duration in unix
*/
Expand Down Expand Up @@ -48,9 +53,12 @@ contract ProtocolTimeManager is
error CannotInitializeWithZeroPeriodDuration();
error CannotSetProtocolStartWithZeroStart();
error CannotSetCycleDurationWithZeroDuration();
error CannotSetStartInFuture();
error CannotSetStartInPast();
error CannotSetZeroPeriodDuration();
error CannotSetZeroCycleDuration();
error CannotSetDuplicateCycleDuration();
error CannotSetDuplicatePeriodDuration();
error ProtocolHasNotBegun();

function initialize(uint256 _cycleDuration, uint256 _periodDuration) external initializer {
if (_cycleDuration == 0) {
Expand All @@ -62,9 +70,8 @@ contract ProtocolTimeManager is

Ownable2StepUpgradeable.__Ownable2Step_init();

start = block.timestamp;
_setCycleDuration(_cycleDuration);
_setPeriodDuration(_periodDuration);
cycleDuration = _cycleDuration;
periodDuration = _periodDuration;
}

/**
Expand All @@ -77,16 +84,18 @@ contract ProtocolTimeManager is

/**
* @notice Sets the start time for the cycle/period intervals
* @param _start The start time for the cycle/period intervals in unix
* @param _delay The start time for the cycle/period intervals in unix
*/
function setProtocolStart(uint256 _start) external onlyOwner {
if (_start == 0) {
function setProtocolStart(uint256 _delay) external onlyOwner {
if (_delay == 0) {
revert CannotSetProtocolStartWithZeroStart();
}
if (_start > block.timestamp) {
revert CannotSetStartInFuture();
}
start = _start;

cycleDurationUpdates.set(_delay, cycleDuration);
periodDurationUpdates.set(_delay, periodDuration);

start = block.timestamp + _delay;
delay = _delay;
}

/**
Expand All @@ -105,17 +114,44 @@ contract ProtocolTimeManager is
_setPeriodDuration(_periodDuration);
}

/**
* @notice Sets the cycle duration
* @param _cycleDuration The duration for which cycles should last in unix
*/
function _setCycleDuration(uint256 _cycleDuration) internal {
if (_cycleDuration == 0) {
revert CannotSetZeroCycleDuration();
}
if (_cycleDuration == cycleDuration) {
revert CannotSetDuplicateCycleDuration();
}
if (start == 0) {
revert ProtocolHasNotBegun();
}
if (start > block.timestamp) {
(uint256 firstKey, ) = cycleDurationUpdates.at(0);
cycleDurationUpdates.set(firstKey, _cycleDuration);
cycleDuration = _cycleDuration;
return;
}

uint256 effectiveTime;
if (start > block.timestamp) {
effectiveTime = 0; // No cycles have started yet
} else {
uint256 timeSinceStart = block.timestamp - start;

(uint256 previousTimestamp, uint256 currentCycleDuration) = cycleDurationUpdates.at(
cycleDurationUpdates.length() - 1
);

uint256 remaining = currentCycleDuration - (timeSinceStart % currentCycleDuration);

effectiveTime = timeSinceStart + remaining;

if ((effectiveTime + delay) < previousTimestamp) {
effectiveTime = (previousTimestamp - delay);
}
}

cycleDurationUpdates.set(effectiveTime + delay, _cycleDuration);
cycleDuration = _cycleDuration;
uint256 timeSinceStart = block.timestamp - start;
cycleDurationUpdates.set(timeSinceStart, cycleDuration);
}

/**
Expand All @@ -126,9 +162,40 @@ contract ProtocolTimeManager is
if (_periodDuration == 0) {
revert CannotSetZeroPeriodDuration();
}
if (_periodDuration == periodDuration) {
revert CannotSetDuplicatePeriodDuration();
}
if (start == 0) {
revert ProtocolHasNotBegun();
}
if (start > block.timestamp) {
(uint256 firstKey, ) = periodDurationUpdates.at(0);
periodDurationUpdates.set(firstKey, _periodDuration);
periodDuration = _periodDuration;
return;
}

uint256 effectiveTime;
if (start > block.timestamp) {
effectiveTime = 0; // No periods have started yet
} else {
uint256 timeSinceStart = block.timestamp - start;

(uint256 previousTimestamp, uint256 currentPeriodDuration) = periodDurationUpdates.at(
periodDurationUpdates.length() - 1
);

uint256 remaining = currentPeriodDuration - (timeSinceStart % currentPeriodDuration);

effectiveTime = timeSinceStart + remaining;

if ((effectiveTime + delay) < previousTimestamp) {
effectiveTime = (previousTimestamp - delay);
}
}

periodDurationUpdates.set(effectiveTime + delay, _periodDuration);
periodDuration = _periodDuration;
uint256 timeSinceStart = block.timestamp - start;
periodDurationUpdates.set(timeSinceStart, periodDuration);
}

/**
Expand Down Expand Up @@ -168,22 +235,33 @@ contract ProtocolTimeManager is
* (where 200 is the totalTimeElapsed since start)
*/
function _getCurrentCycle() internal view returns (uint256) {
if (block.timestamp < start || start == 0) {
revert ProtocolHasNotBegun();
}

uint256 totalTimeElapsed = block.timestamp - start;
uint256 processedCycleIntervals = 0;
uint256 cycles = 1;

for (uint256 i = 0; (i + 1) < cycleDurationUpdates.length(); ++i) {
(uint256 timestamp, uint256 intervalDuration) = cycleDurationUpdates.at(i);

(uint256 nextTimestamp, ) = cycleDurationUpdates.at(i + 1);
uint256 cycleInterval = nextTimestamp - timestamp;

processedCycleIntervals += cycleInterval;
uint256 latestCycleInterval = nextTimestamp - timestamp;

cycles += cycleInterval / intervalDuration;
processedCycleIntervals += latestCycleInterval;

cycles += latestCycleInterval / intervalDuration;
}

if (totalTimeElapsed < processedCycleIntervals) {
return cycles;
}

uint256 remaingCycleInterval = ((totalTimeElapsed - processedCycleIntervals) /
cycleDuration);
uint256 remaingCycleInterval = (totalTimeElapsed - processedCycleIntervals) /
cycleDuration;

uint256 currentCycle = cycles + remaingCycleInterval;
return currentCycle;
}
Expand All @@ -199,23 +277,34 @@ contract ProtocolTimeManager is
* refer to explaination above for funtionality
*/
function _getCurrentPeriod() internal view returns (uint256) {
if (block.timestamp < start || start == 0) {
revert ProtocolHasNotBegun();
}

uint256 totalTimeElapsed = block.timestamp - start;

uint256 processedPeriodIntervals = 0;
uint256 cycles = 1;
uint256 periods = 1;

for (uint256 i = 0; (i + 1) < periodDurationUpdates.length(); ++i) {
(uint256 timestamp, uint256 intervalDuration) = periodDurationUpdates.at(i);

(uint256 nextTimestamp, ) = periodDurationUpdates.at(i + 1);

uint256 periodInterval = nextTimestamp - timestamp;

processedPeriodIntervals += periodInterval;

cycles += periodInterval / intervalDuration;
periods += periodInterval / intervalDuration;
}

if (totalTimeElapsed < processedPeriodIntervals) {
return periods;
}

uint256 remaingPeriodInterval = ((totalTimeElapsed - processedPeriodIntervals) /
periodDuration);
uint256 currentPeriod = cycles + remaingPeriodInterval;
uint256 remaingPeriodInterval = ((totalTimeElapsed - processedPeriodIntervals)) /
periodDuration;
uint256 currentPeriod = periods + (remaingPeriodInterval);
return currentPeriod;
}

Expand Down
Loading

0 comments on commit 4c23d37

Please sign in to comment.