Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "contracts/clamped_oracle/lib/forge-std"]
path = contracts/clamped_oracle/lib/forge-std
url = https://github.com/foundry-rs/forge-std
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions contracts/clamped_oracle/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Show Forge version
run: |
forge --version

- name: Run Forge fmt
run: |
forge fmt --check
id: fmt

- name: Run Forge build
run: |
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions contracts/clamped_oracle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env
66 changes: 66 additions & 0 deletions contracts/clamped_oracle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
6 changes: 6 additions & 0 deletions contracts/clamped_oracle/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions contracts/clamped_oracle/lib/forge-std
Submodule forge-std added at 7117c9
19 changes: 19 additions & 0 deletions contracts/clamped_oracle/script/Counter.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";

contract CounterScript is Script {
Counter public counter;

function setUp() public {}

function run() public {
vm.startBroadcast();

counter = new Counter();

vm.stopBroadcast();
}
}
Empty file.
183 changes: 183 additions & 0 deletions contracts/clamped_oracle/src/ClampedOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import {IClampedOracle} from "./interfaces/IClampedOracle.sol";
import {AggregatorInterface} from "./interfaces/AggregatorInterface.sol";
import {IHydraChainlinkOracle} from "./interfaces/IHydraChainlinkOracle.sol";

contract ClampedOracle is IClampedOracle {
uint256 public constant MAX_BPS = 10_000;

AggregatorInterface private immutable primaryAgg;
IHydraChainlinkOracle private immutable secondaryAgg;

uint256 public immutable override maxDiffBps;

constructor(
address primaryFeed,
address secondaryFeed,
uint256 maxDiffBps_
) {
if (primaryFeed == address(0) || secondaryFeed == address(0))
revert InvalidFeed();
if (maxDiffBps_ > MAX_BPS) revert InvalidBps();

primaryAgg = AggregatorInterface(primaryFeed);
secondaryAgg = IHydraChainlinkOracle(secondaryFeed);
maxDiffBps = maxDiffBps_;

emit ClampedOracleInitialized(primaryFeed, secondaryFeed, maxDiffBps_);
}

function primary() external view override returns (address) {
return address(primaryAgg);
}

function secondary() external view override returns (address) {
return address(secondaryAgg);
}

function decimals() external pure returns (uint8) {
return 8;
}

function latestAnswer() public view override returns (int256) {
(bool pOk, int256 pAns) = _tryLatestAnswerPrimary();
(bool sOk, int256 sAns) = _tryLatestAnswerSecondary();

if (!pOk && !sOk) revert NoValidPrice();
if (!sOk) return pAns;
if (!pOk) return sAns;

if (pAns <= 0 || sAns <= 0) revert NoValidPrice();

uint256 P = uint256(pAns);
uint256 S = uint256(sAns);

uint256 lower = (S * (MAX_BPS - maxDiffBps)) / MAX_BPS;
uint256 upper = (S * (MAX_BPS + maxDiffBps)) / MAX_BPS;

uint256 out = P;
if (out < lower) out = lower;
if (out > upper) out = upper;

return int256(out);
}

function latestTimestamp() public view override returns (uint256) {
(bool pOk, uint256 pTs) = _tryLatestTimestampPrimary();

if (pOk) return pTs;
revert NoValidPrice();
}

function latestRound() external view override returns (uint256) {
return primaryAgg.latestRound();
}

function getAnswer(
uint256 roundId
) external view override returns (int256) {
(bool pOk, int256 pAns) = _tryGetAnswerPrimary(roundId);
(bool sOk, int256 sAns) = _tryGetAnswerSecondary(roundId);

if (!pOk && !sOk) revert NoValidPrice();
if (!sOk) return pAns;
if (!pOk) return sAns;

if (pAns <= 0 || sAns <= 0) revert NoValidPrice();

uint256 P = uint256(pAns);
uint256 S = uint256(sAns);

uint256 lower = (S * (MAX_BPS - maxDiffBps)) / MAX_BPS;
uint256 upper = (S * (MAX_BPS + maxDiffBps)) / MAX_BPS;

uint256 out = P;
if (out < lower) out = lower;
if (out > upper) out = upper;

return int256(out);
}

function getTimestamp(
uint256 roundId
) external view override returns (uint256) {
(bool pOk, uint256 pTs) = _tryGetTimestampPrimary(roundId);

if (pOk) return pTs;
revert NoValidPrice();
}

function _tryLatestAnswerPrimary()
internal
view
returns (bool ok, int256 ans)
{
try primaryAgg.latestAnswer() returns (int256 a) {
if (a <= 0) return (false, 0);
return (true, a);
} catch {
return (false, 0);
}
}

function _tryLatestAnswerSecondary()
internal
view
returns (bool ok, int256 ans)
{
try secondaryAgg.latestAnswer() returns (int256 a) {
if (a <= 0) return (false, 0);
return (true, a);
} catch {
return (false, 0);
}
}

function _tryLatestTimestampPrimary()
internal
view
returns (bool ok, uint256 ts)
{
try primaryAgg.latestTimestamp() returns (uint256 t) {
if (t == 0) return (false, 0);
return (true, t);
} catch {
return (false, 0);
}
}

function _tryGetAnswerPrimary(
uint256 roundId
) internal view returns (bool ok, int256 ans) {
try primaryAgg.getAnswer(roundId) returns (int256 a) {
if (a <= 0) return (false, 0);
return (true, a);
} catch {
return (false, 0);
}
}

function _tryGetAnswerSecondary(
uint256 roundId
) internal view returns (bool ok, int256 ans) {
try secondaryAgg.getAnswer(roundId) returns (int256 a) {
if (a <= 0) return (false, 0);
return (true, a);
} catch {
return (false, 0);
}
}

function _tryGetTimestampPrimary(
uint256 roundId
) internal view returns (bool ok, uint256 ts) {
try primaryAgg.getTimestamp(roundId) returns (uint256 t) {
if (t == 0) return (false, 0);
return (true, t);
} catch {
return (false, 0);
}
}
}
14 changes: 14 additions & 0 deletions contracts/clamped_oracle/src/Counter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract Counter {
uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}
}
26 changes: 26 additions & 0 deletions contracts/clamped_oracle/src/interfaces/AggregatorInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

interface AggregatorInterface {
function latestAnswer() external view returns (int256);

function latestTimestamp() external view returns (uint256);

function latestRound() external view returns (uint256);

function getAnswer(uint256 roundId) external view returns (int256);

function getTimestamp(uint256 roundId) external view returns (uint256);

event AnswerUpdated(
int256 indexed current,
uint256 indexed roundId,
uint256 updatedAt
);

event NewRound(
uint256 indexed roundId,
address indexed startedBy,
uint256 startedAt
);
}
Loading
Loading