Skip to content
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

Add SfrxETH-FrxETH exchange rate adapter #99

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.21;

import {ISfrxEth} from "./interfaces/ISfrxEth.sol";
import {MinimalAggregatorV3Interface} from "../wsteth-exchange-rate-adapter/interfaces/MinimalAggregatorV3Interface.sol";

/// @title SfrxEthFrxEthExchangeRateChainlinkAdapter
/// @notice sfrxETH/frxETH exchange rate price feed
/// @dev The contract should only be deployed on Ethereum and used as a price feed for Morpho oracles.
contract SfrxEthFrxEthExchangeRateChainlinkAdapter is
MinimalAggregatorV3Interface
{
/// @notice inheritdoc MinimalAggregatorV3Interface
/// @dev The calculated price has 18 decimals precision.
uint8 public constant decimals = 18;

/// @notice The description of the price feed.
string public constant description = "sfrxETH/frxETH exchange rate";

/// @notice The address of sfrxETH on Ethereum.
ISfrxEth public constant SFRX_ETH =
ISfrxEth(0xac3E018457B222d93114458476f3E3416Abbe38F);

/// @inheritdoc MinimalAggregatorV3Interface
/// @dev Returns zero for roundId, startedAt, updatedAt and answeredInRound.
/// @dev Silently overflows if `pricePerShare`'s return value is greater than `type(int256).max`.
function latestRoundData()
external
view
returns (uint80, int256, uint256, uint256, uint80)
{
// It is assumed that `pricePerShare` returns a price with 18 decimals precision.
return (0, int256(SFRX_ETH.pricePerShare()), 0, 0, 0);
}
}
6 changes: 6 additions & 0 deletions src/sfrxeth-exchange-rate-adapter/interfaces/ISfrxEth.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;

interface ISfrxEth {
function pricePerShare() external view returns (uint256);
}
71 changes: 71 additions & 0 deletions test/SfrxEthFrxEthExchangeRateChainlinkAdapterTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import "./helpers/Constants.sol";
import "../lib/forge-std/src/Test.sol";
import {MorphoChainlinkOracleV2} from "../src/morpho-chainlink/MorphoChainlinkOracleV2.sol";
import "../src/sfrxeth-exchange-rate-adapter/SfrxEthFrxEthExchangeRateChainlinkAdapter.sol";
import "../src/sfrxeth-exchange-rate-adapter/interfaces/ISfrxEth.sol";

contract SfrxEthFrxEthExchangeRateChainlinkAdapterTest is Test {
ISfrxEth internal constant SFRX_ETH =
ISfrxEth(0xac3E018457B222d93114458476f3E3416Abbe38F);

SfrxEthFrxEthExchangeRateChainlinkAdapter internal adapter;
MorphoChainlinkOracleV2 internal morphoOracle;

function setUp() public {
vm.createSelectFork(vm.envString("ETH_RPC_URL"));
require(block.chainid == 1, "chain isn't Ethereum");
adapter = new SfrxEthFrxEthExchangeRateChainlinkAdapter();
morphoOracle = new MorphoChainlinkOracleV2(
vaultZero,
1,
AggregatorV3Interface(address(adapter)),
feedZero,
18,
vaultZero,
1,
feedZero,
feedZero,
18
);
}

function testDecimals() public {
assertEq(adapter.decimals(), uint8(18));
}

function testDescription() public {
assertEq(adapter.description(), "sfrxETH/frxETH exchange rate");
}

function testLatestRoundData() public {
(
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
) = adapter.latestRoundData();
assertEq(roundId, 0);
assertEq(uint256(answer), SFRX_ETH.pricePerShare());
assertEq(startedAt, 0);
assertEq(updatedAt, 0);
assertEq(answeredInRound, 0);
}

function testLatestRoundDataBounds() public {
(, int256 answer, , , ) = adapter.latestRoundData();
assertGe(uint256(answer), 1087522005449750632); // Exchange rate queried at block 19966877
assertLe(uint256(answer), 1.5e18); // Max bounds of the exchange rate. Should work for a long enough time.
}

function testOracleSfrxEthFrxEthExchangeRate() public {
(, int256 expectedPrice, , , ) = adapter.latestRoundData();
assertEq(
morphoOracle.price(),
uint256(expectedPrice) * 10 ** (36 + 18 - 18 - 18)
);
}
}