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

Update morpho v3 #314

Merged
merged 7 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 2 additions & 19 deletions contracts/mainnet/connectors/morpho-aave-v3/events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ contract Events {
uint256 setId
);

event LogDepositOnBehalf(
address tokenAddress,
uint256 amount,
address onBehalf,
uint256 getId,
uint256 setId
);

event LogDepositOnBehalfWithMaxIterations(
address tokenAddress,
uint256 amount,
Expand Down Expand Up @@ -57,15 +49,6 @@ contract Events {
uint256 setId
);

event LogBorrowOnBehalf(
address tokenAddress,
uint256 amount,
address onBehalf,
address receiver,
uint256 getId,
uint256 setId
);

event LogBorrowWithMaxIterations(
address tokenAddress,
uint256 amount,
Expand All @@ -92,11 +75,11 @@ contract Events {
uint256 setId
);

event LogWithdrawOnBehalf(
event LogWithdrawWithMaxIterations(
address tokenAddress,
uint256 amount,
address onBehalf,
address receiver,
uint256 maxIteration,
uint256 getId,
uint256 setId
);
Expand Down
2 changes: 1 addition & 1 deletion contracts/mainnet/connectors/morpho-aave-v3/helpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract contract Helpers is Stores, Basic {
IMorphoCore public constant MORPHO_AAVE_V3 =
IMorphoCore(0x33333aea097c193e66081E930c33020272b33333);

uint256 public max_iteration = 4;
uint256 public constant max_iteration = 4;
shriyatyagii marked this conversation as resolved.
Show resolved Hide resolved

function _performEthToWethConversion(
address _tokenAddress,
Expand Down
102 changes: 10 additions & 92 deletions contracts/mainnet/connectors/morpho-aave-v3/main.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,47 +84,6 @@ abstract contract MorphoAaveV3 is Helpers, Events {
);
}

/**
* @dev Deposit ETH/ERC20_Token on behalf of a user.
* @notice Deposit a token to Morpho Aave for lending on behalf of a user.
* @param _tokenAddress The address of underlying token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param _amount The amount of the token (in underlying) to deposit. (For max: `uint256(-1)`)
* @param _onBehalf The address of user on behalf of whom we want to deposit.
* @param _getId ID to retrieve amt.
* @param _setId ID stores the amount of tokens deposited.
*/
function depositOnBehalf(
address _tokenAddress,
uint256 _amount,
address _onBehalf,
uint256 _getId,
uint256 _setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
(
TokenInterface _tokenContract,
uint256 _amt
) = _performEthToWethConversion(_tokenAddress, _amount, _getId);

approve(_tokenContract, address(MORPHO_AAVE_V3), _amt);

MORPHO_AAVE_V3.supply(address(_tokenContract), _amt, _onBehalf, max_iteration);

setUint(_setId, _amt);

_eventName = "LogDepositOnBehalf(address,uint256,address,uint256,uint256)";
_eventParam = abi.encode(
_tokenAddress,
_amt,
_onBehalf,
_getId,
_setId
);
}

/**
* @dev Deposit ETH/ERC20_Token on behalf of a user.
* @notice Deposit a token to Morpho Aave for lending on behalf of a user with max iterations.
Expand Down Expand Up @@ -285,50 +244,6 @@ abstract contract MorphoAaveV3 is Helpers, Events {
);
}

/**
* @dev Borrow ETH/ERC20_Token.
* @notice Borrow a token from Morpho Aave V3.
* @param _tokenAddress The address of underlying token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param _amount The amount of the token (in underlying) to borrow.
* @param _onBehalf The address of user on behalf to borrow.
* @param _receiver The address of receiver to receive the borrowed tokens.
Note that if receiver is not the same as the borrower, receiver will receive WETH instead of ETH.
* @param _getId ID to retrieve amt.
* @param _setId ID stores the amount of tokens borrowed.
*/
function borrowOnBehalf(
address _tokenAddress,
uint256 _amount,
address _onBehalf,
address _receiver,
uint256 _getId,
uint256 _setId
)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
uint256 _amt = getUint(_getId, _amount);
bool _isETH = _tokenAddress == ethAddr;
address _token = _isETH ? wethAddr : _tokenAddress;

uint256 _borrowed = MORPHO_AAVE_V3.borrow(_token, _amt, _onBehalf, _receiver, max_iteration);

if(_receiver == address(this)) convertWethToEth(_isETH, TokenInterface(_token), _borrowed);

setUint(_setId, _borrowed);

_eventName = "LogBorrowOnBehalf(address,uint256,addresss,address,uint256,uint256)";
_eventParam = abi.encode(
_tokenAddress,
_borrowed,
_onBehalf,
_receiver,
_getId,
_setId
);
}

/**
* @dev Borrow ETH/ERC20_Token.
* @notice Borrow a token from Morpho Aave.
Expand Down Expand Up @@ -356,6 +271,8 @@ abstract contract MorphoAaveV3 is Helpers, Events {
bool _isETH = _tokenAddress == ethAddr;
address _token = _isETH ? wethAddr : _tokenAddress;

if (_receiver == address(0)) _receiver = address(this);

uint256 _borrowed = MORPHO_AAVE_V3.borrow(_token, _amt, address(this), _receiver, _maxIteration);

if(_receiver == address(this)) convertWethToEth(_isETH, TokenInterface(_token), _borrowed);
Expand Down Expand Up @@ -463,17 +380,16 @@ abstract contract MorphoAaveV3 is Helpers, Events {
* @notice Withdraw a token from Morpho Aave.
* @param _tokenAddress The address of underlying token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param _amount The amount of the token (in underlying) to withdraw. (For max: `uint256(-1)`)
* @param _onBehalf Address for which tokens are being withdrawn.
* @param _receiver Address to which tokens are being transferred.
Note that if receiver is not the same as the supplier, receiver will receive WETH instead of ETH.
* @param _maxIteration Max number of iterations to run.
* @param _getId ID to retrieve amt.
* @param _setId ID stores the amount of tokens withdrawed.
*/
function withdrawOnBehalf(
function withdrawWithMaxIterations(
address _tokenAddress,
uint256 _amount,
address _onBehalf,
address _receiver,
uint256 _maxIteration,
uint256 _getId,
uint256 _setId
)
Expand All @@ -485,19 +401,21 @@ abstract contract MorphoAaveV3 is Helpers, Events {
bool _isEth = _tokenAddress == ethAddr;
address _token = _isEth ? wethAddr : _tokenAddress;

if(_receiver == address(0)) _receiver == address(this);

// Morpho will internally handle max amount conversion by taking the minimum of amount or supplied collateral.
uint256 _withdrawn = MORPHO_AAVE_V3.withdraw(_token, _amt, _onBehalf, _receiver, max_iteration);
uint256 _withdrawn = MORPHO_AAVE_V3.withdraw(_token, _amt, address(this), address(this), _maxIteration);

if(_receiver == address(this)) convertWethToEth(_isEth, TokenInterface(_token), _withdrawn);

setUint(_setId, _withdrawn);

_eventName = "LogWithdrawOnBehalf(address,uint256,address,address,uint256,uint256)";
_eventName = "LogWithdrawWithMaxIterations(address,uint256,address,uint256,uint256,uint256)";
_eventParam = abi.encode(
_tokenAddress,
_withdrawn,
_onBehalf,
_receiver,
_maxIteration,
_getId,
_setId
);
Expand Down
12 changes: 6 additions & 6 deletions test/mainnet/morpho/morpho-aave-v3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ describe("Morpho-Aave-v3", function () {
const spells = [
{
connector: connectorName,
method: "depositOnBehalf",
args: [tokens.eth.address, "10000000000000000000", user, "0", "0"], // 1 ETH
method: "depositOnBehalfWithMaxIterations",
args: [tokens.eth.address, "10000000000000000000", user, 4, "0", "0"], // 1 ETH
},
];

Expand Down Expand Up @@ -254,8 +254,8 @@ describe("Morpho-Aave-v3", function () {
const spells = [
{
connector: connectorName,
method: "withdrawOnBehalf",
args: [tokens.eth.address, dsaMaxValue, dsaWallet0.address, user, "0", "0"], // Max ETH
method: "withdrawOnBehalfWithMaxIterations",
args: [tokens.eth.address, dsaMaxValue, dsaWallet0.address, user, 4, "0", "0"], // Max ETH
},
];

Expand Down Expand Up @@ -293,8 +293,8 @@ describe("Morpho-Aave-v3", function () {
const spells = [
{
connector: connectorName,
method: "borrowOnBehalf",
args: [tokens.eth.address, "200000000000000000", dsaWallet0.address, user, "0", "0"], // 0.7 WETH
method: "borrowOnBehalfWithMaxIterations",
args: [tokens.eth.address, "200000000000000000", dsaWallet0.address, user, 4, "0", "0"], // 0.7 WETH
},
];

Expand Down