Skip to content

Commit

Permalink
Merge pull request #1 from reservoir-protocol/morpho-rusd-adapter
Browse files Browse the repository at this point in the history
Morpho rusd adapter
  • Loading branch information
eukadish authored Oct 18, 2024
2 parents cdbd787 + 5b79a76 commit 93a82e5
Show file tree
Hide file tree
Showing 4 changed files with 578 additions and 6 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

204 changes: 204 additions & 0 deletions src/adapters/MorphoRUSDAdapter.sol
Original file line number Diff line number Diff line change
@@ -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)));
}
}
39 changes: 39 additions & 0 deletions src/adapters/VaultSharesOracleV2.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 93a82e5

Please sign in to comment.