Skip to content

Commit

Permalink
amend cycle count
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick95550 committed Jun 12, 2024
1 parent 37fda8b commit 520b96a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 63 deletions.
13 changes: 3 additions & 10 deletions contracts/ProtocolTimeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";

Check warning on line 5 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path @openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check warning on line 5 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path @openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

Check warning on line 6 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path @openzeppelin/contracts/utils/introspection/ERC165.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check warning on line 6 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path @openzeppelin/contracts/utils/introspection/ERC165.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";

Check warning on line 7 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path @openzeppelin/contracts/utils/structs/EnumerableMap.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check warning on line 7 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path @openzeppelin/contracts/utils/structs/EnumerableMap.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "hardhat/console.sol";

Check failure on line 8 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

Unexpected import of console file

Check warning on line 8 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path hardhat/console.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check failure on line 8 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

Unexpected import of console file

Check warning on line 8 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path hardhat/console.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

import "./IProtocolTimeManager.sol";

Check warning on line 10 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path ./IProtocolTimeManager.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Check warning on line 10 in contracts/ProtocolTimeManager.sol

View workflow job for this annotation

GitHub Actions / build

global import of path ./IProtocolTimeManager.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

Expand Down Expand Up @@ -170,11 +171,7 @@ contract ProtocolTimeManager is
function _getCurrentCycle() internal view returns (uint256) {
uint256 totalTimeElapsed = block.timestamp - start;
uint256 processedCycleIntervals = 0;
uint256 cycles = 0;

if (cycleDurationUpdates.length() == 1) {
return totalTimeElapsed / cycleDuration;
}
uint256 cycles = 1;

for (uint256 i = 0; (i + 1) < cycleDurationUpdates.length(); ++i) {
(uint256 timestamp, uint256 intervalDuration) = cycleDurationUpdates.at(i);
Expand Down Expand Up @@ -205,11 +202,7 @@ contract ProtocolTimeManager is
function _getCurrentPeriod() internal view returns (uint256) {
uint256 totalTimeElapsed = block.timestamp - start;
uint256 processedPeriodIntervals = 0;
uint256 cycles = 0;

if (periodDurationUpdates.length() == 1) {
return totalTimeElapsed / periodDuration;
}
uint256 cycles = 1;

for (uint256 i = 0; (i + 1) < periodDurationUpdates.length(); ++i) {
(uint256 timestamp, uint256 intervalDuration) = periodDurationUpdates.at(i);
Expand Down
79 changes: 26 additions & 53 deletions test/protocolTimeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ describe('Protocol time manager', () => {
await network.provider.send('evm_mine');

const times = await protocolTimeManager.timeNow();
assert.equal(Number(times[0]), 3);
assert.equal(Number(times[1]), 3);
assert.equal(Number(times[0]), 4);
assert.equal(Number(times[1]), 4);

await protocolTimeManager.setCycleDuration(3000);
await protocolTimeManager.setPeriodDuration(2000);
Expand All @@ -138,120 +138,127 @@ describe('Protocol time manager', () => {
await network.provider.send('evm_mine');

const timesTwo = await protocolTimeManager.timeNow();
assert.equal(Number(timesTwo[0]), 5);
assert.equal(Number(timesTwo[1]), 6);
assert.equal(Number(timesTwo[0]), 6);
assert.equal(Number(timesTwo[1]), 7);
});

it('returns one for first cycle', async () => {
await network.provider.send('evm_increaseTime', [500]);
await network.provider.send('evm_mine');
const currentCycle = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycle), 1);
});

it('can get current cycle without updated duration', async () => {
await network.provider.send('evm_increaseTime', [3000]);
await network.provider.send('evm_increaseTime', [3500]);
await network.provider.send('evm_mine');

const currentCycle = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycle), 3);
assert.equal(Number(currentCycle), 4);
});

it('can get current cycle with one updated duration', async () => {
await network.provider.send('evm_increaseTime', [3000]);
await network.provider.send('evm_mine');

const currentCycle = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycle), 3);
assert.equal(Number(currentCycle), 4);

await protocolTimeManager.setCycleDuration(200);

await network.provider.send('evm_increaseTime', [7000]);
await network.provider.send('evm_mine');

const currentCycleTwo = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycleTwo), 38);
assert.equal(Number(currentCycleTwo), 39);
});

it('can get current cycle with multiple updated durations', async () => {
await network.provider.send('evm_increaseTime', [3000]);
await network.provider.send('evm_mine');

const currentCycle = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycle), 3);
assert.equal(Number(currentCycle), 4);

await protocolTimeManager.setCycleDuration(200);

await network.provider.send('evm_increaseTime', [7000]);
await network.provider.send('evm_mine');

const currentCycleTwo = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycleTwo), 38);
assert.equal(Number(currentCycleTwo), 39);

await protocolTimeManager.setCycleDuration(125);

await network.provider.send('evm_increaseTime', [5800]);
await network.provider.send('evm_mine');

const currentCycleThree = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycleThree), 84);
assert.equal(Number(currentCycleThree), 85);

await protocolTimeManager.setCycleDuration(2);

await network.provider.send('evm_increaseTime', [200]);
await network.provider.send('evm_mine');

const currentCycleFour = await protocolTimeManager.getCurrentCycle();
assert.equal(Number(currentCycleFour), 184);
assert.equal(Number(currentCycleFour), 185);
});

it('can get current period without updated duration', async () => {
await network.provider.send('evm_increaseTime', [3000]);
await network.provider.send('evm_mine');

const currentPeriod = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriod), 3);
assert.equal(Number(currentPeriod), 4);
});

it('can get current period with one updated duration', async () => {
await network.provider.send('evm_increaseTime', [3000]);
await network.provider.send('evm_mine');

const currentPeriod = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriod), 3);
assert.equal(Number(currentPeriod), 4);

await protocolTimeManager.setPeriodDuration(200);

await network.provider.send('evm_increaseTime', [7000]);
await network.provider.send('evm_mine');

const currentPeriodTwo = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriodTwo), 38);
assert.equal(Number(currentPeriodTwo), 39);
});

it('can get current period with multiple updated durations', async () => {
await network.provider.send('evm_increaseTime', [3000]);
await network.provider.send('evm_mine');

const currentPeriod = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriod), 3);
assert.equal(Number(currentPeriod), 4);

await protocolTimeManager.setPeriodDuration(200);

await network.provider.send('evm_increaseTime', [7000]);
await network.provider.send('evm_mine');

const currentPeriodTwo = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriodTwo), 38);
assert.equal(Number(currentPeriodTwo), 39);

await protocolTimeManager.setPeriodDuration(125);

await network.provider.send('evm_increaseTime', [5800]);
await network.provider.send('evm_mine');

const currentPeriodThree = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriodThree), 84);
assert.equal(Number(currentPeriodThree), 85);

await protocolTimeManager.setPeriodDuration(2);

await network.provider.send('evm_increaseTime', [200]);
await network.provider.send('evm_mine');

const currentPeriodFour = await protocolTimeManager.getCurrentPeriod();
assert.equal(Number(currentPeriodFour), 184);
assert.equal(Number(currentPeriodFour), 185);
});

it('supports only protocol time manager interface', async () => {
Expand Down Expand Up @@ -292,37 +299,3 @@ describe('Protocol time manager', () => {
);
});
});

// await network.provider.send('evm_increaseTime', [3000]);
// await network.provider.send('evm_mine');
// const x = await protocolTimeManager.getCurrentCycle();
// console.log('x ', x);
// await protocolTimeManager.setCycleDuration(1500);
// await network.provider.send('evm_increaseTime', [4000]);
// await network.provider.send('evm_mine');
// const y = await protocolTimeManager.getCurrentCycle();
// console.log('y ', y);

// await protocolTimeManager.setCycleDuration(500);
// await network.provider.send('evm_increaseTime', [5000]);
// await network.provider.send('evm_mine');
// const z = await protocolTimeManager.getCurrentCycle();
// console.log('z ', z);

// await protocolTimeManager.setCycleDuration(700);
// await network.provider.send('evm_increaseTime', [11000]);
// await network.provider.send('evm_mine');
// const w = await protocolTimeManager.getCurrentCycle();
// console.log('w ', w);

// await protocolTimeManager.setCycleDuration(673);
// await network.provider.send('evm_increaseTime', [6543]);
// await network.provider.send('evm_mine');
// const q = await protocolTimeManager.getCurrentCycle();
// console.log('y ', q);

// // await protocolTimeManager.setCycleDuration(1000);
// // await network.provider.send('evm_increaseTime', [3000]);
// // await network.provider.send('evm_mine');
// // const w = await protocolTimeManager.getCurrentCycle();
// // console.log('w ', w);

0 comments on commit 520b96a

Please sign in to comment.