generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 17
[VEN-1895] Add Arbitrum sequencer downtime validation for Chainlink Oracle #128
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ab8e8d
feat: add Chainlink Oracle for Arbitrum checking Sequencer uptime.
0xlucian 9fae6ed
refactor: rename ArbiChainlinkOracle
0xlucian e878ad2
refactor: Apply suggestions from code review
0xlucian d8af595
refactor: add comments on sequencer oracle return values
0xlucian 82f1e42
fix: VTIME-3
Debugger022 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 hidden or 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 hidden or 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,45 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.13; | ||
|
||
import { ChainlinkOracle } from "./ChainlinkOracle.sol"; | ||
import { AggregatorV3Interface } from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
|
||
/** | ||
@title Sequencer Chain Link Oracle | ||
@notice Oracle to fetch price using chainlink oracles on L2s with sequencer | ||
*/ | ||
contract SequencerChainlinkOracle is ChainlinkOracle { | ||
/// @notice L2 Sequencer feed | ||
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable | ||
AggregatorV3Interface public immutable sequencer; | ||
|
||
/// @notice L2 Sequencer grace period | ||
uint256 public constant GRACE_PERIOD_TIME = 3600; | ||
0xlucian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
@notice Contract constructor | ||
@param _sequencer L2 sequencer | ||
0xlucian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@custom:oz-upgrades-unsafe-allow constructor | ||
*/ | ||
constructor(AggregatorV3Interface _sequencer) ChainlinkOracle() { | ||
require(address(_sequencer) != address(0), "zero address"); | ||
|
||
sequencer = _sequencer; | ||
} | ||
|
||
/// @inheritdoc ChainlinkOracle | ||
function getPrice(address asset) public view override returns (uint) { | ||
if (!isSequencerActive()) revert("L2 sequencer unavailable"); | ||
return super.getPrice(asset); | ||
} | ||
|
||
function isSequencerActive() internal view returns (bool) { | ||
// answer from oracle is a variable with a value of either 1 or 0 | ||
// 0: The sequencer is up | ||
// 1: The sequencer is down | ||
// startedAt: This timestamp indicates when the sequencer changed status | ||
(, int256 answer, uint256 startedAt, , ) = sequencer.latestRoundData(); | ||
if (block.timestamp - startedAt <= GRACE_PERIOD_TIME || answer == 1) return false; | ||
return true; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,73 @@ | ||
import { FakeContract, MockContract, smock } from "@defi-wonderland/smock"; | ||
import chai from "chai"; | ||
import { parseUnits } from "ethers/lib/utils"; | ||
import { ethers } from "hardhat"; | ||
|
||
import { AggregatorV3Interface, SequencerChainlinkOracle, SequencerChainlinkOracle__factory } from "../typechain-types"; | ||
import { getTime } from "./utils/time"; | ||
|
||
const { expect } = chai; | ||
chai.use(smock.matchers); | ||
|
||
describe("SequencerChainlinkOracle", () => { | ||
let sequencerChainlinkOracle: MockContract<SequencerChainlinkOracle>; | ||
let sequencerFeed: FakeContract<AggregatorV3Interface>; | ||
const BNB_ADDR = "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"; | ||
const GRACE_PERIOD = 3600; | ||
const expectedPrice = parseUnits("1", 18); | ||
before(async () => { | ||
await ethers.provider.send("evm_mine", []); // Mine a block to ensure provider is initialized | ||
sequencerFeed = await smock.fake("AggregatorV3Interface"); | ||
const sequencerChainlinkOracleFactory = await smock.mock<SequencerChainlinkOracle__factory>( | ||
"SequencerChainlinkOracle", | ||
); | ||
sequencerChainlinkOracle = await sequencerChainlinkOracleFactory.deploy(sequencerFeed.address); | ||
// configure a hardcoded price just for the sake of returning any value | ||
await sequencerChainlinkOracle.setVariable("prices", { | ||
"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB": expectedPrice, // native-token address hardcoded price | ||
}); | ||
}); | ||
it("Should revert if sequencer is down", async () => { | ||
sequencerFeed.latestRoundData.returns({ | ||
roundId: 0, // arbitraty value (not used in logic) | ||
answer: 1, // (1 - for 'sequencer down') | ||
startedAt: 1, // block.timestamp - startedAt should be > 3600 (GRACE_PERIOD) | ||
updatedAt: 1, // arbitraty value (not used in logic) | ||
answeredInRound: 1, // arbitraty value (not used in logic) | ||
}); | ||
|
||
await expect(sequencerChainlinkOracle.getPrice(BNB_ADDR)).to.be.revertedWith("L2 sequencer unavailable"); | ||
}); | ||
it("Should revert if sequencer is up, but GRACE_PERIOD has not passed", async () => { | ||
sequencerFeed.latestRoundData.returns({ | ||
roundId: 0, // arbitraty value (not used in logic) | ||
answer: 1, // (1 - for 'sequencer down') | ||
startedAt: (await getTime()) - GRACE_PERIOD, // block.timestamp - startedAt should be = 3600 (GRACE_PERIOD) | ||
updatedAt: 1, // arbitraty value (not used in logic) | ||
answeredInRound: 1, // arbitraty value (not used in logic) | ||
}); | ||
|
||
await expect(sequencerChainlinkOracle.getPrice(BNB_ADDR)).to.be.revertedWith("L2 sequencer unavailable"); | ||
|
||
sequencerFeed.latestRoundData.returns({ | ||
roundId: 0, // arbitraty value (not used in logic) | ||
answer: 1, // (1 - for 'sequencer down') | ||
startedAt: await getTime(), // block.timestamp - startedAt should be < 3600 (GRACE_PERIOD) | ||
updatedAt: 1, // arbitraty value (not used in logic) | ||
answeredInRound: 1, // arbitraty value (not used in logic) | ||
}); | ||
|
||
await expect(sequencerChainlinkOracle.getPrice(BNB_ADDR)).to.be.revertedWith("L2 sequencer unavailable"); | ||
}); | ||
it("Should return price", async () => { | ||
sequencerFeed.latestRoundData.returns({ | ||
roundId: 0, // arbitraty value (not used in logic) | ||
answer: 0, // (0 - for 'sequencer up') | ||
startedAt: 1, // block.timestamp - startedAt should be > 3600 (GRACE_PERIOD) | ||
updatedAt: 1, // arbitraty value (not used in logic) | ||
answeredInRound: 1, // arbitraty value (not used in logic) | ||
}); | ||
|
||
expect(await sequencerChainlinkOracle.getPrice(BNB_ADDR)).to.equal(expectedPrice); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.