Skip to content

Commit

Permalink
Merge branch 'morpho-rusd-adapter' of github.com:reservoir-protocol/r…
Browse files Browse the repository at this point in the history
…eservoir into morpho-rusd-adapter
  • Loading branch information
nikakhachi committed Oct 28, 2024
2 parents afac7ce + 8ad7891 commit cbfee96
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PRIVATE_KEY=
# RPC_URL=http://127.0.0.1:8545
RPC_URL=https://gateway.tenderly.co/public/polygon

MAINNET_RPC_URL=https://eth-mainnet.nodereal.io/v1/1659dfb40aa24bbb8153a677b98064d7

ETHERSCAN_API_KEY=

OWNER=
Expand Down
16 changes: 11 additions & 5 deletions src/adapters/MorphoRUSDAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {AccessControl} from "openzeppelin-contracts/contracts/access/AccessContr

import {IERC4626} from "openzeppelin-contracts/contracts/interfaces/IERC4626.sol";
import {IERC20} from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import {SafeCast} from "openzeppelin-contracts/contracts/utils/math/SafeCast.sol";

import {Stablecoin} from "../Stablecoin.sol";
import {IOracle} from "src/interfaces/IOracle.sol";
Expand Down Expand Up @@ -60,10 +61,15 @@ contract MorphoRUSDAdapter is IAssetAdapter, AccessControl {

function redeem(uint256 shares) public onlyRole(CONTROLLER) {
Stablecoin underlyingStablecoin = Stablecoin(address(underlying));

uint256 initialBalance = underlyingStablecoin.balanceOf(address(this));

vault.redeem(shares, address(this), address(this));
underlyingStablecoin.burn(
underlyingStablecoin.balanceOf(address(this))
);

uint256 receivedAmount = underlyingStablecoin.balanceOf(address(this)) -
initialBalance;

underlyingStablecoin.burn(receivedAmount);

emit Redeem(msg.sender, shares, block.timestamp);
}
Expand Down Expand Up @@ -187,13 +193,13 @@ contract MorphoRUSDAdapter is IAssetAdapter, AccessControl {
{
int256 latestAnswer = underlyingPriceOracle.latestAnswer();

return latestAnswer > 0 ? uint256(latestAnswer) : 0;
return latestAnswer > 0 ? SafeCast.toUint256(latestAnswer) : 0;
}

function _fundPriceOracleLatestAnswer() private view returns (uint256) {
int256 latestAnswer = fundPriceOracle.latestAnswer();

return latestAnswer > 0 ? uint256(latestAnswer) : 0;
return latestAnswer > 0 ? SafeCast.toUint256(latestAnswer) : 0;
}

function recover(address _token) external onlyRole(MANAGER) {
Expand Down
40 changes: 40 additions & 0 deletions test/morpho/MorphoRUSDAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ contract MorphoRUSDAdapterTest is Test {
assertEq(adapter.fundBalance(), metamorpho.balanceOf(address(adapter)));
}

function test_redeem_with_accidental_sent_tokens(
uint256 depositAmount,
uint256 redeemAmount,
uint256 accidentalySentAmount
) external {
vm.assume(depositAmount <= 1_000_000_000e18);
vm.assume(redeemAmount <= depositAmount);
vm.assume(accidentalySentAmount <= 1_000_000_000e18);

adapter.deposit(depositAmount);

deal(address(rusd), address(this), accidentalySentAmount, true);
rusd.transfer(address(adapter), accidentalySentAmount);

adapter.redeem(redeemAmount);

assertEq(rusd.balanceOf(address(adapter)), accidentalySentAmount);
}

function testDepositUnauthorized(uint256 amount) external {
adapter.revokeRole(adapter.CONTROLLER(), address(this));

Expand Down Expand Up @@ -371,4 +390,25 @@ contract MorphoRUSDAdapterTest is Test {
vm.expectRevert();
adapter.setFundRiskWeight(riskWeight);
}

function test_recover(uint256 _amount) external {
ERC20 testToken = new ERC20("Test Token", "TTT");

deal(address(testToken), address(adapter), _amount);

assertEq(testToken.balanceOf(address(this)), 0);
assertEq(testToken.balanceOf(address(adapter)), _amount);

adapter.recover(address(testToken));

assertEq(testToken.balanceOf(address(this)), _amount);
assertEq(testToken.balanceOf(address(adapter)), 0);
}

function test_recover_as_non_owner() external {
adapter.revokeRole(adapter.MANAGER(), address(this));

vm.expectRevert();
adapter.recover(address(rusd));
}
}
1 change: 1 addition & 0 deletions test/morpho/MorphoUnderlyingAdapter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ contract MorphoUnderlyingAdapterTest is Test {

function testRecover(uint256 _amount, address _reciever) external {
vm.assume(_reciever != address(0));
vm.assume(_reciever != address(adapter));

ERC20 testToken = new ERC20("Test Token", "TTT");
deal(address(testToken), address(adapter), _amount);
Expand Down

0 comments on commit cbfee96

Please sign in to comment.