diff --git a/README.md b/README.md index f159dce..9da571d 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ -[![Build](https://github.com/fortunafi/reservoir/actions/workflows/test.yml/badge.svg?branch=wissam/add-code-coverage)](https://github.com/fortunafi/reservoir/actions/workflows/test.yml) -[![Coverage](https://reservoir-code-coverage-public.s3.us-west-2.amazonaws.com/master/coverage.svg?no-cache)](https://github.com/fortunafi/reservoir/actions/workflows/coverage.yml) - This repository contains the full suite of smart contracts that define the Reservoir protocol. User's are able to -purchase the native stablecoin *rUSD* and exchange it for a fixed term or variable rate yield bearing tokens *trUSD* and -*srUSD*. The liabilities of the protocol are backed by a mix of real world assets (RWA)s and onchain yield bearing assets in lending protocols and AMMs. +purchase the native stablecoin _rUSD_ and exchange it for a fixed term or variable rate yield bearing tokens _trUSD_ and +_srUSD_. The liabilities of the protocol are backed by a mix of real world assets (RWA)s and onchain yield bearing assets in lending protocols and AMMs. The assets where capital is allocated is fully configurable through governance, and solvency in the protocol is controlled by ratios set through governance that constrain leverage in the system. @@ -19,4 +16,3 @@ solvency in the protocol is controlled by ratios set through governance that con Scripts used for deployment can be found under `bin/`. ## Audits - diff --git a/src/adapters/MorphoRUSDAdapter.sol b/src/adapters/MorphoRUSDAdapter.sol new file mode 100644 index 0000000..02f79ac --- /dev/null +++ b/src/adapters/MorphoRUSDAdapter.sol @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.24; + +import {AccessControl} from "openzeppelin-contracts/contracts/access/AccessControl.sol"; + +import {IERC4626} from "openzeppelin-contracts/contracts/interfaces/IERC4626.sol"; +import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol"; + +import {Stablecoin} from "../Stablecoin.sol"; +import {IOracle} from "src/interfaces/IOracle.sol"; +import {IAssetAdapter} from "src/interfaces/IAssetAdapter.sol"; + +contract MorphoRUSDAdapter is IAssetAdapter, AccessControl { + bytes32 public constant MANAGER = + keccak256(abi.encode("asset.adapter.manager")); + + bytes32 public constant CONTROLLER = + keccak256(abi.encode("asset.adapter.controller")); + + IERC20 public immutable underlying; + IERC4626 public immutable vault; + uint256 public immutable duration; + + IOracle public immutable underlyingPriceOracle; + IOracle public immutable fundPriceOracle; + + uint256 public underlyingRiskWeight; // 100% = 1e6 + uint256 public fundRiskWeight; // 100% = 1e6 + + constructor( + address _admin, + address _underlyingAddr, + address _vaultAddr, + address _underlyingPriceOracleAddr, + address _fundPriceOracleAddr, + uint256 _duration + ) { + _grantRole(DEFAULT_ADMIN_ROLE, _admin); + + underlying = Stablecoin(_underlyingAddr); + vault = IERC4626(_vaultAddr); + duration = _duration; + + underlyingPriceOracle = IOracle(_underlyingPriceOracleAddr); + fundPriceOracle = IOracle(_fundPriceOracleAddr); + } + + function allocate(uint256 amount) external {} + + function withdraw(uint256 amount) external {} + + function deposit(uint256 assets) public onlyRole(CONTROLLER) { + Stablecoin(address(underlying)).mint(address(this), assets); + underlying.approve(address(vault), assets); + vault.deposit(assets, address(this)); + + emit Deposit(msg.sender, assets, block.timestamp); + } + + function redeem(uint256 shares) public onlyRole(CONTROLLER) { + Stablecoin underlyingStablecoin = Stablecoin(address(underlying)); + vault.redeem(shares, address(this), address(this)); + underlyingStablecoin.burn( + underlyingStablecoin.balanceOf(address(this)) + ); + + emit Redeem(msg.sender, shares, block.timestamp); + } + + function setUnderlyingRiskWeight( + uint256 _riskWeight + ) external onlyRole(MANAGER) { + require(1e6 > _riskWeight, "FA: Risk Weight can not be above 100%"); + + underlyingRiskWeight = _riskWeight; + + emit UnderlyingRiskWeightUpdate(_riskWeight, block.timestamp); + } + + function setFundRiskWeight(uint256 _riskWeight) external onlyRole(MANAGER) { + require(1e6 > _riskWeight, "FA: Risk Weight can not be above 100%"); + + fundRiskWeight = _riskWeight; + + emit FundRiskWeightUpdate(_riskWeight, block.timestamp); + } + + function totalValue() external view returns (uint256 total) { + total += _underlyingTotalValue(); + total += _fundTotalValue(); + } + + function totalRiskValue() external view returns (uint256 total) { + total += _underlyingTotalRiskValue(); + total += _fundTotalRiskValue(); + } + + function underlyingTotalRiskValue() external view returns (uint256) { + return _underlyingTotalRiskValue(); + } + + function _underlyingTotalRiskValue() private view returns (uint256) { + return _underlyingRiskValue(0); + } + + function underlyingRiskValue( + uint256 amount + ) external view returns (uint256) { + return _underlyingRiskValue(amount); + } + + function _underlyingRiskValue( + uint256 amount + ) private view returns (uint256) { + return (underlyingRiskWeight * _underlyingValue(amount)) / 1e6; + } + + function underlyingTotalValue() external view returns (uint256) { + return _underlyingTotalValue(); + } + + function _underlyingTotalValue() private view returns (uint256) { + return _underlyingValue(0); + } + + function underlyingValue(uint256 amount) external view returns (uint256) { + return _underlyingValue(amount); + } + + function _underlyingValue(uint256 amount) private view returns (uint256) { + return (_underlyingPriceOracleLatestAnswer() * amount * 1e12) / 1e8; + } + + function underlyingBalance() external pure returns (uint256) { + return _underlyingBalance(); + } + + function _underlyingBalance() private pure returns (uint256) { + return 0; + } + + function fundTotalRiskValue() external view returns (uint256) { + return _fundTotalRiskValue(); + } + + function _fundTotalRiskValue() private view returns (uint256) { + return _fundRiskValue(_fundBalance()); + } + + function fundRiskValue(uint256 amount) external view returns (uint256) { + return _fundRiskValue(amount); + } + + function _fundRiskValue(uint256 amount) private view returns (uint256) { + return (fundRiskWeight * _fundValue(amount)) / 1e6; + } + + function fundTotalValue() external view returns (uint256) { + return _fundTotalValue(); + } + + function _fundTotalValue() private view returns (uint256) { + return _fundValue((_fundBalance())); + } + + function fundValue(uint256 amount) external view returns (uint256) { + return _fundValue(amount); + } + + function _fundValue(uint256 amount) private view returns (uint256) { + return (_fundPriceOracleLatestAnswer() * amount) / 1e8; + } + + function fundBalance() external view returns (uint256) { + return _fundBalance(); + } + + function _fundBalance() private view returns (uint256) { + return vault.balanceOf(address(this)); + } + + function _underlyingPriceOracleLatestAnswer() + private + view + returns (uint256) + { + int256 latestAnswer = underlyingPriceOracle.latestAnswer(); + + return latestAnswer > 0 ? uint256(latestAnswer) : 0; + } + + function _fundPriceOracleLatestAnswer() private view returns (uint256) { + int256 latestAnswer = fundPriceOracle.latestAnswer(); + + return latestAnswer > 0 ? uint256(latestAnswer) : 0; + } + + function recover(address _token) external onlyRole(MANAGER) { + IERC20 token = IERC20(_token); + + token.transfer(msg.sender, token.balanceOf(address(this))); + } +} diff --git a/src/adapters/VaultSharesOracleV2.sol b/src/adapters/VaultSharesOracleV2.sol new file mode 100644 index 0000000..c313633 --- /dev/null +++ b/src/adapters/VaultSharesOracleV2.sol @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.24; + +import {IERC4626} from "openzeppelin-contracts/contracts/interfaces/IERC4626.sol"; +import {IOracle} from "src/interfaces/IOracle.sol"; + +import {AggregatorV3Interface} from "chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; + +contract VaultSharesOracleV2 is IOracle { + AggregatorV3Interface public immutable underlyingAssetAggregator; + IERC4626 public immutable vault; + uint8 public immutable underlyingDecimals; + + constructor( + AggregatorV3Interface _underlyingAssetAggregator, + IERC4626 _vault, + uint8 _underlyingDecimals + ) { + underlyingAssetAggregator = _underlyingAssetAggregator; + vault = _vault; + underlyingDecimals = _underlyingDecimals; + } + + function latestAnswer() external view returns (int256) { + int256 answer; + uint256 updatedAt; + + (, answer, , updatedAt, ) = underlyingAssetAggregator.latestRoundData(); + + int256 finalizedAnswer = (block.timestamp > 1.1 days + updatedAt) + ? int256(1e8) + : answer; + + return + (finalizedAnswer * int256(vault.convertToAssets(1e18))) / + int256(10 ** underlyingDecimals); + } +} diff --git a/test/morpho/MorphoRUSDAdapter.t.sol b/test/morpho/MorphoRUSDAdapter.t.sol new file mode 100644 index 0000000..4f548e7 --- /dev/null +++ b/test/morpho/MorphoRUSDAdapter.t.sol @@ -0,0 +1,333 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.24; + +import {IERC4626} from "openzeppelin-contracts/contracts/interfaces/IERC4626.sol"; +import {AggregatorV3Interface} from "chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; + +import {MorphoRUSDAdapter} from "src/adapters/MorphoRUSDAdapter.sol"; +import {VaultSharesOracleV2} from "src/adapters/VaultSharesOracleV2.sol"; +import {Stablecoin} from "src/Stablecoin.sol"; +import {ERC20} from "openzeppelin-contracts/contracts/token/ERC20/ERC20.sol"; + +import {Test} from "forge-std/Test.sol"; +import {console} from "forge-std/console.sol"; + +address constant CHAINLINK_USDC_FEED = 0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6; +address constant RUSD_METAMORPHO_VAULT = 0xBeEf11eCb698f4B5378685C05A210bdF71093521; +address constant RUSD_ADDRESS = 0x09D4214C03D01F49544C0448DBE3A27f768F2b34; +address constant RUSD_DEFAULT_ADMIN = 0xb7570e32dED63B25163369D5eb4D8e89E70e5602; + +contract MorphoRUSDAdapterTest is Test { + event Deposit(address indexed signer, uint256 amount, uint256 timestamp); + event Redeem(address indexed signer, uint256 amount, uint256 timestamp); + event UnderlyingRiskWeightUpdate(uint256 riskWeight, uint256 timestamp); + event FundRiskWeightUpdate(uint256 riskWeight, uint256 timestamp); + + Stablecoin public rusd = Stablecoin(RUSD_ADDRESS); + + IERC4626 public metamorpho = IERC4626(RUSD_METAMORPHO_VAULT); + + uint256 public constant TESTING_CAP = 5_000_000_000e18; + + MorphoRUSDAdapter public adapter; + VaultSharesOracleV2 public vaultSharesOracleV2; + + uint256 public constant INITIAL_DURATION = 1 days; + + string MAINNET_RPC_URL = vm.envString("MAINNET_RPC_URL"); + + function setUp() external { + vm.createSelectFork(MAINNET_RPC_URL); + + bytes32 RUSD_MINTER = rusd.MINTER(); + + vm.prank(RUSD_DEFAULT_ADMIN); + rusd.grantRole(RUSD_MINTER, address(this)); + + // Deply Oracle for the Vault Shares + vaultSharesOracleV2 = new VaultSharesOracleV2( + AggregatorV3Interface(CHAINLINK_USDC_FEED), + metamorpho, + 18 + ); + + // Deploy and Configure Morpho Adapter + adapter = new MorphoRUSDAdapter( + address(this), + address(rusd), + address(metamorpho), + CHAINLINK_USDC_FEED, + address(vaultSharesOracleV2), + INITIAL_DURATION + ); + adapter.grantRole(adapter.CONTROLLER(), address(this)); + adapter.grantRole(adapter.MANAGER(), address(this)); + + // Configure rUSD roles + vm.prank(RUSD_DEFAULT_ADMIN); + rusd.grantRole(RUSD_MINTER, address(adapter)); + vm.prank(RUSD_DEFAULT_ADMIN); + rusd.grantRole(RUSD_MINTER, address(this)); + } + + function testInitialBalance() external { + assertTrue(adapter.hasRole(0x0, address(this))); + assertTrue(adapter.hasRole(adapter.CONTROLLER(), address(this))); + assertTrue(adapter.hasRole(adapter.MANAGER(), address(this))); + + assertEq(adapter.duration(), INITIAL_DURATION); + + assertEq(address(adapter.underlyingPriceOracle()), CHAINLINK_USDC_FEED); + assertEq( + address(adapter.fundPriceOracle()), + address(vaultSharesOracleV2) + ); + + assertEq(adapter.fundRiskWeight(), 0e6); + assertEq(adapter.underlyingRiskWeight(), 0e6); + + assertEq(address(adapter.underlying()), address(rusd)); + assertEq(address(adapter.vault()), address(metamorpho)); + } + + function testDepositRedeemFlow( + uint256 depositAmount1, + uint256 redeemAmount1, + uint256 depositAmount2, + uint256 redeemAmount2, + uint32 riskWeight + ) external { + vm.assume(depositAmount1 <= 1_000_000_000e18); + vm.assume(redeemAmount1 <= depositAmount1); + vm.assume(depositAmount2 <= 1_000_000_000e18); + vm.assume(redeemAmount2 <= depositAmount2); + vm.assume(riskWeight < 1e6); + + adapter.setFundRiskWeight(riskWeight); + + uint256 initialRusdTotalSupply = rusd.totalSupply(); + + vm.expectEmit(true, true, true, true); + emit Deposit(address(this), depositAmount1, block.timestamp); + adapter.deposit(depositAmount1); + + assertEq( + metamorpho.convertToAssets(metamorpho.balanceOf(address(adapter))), + depositAmount1 + ); + assertEq(rusd.balanceOf(address(adapter)), 0); + assertEq(rusd.balanceOf(address(this)), 0); + + assertEq(rusd.totalSupply(), initialRusdTotalSupply + depositAmount1); + + assertEq( + adapter.totalValue(), + (uint256(vaultSharesOracleV2.latestAnswer()) * depositAmount1) / 1e8 + ); + assertEq(adapter.fundTotalValue(), adapter.totalValue()); + assertEq( + adapter.totalRiskValue(), + (riskWeight * + ((uint256(vaultSharesOracleV2.latestAnswer()) * + depositAmount1) / 1e8)) / 1e6 + ); + assertEq(adapter.fundTotalRiskValue(), adapter.totalRiskValue()); + assertEq(adapter.underlyingTotalRiskValue(), 0); + assertEq(adapter.underlyingTotalValue(), 0); + assertEq(adapter.underlyingBalance(), 0); + assertEq(adapter.fundBalance(), metamorpho.balanceOf(address(adapter))); + + vm.expectEmit(true, true, true, true); + emit Redeem(address(this), redeemAmount1, block.timestamp); + adapter.redeem(redeemAmount1); + + assertEq( + metamorpho.convertToAssets(metamorpho.balanceOf(address(adapter))), + depositAmount1 - redeemAmount1 + ); + assertEq(rusd.balanceOf(address(adapter)), 0); + assertEq(rusd.balanceOf(address(this)), 0); + + assertEq( + rusd.totalSupply(), + initialRusdTotalSupply + depositAmount1 - redeemAmount1 + ); + + assertEq( + adapter.totalValue(), + (uint256(vaultSharesOracleV2.latestAnswer()) * + (depositAmount1 - redeemAmount1)) / 1e8 + ); + assertEq(adapter.fundTotalValue(), adapter.totalValue()); + assertEq( + adapter.totalRiskValue(), + (riskWeight * + ((uint256(vaultSharesOracleV2.latestAnswer()) * + (depositAmount1 - redeemAmount1)) / 1e8)) / 1e6 + ); + assertEq(adapter.fundTotalRiskValue(), adapter.totalRiskValue()); + assertEq(adapter.underlyingTotalRiskValue(), 0); + assertEq(adapter.underlyingTotalValue(), 0); + assertEq(adapter.underlyingBalance(), 0); + assertEq(adapter.fundBalance(), metamorpho.balanceOf(address(adapter))); + + vm.expectEmit(true, true, true, true); + emit Deposit(address(this), depositAmount2, block.timestamp); + adapter.deposit(depositAmount2); + + assertEq( + metamorpho.convertToAssets(metamorpho.balanceOf(address(adapter))), + depositAmount1 - redeemAmount1 + depositAmount2 + ); + assertEq(rusd.balanceOf(address(adapter)), 0); + assertEq(rusd.balanceOf(address(this)), 0); + + assertEq( + rusd.totalSupply(), + initialRusdTotalSupply + + depositAmount1 - + redeemAmount1 + + depositAmount2 + ); + + assertEq( + adapter.totalValue(), + (uint256(vaultSharesOracleV2.latestAnswer()) * + (depositAmount1 - redeemAmount1 + depositAmount2)) / 1e8 + ); + assertEq(adapter.fundTotalValue(), adapter.totalValue()); + assertEq( + adapter.totalRiskValue(), + (riskWeight * + ((uint256(vaultSharesOracleV2.latestAnswer()) * + (depositAmount1 - redeemAmount1 + depositAmount2)) / 1e8)) / + 1e6 + ); + assertEq(adapter.fundTotalRiskValue(), adapter.totalRiskValue()); + assertEq(adapter.underlyingTotalRiskValue(), 0); + assertEq(adapter.underlyingTotalValue(), 0); + assertEq(adapter.underlyingBalance(), 0); + assertEq(adapter.fundBalance(), metamorpho.balanceOf(address(adapter))); + + vm.expectEmit(true, true, true, true); + emit Redeem(address(this), redeemAmount2, block.timestamp); + adapter.redeem(redeemAmount2); + + assertEq( + metamorpho.convertToAssets(metamorpho.balanceOf(address(adapter))), + depositAmount1 - redeemAmount1 + depositAmount2 - redeemAmount2 + ); + assertEq(rusd.balanceOf(address(adapter)), 0); + assertEq(rusd.balanceOf(address(this)), 0); + + assertEq( + rusd.totalSupply(), + initialRusdTotalSupply + + depositAmount1 - + redeemAmount1 + + depositAmount2 - + redeemAmount2 + ); + + assertEq( + adapter.totalValue(), + (uint256(vaultSharesOracleV2.latestAnswer()) * + (depositAmount1 - + redeemAmount1 + + depositAmount2 - + redeemAmount2)) / 1e8 + ); + assertEq(adapter.fundTotalValue(), adapter.totalValue()); + assertEq( + adapter.totalRiskValue(), + (riskWeight * + ((uint256(vaultSharesOracleV2.latestAnswer()) * + (depositAmount1 - + redeemAmount1 + + depositAmount2 - + redeemAmount2)) / 1e8)) / 1e6 + ); + assertEq(adapter.fundTotalRiskValue(), adapter.totalRiskValue()); + assertEq(adapter.underlyingTotalRiskValue(), 0); + assertEq(adapter.underlyingTotalValue(), 0); + assertEq(adapter.underlyingBalance(), 0); + assertEq(adapter.fundBalance(), metamorpho.balanceOf(address(adapter))); + } + + //! DONT KNOW WHATS THE REAL CAP + // function testDepositMoreThenCap(uint256 amount) external { + // vm.assume(amount > CAP); + + // vm.expectRevert(); + // adapter.deposit(amount); + // } + + function testDepositUnauthorized(uint256 amount) external { + adapter.revokeRole(adapter.CONTROLLER(), address(this)); + + vm.expectRevert(); + adapter.deposit(amount); + } + + //? NEED TO KNOW THE CAP OR THIS TEST + function testRedeemMoreThenDeposited( + uint256 depositAmount, + uint256 redeemAmount + ) external { + vm.assume(depositAmount <= TESTING_CAP); + vm.assume(redeemAmount > depositAmount); + + adapter.deposit(depositAmount); + + vm.expectRevert(); + adapter.redeem(redeemAmount); + } + + function testRedeemUnauthorized(uint256 amount) external { + adapter.revokeRole(adapter.CONTROLLER(), address(this)); + + vm.expectRevert(); + adapter.redeem(amount); + } + + function testSetUnderlyingRiskWeight(uint256 riskWeight) external { + if (riskWeight < 1e6) { + vm.expectEmit(true, true, true, true); + emit UnderlyingRiskWeightUpdate(riskWeight, block.timestamp); + adapter.setUnderlyingRiskWeight(riskWeight); + assertEq(adapter.underlyingRiskWeight(), riskWeight); + } else { + vm.expectRevert("FA: Risk Weight can not be above 100%"); + adapter.setUnderlyingRiskWeight(riskWeight); + } + } + + function testSetUnderlyingiskWeightUnauthorized( + uint256 riskWeight + ) external { + adapter.revokeRole(adapter.MANAGER(), address(this)); + + vm.expectRevert(); + adapter.setUnderlyingRiskWeight(riskWeight); + } + + function testSetFundRiskWeight(uint256 riskWeight) external { + if (riskWeight < 1e6) { + vm.expectEmit(true, true, true, true); + emit FundRiskWeightUpdate(riskWeight, block.timestamp); + adapter.setFundRiskWeight(riskWeight); + assertEq(adapter.fundRiskWeight(), riskWeight); + } else { + vm.expectRevert("FA: Risk Weight can not be above 100%"); + adapter.setFundRiskWeight(riskWeight); + } + } + + function testSetFundRiskWeightUnauthorized(uint256 riskWeight) external { + adapter.revokeRole(adapter.MANAGER(), address(this)); + + vm.expectRevert(); + adapter.setFundRiskWeight(riskWeight); + } +}