-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: redstone oracle adapter test #13
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
22fce48
feat: redstone lib
md0x d40df80
feat: remappings
md0x ca1a2e6
forge install: openzeppelin-contracts-upgradeable
md0x 84fbaf7
fixes
md0x 35fecdc
forge install: openzeppelin-contracts-upgradeable
md0x 3f422bf
fixes
md0x 94ef06c
wip
md0x e2ad14b
wip
md0x da4e998
ffi
md0x 7ab11ab
working
md0x 26c33a1
refactor: cleanup
md0x 0a1d27f
refactor: cleanup2
md0x 1c71533
feat: timestamp
md0x e890b38
refactor: clean
md0x ff7c301
more tests
chrismaree 7744fe0
fix: ci
md0x cfc7eee
fix: ci bis
md0x 0a7c2a3
refactor: clean libs
md0x 9a5b832
forge install: redstone-contracts
md0x 282fbba
feat: remappings
md0x 48ebe72
Merge branch 'master' into pablo/redstone-adapter
md0x da0ab20
fix: redstone tests
md0x 08875ca
fix: redstone test round id
md0x 08cd2d5
feat: update redstone lib
md0x 5cf167b
feat: update redstone lib
md0x 5808795
feat: update redstone lib
md0x 9931d9b
feat: update remappings
md0x fdafdc4
feat: updates
md0x e4073cf
refactor: del test redstone oracle
md0x 4c625ea
feat: rename redstone remapping
md0x 31ac93f
feat: clean submodules
md0x fcf2613
refactor: console log
md0x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule redstone-contracts
added at
8bcda0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
ds-test/=lib/forge-std/lib/ds-test/src/ | ||
forge-std/=lib/forge-std/src/ | ||
openzeppelin-contracts/=lib/openzeppelin-contracts/ | ||
redstone-oracle/=lib/redstone-contracts/src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const { appendFileSync } = require("fs"); | ||
const { RedstonePayload } = require("@redstone-finance/protocol"); | ||
const ethers = require("ethers"); | ||
const sdk = require("@redstone-finance/sdk"); | ||
const args = process.argv.slice(2); | ||
|
||
const exit = (code, message) => { | ||
process.stderr.write(message); | ||
appendFileSync("./getRedstonePayload.log.txt", message); | ||
process.exit(code); | ||
}; | ||
|
||
const parsePrice = (value) => { | ||
const hexString = ethers.utils.hexlify(value); | ||
const bigNumberPrice = ethers.BigNumber.from(hexString); | ||
return bigNumberPrice.toNumber(); | ||
}; | ||
|
||
const pickMedian = (arr) => { | ||
if (arr.length === 0) { | ||
throw new Error("Cannot pick median of empty array"); | ||
} | ||
arr.sort((a, b) => a - b); | ||
const middleIndex = Math.floor(arr.length / 2); | ||
if (arr.length % 2 === 0) { | ||
return (arr[middleIndex - 1] + arr[middleIndex]) / 2; | ||
} else { | ||
return arr[middleIndex]; | ||
} | ||
}; | ||
|
||
const main = async () => { | ||
if (args.length === 0) { | ||
exit(1, "You have to provide a data Feed"); | ||
} | ||
|
||
const dataFeed = args[0]; | ||
|
||
const getLatestSignedPrice = await sdk.requestDataPackages({ | ||
dataServiceId: "redstone-primary-prod", | ||
uniqueSignersCount: 3, | ||
dataFeeds: [dataFeed], | ||
urls: ["https://oracle-gateway-1.a.redstone.finance"], | ||
}); | ||
|
||
const prices = getLatestSignedPrice[dataFeed].map((dataPackage) => | ||
parsePrice(dataPackage.dataPackage.dataPoints[0].value) | ||
); | ||
|
||
const medianPrice = pickMedian(prices); | ||
|
||
const payload = RedstonePayload.prepare(getLatestSignedPrice[dataFeed], ""); | ||
|
||
const timestampMS = | ||
getLatestSignedPrice[dataFeed][0].dataPackage.timestampMilliseconds; | ||
|
||
const encodedData = ethers.utils.defaultAbiCoder.encode( | ||
["bytes", "uint256", "uint256"], | ||
["0x" + payload, timestampMS, medianPrice] | ||
); | ||
|
||
process.stdout.write(encodedData); | ||
|
||
process.exit(0); | ||
}; | ||
|
||
main().catch((error) => { | ||
exit(4, `An error occurred: ${error.message}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
test/fork/adapters/RedStoneAsChainlinkSourceAdapter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.17; | ||
|
||
// These tests show that we can treat redstone exactly as chainlink and use it within the Oval ecosystem without the | ||
// need for a new adapter. This assumes the use of Redstone Classic. | ||
|
||
import {CommonTest} from "../../Common.sol"; | ||
import {BaseController} from "../../../src/controllers/BaseController.sol"; | ||
|
||
import {DecimalLib} from "../../../src/adapters/lib/DecimalLib.sol"; | ||
import {ChainlinkSourceAdapter} from "../../../src/adapters/source-adapters/ChainlinkSourceAdapter.sol"; | ||
|
||
import {IAggregatorV3Source} from "../../../src/interfaces/chainlink/IAggregatorV3Source.sol"; | ||
import {MergedPriceFeedAdapterWithRounds} from | ||
"redstone-oracle/on-chain-relayer/contracts/price-feeds/with-rounds/MergedPriceFeedAdapterWithRounds.sol"; | ||
|
||
contract TestedSourceAdapter is ChainlinkSourceAdapter { | ||
constructor(IAggregatorV3Source source) ChainlinkSourceAdapter(source) {} | ||
|
||
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) {} | ||
|
||
function lockWindow() public view virtual override returns (uint256) {} | ||
|
||
function maxTraversal() public view virtual override returns (uint256) {} | ||
} | ||
|
||
contract RedstoneAsChainlinkSourceAdapterTest is CommonTest { | ||
uint256 targetBlock = 19889008; | ||
|
||
MergedPriceFeedAdapterWithRounds redstone; | ||
TestedSourceAdapter sourceAdapter; | ||
|
||
function setUp() public { | ||
vm.createSelectFork("mainnet", targetBlock); | ||
redstone = MergedPriceFeedAdapterWithRounds(0xdDb6F90fFb4d3257dd666b69178e5B3c5Bf41136); // Redstone weETH | ||
sourceAdapter = new TestedSourceAdapter(IAggregatorV3Source(address(redstone))); | ||
} | ||
|
||
function testCorrectlyStandardizesOutputs() public { | ||
(, int256 latestRedstoneAnswer,, uint256 latestRedstoneTimestamp,) = redstone.latestRoundData(); | ||
(int256 latestSourceAnswer, uint256 latestSourceTimestamp) = sourceAdapter.getLatestSourceData(); | ||
assertTrue(scaleRedstoneTo18(latestRedstoneAnswer) == latestSourceAnswer); | ||
assertTrue(latestSourceTimestamp == latestRedstoneTimestamp); | ||
} | ||
|
||
function testCanApplyRedstoneUpdateToSource() public { | ||
(uint80 roundId, int256 latestAnswer, uint256 latestTimestamp,,) = redstone.latestRoundData(); | ||
// Values read from the contract at block before applying the update. | ||
assertTrue(roundId == 2009); | ||
assertTrue(latestAnswer == 316882263951); | ||
assertTrue(latestTimestamp == 1715934227); | ||
applyKnownRedstoneUpdate(); | ||
(roundId, latestAnswer, latestTimestamp,,) = redstone.latestRoundData(); | ||
assertTrue(roundId == 2010); | ||
assertTrue(latestAnswer == 313659742144); | ||
assertTrue(latestTimestamp == block.timestamp); | ||
} | ||
|
||
function testCorrectlyLooksBackThroughRounds() public { | ||
// Try fetching the price from some periods in the past and make sure it returns the corespending value for | ||
// given historic lookback. By looking at the contract history on-chain around the blocknumber, we can see | ||
// how many rounds back we expect to look. Looking 1 hour back shows no updates were applied in that interval. | ||
// we should be able to query one hour ago and get the latest round data from both the redstone source and the | ||
// adapter. | ||
uint256 targetTime = block.timestamp - 1 hours; | ||
(uint80 latestRound,,,,) = redstone.latestRoundData(); | ||
|
||
(int256 lookBackPrice, uint256 lookBackTimestamp, uint256 roundId) = | ||
sourceAdapter.tryLatestDataAt(targetTime, 10); | ||
(, int256 answer,, uint256 updatedAt,) = redstone.getRoundData(latestRound); | ||
assertTrue(roundId == latestRound); | ||
assertTrue(updatedAt <= targetTime); | ||
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice); | ||
assertTrue(updatedAt == lookBackTimestamp); | ||
|
||
// 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); | ||
(, answer,, updatedAt,) = redstone.getRoundData(latestRound - 1); | ||
assertTrue(updatedAt <= targetTime); | ||
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice); | ||
assertTrue(updatedAt == lookBackTimestamp); | ||
|
||
// 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); | ||
(, answer,, updatedAt,) = redstone.getRoundData(latestRound - 2); | ||
assertTrue(updatedAt <= targetTime); | ||
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice); | ||
assertTrue(updatedAt == lookBackTimestamp); | ||
|
||
// 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); | ||
(, answer,, updatedAt,) = redstone.getRoundData(latestRound - 5); | ||
assertTrue(updatedAt <= targetTime); | ||
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice); | ||
assertTrue(updatedAt == lookBackTimestamp); | ||
} | ||
|
||
function testCorrectlyBoundsMaxLookBack() public { | ||
// If we limit how far we can lookback the source should correctly return the oldest data it can find, up to | ||
// that limit. From the previous tests we showed that looking back 2 hours 23 hours returns the price from round | ||
// 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); | ||
(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); | ||
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice); | ||
assertTrue(updatedAt == lookBackTimestamp); | ||
targetTime = block.timestamp - 10 hours; | ||
(lookBackPrice, lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 2); | ||
assertTrue(scaleRedstoneTo18(answer) == lookBackPrice); | ||
assertTrue(updatedAt == lookBackTimestamp); | ||
} | ||
|
||
function testNonHistoricalData() public { | ||
uint256 targetTime = block.timestamp - 1 hours; | ||
|
||
(, int256 answer,, uint256 updatedAt,) = redstone.latestRoundData(); | ||
|
||
(int256 lookBackPrice, uint256 lookBackTimestamp,) = sourceAdapter.tryLatestDataAt(targetTime, 0); | ||
assertEq(lookBackPrice / 10 ** 10, answer); | ||
assertEq(lookBackTimestamp, updatedAt); | ||
} | ||
|
||
function applyKnownRedstoneUpdate() internal { | ||
// Update payload taken from: https://etherscan.io/tx/0xbcde8a894337a7e1f29dcad1f78cb0246c1b29305b5c62e43cf0e1801acc11c9 | ||
bytes memory updatePayload = | ||
hex"c14c92040000000000000000000000000000000000000000000000000000018f86086c107765455448000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000490793d7c0018f86086c1000000020000001f6c1ccae51e44aa9e5f57431f59cbd228c190ba072e1eb0af683fef58e02ac0b7f6432b819356ed51210945643f7362871371344395bf48df443663ee525dcef1c7765455448000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000490793d7c0018f86086c100000002000000180353e157cebadd3ba9a25140b95360e0deb02f23274d275301e53680c135c7c0033dcf794366503d9d500d9003842d03abe812bc9535b644e1b5f0cee72845f1c00023137313539343036363233373123302e332e3623646174612d7061636b616765732d77726170706572000029000002ed57011e0000"; | ||
vm.prank(0x517a67D809549093bD3Ef7C6195546B8BDF24C04); // Permissioned Redstone updater. | ||
|
||
(bool success,) = address(redstone).call(updatePayload); | ||
require(success, "Failed to update Redstone data"); | ||
} | ||
|
||
function scaleRedstoneTo18(int256 input) public pure returns (int256) { | ||
return (input * 10 ** 18) / 10 ** 8; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is to remove an unrelated warning of variable shadowing |
||
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))); | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also what happened here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized it wasn't needed when I was cleaning up the remappings.
Everything is imported with
"openzeppelin-contracts/..."
and Foundry automatically reads from the lib folder.