Skip to content

Commit

Permalink
fix: test timestamps
Browse files Browse the repository at this point in the history
Signed-off-by: Reinis Martinsons <[email protected]>
  • Loading branch information
Reinis-FRP committed May 13, 2024
1 parent b9b4b2f commit a043bae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
34 changes: 27 additions & 7 deletions test/unit/Oval.ChainlinkDestinationAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contract OvalChainlinkDestinationAdapter is CommonTest {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
publishRoundData(newAnswer, beforeLockWindow);
publishRoundData(newAnswer, newTimestamp);

// Before updating, initial values from cache would be returned.
(int256 latestAnswer, uint256 latestTimestamp, uint256 latestRoundId) = oval.internalLatestData();
Expand All @@ -76,32 +76,52 @@ contract OvalChainlinkDestinationAdapter is CommonTest {
vm.prank(permissionedUnlocker);
oval.unlockLatestValue();
verifyOvalMatchesOval();

(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
oval.latestRoundData();

// Check that Oval return the correct values scaled to the source oracle decimals.
assertTrue(roundId == latestPublishedRound);
assertTrue(answer == newAnswer / internalDecimalsToSourceDecimals);
assertTrue(startedAt == newTimestamp);
assertTrue(updatedAt == newTimestamp);
assertTrue(answeredInRound == latestPublishedRound);
}

function testReturnUninitializedRoundData() public {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
publishRoundData(newAnswer, beforeLockWindow);
publishRoundData(newAnswer, newTimestamp);

// Before updating, uninitialized values would be returned.
(int256 latestAnswer, uint256 latestTimestamp) = oval.internalDataAtRound(latestPublishedRound);
assertTrue(latestAnswer == 0 && latestTimestamp == 0);
(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
oval.getRoundData(uint80(latestPublishedRound));
assertTrue(roundId == latestPublishedRound);
assertTrue(answer == 0);
assertTrue(startedAt == 0);
assertTrue(updatedAt == 0);
assertTrue(answeredInRound == latestPublishedRound);
}

function testReturnUnlockedRoundData() public {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
publishRoundData(newAnswer, beforeLockWindow);
publishRoundData(newAnswer, newTimestamp);

// Unlock new round values.
vm.prank(permissionedUnlocker);
oval.unlockLatestValue();
verifyOvalMatchesOval();

// After unlock we should return the new values.
(int256 latestAnswer, uint256 latestTimestamp) = oval.internalDataAtRound(latestPublishedRound);
assertTrue(latestAnswer == newAnswer && latestTimestamp == beforeLockWindow);
(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
oval.getRoundData(uint80(latestPublishedRound));
assertTrue(roundId == latestPublishedRound);
assertTrue(answer == newAnswer / internalDecimalsToSourceDecimals);
assertTrue(startedAt == newTimestamp);
assertTrue(updatedAt == newTimestamp);
assertTrue(answeredInRound == latestPublishedRound);
}
}
27 changes: 27 additions & 0 deletions test/unit/Oval.UnlockLatestValue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,31 @@ contract OvalUnlockLatestValue is CommonTest {
assertTrue(latestAnswer == newAnswer && latestTimestamp == beforeLockWindow);
assertTrue(latestRoundId == latestPublishedRound - 1);
}

function testReturnUninitializedRoundData() public {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
publishRoundData(newAnswer, newTimestamp);

// Before updating, uninitialized values would be returned.
(int256 latestAnswer, uint256 latestTimestamp) = oval.internalDataAtRound(latestPublishedRound);
assertTrue(latestAnswer == 0 && latestTimestamp == 0);
}

function testReturnUnlockedRoundData() public {
// Advance time to within the lock window and update the source.
uint256 beforeLockWindow = block.timestamp + oval.lockWindow() - 1;
vm.warp(beforeLockWindow);
publishRoundData(newAnswer, newTimestamp);

// Unlock new round values.
vm.prank(permissionedUnlocker);
oval.unlockLatestValue();
verifyOvalMatchesOval();

// After unlock we should return the new values.
(int256 latestAnswer, uint256 latestTimestamp) = oval.internalDataAtRound(latestPublishedRound);
assertTrue(latestAnswer == newAnswer && latestTimestamp == newTimestamp);
}
}

0 comments on commit a043bae

Please sign in to comment.