-
Notifications
You must be signed in to change notification settings - Fork 7
Feat: sdeUSD market #110
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
Feat: sdeUSD market #110
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
014d96a
add initial sdeUSD feed
0xtj24 fb6e11c
add sdeUSD market test
0xtj24 5344ed9
add generalized ERC4626 feed for ChainlinkCurve feeds, update tests
0xtj24 d76af76
update natspec
0xtj24 b2666fb
add sdeUSD ale tests, add standard ale test refactor
0xtj24 fd69849
use previewRedeem in ERC4626Feed
0xtj24 4ac3034
use try catch for previewRedeem
0xtj24 e6820f7
add ERC4626Feed support for asset decimals < 18, add test
0xtj24 2dd3e76
rm logs
0xtj24 c541cf0
fix typo
0xtj24 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.4; | ||
|
|
||
| import {IChainlinkCurveFeed} from "src/interfaces/IChainlinkCurveFeed.sol"; | ||
| import {IERC4626} from "lib/openzeppelin-contracts/contracts/interfaces/IERC4626.sol"; | ||
| import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol"; | ||
| import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; | ||
|
|
||
| /// @title ERC4626Feed | ||
| /// @notice This contract is a generalized contract for an ERC4626 vault which has a feed in a Normalized Asset to USD price | ||
| /// @dev It will convert the normalized asset to USD price to the Asset to USD price using the vault's rate | ||
| /// @dev This is contract is meant to be used in combination with ChainlinkCurveFeed or ChainlinkCurve2CoinsFeed contracts. | ||
| /// @dev Underlying asset decimals must be 18 or lower. | ||
|
|
||
| contract ERC4626Feed { | ||
| using FixedPointMathLib for uint256; | ||
| error DecimalsMismatch(); | ||
|
|
||
| // ChainlinkCurve feed for the normalized asset to USD price | ||
| IChainlinkCurveFeed public immutable feed; | ||
| // ERC4626 vault asset | ||
| IERC4626 public immutable vault; | ||
| // Asset scale | ||
| uint256 public immutable assetScale; | ||
| // Scaling factor | ||
| uint256 public constant SCALE = 1e18; | ||
| // Description of the feed | ||
| string public description; | ||
|
|
||
| constructor(address _vault, address _feed) { | ||
| feed = IChainlinkCurveFeed(_feed); | ||
| vault = IERC4626(_vault); | ||
| assetScale = 10 ** IERC20Metadata(vault.asset()).decimals(); | ||
|
|
||
| if ( | ||
| feed.decimals() != 18 || | ||
| vault.decimals() != 18 || | ||
| assetScale > SCALE | ||
| ) revert DecimalsMismatch(); | ||
|
|
||
| description = string( | ||
| abi.encodePacked( | ||
| feed.description(), | ||
| " using ", | ||
| vault.symbol(), | ||
| " vault rate" | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return roundId The round ID from the feed | ||
| * @return assetToUsdPrice The latest asset price in USD | ||
| * @return startedAt The timestamp when the latest round of feed started | ||
| * @return updatedAt The timestamp when the latest round of feed was updated | ||
| * @return answeredInRound The round ID in which the answer was computed | ||
| */ | ||
| function latestRoundData() | ||
| public | ||
| view | ||
| returns (uint80, int256, uint256, uint256, uint80) | ||
| { | ||
| ( | ||
| uint80 roundId, | ||
| int256 normalizedAssetToUsdPrice, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) = feed.latestRoundData(); | ||
|
|
||
| uint256 assetToUnderlyingRate; | ||
|
|
||
| try vault.previewRedeem(SCALE) returns (uint256 rate) { | ||
| assetToUnderlyingRate = rate.divWadDown(assetScale); | ||
| } catch { | ||
| assetToUnderlyingRate = vault.convertToAssets(SCALE).divWadDown( | ||
| assetScale | ||
| ); | ||
| } | ||
|
|
||
| // Multiply Normalized Asset/USD price by asset/underlying rate to get Asset/USD price | ||
| int256 assetToUsdPrice = int256( | ||
| (uint256(normalizedAssetToUsdPrice) * assetToUnderlyingRate) / SCALE | ||
| ); | ||
|
|
||
| return ( | ||
| roundId, | ||
| assetToUsdPrice, | ||
| startedAt, | ||
| updatedAt, | ||
| answeredInRound | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| @notice Retrieves the latest asset price | ||
| @return price The latest asset price | ||
| */ | ||
| function latestAnswer() external view returns (int256) { | ||
| (, int256 price, , , ) = latestRoundData(); | ||
| return price; | ||
| } | ||
|
|
||
| /** | ||
| * @notice Retrieves number of decimals for the price feed | ||
| * @return decimals The number of decimals for the price feed | ||
| */ | ||
| function decimals() public pure returns (uint8) { | ||
| return 18; | ||
| } | ||
| } | ||
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,31 @@ | ||
| pragma solidity ^0.8.13; | ||
|
|
||
| import {IChainlinkBasePriceFeed} from "src/interfaces/IChainlinkFeed.sol"; | ||
| import {ICurvePool} from "src/interfaces/ICurvePool.sol"; | ||
|
|
||
| interface IChainlinkCurveFeed { | ||
| function decimals() external view returns (uint8 decimals); | ||
|
|
||
| function latestRoundData() | ||
| external | ||
| view | ||
| returns ( | ||
| uint80 roundId, | ||
| int256 crvUsdPrice, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ); | ||
|
|
||
| function latestAnswer() external view returns (int256 price); | ||
|
|
||
| function description() external view returns (string memory description); | ||
|
|
||
| function assetToUsd() external view returns (IChainlinkBasePriceFeed feed); | ||
|
|
||
| function curvePool() external view returns (ICurvePool pool); | ||
|
|
||
| function targetIndex() external view returns (uint256 index); | ||
|
|
||
| function assetOrTargetK() external view returns (uint256 k); | ||
| } |
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,192 @@ | ||
| // SPDX-License-Identifier: UNLICENSED | ||
| pragma solidity ^0.8.19; | ||
|
|
||
| import "forge-std/Test.sol"; | ||
| import "src/feeds/ERC4626Feed.sol"; | ||
| import "src/interfaces/IChainlinkFeed.sol"; | ||
| import {ChainlinkCurveFeed, ICurvePool} from "src/feeds/ChainlinkCurveFeed.sol"; | ||
| import "forge-std/console.sol"; | ||
| import {ERC20 as ERC20Mock} from "test/mocks/ERC20.sol"; | ||
| import {ERC4626, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol"; | ||
| import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
|
||
| contract Mock4626 is ERC4626 { | ||
| constructor(IERC20 _asset) ERC20("MOCK", "MOCK") ERC4626(_asset) {} | ||
|
|
||
| function _decimalsOffset() internal view override returns (uint8) { | ||
| return 12; | ||
| } | ||
| } | ||
|
|
||
| contract SdeUSDFeedForkTest is Test { | ||
| ChainlinkCurveFeed curveFeed; | ||
| ERC4626Feed feed; | ||
| address curvePool = address(0x82202CAEC5E6d85014eADC68D4912F3C90093e7C); | ||
| uint256 k = 0; | ||
| uint256 targetIndex = 1; | ||
| address sdeUSD = address(0x5C5b196aBE0d54485975D1Ec29617D42D9198326); | ||
| address dolaFeed = address(0x6255981e2a1EBeA600aFC506185590eD383517be); | ||
|
|
||
| ERC20Mock mock6Decimals; | ||
| Mock4626 mock6Vault; | ||
| ERC4626Feed feed6Decimals; | ||
|
|
||
| function setUp() public { | ||
| string memory url = vm.rpcUrl("mainnet"); | ||
| vm.createSelectFork(url); | ||
| curveFeed = new ChainlinkCurveFeed(dolaFeed, curvePool, k, targetIndex); | ||
| feed = new ERC4626Feed(sdeUSD, address(curveFeed)); | ||
| } | ||
|
|
||
| function test_6Decimals_underlying_asset_Feed_returns_18() public { | ||
| mock6Decimals = new ERC20Mock("Mock6", "M6", 6); | ||
| mock6Vault = new Mock4626(IERC20(address(mock6Decimals))); | ||
| assertEq(mock6Vault.decimals(), 18); | ||
|
|
||
| feed6Decimals = new ERC4626Feed( | ||
| address(mock6Vault), | ||
| address(curveFeed) | ||
| ); | ||
| uint256 amount = 10000e6; | ||
| assertEq(feed6Decimals.decimals(), 18); | ||
| assertEq(feed6Decimals.assetScale(), 1e6); | ||
|
|
||
| mock6Decimals.mint(address(this), amount); | ||
| mock6Decimals.approve(address(mock6Vault), amount); | ||
| mock6Vault.deposit(amount, address(this)); | ||
|
|
||
| assertEq(mock6Vault.convertToAssets(1e18), 1e6); | ||
| assertEq(mock6Vault.previewRedeem(1e18), 1e6); | ||
| assertEq(mock6Vault.convertToShares(1e6), 1e18); | ||
| assertEq(mock6Vault.previewDeposit(1e6), 1e18); | ||
|
|
||
| uint256 sdeUSDNormalizedToDola = ICurvePool(curvePool).price_oracle( | ||
| curveFeed.assetOrTargetK() | ||
| ); | ||
|
|
||
| int256 dolaToUsdPrice = curveFeed.assetToUsd().latestAnswer(); | ||
| int256 sdeUSDNormalizedToUsdPrice = int256( | ||
| (sdeUSDNormalizedToDola * uint(dolaToUsdPrice)) / 1e18 | ||
| ); | ||
|
|
||
| uint256 sdeUSDToDeUSDRate = mock6Vault.previewRedeem(1e18); | ||
|
|
||
| uint256 price = (uint(sdeUSDNormalizedToUsdPrice) * sdeUSDToDeUSDRate) / | ||
| 1e6; | ||
| assertEq(feed6Decimals.latestAnswer(), int256(price)); | ||
| } | ||
|
|
||
| function test_decimals() public { | ||
| assertEq(feed.decimals(), 18); | ||
| assertEq(feed.assetScale(), 1e18); | ||
| } | ||
|
|
||
| function test_description() public { | ||
| assertEq(feed.description(), "sdeUSD / USD using sdeUSD vault rate"); | ||
| } | ||
|
|
||
| function test_latestRoundData() public { | ||
| ( | ||
| uint80 roundId, | ||
| int256 sdeUSDToUsdPrice, | ||
| uint256 startedAt, | ||
| uint256 updatedAt, | ||
| uint80 answeredInRound | ||
| ) = feed.latestRoundData(); | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); | ||
| ( | ||
| uint80 roundId2, | ||
| , | ||
| uint256 startedAt2, | ||
| uint256 updatedAt2, | ||
| uint80 answeredInRound2 | ||
| ) = IChainlinkBasePriceFeed(dolaFeed).latestRoundData(); | ||
| // Data are | ||
| assertEq(roundId, roundId2); | ||
| assertEq(sdeUSDToUsdPrice, _calculateSdeUSDPrice()); | ||
| assertEq(startedAt, startedAt2); | ||
| assertEq(updatedAt, updatedAt2); | ||
| assertEq(answeredInRound, answeredInRound2); | ||
| } | ||
|
|
||
| function test_sdeUSD_upward_depeg() public { | ||
| int256 answer = feed.latestAnswer(); | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); | ||
| uint256 mockRate = 2e18; | ||
| _mockVaultRate(sdeUSD, mockRate); | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); | ||
| assertGt(feed.latestAnswer(), answer); | ||
| } | ||
|
|
||
| function test_sdeUSD_downward_depeg() public { | ||
| int256 answer = feed.latestAnswer(); | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); | ||
| uint256 mockRate = 0.5e18; | ||
| _mockVaultRate(sdeUSD, mockRate); | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); | ||
| assertLt(feed.latestAnswer(), answer); | ||
| } | ||
|
|
||
| function test_previewRedeemRevert_useConvertToAssets() public { | ||
| // Mock preview redeem rate | ||
| _mockVaultRate(sdeUSD, 2e18); | ||
| // Answer is equal to preview redeem rate but not equal to convert to assets | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPrice()); | ||
| assertGt(feed.latestAnswer(), _calculateSdeUSDPriceConvertToAssets()); | ||
| // Mock preview redeem revert to use convert to assets | ||
| _mockPreviewRevert(sdeUSD); | ||
| assertEq(feed.latestAnswer(), _calculateSdeUSDPriceConvertToAssets()); | ||
| } | ||
|
|
||
| function _calculateSdeUSDPrice() internal view returns (int256) { | ||
| uint256 sdeUSDNormalizedToDola = ICurvePool(curvePool).price_oracle( | ||
| curveFeed.assetOrTargetK() | ||
| ); | ||
|
|
||
| int256 dolaToUsdPrice = curveFeed.assetToUsd().latestAnswer(); | ||
| int256 sdeUSDNormalizedToUsdPrice = int256( | ||
| (sdeUSDNormalizedToDola * uint(dolaToUsdPrice)) / 1e18 | ||
| ); | ||
|
|
||
| uint256 sdeUSDToDeUSDRate = IERC4626(sdeUSD).previewRedeem(1e18); | ||
| return | ||
| (sdeUSDNormalizedToUsdPrice * int(sdeUSDToDeUSDRate)) / | ||
| int256(1e18); | ||
| } | ||
|
|
||
| function _calculateSdeUSDPriceConvertToAssets() | ||
| internal | ||
| view | ||
| returns (int256) | ||
| { | ||
| uint256 sdeUSDNormalizedToDola = ICurvePool(curvePool).price_oracle( | ||
| curveFeed.assetOrTargetK() | ||
| ); | ||
|
|
||
| int256 dolaToUsdPrice = curveFeed.assetToUsd().latestAnswer(); | ||
| int256 sdeUSDNormalizedToUsdPrice = int256( | ||
| (sdeUSDNormalizedToDola * uint(dolaToUsdPrice)) / 1e18 | ||
| ); | ||
|
|
||
| uint256 sdeUSDToDeUSDRate = IERC4626(sdeUSD).convertToAssets(1e18); | ||
| return | ||
| (sdeUSDNormalizedToUsdPrice * int(sdeUSDToDeUSDRate)) / | ||
| int256(1e18); | ||
| } | ||
|
|
||
| function _mockVaultRate(address vault, uint256 mockRate) internal { | ||
| vm.mockCall( | ||
| vault, | ||
| abi.encodeWithSelector(IERC4626.previewRedeem.selector, 1e18), | ||
| abi.encode(mockRate) | ||
| ); | ||
| } | ||
|
|
||
| function _mockPreviewRevert(address vault) internal { | ||
| vm.mockCallRevert( | ||
| vault, | ||
| abi.encodeWithSelector(IERC4626.previewRedeem.selector, 1e18), | ||
| "mock revert" | ||
| ); | ||
| } | ||
| } |
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,43 @@ | ||
| // SPDX-License-Identifier: UNLICENSED | ||
| pragma solidity ^0.8.13; | ||
|
|
||
| import "forge-std/Test.sol"; | ||
| import "./MarketBaseForkTest.sol"; | ||
| import "src/feeds/ERC4626Feed.sol"; | ||
| import "src/Market.sol"; | ||
| import {ChainlinkCurveFeed} from "src/feeds/ChainlinkCurveFeed.sol"; | ||
|
|
||
| contract SdeUSDMarketForkTest is MarketBaseForkTest { | ||
| address curvePool = address(0x82202CAEC5E6d85014eADC68D4912F3C90093e7C); | ||
| uint256 k = 0; | ||
| uint256 targetIndex = 1; | ||
| address sdeUSD = address(0x5C5b196aBE0d54485975D1Ec29617D42D9198326); | ||
| address dolaFeed = address(0x6255981e2a1EBeA600aFC506185590eD383517be); | ||
|
|
||
| function setUp() public virtual { | ||
| //This will fail if there's no mainnet variable in foundry.toml | ||
| string memory url = vm.rpcUrl("mainnet"); | ||
| vm.createSelectFork(url, 21880783); | ||
| address curveFeed = address( | ||
| new ChainlinkCurveFeed(dolaFeed, curvePool, k, targetIndex) | ||
| ); | ||
| address feedAddr = address(new ERC4626Feed(sdeUSD, curveFeed)); | ||
|
|
||
| address marketAddr = address( | ||
| new Market( | ||
| gov, | ||
| lender, | ||
| pauseGuardian, | ||
| simpleERC20EscrowAddr, | ||
| IDolaBorrowingRights(dbrAddr), | ||
| IERC20(sdeUSD), | ||
| IOracle(oracleAddr), | ||
| 5000, | ||
| 1000, | ||
| 1000, | ||
| false | ||
| ) | ||
| ); | ||
| _advancedInit(marketAddr, feedAddr, false); | ||
| } | ||
| } |
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.
this should be correct as it should normalize the rate of a non 18 decimal asset to an 18 decimal rate.
so in example a token with 6 decimals: 1e6 * 1e18 / 1e6 = 1e18