Skip to content

Commit

Permalink
fix: redstone tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Maldonado <[email protected]>
  • Loading branch information
md0x committed May 20, 2024
1 parent 48ebe72 commit da0ab20
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
24 changes: 8 additions & 16 deletions src/oracles/RedstonePriceFeedWithRounds.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.17;

import "redstone-oracles-monorepo/packages/on-chain-relayer/contracts/price-feeds/with-rounds/MergedPriceFeedAdapterWithRounds.sol";
import
"redstone-oracles-monorepo/packages/on-chain-relayer/contracts/price-feeds/with-rounds/MergedPriceFeedAdapterWithRounds.sol";

contract RedstonePriceFeedWithRounds is MergedPriceFeedAdapterWithRounds {
bytes32 private immutable dataFeedId;

Expand All @@ -21,26 +23,16 @@ contract RedstonePriceFeedWithRounds is MergedPriceFeedAdapterWithRounds {
return 3;
}

function getAuthorisedSignerIndex(
address signerAddress
) public view virtual override returns (uint8) {
function getAuthorisedSignerIndex(address signerAddress) public view virtual override returns (uint8) {
if (signerAddress == 0x8BB8F32Df04c8b654987DAaeD53D6B6091e3B774) {
return 0;
} else if (
signerAddress == 0xdEB22f54738d54976C4c0fe5ce6d408E40d88499
) {
} else if (signerAddress == 0xdEB22f54738d54976C4c0fe5ce6d408E40d88499) {
return 1;
} else if (
signerAddress == 0x51Ce04Be4b3E32572C4Ec9135221d0691Ba7d202
) {
} else if (signerAddress == 0x51Ce04Be4b3E32572C4Ec9135221d0691Ba7d202) {
return 2;
} else if (
signerAddress == 0xDD682daEC5A90dD295d14DA4b0bec9281017b5bE
) {
} else if (signerAddress == 0xDD682daEC5A90dD295d14DA4b0bec9281017b5bE) {
return 3;
} else if (
signerAddress == 0x9c5AE89C4Af6aA32cE58588DBaF90d18a855B6de
) {
} else if (signerAddress == 0x9c5AE89C4Af6aA32cE58588DBaF90d18a855B6de) {
return 4;
} else {
revert SignerNotAuthorised(signerAddress);
Expand Down
20 changes: 11 additions & 9 deletions test/fork/adapters/RedStoneAsChainlinkSourceAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {RedstonePriceFeedWithRounds} from "../../../src/oracles/RedstonePriceFee
contract TestedSourceAdapter is ChainlinkSourceAdapter {
constructor(IAggregatorV3Source source) ChainlinkSourceAdapter(source) {}

function internalLatestData() public view override returns (int256, uint256) {}
function internalLatestData() public view override returns (int256, uint256, uint256) {}

function internalDataAtRound(uint256 roundId) public view override returns (int256, uint256) {}

function canUnlock(address caller, uint256 cachedLatestTimestamp) public view virtual override returns (bool) {}

Expand Down Expand Up @@ -66,7 +68,7 @@ contract RedstoneAsChainlinkSourceAdapterTest is CommonTest {
uint256 targetTime = block.timestamp - 1 hours;
(uint80 latestRound,,,,) = redstone.latestRoundData();

(int256 lookBackPrice, uint256 lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(int256 lookBackPrice, uint256 lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(, int256 answer,, uint256 updatedAt,) = redstone.getRoundData(latestRound);
assertTrue(updatedAt <= targetTime);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
Expand All @@ -75,7 +77,7 @@ contract RedstoneAsChainlinkSourceAdapterTest is CommonTest {
// Next, try looking back 2 hours. by looking on-chain we can see only one update was applied. Therefore we
// should get the values from latestRound -1 (one update applied relative to the "latest" round).
targetTime = block.timestamp - 2 hours;
(lookBackPrice, lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(lookBackPrice, lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(, answer,, updatedAt,) = redstone.getRoundData(latestRound - 1);
assertTrue(updatedAt <= targetTime);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
Expand All @@ -84,7 +86,7 @@ contract RedstoneAsChainlinkSourceAdapterTest is CommonTest {
// Next, try land at 2 rounds ago. Again, by looking on-chain, we can see this is ~2 23 mins before the current
// fork timestamp. We should be able to show the value is the oldest value within this interval.
targetTime = block.timestamp - 2 hours - 23 minutes;
(lookBackPrice, lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(lookBackPrice, lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(, answer,, updatedAt,) = redstone.getRoundData(latestRound - 2);
assertTrue(updatedAt <= targetTime);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
Expand All @@ -93,7 +95,7 @@ contract RedstoneAsChainlinkSourceAdapterTest is CommonTest {
// Now, try 3 hours old. On-chain there were 5 updates in this interval. we should be able to show the value is
// the oldest value within this interval.
targetTime = block.timestamp - 3 hours;
(lookBackPrice, lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(lookBackPrice, lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 10);
(, answer,, updatedAt,) = redstone.getRoundData(latestRound - 5);
assertTrue(updatedAt <= targetTime);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
Expand All @@ -106,19 +108,19 @@ contract RedstoneAsChainlinkSourceAdapterTest is CommonTest {
// 2. If we try look back longer than this we should get the price from round 2, no matter how far we look back,
// if we bound the maximum lookback to 2 rounds.
uint256 targetTime = block.timestamp - 2 hours - 23 minutes;
(int256 lookBackPrice, uint256 lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 2);
(int256 lookBackPrice, uint256 lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 2);
(uint80 latestRound,,,,) = redstone.latestRoundData();
(, int256 answer,, uint256 updatedAt,) = redstone.getRoundData(latestRound - 2);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
assertTrue(updatedAt == lookBackTimestamp);

// Now, lookback longer than 2 hours. should get the same value as before.
targetTime = block.timestamp - 3 hours;
(lookBackPrice, lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 2);
(lookBackPrice, lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 2);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
assertTrue(updatedAt == lookBackTimestamp);
targetTime = block.timestamp - 10 hours;
(lookBackPrice, lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 2);
(lookBackPrice, lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 2);
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice);
assertTrue(updatedAt == lookBackTimestamp);
}
Expand All @@ -128,7 +130,7 @@ contract RedstoneAsChainlinkSourceAdapterTest is CommonTest {

(, int256 answer,, uint256 updatedAt,) = redstone.latestRoundData();

(int256 lookBackPrice, uint256 lookBackTimestamp) = sourceAdapter.tryLatestDataAt(targetTime, 0);
(int256 lookBackPrice, uint256 lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 0);
assertEq(lookBackPrice / 10 ** 10, answer);
assertEq(lookBackTimestamp, updatedAt);
}
Expand Down
4 changes: 2 additions & 2 deletions test/unit/CoinbaseSourceAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ contract CoinbaseSourceAdapterTest is CommonTest {
string public ticker = "ETH";
uint256 public price = 3000e6;

function pushPrice(string memory ticker, uint256 price, uint256 timestamp) public {
function pushPrice(string memory ticker, uint256 priceToPush, uint256 timestamp) public {
string memory kind = "price";

bytes memory encodedData = abi.encode(kind, timestamp, ticker, price);
bytes memory encodedData = abi.encode(kind, timestamp, ticker, priceToPush);

bytes32 hash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", keccak256(encodedData)));

Expand Down

0 comments on commit da0ab20

Please sign in to comment.